rolldown_plugin_vite_html 0.1.0

Rolldown plugin for Vite HTML handling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
mod html;
mod utils;

use std::{borrow::Cow, path::Path, pin::Pin, rc::Rc, sync::Arc};

use cow_utils::CowUtils as _;
use html5gum::Span;
use oxc::ast_visit::Visit;
use rolldown_common::side_effects::HookSideEffects;
use rolldown_plugin::{HookTransformOutput, HookUsage, LogWithoutPlugin, Plugin};
use rolldown_plugin_utils::{
  AssetUrlResult, RenderBuiltUrl, ToOutputFilePathEnv, UsizeOrFunction,
  constants::{CSSBundleName, HTMLProxyMapItem},
  partial_encode_url_path,
};
use rolldown_utils::{dashmap::FxDashMap, pattern_filter::normalize_path};
use rustc_hash::{FxHashMap, FxHashSet};
use sugar_path::SugarPath as _;

use crate::utils::{
  get_css_files_for_chunk,
  html_tag::{AttrValue, HtmlTagDescriptor},
  inject_to_head,
};

pub type ResolveDependencies = dyn Fn(
    &str,
    Vec<String>,
    &str,
    &str,
  ) -> Pin<Box<dyn Future<Output = anyhow::Result<Vec<String>>> + Send>>
  + Send
  + Sync;

pub enum ResolveDependenciesEither {
  True,
  Fn(Arc<ResolveDependencies>),
}

#[expect(clippy::struct_excessive_bools)]
#[derive(derive_more::Debug, Default)]
pub struct ViteHtmlPlugin {
  pub is_lib: bool,
  pub is_ssr: bool,
  pub url_base: String,
  pub public_dir: String,
  pub decoded_base: String,
  pub css_code_split: bool,
  pub module_preload_polyfill: bool,
  #[debug(skip)]
  pub asset_inline_limit: UsizeOrFunction,
  #[debug(skip)]
  pub render_built_url: Option<Arc<RenderBuiltUrl>>,
  #[debug(skip)]
  pub resolve_dependencies: Option<ResolveDependenciesEither>,
  html_result_map: FxDashMap<(String, String), (String, bool)>,
}

impl Plugin for ViteHtmlPlugin {
  fn name(&self) -> Cow<'static, str> {
    Cow::Borrowed("builtin:vite-html")
  }

  fn register_hook_usage(&self) -> rolldown_plugin::HookUsage {
    HookUsage::BuildStart | HookUsage::Transform | HookUsage::GenerateBundle
  }

  async fn build_start(
    &self,
    _ctx: &rolldown_plugin::PluginContext,
    _args: &rolldown_plugin::HookBuildStartArgs<'_>,
  ) -> rolldown_plugin::HookNoopReturn {
    self.html_result_map.clear();
    Ok(())
  }

  #[expect(clippy::too_many_lines)]
  async fn transform(
    &self,
    ctx: rolldown_plugin::SharedTransformPluginContext,
    args: &rolldown_plugin::HookTransformArgs<'_>,
  ) -> rolldown_plugin::HookTransformReturn {
    if !args.id.ends_with(".html") {
      return Ok(None);
    }

    let id = normalize_path(args.id);
    let path = args.id.relative(ctx.cwd());
    let path_lossy = path.to_string_lossy();
    let relative_url_path = normalize_path(&path_lossy);

    let public_path = rolldown_utils::concat_string!("/", relative_url_path);
    let public_base = self.get_base_in_html(&relative_url_path);
    let public_to_relative = |filename: &Path, _: &Path| {
      AssetUrlResult::WithoutRuntime(rolldown_utils::concat_string!(
        &public_base,
        filename.to_string_lossy()
      ))
    };
    let env = ToOutputFilePathEnv {
      is_ssr: self.is_ssr,
      host_id: &relative_url_path,
      url_base: &self.url_base,
      decoded_base: &self.decoded_base,
      render_built_url: self.render_built_url.as_deref(),
    };

    let mut js = String::new();
    let mut inline_module_count = 0usize;
    let mut every_script_is_async = true;
    let mut some_scripts_are_async = false;
    let mut some_scripts_are_defer = false;

    let mut style_urls = Vec::new();
    let mut script_urls = Vec::new();

    // TODO: Support module_side_effects for module info
    // let mut set_modules = Vec::new();
    let mut src_tasks = Vec::new();
    let mut srcset_tasks = Vec::new();
    let mut overwrite_attrs = Vec::new();
    let mut s = string_wizard::MagicString::new(args.code);

    // TODO: Extract into a function
    {
      let dom = html::parser::parse_html(args.code);
      let mut stack = vec![dom.document];
      while let Some(node) = stack.pop() {
        match &node.data {
          html::sink::NodeData::Element { name, attrs, span } => {
            let mut should_remove = false;
            if &**name == "script" {
              let mut src = None;
              let mut is_async = false;
              let mut is_module = false;
              let mut is_ignored = false;
              for attr in attrs.borrow().iter() {
                match &*attr.name {
                  "src" => {
                    if src.is_none() {
                      src = Some((attr.value.clone(), attr.span));
                    }
                  }
                  "type" if attr.value == "module" => {
                    is_module = true;
                  }
                  "async" => {
                    is_async = true;
                  }
                  "vite-ignore" => {
                    is_ignored = true;
                    s.remove(attr.span.start, attr.span.end);
                  }
                  _ => {}
                }
              }
              if !is_ignored {
                let is_public_file = src.as_ref().is_some_and(|(s, _)| {
                  rolldown_plugin_utils::check_public_file(s, &self.public_dir).is_some()
                });
                if is_public_file && let Some((ref url, span)) = src {
                  overwrite_attrs.push((url[1..].to_owned(), span));
                }
                if is_module {
                  inline_module_count += 1;
                  if let Some((url, _)) = src.as_ref()
                    && !is_public_file
                    && !utils::is_excluded_url(url)
                  {
                    // TODO: Support module_side_effects for module info
                    // set_modules.push(url);
                    // add `<script type="module" src="..."/>` as an import
                    js.push_str(&rolldown_utils::concat_string!(
                      "import ",
                      rolldown_plugin_utils::to_string_literal(url),
                      "\n"
                    ));
                    should_remove = true;
                  } else if let Some(node) = node.children.borrow_mut().pop() {
                    let html::sink::NodeData::Text { contents, .. } = &node.data else {
                      panic!("Expected text node but received: {:#?}", node.data);
                    };
                    self.add_to_html_proxy_cache(
                      &ctx,
                      public_path.clone(),
                      inline_module_count - 1,
                      HTMLProxyMapItem { code: contents.into(), map: None },
                    );
                    js.push_str(&rolldown_utils::concat_string!(
                      "import \"",
                      id,
                      "?html-proxy&index=",
                      itoa::Buffer::new().format(inline_module_count - 1),
                      ".js\"\n"
                    ));
                    should_remove = true;
                  }
                  every_script_is_async = every_script_is_async && is_async;
                  some_scripts_are_async = some_scripts_are_async || is_async;
                  some_scripts_are_defer = some_scripts_are_defer || !is_async;
                } else if let Some((url, _)) = src.as_ref()
                  && !is_public_file
                {
                  if !utils::is_excluded_url(url) {
                    let message = rolldown_utils::concat_string!(
                      "<script src='",
                      url,
                      "'> in '",
                      public_path,
                      "' can't be bundled without type='module' attribute"
                    );
                    ctx.warn(LogWithoutPlugin { message, ..Default::default() });
                  }
                } else if let Some(node) = node.children.borrow_mut().pop() {
                  let html::sink::NodeData::Text { contents, span } = &node.data else {
                    panic!("Expected text node but received: {:#?}", node.data);
                  };
                  if utils::constant::INLINE_IMPORT.is_match(contents) {
                    let allocator = oxc::allocator::Allocator::default();
                    let parser_ret = oxc::parser::Parser::new(
                      &allocator,
                      contents,
                      oxc::span::SourceType::default(),
                    )
                    .parse();
                    if parser_ret.panicked
                      && let Some(err) = parser_ret
                        .errors
                        .iter()
                        .find(|e| e.severity == oxc::diagnostics::Severity::Error)
                    {
                      return Err(anyhow::anyhow!(format!(
                        "Failed to parse inline script in '{}': {:?}",
                        public_path, err.message
                      )));
                    }
                    let mut visitor = utils::ScriptInlineImportVisitor {
                      offset: span.start,
                      script_urls: &mut script_urls,
                    };
                    visitor.visit_program(&parser_ret.program);
                  }
                }
              }
            }

            // Handle attributes like src/href
            if matches!(
              &**name,
              "audio"
                | "embed"
                | "img"
                | "image"
                | "input"
                | "link"
                | "meta"
                | "object"
                | "source"
                | "track"
                | "use"
                | "video"
            ) {
              let attrs_borrowed = attrs.borrow();
              if let Some(attr) = attrs_borrowed.iter().find(|a| &*a.name == "vite-ignore") {
                s.remove(attr.span.start, attr.span.end);
              } else {
                // Collect all attributes into a map for filtering
                let attr_map = attrs_borrowed
                  .iter()
                  .filter_map(|a| (!a.value.is_empty()).then_some((a.name.as_ref(), a)))
                  .collect::<FxHashMap<_, _>>();

                // Define which attributes to process based on element type
                let (src_attrs, srcset_attrs): (&[&str], &[&str]) = match &**name {
                  "audio" | "embed" | "input" | "track" => (&["src"], &[]),
                  "img" | "source" => (&["src"], &["srcset"]),
                  "image" | "use" => (&["href", "xlink:href"], &[]),
                  "link" => (&["href"], &["imagesrcset"]),
                  "meta" => (&["content"], &[]),
                  "object" => (&["data"], &[]),
                  "video" => (&["src", "poster"], &[]),
                  _ => unreachable!("Element type should be matched in outer condition"),
                };

                // Process srcset attributes (complex, multi-URL handling)
                for srcset_attr in srcset_attrs {
                  if let Some(attr) = attr_map.get(srcset_attr) {
                    srcset_tasks.push((attr.value.clone(), attr.span));
                  }
                }

                // Process src/href attributes
                for src_attr in src_attrs {
                  if let Some(attr) = attr_map.get(src_attr) {
                    let decode_url =
                      rolldown_plugin_utils::uri::decode_uri(&attr.value).into_owned();
                    if rolldown_plugin_utils::check_public_file(&decode_url, &self.public_dir)
                      .is_some()
                    {
                      overwrite_attrs.push((decode_url, attr.span));
                    } else if !utils::is_excluded_url(&decode_url) {
                      if &**name == "link"
                        && rolldown_plugin_utils::css::is_css_request(&decode_url)
                        && !(attr_map.contains_key("media") || attr_map.contains_key("disabled"))
                      {
                        js.push_str("import ");
                        js.push_str(&rolldown_plugin_utils::to_string_literal(&decode_url));
                        js.push_str(";\n");
                        style_urls.push((decode_url, attr.span));
                      }
                    } else {
                      let should_inline = (&**name == "link"
                        && attr_map.get("rel").is_some_and(|attr| {
                          utils::parse_rel_attr(&attr.value).into_iter().any(|v| {
                            ["icon", "apple-touch-icon", "apple-touch-startup-image", "manifest"]
                              .contains(&v.as_str())
                          })
                        }))
                      .then_some(false);
                      src_tasks.push((decode_url, attr.span, should_inline));
                    }
                  }
                }
              }
            }

            // Handle <tag style="..." />
            if let Some(attr) = attrs.borrow().iter().find(|a| {
              &*a.name == "style" && (a.value.contains("url(") || a.value.contains("image-set("))
            }) {
              self.handle_style_tag_or_attribute(
                &mut s,
                &mut js,
                &id,
                &ctx,
                public_path.clone(),
                &mut inline_module_count,
                true,
                (attr.value.as_str(), attr.span),
              )?;
            }

            // Handle <style>...</style>
            if &**name == "style"
              && let Some(node) = node.children.borrow_mut().pop()
            {
              let html::sink::NodeData::Text { ref contents, span } = node.data else {
                panic!("Expected text node but received: {:#?}", node.data);
              };
              self.handle_style_tag_or_attribute(
                &mut s,
                &mut js,
                &id,
                &ctx,
                public_path.clone(),
                &mut inline_module_count,
                false,
                (contents, span),
              )?;
            }

            if should_remove {
              s.remove(span.start, span.end);
            }
          }
          _ => {}
        }
        for child in node.children.borrow().iter() {
          stack.push(Rc::clone(child));
        }
      }
    }

    for (url, span, should_inline) in src_tasks {
      let processed_encoded_url = self.process_asset_url(&ctx, &url, &id, should_inline).await?;
      if processed_encoded_url != url {
        overwrite_attrs.push((processed_encoded_url.into_owned(), span));
      }
    }

    for (task, span) in srcset_tasks {
      let processed_encoded_url = self.process_src_set(&ctx, &task, &id).await?;
      if processed_encoded_url != task {
        overwrite_attrs.push((processed_encoded_url, span));
      }
    }

    for (url, span) in overwrite_attrs {
      let asset_url = env.to_output_file_path(&url, "html", true, public_to_relative).await?;
      utils::overwrite_check_public_file(
        &mut s,
        span.start..span.end,
        partial_encode_url_path(&asset_url.to_asset_url_in_css_or_html()).into_owned(),
      )?;
    }

    if some_scripts_are_async && some_scripts_are_defer {
      let message = rolldown_utils::concat_string!(
        "\nMixed async and defer script modules in ",
        id,
        ", output script will fallback to defer. Every script, including inline ones, need to be marked as async for your output script to be async."
      );
      ctx.warn(LogWithoutPlugin { message, ..Default::default() });
    }

    for (url, range) in script_urls {
      let url = if rolldown_plugin_utils::check_public_file(&url, &self.public_dir).is_some() {
        env
          .to_output_file_path(&url, "html", true, public_to_relative)
          .await?
          .to_asset_url_in_css_or_html()
      } else if !utils::is_excluded_url(&url) {
        self.url_to_built_url(&ctx, &url, &id, None).await?
      } else {
        continue;
      };
      utils::overwrite_check_public_file(
        &mut s,
        range,
        partial_encode_url_path(&url).into_owned(),
      )?;
    }

    let resolved_style_urls = rolldown_utils::futures::block_on_spawn_all(
      style_urls.into_iter().map(async |(url, range): (String, Span)| {
        let resolved = ctx.resolve(&url, Some(&id), None).await;
        (url, range, resolved)
      }),
    )
    .await;

    for (url, span, resolved) in resolved_style_urls {
      match resolved?.ok() {
        Some(_) => {
          s.remove(span.start, span.end);
        }
        None => {
          ctx.warn(LogWithoutPlugin {
            message: format!("\n{url} doesn't exist at build time, it will remain unchanged to be resolved at runtime"),
            ..Default::default()
          });
          js = js
            .cow_replace(
              &rolldown_utils::concat_string!(
                "import ",
                rolldown_plugin_utils::to_string_literal(&url),
                "\n"
              ),
              "",
            )
            .into_owned();
        }
      }
    }

    self
      .html_result_map
      .insert((args.id.to_string(), public_path), (s.to_string(), every_script_is_async));

    if self.module_preload_polyfill && (some_scripts_are_async || some_scripts_are_defer) {
      js = rolldown_utils::concat_string!(
        "import \"",
        utils::constant::MODULE_PRELOAD_POLYFILL,
        "\"\n",
        js
      );
    }

    // TODO: Support module_side_effects for module info
    // for url in set_modules {
    //   match ctx.resolve(&url, Some(args.id), None).await? {
    //     Ok(resolved_id) => match ctx.get_module_info(&resolved_id.id) {
    //       Some(module_info) => module_info.module_side_effects = true,
    //       None => {
    //         if !resolved_id.external.is_external() {
    //           ctx.resolve(specifier, importer, extra_options)
    //         }
    //       },
    //     },
    //     Err(_) => return Err(anyhow::anyhow!("Failed to resolve {url} from {}", args.id)),
    //   }
    // }

    // Force this module to keep from being shared between other entry points.
    // If the resulting chunk is empty, it will be removed in generateBundle.
    Ok(Some(HookTransformOutput {
      code: js.into(),
      side_effects: Some(HookSideEffects::NoTreeshake),
      ..Default::default()
    }))
  }

  #[expect(unused_variables)]
  async fn generate_bundle(
    &self,
    ctx: &rolldown_plugin::PluginContext,
    args: &mut rolldown_plugin::HookGenerateBundleArgs<'_>,
  ) -> rolldown_plugin::HookNoopReturn {
    let mut inline_entry_chunk = FxHashSet::default();
    let mut analyzed_imported_css_files = FxHashMap::default();
    for item in &self.html_result_map {
      let ((id, assets_base), (html, is_async)) = item.pair();

      let mut result = html.to_string();

      let path = id.relative(ctx.cwd());
      let path_lossy = path.to_string_lossy();
      let relative_url_path = normalize_path(&path_lossy);

      let mut can_inline_entry = false;

      let chunk = args.bundle.iter().find_map(|o| match o {
        rolldown_common::Output::Chunk(chunk) => (chunk.is_entry
          && chunk
            .facade_module_id
            .as_ref()
            .is_some_and(|facade_module_id| facade_module_id.resource_id() == id))
        .then_some(chunk),
        rolldown_common::Output::Asset(_) => None,
      });

      // inject chunk asset links
      if let Some(chunk) = chunk {
        // an entry chunk can be inlined if
        //  - it's an ES module (e.g. not generated by the legacy plugin)
        //  - it contains no meaningful code other than import statements
        if args.options.format.is_esm() && utils::is_entirely_import(&chunk.code) {
          can_inline_entry = true;
        }

        // when not inlined, inject <script> for entry and modulepreload its dependencies
        // when inlined, discard entry chunk and inject <script> for everything in post-order
        let imports = utils::get_imported_chunks(chunk, args.bundle);

        let mut asset_tags = if can_inline_entry {
          let mut tags = Vec::with_capacity(imports.len());
          for imported_chunk in imports {
            let mut tag = HtmlTagDescriptor::new("script");
            let url = match imported_chunk {
              utils::ImportedChunk::External(external) => external.to_string(),
              utils::ImportedChunk::Chunk(chunk) => {
                self
                  .to_output_file_path(&chunk.filename, assets_base, false, &relative_url_path)
                  .await?
              }
            };
            tag.attrs = Some(FxHashMap::from_iter([
              ("type", AttrValue::String("module".to_owned())),
              ("crossorigin", AttrValue::Boolean(true)),
              ("src", AttrValue::String(url)),
            ]));
            tags.push(tag);
          }
          tags
        } else {
          let mut tags = vec![{
            let mut tag = HtmlTagDescriptor::new("script");
            let url = self
              .to_output_file_path(&chunk.filename, assets_base, false, &relative_url_path)
              .await?;
            tag.attrs = Some(FxHashMap::from_iter([
              ("type", AttrValue::String("module".to_owned())),
              ("crossorigin", AttrValue::Boolean(true)),
              ("src", AttrValue::String(url)),
            ]));
            if *is_async {
              tag.attrs.as_mut().unwrap().insert("async", AttrValue::Boolean(true));
            }
            tag
          }];
          if let Some(resolve_dependencies) = &self.resolve_dependencies {
            let imports_filenames = imports
              .iter()
              .filter_map(|c| match c {
                utils::ImportedChunk::Chunk(chunk) => Some(chunk.filename.to_string()),
                utils::ImportedChunk::External(_) => None,
              })
              .collect::<Vec<_>>();
            let resolved_deps = match resolve_dependencies {
              ResolveDependenciesEither::True => imports_filenames,
              ResolveDependenciesEither::Fn(r) => {
                r(&chunk.filename, imports_filenames, &relative_url_path, "html").await?
              }
            };
            for dep in resolved_deps {
              let mut tag = HtmlTagDescriptor::new("link");
              let url = self
                .to_output_file_path(&chunk.filename, assets_base, false, &relative_url_path)
                .await?;
              tag.attrs = Some(FxHashMap::from_iter([
                ("rel", AttrValue::String("modulepreload".to_owned())),
                ("crossorigin", AttrValue::Boolean(true)),
                ("href", AttrValue::String(url)),
              ]));
              tags.push(tag);
            }
          }
          tags
        };

        let css_files =
          get_css_files_for_chunk(ctx, chunk, args.bundle, &mut analyzed_imported_css_files);
        asset_tags.reserve(css_files.len());
        for css_file in css_files {
          let url =
            self.to_output_file_path(&css_file, assets_base, false, &relative_url_path).await?;
          let mut tag = HtmlTagDescriptor::new("link");
          tag.attrs = Some(FxHashMap::from_iter([
            ("rel", AttrValue::String("stylesheet".to_owned())),
            ("crossorigin", AttrValue::Boolean(true)),
            ("href", AttrValue::String(url)),
          ]));
          asset_tags.push(tag);
        }

        result = inject_to_head(&result, &asset_tags, false).into_owned();
      }

      if !self.css_code_split {
        let css_bundle_name = ctx.meta().get::<CSSBundleName>();
        if let Some(css_bundle_name) = css_bundle_name
          && args.bundle.iter().any(
            |o| matches!(o, rolldown_common::Output::Asset(asset) if asset.names.contains(&css_bundle_name.0)),
          )
        {
          let url = self.to_output_file_path(&css_bundle_name.0, assets_base, false, &relative_url_path).await?;
          result = utils::inject_to_head(&result, &[
            HtmlTagDescriptor {
              tag: "link",
              attrs: Some(FxHashMap::from_iter([
                ("rel", AttrValue::String("stylesheet".to_owned())),
                ("crossorigin", AttrValue::Boolean(true)),
                (
                  "href",
                  AttrValue::String(url),
                ),
              ])),
                ..Default::default()
            }
          ], false).into_owned();
        }
      }

      if let Some(s) = Self::handle_inline_css(ctx, &result) {
        result = s.to_string();
      }

      // TODO: applyHtmlTransforms
      // result = await applyHtmlTransforms(..)

      if let Some(s) =
        self.handle_html_asset_url(ctx, html, chunk, assets_base, &relative_url_path).await?
      {
        result = s;
      }

      if let Some(chunk) = chunk
        && can_inline_entry
      {
        inline_entry_chunk.insert(chunk.filename.clone());
      }

      ctx
        .emit_file_async(rolldown_common::EmittedAsset {
          name: None,
          original_file_name: Some(id.to_string()),
          file_name: Some(relative_url_path.into()),
          source: rolldown_common::StrOrBytes::Str(result),
        })
        .await?;
    }

    // all imports from entry have been inlined to html, prevent outputting it
    args.bundle.retain(|o| match o {
      rolldown_common::Output::Chunk(chunk) => !inline_entry_chunk.contains(&chunk.filename),
      rolldown_common::Output::Asset(asset) => true,
    });

    Ok(())
  }
}