Skip to content

Commit

Permalink
feat(parser): add new javascript parser options
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Sep 23, 2024
1 parent 45a3997 commit 8ed9800
Show file tree
Hide file tree
Showing 10 changed files with 482 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,10 @@ export interface RawJavascriptParserOptions {
worker: Array<string>
overrideStrict?: string
importMeta: boolean
requireAsExpression: boolean
requireDynamic: boolean
requireResolve: boolean
importDynamic: boolean
}

export interface RawLazyCompilationOption {
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ pub struct RawJavascriptParserOptions {
pub worker: Vec<String>,
pub override_strict: Option<String>,
pub import_meta: bool,
pub require_as_expression: bool,
pub require_dynamic: bool,
pub require_resolve: bool,
pub import_dynamic: bool,
}

impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
Expand Down Expand Up @@ -301,6 +305,10 @@ impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
.override_strict
.map(|e| OverrideStrict::from(e.as_str())),
import_meta: value.import_meta,
require_as_expression: value.require_as_expression,
require_dynamic: value.require_dynamic,
require_resolve: value.require_resolve,
import_dynamic: value.import_dynamic,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ pub struct JavascriptParserOptions {
pub worker: Vec<String>,
pub override_strict: Option<OverrideStrict>,
pub import_meta: bool,
pub require_as_expression: bool,
pub require_dynamic: bool,
pub require_resolve: bool,
pub import_dynamic: bool,
}

#[derive(Debug, Clone, MergeFrom)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub struct CommonJsImportsParserPlugin;

impl CommonJsImportsParserPlugin {
fn process_resolve(&self, parser: &mut JavascriptParser, call_expr: &CallExpr, weak: bool) {
if !parser.javascript_options.require_resolve {
return;
}

if call_expr.args.len() != 1 {
return;
}
Expand Down Expand Up @@ -268,6 +272,10 @@ impl CommonJsImportsParserPlugin {
}
}

if !parser.javascript_options.require_dynamic && !param.is_string() {
return Some(true);
}

// FIXME: should support `LocalModuleDependency`
if self
.process_require_item(parser, call_expr.span, &param)
Expand All @@ -290,6 +298,10 @@ impl CommonJsImportsParserPlugin {
parser: &mut JavascriptParser,
ident: &Ident,
) -> Option<bool> {
if !parser.javascript_options.require_as_expression {
return Some(true);
}

let start = ident.span().real_lo();
let end = ident.span().real_hi();
let mut dep = CommonJsRequireContextDependency::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl JavascriptParserPlugin for ImportParserPlugin {
parser.blocks.push(Box::new(block));
Some(true)
} else {
if !parser.javascript_options.import_dynamic {
return Some(true);
}

let ContextModuleScanResult {
context,
reg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ Object {
"importMeta": true,
"overrideStrict": undefined,
"reexportExportsPresence": undefined,
"requireDynamic": true,
"requireResolve": true,
"strictExportPresence": false,
"url": true,
"worker": Array [
Expand Down
428 changes: 428 additions & 0 deletions packages/rspack/etc/api.md

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ function getRawJavascriptParserOptions(
? ["..."]
: []
: parser.worker ?? ["..."],
overrideStrict: parser.overrideStrict
overrideStrict: parser.overrideStrict,
requireAsExpression: parser.requireAsExpression ?? true,
requireDynamic: parser.requireDynamic ?? true,
requireResolve: parser.requireResolve ?? true,
importDynamic: parser.importDynamic ?? true
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ const applyJavascriptParserOptionsDefaults = (
D(parserOptions, "importExportsPresence", undefined);
D(parserOptions, "reexportExportsPresence", undefined);
D(parserOptions, "strictExportPresence", false);
D(parserOptions, "requireAsExpression", true);
D(parserOptions, "requireDynamic", true);
D(parserOptions, "requireResolve", true);
D(parserOptions, "importDynamic", true);
D(parserOptions, "worker", ["..."]);
D(parserOptions, "overrideStrict", undefined);
D(parserOptions, "importMeta", true);
Expand Down
12 changes: 11 additions & 1 deletion packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ const reexportExportsPresence = z
const strictExportPresence = z.boolean();
const worker = z.array(z.string()).or(z.boolean());
const overrideStrict = z.enum(["strict", "non-strict"]);
const requireAsExpression = z.boolean();
const requireDynamic = z.boolean();
const requireResolve = z.boolean();
const importDynamic = z.boolean();

const javascriptParserOptions = z.strictObject({
dynamicImportMode: dynamicImportMode.optional(),
Expand All @@ -616,7 +620,13 @@ const javascriptParserOptions = z.strictObject({
reexportExportsPresence: reexportExportsPresence.optional(),
strictExportPresence: strictExportPresence.optional(),
worker: worker.optional(),
overrideStrict: overrideStrict.optional()
overrideStrict: overrideStrict.optional(),
// #region Not available in webpack yet.
requireAsExpression: requireAsExpression.optional(),
requireDynamic: requireDynamic.optional(),
requireResolve: requireResolve.optional(),
importDynamic: importDynamic.optional()
// #endregion
});
export type JavascriptParserOptions = z.infer<typeof javascriptParserOptions>;

Expand Down

0 comments on commit 8ed9800

Please sign in to comment.