rspack_plugin_javascript 0.103.0-rc.0

rspack javascript plugin
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
use std::{
  borrow::Cow,
  sync::{Arc, LazyLock},
};

use regex::Regex;
use rspack_cacheable::{
  cacheable, cacheable_dyn,
  with::{AsInner, Skip},
};
use rspack_core::{
  ArcComputed, AsyncDependenciesBlockIdentifier, BuildMetaExportsType,
  COLLECTED_TYPESCRIPT_INFO_PARSE_META_KEY, ChunkGraph, CollectedTypeScriptInfo, Compilation,
  DependenciesBlock, Dependency, GenerateContext, ImportMeta, Module, ModuleArgument,
  ModuleCodeTemplate, ModuleGraph, ModuleType, ParseContext, ParseResult, ParserAndGenerator,
  ResolvedModuleOptions, RuntimeGlobals, RuntimeGlobalsRenderMode, RuntimeVariable,
  SideEffectsBailoutItem, SourceType, TemplateContext, TemplateReplaceSource,
  diagnostics::map_box_diagnostics_to_module_parse_diagnostics,
  remove_bom, render_init_fragments,
  rspack_sources::{BoxSource, ReplaceSource, Source, SourceExt},
};
use rspack_error::{
  Diagnostic, Error, IntoTWithDiagnosticArray, Result, Severity as RspackSeverity,
  TWithDiagnosticArray,
};
use rspack_util::swc::RspackComments;
use rustc_hash::FxHashSet;
use swc_next_allocator::Allocator;
use swc_next_ecma_ast::{
  Ast, Diagnostic as SwcDiagnostic, GetSpan, Lang, NodeId, NodeKindData, Severity as SwcSeverity,
  SourceType as SwcSourceType, Span,
};
use swc_next_ecma_parser::{
  CommentMode, Options, ParseReturn, Parser, Token, TokenParserConfig, TokenTag,
};
use swc_next_ecma_semantic::{AnalyzeOptions, SemanticReturn, SyntaxChecks, analyze};

use crate::{
  BoxJavascriptParserPlugin,
  dependency::ESMCompatibilityDependency,
  visitors::{ParsedJavaScriptAst, ScanDependenciesResult, scan_dependencies},
};

#[derive(Debug)]
pub struct ParserRuntimeRequirementsData {
  pub render_mode: RuntimeGlobalsRenderMode,
  pub context: &'static str,
  pub module: Arc<str>,
  pub rspack_module: &'static str,
  pub exports: Arc<str>,
  pub require: Arc<str>,
  pub compatibility_runtime_scope: Arc<str>,
  pub require_regex: &'static LazyLock<Regex>,
  pub module_cache: Arc<str>,
  pub entry_module_id: Arc<str>,
}

static LEGACY_REQUIRE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
  Regex::new("__webpack_require__\\s*(!?\\.)").expect("should init `REQUIRE_FUNCTION_REGEX`")
});

fn append_swc_next_diagnostics<'a>(
  diagnostics: &mut Vec<Diagnostic>,
  source: &str,
  errors: impl IntoIterator<Item = swc_next_ecma_ast::Diagnostic<'a>>,
) {
  let mut visited = FxHashSet::default();
  let mut shared_source = None;
  diagnostics.extend(errors.into_iter().filter_map(|diagnostic| {
    let span = diagnostic.span;
    if !visited.insert((span.start, span.end)) {
      return None;
    }
    let source = shared_source.get_or_insert_with(|| Arc::<str>::from(source));
    let mut error = Error::from_shared_source(
      Some(source.clone()),
      span.start as usize,
      span.end as usize,
      "JavaScript parse error".to_string(),
      diagnostic.message.into_owned(),
    );
    error.severity = match diagnostic.severity {
      SwcSeverity::Error => RspackSeverity::Error,
      SwcSeverity::Warning => RspackSeverity::Warning,
      SwcSeverity::Hint | SwcSeverity::Info => RspackSeverity::Warning,
    };
    Some(error.into())
  }));
}

impl ParserRuntimeRequirementsData {
  pub fn new(runtime_template: &ModuleCodeTemplate) -> Self {
    let require_name =
      runtime_template.render_runtime_globals_without_adding(&RuntimeGlobals::REQUIRE);
    let compatibility_runtime_scope = runtime_template.render_runtime_scope();
    let module_name =
      runtime_template.render_runtime_globals_without_adding(&RuntimeGlobals::MODULE);
    let exports_name =
      runtime_template.render_runtime_globals_without_adding(&RuntimeGlobals::EXPORTS);
    let module_cache_name =
      runtime_template.render_runtime_globals_without_adding(&RuntimeGlobals::MODULE_CACHE);
    let entry_module_id_name =
      runtime_template.render_runtime_globals_without_adding(&RuntimeGlobals::ENTRY_MODULE_ID);
    let context_name = runtime_template.render_runtime_variable(&RuntimeVariable::Context);
    let rspack_module_name = runtime_template.render_runtime_variable(&RuntimeVariable::Module);
    Self {
      render_mode: runtime_template.render_mode(),
      require_regex: &LEGACY_REQUIRE_REGEX,
      context: context_name,
      module: module_name,
      rspack_module: rspack_module_name,
      exports: exports_name,
      require: require_name,
      compatibility_runtime_scope,
      module_cache: module_cache_name,
      entry_module_id: entry_module_id_name,
    }
  }

  pub fn module_argument(&self, module_argument: &ModuleArgument) -> String {
    match module_argument {
      ModuleArgument::Module => self.module.to_string(),
      ModuleArgument::RspackModule => self.rspack_module.to_string(),
    }
  }
}

#[cacheable]
pub struct JavaScriptParserAndGenerator {
  #[cacheable(with=AsInner)]
  import_meta: ArcComputed<ResolvedModuleOptions, ImportMeta>,
  #[cacheable(with=Skip)]
  parser_plugins: Vec<BoxJavascriptParserPlugin>,
}

impl std::fmt::Debug for JavaScriptParserAndGenerator {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("JavaScriptParserAndGenerator")
      .field("parser_plugins", &"...")
      .finish()
  }
}

impl JavaScriptParserAndGenerator {
  pub(crate) fn concatenation_bailout_reason(
    &self,
    module: &dyn rspack_core::Module,
    allow_empty_commonjs: bool,
  ) -> Option<Cow<'static, str>> {
    // Only ESM eligibility may be skipped for a validated empty CommonJS candidate.
    // All other bailouts stay on the ordinary concatenation path.
    if !allow_empty_commonjs {
      if module.build_meta().exports_type() != BuildMetaExportsType::Namespace {
        return Some("Module is not an ECMAScript module".into());
      }

      if let Some(deps) = module.get_presentational_dependencies() {
        if !deps.iter().any(|dep| {
          // https://github.com/webpack/webpack/blob/b9fb99c63ca433b24233e0bbc9ce336b47872c08/lib/javascript/JavascriptGenerator.js#L65-L74
          dep
            .as_any()
            .downcast_ref::<ESMCompatibilityDependency>()
            .is_some()
        }) {
          return Some("Module is not an ECMAScript module".into());
        }
      } else {
        return Some("Module is not an ECMAScript module".into());
      }
    }

    if let Some(bailout) = module.build_info().module_concatenation_bailout.as_deref() {
      return Some(format!("Module uses {bailout}").into());
    }
    None
  }

  pub fn new(module_options: Arc<ResolvedModuleOptions>) -> Self {
    Self {
      import_meta: ArcComputed::new(module_options, |options| options.into()),
      parser_plugins: Vec::default(),
    }
  }

  pub fn add_parser_plugin(&mut self, parser_plugin: BoxJavascriptParserPlugin) {
    self.parser_plugins.push(parser_plugin);
  }

  fn source_block(
    &self,
    compilation: &Compilation,
    block_id: &AsyncDependenciesBlockIdentifier,
    source: &mut TemplateReplaceSource,
    context: &mut TemplateContext,
  ) {
    let module_graph = compilation.get_module_graph();
    let block = module_graph
      .block_by_id(block_id)
      .expect("should have block");
    //    let block = block_id.expect_get(compilation);
    block.get_dependencies().iter().for_each(|dependency| {
      self.source_dependency(compilation, dependency.as_ref(), source, context)
    });
    block
      .get_blocks()
      .iter()
      .for_each(|block_id| self.source_block(compilation, block_id, source, context));
  }

  fn source_dependency(
    &self,
    compilation: &Compilation,
    dependency: &dyn Dependency,
    source: &mut TemplateReplaceSource,
    context: &mut TemplateContext,
  ) {
    if let Some(dependency) = dependency.as_dependency_code_generation() {
      if let Some(template) = dependency
        .dependency_template()
        .and_then(|template_type| compilation.get_dependency_template(template_type))
      {
        template.render(dependency, source, context)
      } else {
        panic!(
          "Can not find dependency template of {:?}",
          dependency.dependency_template()
        );
      }
    }
  }
}

static SOURCE_TYPES: &[SourceType; 1] = &[SourceType::JavaScript];

#[cacheable_dyn]
#[async_trait::async_trait]
impl ParserAndGenerator for JavaScriptParserAndGenerator {
  fn source_types(&self, _module: &dyn Module, _module_graph: &ModuleGraph) -> &[SourceType] {
    SOURCE_TYPES
  }

  fn size(&self, module: &dyn Module, _source_type: Option<&SourceType>) -> f64 {
    module.source().map_or(0, |source| source.size()) as f64
  }

  #[tracing::instrument("JavaScriptParser:parse", skip_all,fields(
    resource = parse_context.resource_data.resource()
  ))]
  async fn parse<'a>(
    &mut self,
    parse_context: ParseContext<'a>,
  ) -> Result<TWithDiagnosticArray<ParseResult>> {
    let ParseContext {
      source,
      module_type,
      module_layer,
      resource_data,
      compiler_options,
      runtime_template,
      factory_meta,
      build_info,
      build_meta,
      module_identifier,
      loaders,
      module_parser_options,
      mut parse_meta,
      ..
    } = parse_context;
    let mut diagnostics: Vec<Diagnostic> = vec![];

    if let Some(collected_ts_info) = parse_meta.remove(COLLECTED_TYPESCRIPT_INFO_PARSE_META_KEY)
      && let Ok(collected_ts_info) = collected_ts_info
        .into_any()
        .downcast::<CollectedTypeScriptInfo>()
    {
      build_info.collected_typescript_info = Some(*collected_ts_info);
    }

    let default_with_diagnostics = |source: Arc<dyn Source>, diagnostics: Vec<Diagnostic>| {
      Ok(
        ParseResult {
          source,
          dependencies: vec![],
          blocks: vec![],
          presentational_dependencies: vec![],
          code_generation_dependencies: vec![],
          side_effects_bailout: None,
        }
        .with_diagnostic(map_box_diagnostics_to_module_parse_diagnostics(
          diagnostics,
          loaders,
        )),
      )
    };

    let source = remove_bom(source);
    let source_string = source.source().into_string_lossy();

    let jsx = module_parser_options
      .and_then(|options| options.get_javascript())
      .and_then(|options| options.jsx)
      .unwrap_or(false);

    let allocator = Allocator::new();
    let source_type = match module_type {
      ModuleType::JsEsm => SwcSourceType::Module,
      ModuleType::JsDynamic => SwcSourceType::CommonJs,
      _ => SwcSourceType::Unambiguous,
    };
    let ParseReturn {
      ast,
      tokens,
      diagnostics: parse_diagnostics,
    } = Parser::init(
      &allocator,
      source_string.as_ref(),
      Options {
        source_type,
        lang: if jsx { Lang::Jsx } else { Lang::Js },
        preserve_parens: false,
        comments: CommentMode::Flat,
      },
      TokenParserConfig,
    )
    .parse();

    if !parse_diagnostics.is_empty() {
      append_swc_next_diagnostics(&mut diagnostics, &source_string, parse_diagnostics);
      return default_with_diagnostics(source, diagnostics);
    }

    let mut semicolons = Default::default();
    let scan_diagnostics = AstScanner::new(&mut semicolons, &tokens).scan(&ast);
    if !scan_diagnostics.is_empty() {
      append_swc_next_diagnostics(&mut diagnostics, &source_string, scan_diagnostics);
      return default_with_diagnostics(source, diagnostics);
    }

    // Keep the legacy SWC parser's permissive behavior for these semantic checks.
    let check_syntax = SyntaxChecks::all()
      - SyntaxChecks::REDECLARATIONS
      - SyntaxChecks::TS_IMPORT_ALIAS_CONFLICTS
      - SyntaxChecks::REGEXP_LITERALS
      - SyntaxChecks::PRIVATE_NAME_REFERENCES
      - SyntaxChecks::DUPLICATE_PRIVATE_NAMES
      - SyntaxChecks::DELETE_PRIVATE_FIELDS
      - SyntaxChecks::SUPER_CALLS
      - SyntaxChecks::FUNCTION_DECLARATIONS
      - SyntaxChecks::CLASS_MEMBER_NAMES
      - SyntaxChecks::STRICT_BINDING_NAMES
      - SyntaxChecks::STRICT_ASSIGNMENT_TARGETS
      - SyntaxChecks::DUPLICATE_EXPORTS
      - SyntaxChecks::UNRESOLVED_EXPORTS
      - SyntaxChecks::CONTINUE_STATEMENTS
      - SyntaxChecks::BREAK_STATEMENTS
      - SyntaxChecks::AWAIT_EXPRESSIONS
      - SyntaxChecks::YIELD_EXPRESSIONS;
    let SemanticReturn {
      semantic,
      diagnostics: semantic_diagnostics,
    } = analyze(
      &ast,
      AnalyzeOptions {
        check_syntax,
        build_module_record: false,
      },
    );
    if !semantic_diagnostics.is_empty() {
      append_swc_next_diagnostics(&mut diagnostics, &source_string, semantic_diagnostics);
      return default_with_diagnostics(source, diagnostics);
    }

    let program = ast.root_program();
    let comments = RspackComments::from_ast(&ast);
    let parsed_ast = ParsedJavaScriptAst {
      ast: &ast,
      comments: &comments,
      semantic: &semantic,
      program,
    };
    let parser_runtime_requirements = ParserRuntimeRequirementsData::new(runtime_template);

    let ScanDependenciesResult {
      dependencies,
      blocks,
      presentational_dependencies,
      mut warning_diagnostics,
      mut side_effects_item,
    } = match scan_dependencies(
      &source_string,
      &parsed_ast,
      resource_data,
      compiler_options,
      module_type,
      module_layer,
      factory_meta,
      build_meta,
      build_info,
      module_identifier,
      module_parser_options,
      ArcComputed::clone(&self.import_meta),
      &mut semicolons,
      &mut self.parser_plugins,
      parse_meta,
      &parser_runtime_requirements,
    ) {
      Ok(result) => result,
      Err(mut e) => {
        diagnostics.append(&mut e);
        return default_with_diagnostics(source, diagnostics);
      }
    };
    diagnostics.append(&mut warning_diagnostics);
    let mut side_effects_bailout = None;

    if compiler_options.optimization.side_effects.is_true() {
      let has_side_effects = side_effects_item.is_some();
      build_meta.set_side_effect_free(!has_side_effects);
      if has_side_effects {
        build_info.deferred_pure_checks.clear();
      }
      side_effects_bailout = side_effects_item.take().and_then(|item| -> Option<_> {
        let msg = item.loc?.to_string();
        Some(SideEffectsBailoutItem { msg, ty: item.ty })
      });
    }

    Ok(
      ParseResult {
        source,
        dependencies,
        blocks,
        presentational_dependencies,
        code_generation_dependencies: vec![],
        side_effects_bailout,
      }
      .with_diagnostic(map_box_diagnostics_to_module_parse_diagnostics(
        diagnostics,
        loaders,
      )),
    )
  }

  async fn generate(
    &self,
    source: &BoxSource,
    module: &dyn Module,
    generate_context: &mut GenerateContext,
  ) -> Result<BoxSource> {
    if matches!(
      generate_context.requested_source_type,
      SourceType::JavaScript
    ) {
      let mut source = ReplaceSource::new(source.clone());
      let compilation = generate_context.compilation;
      let mut init_fragments = vec![];
      let mut context = TemplateContext {
        compilation,
        module,
        init_fragments: &mut init_fragments,
        runtime: generate_context.runtime,
        concatenation_scope: generate_context.concatenation_scope.take(),
        data: generate_context.data,
        runtime_template: generate_context.runtime_template,
      };

      module.get_dependencies().iter().for_each(|dependency| {
        self.source_dependency(compilation, dependency.as_ref(), &mut source, &mut context)
      });

      if let Some(dependencies) = module.get_presentational_dependencies() {
        dependencies.iter().for_each(|dependency| {
          if let Some(template) = dependency
            .dependency_template()
            .and_then(|template_type| compilation.get_dependency_template(template_type))
          {
            template.render(dependency.as_ref(), &mut source, &mut context)
          } else {
            panic!(
              "Can not find dependency template of {:?}",
              dependency.dependency_template()
            );
          }
        });
      };

      module
        .get_blocks()
        .iter()
        .for_each(|block_id| self.source_block(compilation, block_id, &mut source, &mut context));
      generate_context.concatenation_scope = context.concatenation_scope.take();
      render_init_fragments(source.boxed(), init_fragments, generate_context)
    } else {
      panic!(
        "Unsupported source type: {:?}",
        generate_context.requested_source_type
      )
    }
  }

  fn get_concatenation_bailout_reason(
    &self,
    module: &dyn rspack_core::Module,
    _mg: &ModuleGraph,
    _cg: &ChunkGraph,
  ) -> Option<Cow<'static, str>> {
    self.concatenation_bailout_reason(module, false)
  }
}

/// Collects inserted semicolon positions and rejects syntax unsupported by the
/// legacy parser in a single pass over the flat AST.
pub struct AstScanner<'view> {
  semicolons: &'view mut FxHashSet<u32>,
  tokens: &'view [Token],
}

impl<'view> AstScanner<'view> {
  pub fn new(semicolons: &'view mut FxHashSet<u32>, tokens: &'view [Token]) -> Self {
    Self { semicolons, tokens }
  }

  #[inline]
  fn curr_token(&self, span: Span) -> Option<usize> {
    self
      .tokens
      .binary_search_by(|token| token.span().start.cmp(&span.start))
      .ok()
  }

  #[inline]
  fn next_token(&self, span: Span) -> Option<usize> {
    self
      .tokens
      .binary_search_by(|token| token.span().end.cmp(&span.end))
      .ok()
      .map(|index| index + 1)
  }

  #[inline]
  fn can_insert_semi(&self, token_index: usize) -> bool {
    if token_index == self.tokens.len() {
      return true;
    }
    let token = self.tokens[token_index];
    token.tag() == TokenTag::RightBrace || token.has_line_terminator_before()
  }

  #[inline]
  fn semi(&mut self, span: Span) {
    let Some(index) = self.curr_token(span) else {
      return;
    };
    if index > 0 {
      let previous = self.tokens[index - 1];
      if previous.tag() != TokenTag::Semicolon && self.can_insert_semi(index) {
        self.semicolons.insert(previous.span().end);
      }
    }
  }

  #[inline]
  fn post_semi(&mut self, span: Span) {
    let Some(index) = self.next_token(span) else {
      return;
    };
    if index > 0 {
      let previous = self.tokens[index - 1];
      if previous.tag() != TokenTag::Semicolon && self.can_insert_semi(index) {
        self.semicolons.insert(previous.span().end);
      }
    }
  }

  /// Requires successful parser output before transformations can detach nodes.
  pub fn scan(&mut self, ast: &Ast<'_>) -> Vec<SwcDiagnostic<'static>> {
    let mut diagnostics = Vec::new();
    for index in 0..ast.node_count() {
      let node = NodeId::from_raw_unchecked(index as u32);
      match ast.kind_data(node) {
        NodeKindData::Decorator(_) => {
          let start = ast.span(node).start;
          diagnostics.push(SwcDiagnostic::error(
            "Decorators are not supported",
            Span::new(start, start + 1),
          ));
        }
        NodeKindData::PropertyDefinition(property) => {
          if property.accessor(ast) {
            diagnostics.push(SwcDiagnostic::error(
              "Auto-accessors are not supported",
              property.key(ast).span(ast),
            ));
          }
          self.post_semi(ast.span(node));
        }
        NodeKindData::UpdateExpression(_) => self.semi(ast.span(node)),
        NodeKindData::ExpressionStatement(_)
        | NodeKindData::VariableDeclaration(_)
        | NodeKindData::ContinueStatement(_)
        | NodeKindData::BreakStatement(_)
        | NodeKindData::ReturnStatement(_)
        | NodeKindData::ThrowStatement(_)
        | NodeKindData::YieldExpression(_)
        | NodeKindData::ImportDeclaration(_)
        | NodeKindData::ExportNamedDeclaration(_)
        | NodeKindData::ExportDefaultDeclaration(_)
        | NodeKindData::ExportAllDeclaration(_)
        | NodeKindData::DebuggerStatement(_) => self.post_semi(ast.span(node)),
        _ => {}
      }
    }
    diagnostics
  }
}