Skip to content

Commit a34af31

Browse files
fix: add unknown_context_critical option for unknown require as expression (#10329)
fix: add unknown_context_critical option for unknown require as expression (#10329) --------- Co-authored-by: ahabhgk <[email protected]>
1 parent 580b9cd commit a34af31

File tree

20 files changed

+116
-13
lines changed

20 files changed

+116
-13
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,7 @@ export interface RawJavascriptParserOptions {
23052305
dynamicImportFetchPriority?: string
23062306
url?: string
23072307
exprContextCritical?: boolean
2308+
unknownContextCritical?: boolean
23082309
wrappedContextCritical?: boolean
23092310
wrappedContextRegExp?: RegExp
23102311
exportsPresence?: string

crates/rspack/src/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,7 @@ impl ModuleOptionsBuilder {
16991699
dynamic_import_fetch_priority: None,
17001700
url: Some(JavascriptParserUrl::Enable),
17011701
expr_context_critical: Some(true),
1702+
unknown_context_critical: Some(true),
17021703
wrapped_context_critical: Some(false),
17031704
wrapped_context_reg_exp: Some(RspackRegex::new(".*").expect("should initialize `Regex`")),
17041705
strict_export_presence: Some(false),

crates/rspack/tests/snapshots/defaults__default_options.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: crates/rspack/tests/defaults.rs
3-
assertion_line: 16
43
expression: options
54
---
65
CompilerOptions {
@@ -1396,6 +1395,9 @@ CompilerOptions {
13961395
url: Some(
13971396
Enable,
13981397
),
1398+
unknown_context_critical: Some(
1399+
true,
1400+
),
13991401
expr_context_critical: Some(
14001402
true,
14011403
),

crates/rspack_binding_api/src/raw_options/raw_module/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ pub struct RawJavascriptParserOptions {
268268
pub dynamic_import_fetch_priority: Option<String>,
269269
pub url: Option<String>,
270270
pub expr_context_critical: Option<bool>,
271+
pub unknown_context_critical: Option<bool>,
271272
pub wrapped_context_critical: Option<bool>,
272273
#[napi(ts_type = "RegExp")]
273274
pub wrapped_context_reg_exp: Option<RspackRegex>,
@@ -315,6 +316,7 @@ impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
315316
.map(|x| DynamicImportFetchPriority::from(x.as_str())),
316317
url: value.url.map(|v| JavascriptParserUrl::from(v.as_str())),
317318
expr_context_critical: value.expr_context_critical,
319+
unknown_context_critical: value.unknown_context_critical,
318320
wrapped_context_reg_exp: value.wrapped_context_reg_exp,
319321
wrapped_context_critical: value.wrapped_context_critical,
320322
exports_presence: value

crates/rspack_core/src/options/module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ pub struct JavascriptParserOptions {
266266
pub dynamic_import_prefetch: Option<JavascriptParserOrder>,
267267
pub dynamic_import_fetch_priority: Option<DynamicImportFetchPriority>,
268268
pub url: Option<JavascriptParserUrl>,
269+
pub unknown_context_critical: Option<bool>,
269270
pub expr_context_critical: Option<bool>,
270271
pub wrapped_context_critical: Option<bool>,
271272
pub wrapped_context_reg_exp: Option<RspackRegex>,

crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,20 @@ impl CommonJsImportsParserPlugin {
363363
None,
364364
parser.in_try,
365365
);
366-
*dep.critical_mut() = Some(
367-
create_traceable_error(
368-
"Critical dependency".into(),
369-
"require function is used in a way in which dependencies cannot be statically extracted"
370-
.to_string(),
371-
parser.source_file,
372-
ident.span().into(),
373-
)
374-
.with_severity(Severity::Warn)
375-
.boxed()
376-
.into(),
377-
);
366+
if let Some(true) = parser.javascript_options.unknown_context_critical {
367+
*dep.critical_mut() = Some(
368+
create_traceable_error(
369+
"Critical dependency".into(),
370+
"require function is used in a way in which dependencies cannot be statically extracted"
371+
.to_string(),
372+
parser.source_file,
373+
ident.span().into(),
374+
)
375+
.with_severity(Severity::Warn)
376+
.boxed()
377+
.into(),
378+
);
379+
}
378380
parser.dependencies.push(Box::new(dep));
379381
Some(true)
380382
}

packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Object {
202202
requireResolve: true,
203203
strictExportPresence: false,
204204
typeReexportsPresence: no-tolerant,
205+
unknownContextCritical: true,
205206
url: true,
206207
worker: Array [
207208
...,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
it("should import context module", async () => {
2+
try {
3+
require(["unknown/a"], function (a) {
4+
expect(a.default).toBe("a")
5+
})
6+
} catch (e) {
7+
}
8+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
module: {
4+
parser: {
5+
javascript: {
6+
unknownContextCritical: false
7+
}
8+
}
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = []

0 commit comments

Comments
 (0)