Skip to content

Commit 3d3e434

Browse files
authoredOct 15, 2024··
feat(es): Introduce runPluginFirst for Wasm plugins (#9645)
**Related issue:** - Closes #9132
1 parent 8a19201 commit 3d3e434

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed
 

‎crates/swc/src/config/mod.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ impl Options {
582582
let disable_all_lints = experimental.disable_all_lints.into_bool();
583583

584584
#[cfg(feature = "plugin")]
585-
let plugin_transforms = {
585+
let plugin_transforms: Box<dyn Fold> = {
586586
let transform_filename = match base {
587587
FileName::Real(path) => path.as_os_str().to_str().map(String::from),
588588
FileName::Custom(filename) => Some(filename.to_owned()),
@@ -643,13 +643,13 @@ impl Options {
643643
}
644644
}
645645

646-
crate::plugin::plugins(
646+
Box::new(crate::plugin::plugins(
647647
experimental.plugins,
648648
transform_metadata_context,
649649
comments.cloned(),
650650
cm.clone(),
651651
unresolved_mark,
652-
)
652+
))
653653
}
654654

655655
// Native runtime plugin target, based on assumption we have
@@ -663,26 +663,28 @@ impl Options {
663663
skipped. Refer https://github.com/swc-project/swc/issues/3934 for the details.",
664664
);
665665

666-
noop()
666+
Box::new(noop())
667667
}
668668
};
669669

670670
#[cfg(not(feature = "plugin"))]
671-
let plugin_transforms = {
671+
let plugin_transforms: Box<dyn Fold> = {
672672
if experimental.plugins.is_some() {
673673
handler.warn(
674674
"Plugin is not supported with current @swc/core. Plugin transform will be \
675675
skipped.",
676676
);
677677
}
678-
noop()
678+
Box::new(noop())
679679
};
680680

681+
let mut plugin_transforms = Some(plugin_transforms);
682+
681683
let pass: Box<dyn Fold> = if experimental
682684
.disable_builtin_transforms_for_internal_testing
683685
.into_bool()
684686
{
685-
Box::new(plugin_transforms)
687+
plugin_transforms.unwrap()
686688
} else {
687689
let decorator_pass: Box<dyn Fold> =
688690
match transform.decorator_version.unwrap_or_default() {
@@ -698,6 +700,11 @@ impl Options {
698700
};
699701

700702
Box::new(chain!(
703+
if experimental.run_plugin_first.into_bool() {
704+
option_pass(plugin_transforms.take())
705+
} else {
706+
Box::new(noop())
707+
},
701708
Optional::new(
702709
lint_to_fold(swc_ecma_lints::rules::all(LintParams {
703710
program: &program,
@@ -748,7 +755,7 @@ impl Options {
748755
),
749756
syntax.typescript()
750757
),
751-
plugin_transforms,
758+
option_pass(plugin_transforms.take()),
752759
custom_before_pass(&program),
753760
// handle jsx
754761
Optional::new(
@@ -1225,6 +1232,9 @@ pub struct JscExperimental {
12251232
#[serde(default)]
12261233
pub cache_root: Option<String>,
12271234

1235+
#[serde(default)]
1236+
pub run_plugin_first: BoolConfig<false>,
1237+
12281238
#[serde(default)]
12291239
pub disable_builtin_transforms_for_internal_testing: BoolConfig<false>,
12301240

@@ -1751,3 +1761,10 @@ fn build_resolver(
17511761

17521762
r
17531763
}
1764+
1765+
fn option_pass(pass: Option<Box<dyn Fold>>) -> Box<dyn Fold> {
1766+
match pass {
1767+
None => Box::new(noop()),
1768+
Some(pass) => pass,
1769+
}
1770+
}

‎packages/types/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export interface TerserMangleOptions {
305305
/**
306306
* Pass `true` to mangle names declared in the top level scope.
307307
*/
308-
topLevel?: boolean
308+
topLevel?: boolean;
309309

310310
/**
311311
* @deprecated An alias for compatibility with terser.
@@ -349,7 +349,7 @@ export interface TerserMangleOptions {
349349
reserved?: string[];
350350
}
351351

352-
export interface TerserManglePropertiesOptions { }
352+
export interface TerserManglePropertiesOptions {}
353353

354354
/**
355355
* Programmatic options.
@@ -662,6 +662,13 @@ export interface JscConfig {
662662
*/
663663
plugins?: Array<[string, Record<string, any>]>;
664664

665+
/**
666+
* Run Wasm plugins before stripping TypeScript or decorators.
667+
*
668+
* See https://github.com/swc-project/swc/issues/9132 for more details.
669+
*/
670+
runPluginFirst?: boolean;
671+
665672
/**
666673
* Disable builtin transforms. If enabled, only Wasm plugins are used.
667674
*/
@@ -1159,7 +1166,7 @@ export interface Output {
11591166
map?: string;
11601167
}
11611168

1162-
export interface MatchPattern { }
1169+
export interface MatchPattern {}
11631170

11641171
// -------------------------------
11651172
// ---------- Ast nodes ----------
@@ -1391,7 +1398,7 @@ export type Expression =
13911398
| OptionalChainingExpression
13921399
| Invalid;
13931400

1394-
interface ExpressionBase extends Node, HasSpan { }
1401+
interface ExpressionBase extends Node, HasSpan {}
13951402

13961403
export interface Identifier extends ExpressionBase {
13971404
type: "Identifier";

0 commit comments

Comments
 (0)
Please sign in to comment.