Skip to content

Commit 7954d46

Browse files
committed
fix: esm-commonjs-mix test case
1 parent 82a862a commit 7954d46

4 files changed

Lines changed: 109 additions & 86 deletions

File tree

crates/rspack_plugin_javascript/src/parser_plugin/esm_detection_parser_plugin.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ impl ESMDetectionParserPlugin {
4343
}
4444
}
4545

46+
// nonHarmonyIdentifiers
47+
fn is_non_esm_identifier(name: &str) -> bool {
48+
name == "exports" || name == "define"
49+
}
50+
4651
// Port from https://github.com/webpack/webpack/blob/main/lib/dependencies/HarmonyDetectionParserPlugin.js
4752
impl JavascriptParserPlugin for ESMDetectionParserPlugin {
4853
fn program(&self, parser: &mut JavascriptParser, ast: &Program) -> Option<bool> {
@@ -97,17 +102,35 @@ impl JavascriptParserPlugin for ESMDetectionParserPlugin {
97102
expr: &UnaryExpr,
98103
for_name: &str,
99104
) -> Option<BasicEvaluatedExpression> {
100-
(parser.is_esm && for_name == "exports")
105+
(parser.is_esm && is_non_esm_identifier(for_name))
101106
.then(|| BasicEvaluatedExpression::with_range(expr.span().real_lo(), expr.span_hi().0))
102107
}
103108

109+
fn r#typeof(
110+
&self,
111+
parser: &mut JavascriptParser,
112+
_expr: &UnaryExpr,
113+
for_name: &str,
114+
) -> Option<bool> {
115+
(parser.is_esm && is_non_esm_identifier(for_name)).then_some(true)
116+
}
117+
104118
fn identifier(
105119
&self,
106120
parser: &mut JavascriptParser,
107121
_ident: &Ident,
108122
for_name: &str,
109123
) -> Option<bool> {
110-
(parser.is_esm && for_name == "exports").then_some(true)
124+
(parser.is_esm && is_non_esm_identifier(for_name)).then_some(true)
125+
}
126+
127+
fn call(
128+
&self,
129+
parser: &mut JavascriptParser,
130+
_expr: &swc_core::ecma::ast::CallExpr,
131+
for_name: &str,
132+
) -> Option<bool> {
133+
(parser.is_esm && is_non_esm_identifier(for_name)).then_some(true)
111134
}
112135
}
113136

packages/rspack/etc/core.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9575,7 +9575,7 @@ export { RspackOptionsApply as WebpackOptionsApply }
95759575
// @public (undocumented)
95769576
export interface RspackOptionsNormalized {
95779577
// (undocumented)
9578-
amd?: string;
9578+
amd?: Amd;
95799579
// (undocumented)
95809580
bail?: Bail;
95819581
// (undocumented)

packages/rspack/src/config/adapter.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,19 @@ const getRawModuleRule = (
264264
: undefined,
265265
descriptionData: rule.descriptionData
266266
? Object.fromEntries(
267-
Object.entries(rule.descriptionData).map(([k, v]) => [
268-
k,
269-
getRawRuleSetCondition(v)
270-
])
271-
)
267+
Object.entries(rule.descriptionData).map(([k, v]) => [
268+
k,
269+
getRawRuleSetCondition(v)
270+
])
271+
)
272272
: undefined,
273273
with: rule.with
274274
? Object.fromEntries(
275-
Object.entries(rule.with).map(([k, v]) => [
276-
k,
277-
getRawRuleSetCondition(v)
278-
])
279-
)
275+
Object.entries(rule.with).map(([k, v]) => [
276+
k,
277+
getRawRuleSetCondition(v)
278+
])
279+
)
280280
: undefined,
281281
resource: rule.resource ? getRawRuleSetCondition(rule.resource) : undefined,
282282
resourceQuery: rule.resourceQuery
@@ -303,27 +303,27 @@ const getRawModuleRule = (
303303
resolve: rule.resolve ? getRawResolve(rule.resolve) : undefined,
304304
oneOf: rule.oneOf
305305
? rule.oneOf
306-
.filter(Boolean)
307-
.map((rule, index) =>
308-
getRawModuleRule(
309-
rule as RuleSetRule,
310-
`${path}.oneOf[${index}]`,
311-
options,
312-
(rule as RuleSetRule).type ?? upperType
306+
.filter(Boolean)
307+
.map((rule, index) =>
308+
getRawModuleRule(
309+
rule as RuleSetRule,
310+
`${path}.oneOf[${index}]`,
311+
options,
312+
(rule as RuleSetRule).type ?? upperType
313+
)
313314
)
314-
)
315315
: undefined,
316316
rules: rule.rules
317317
? rule.rules
318-
.filter(Boolean)
319-
.map((rule, index) =>
320-
getRawModuleRule(
321-
rule as RuleSetRule,
322-
`${path}.rules[${index}]`,
323-
options,
324-
(rule as RuleSetRule).type ?? upperType
318+
.filter(Boolean)
319+
.map((rule, index) =>
320+
getRawModuleRule(
321+
rule as RuleSetRule,
322+
`${path}.rules[${index}]`,
323+
options,
324+
(rule as RuleSetRule).type ?? upperType
325+
)
325326
)
326-
)
327327
: undefined,
328328
enforce: rule.enforce
329329
};

packages/rspack/src/config/normalization.ts

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ export const getNormalizedRspackOptions = (
9595
ignoreWarnings:
9696
config.ignoreWarnings !== undefined
9797
? config.ignoreWarnings.map(ignore => {
98-
if (typeof ignore === "function") {
99-
return ignore;
100-
}
101-
return (warning: Error) => {
102-
return ignore.test(warning.message);
103-
};
104-
})
98+
if (typeof ignore === "function") {
99+
return ignore;
100+
}
101+
return (warning: Error) => {
102+
return ignore.test(warning.message);
103+
};
104+
})
105105
: undefined,
106106
name: config.name,
107107
dependencies: config.dependencies,
@@ -112,14 +112,14 @@ export const getNormalizedRspackOptions = (
112112
? { main: {} }
113113
: typeof config.entry === "function"
114114
? (
115-
fn => () =>
116-
Promise.resolve().then(fn).then(getNormalizedEntryStatic)
117-
)(config.entry)
115+
fn => () =>
116+
Promise.resolve().then(fn).then(getNormalizedEntryStatic)
117+
)(config.entry)
118118
: getNormalizedEntryStatic(config.entry),
119119
output: nestedConfig(config.output, output => {
120120
if ("cssHeadDataCompression" in output) {
121121
util.deprecate(
122-
() => { },
122+
() => {},
123123
"cssHeadDataCompression is not used now, see https://github.com/web-infra-dev/rspack/pull/8534, this option could be removed in the future"
124124
)();
125125
}
@@ -128,14 +128,14 @@ export const getNormalizedRspackOptions = (
128128
const libraryAsName = library;
129129
const libraryBase =
130130
typeof library === "object" &&
131-
library &&
132-
!Array.isArray(library) &&
133-
"type" in library
131+
library &&
132+
!Array.isArray(library) &&
133+
"type" in library
134134
? library
135135
: libraryAsName || output.libraryTarget
136136
? ({
137-
name: libraryAsName
138-
} as LibraryOptions)
137+
name: libraryAsName
138+
} as LibraryOptions)
139139
: undefined;
140140
return {
141141
path: output.path,
@@ -350,22 +350,22 @@ export const getNormalizedRspackOptions = (
350350
incremental: optionalNestedConfig(experiments.incremental, options =>
351351
options === true
352352
? ({
353-
make: true,
354-
inferAsyncModules: true,
355-
providedExports: true,
356-
dependenciesDiagnostics: true,
357-
sideEffects: true,
358-
buildChunkGraph: true,
359-
moduleIds: true,
360-
chunkIds: true,
361-
modulesHashes: true,
362-
modulesCodegen: true,
363-
modulesRuntimeRequirements: true,
364-
chunksRuntimeRequirements: true,
365-
chunksHashes: true,
366-
chunksRender: true,
367-
emitAssets: true
368-
} satisfies Incremental)
353+
make: true,
354+
inferAsyncModules: true,
355+
providedExports: true,
356+
dependenciesDiagnostics: true,
357+
sideEffects: true,
358+
buildChunkGraph: true,
359+
moduleIds: true,
360+
chunkIds: true,
361+
modulesHashes: true,
362+
modulesCodegen: true,
363+
modulesRuntimeRequirements: true,
364+
chunksRuntimeRequirements: true,
365+
chunksHashes: true,
366+
chunksRender: true,
367+
emitAssets: true
368+
} satisfies Incremental)
369369
: options
370370
)
371371
})),
@@ -476,14 +476,14 @@ const keyedNestedConfig = <T, R>(
476476
value === undefined
477477
? {}
478478
: Object.keys(value).reduce(
479-
(obj, key) => {
480-
obj[key] = (customKeys && key in customKeys ? customKeys[key] : fn)(
481-
value[key]
482-
);
483-
return obj;
484-
},
485-
{} as Record<string, R>
486-
);
479+
(obj, key) => {
480+
obj[key] = (customKeys && key in customKeys ? customKeys[key] : fn)(
481+
value[key]
482+
);
483+
return obj;
484+
},
485+
{} as Record<string, R>
486+
);
487487
if (customKeys) {
488488
for (const key of Object.keys(customKeys)) {
489489
if (!(key in result)) {
@@ -578,22 +578,22 @@ export interface ModuleOptionsNormalized {
578578
export type ExperimentCacheNormalized =
579579
| boolean
580580
| {
581-
type: "memory";
582-
}
581+
type: "memory";
582+
}
583583
| {
584-
type: "persistent";
585-
buildDependencies: string[];
586-
version: string;
587-
snapshot: {
588-
immutablePaths: Array<string | RegExp>;
589-
unmanagedPaths: Array<string | RegExp>;
590-
managedPaths: Array<string | RegExp>;
591-
};
592-
storage: {
593-
type: "filesystem";
594-
directory: string;
595-
};
596-
};
584+
type: "persistent";
585+
buildDependencies: string[];
586+
version: string;
587+
snapshot: {
588+
immutablePaths: Array<string | RegExp>;
589+
unmanagedPaths: Array<string | RegExp>;
590+
managedPaths: Array<string | RegExp>;
591+
};
592+
storage: {
593+
type: "filesystem";
594+
directory: string;
595+
};
596+
};
597597

598598
export interface ExperimentsNormalized {
599599
cache?: ExperimentCacheNormalized;
@@ -616,8 +616,8 @@ export type IgnoreWarningsNormalized = ((
616616
export type OptimizationRuntimeChunkNormalized =
617617
| false
618618
| {
619-
name: string | ((entrypoint: { name: string }) => string);
620-
};
619+
name: string | ((entrypoint: { name: string }) => string);
620+
};
621621

622622
export interface RspackOptionsNormalized {
623623
name?: Name;

0 commit comments

Comments
 (0)