Skip to content

Commit

Permalink
feat(es): Introduce runPluginFirst for Wasm plugins (#9645)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9132
  • Loading branch information
kdy1 authored Oct 15, 2024
1 parent 8a19201 commit 3d3e434
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
33 changes: 25 additions & 8 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl Options {
let disable_all_lints = experimental.disable_all_lints.into_bool();

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

crate::plugin::plugins(
Box::new(crate::plugin::plugins(
experimental.plugins,
transform_metadata_context,
comments.cloned(),
cm.clone(),
unresolved_mark,
)
))
}

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

noop()
Box::new(noop())
}
};

#[cfg(not(feature = "plugin"))]
let plugin_transforms = {
let plugin_transforms: Box<dyn Fold> = {
if experimental.plugins.is_some() {
handler.warn(
"Plugin is not supported with current @swc/core. Plugin transform will be \
skipped.",
);
}
noop()
Box::new(noop())
};

let mut plugin_transforms = Some(plugin_transforms);

let pass: Box<dyn Fold> = if experimental
.disable_builtin_transforms_for_internal_testing
.into_bool()
{
Box::new(plugin_transforms)
plugin_transforms.unwrap()
} else {
let decorator_pass: Box<dyn Fold> =
match transform.decorator_version.unwrap_or_default() {
Expand All @@ -698,6 +700,11 @@ impl Options {
};

Box::new(chain!(
if experimental.run_plugin_first.into_bool() {
option_pass(plugin_transforms.take())
} else {
Box::new(noop())
},
Optional::new(
lint_to_fold(swc_ecma_lints::rules::all(LintParams {
program: &program,
Expand Down Expand Up @@ -748,7 +755,7 @@ impl Options {
),
syntax.typescript()
),
plugin_transforms,
option_pass(plugin_transforms.take()),
custom_before_pass(&program),
// handle jsx
Optional::new(
Expand Down Expand Up @@ -1225,6 +1232,9 @@ pub struct JscExperimental {
#[serde(default)]
pub cache_root: Option<String>,

#[serde(default)]
pub run_plugin_first: BoolConfig<false>,

#[serde(default)]
pub disable_builtin_transforms_for_internal_testing: BoolConfig<false>,

Expand Down Expand Up @@ -1751,3 +1761,10 @@ fn build_resolver(

r
}

fn option_pass(pass: Option<Box<dyn Fold>>) -> Box<dyn Fold> {
match pass {
None => Box::new(noop()),
Some(pass) => pass,
}
}
15 changes: 11 additions & 4 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export interface TerserMangleOptions {
/**
* Pass `true` to mangle names declared in the top level scope.
*/
topLevel?: boolean
topLevel?: boolean;

/**
* @deprecated An alias for compatibility with terser.
Expand Down Expand Up @@ -349,7 +349,7 @@ export interface TerserMangleOptions {
reserved?: string[];
}

export interface TerserManglePropertiesOptions { }
export interface TerserManglePropertiesOptions {}

/**
* Programmatic options.
Expand Down Expand Up @@ -662,6 +662,13 @@ export interface JscConfig {
*/
plugins?: Array<[string, Record<string, any>]>;

/**
* Run Wasm plugins before stripping TypeScript or decorators.
*
* See https://github.com/swc-project/swc/issues/9132 for more details.
*/
runPluginFirst?: boolean;

/**
* Disable builtin transforms. If enabled, only Wasm plugins are used.
*/
Expand Down Expand Up @@ -1159,7 +1166,7 @@ export interface Output {
map?: string;
}

export interface MatchPattern { }
export interface MatchPattern {}

// -------------------------------
// ---------- Ast nodes ----------
Expand Down Expand Up @@ -1391,7 +1398,7 @@ export type Expression =
| OptionalChainingExpression
| Invalid;

interface ExpressionBase extends Node, HasSpan { }
interface ExpressionBase extends Node, HasSpan {}

export interface Identifier extends ExpressionBase {
type: "Identifier";
Expand Down

0 comments on commit 3d3e434

Please sign in to comment.