Skip to content

Commit

Permalink
fix: amd alias define renaming (#9117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Jan 26, 2025
1 parent 12e6012 commit e120d49
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AMDRequireDependenciesBlockParserPlugin {
let mut dep = AMDRequireItemDependency::new(request.as_str().into(), None);
dep.set_optional(parser.in_try);
deps.push(AMDRequireArrayItem::AMDRequireItemDependency { dep_id: *dep.id() });
parser.dependencies.push(Box::new(dep));
block_deps.push(Box::new(dep));
}
}
let range = param.range();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,22 @@ impl JavascriptParserPlugin for RequireEnsureDependenciesBlockParserPlugin {
None
},
))];
/* TODO:
* 1. Webpack calls `parser.in_scope`.
* 2. Webpack sets `parser.state.current = depBlock`, but rspack doesn't support nested block yet.
*/
for item in dependencies_items.iter() {
if let Some(item) = item.as_string() {
deps.push(Box::new(RequireEnsureItemDependency::new(
item.as_str().into(),
expr.span.into(),
)));
} else {
return None;
// TODO: Webpack sets `parser.state.current = depBlock`, but rspack doesn't support nested block yet.
let mut failed = false;
parser.in_function_scope(true, std::iter::empty(), |_| {
for item in dependencies_items.iter() {
if let Some(item) = item.as_string() {
deps.push(Box::new(RequireEnsureItemDependency::new(
item.as_str().into(),
expr.span.into(),
)));
} else {
failed = true;
}
}
});
if failed {
return None;
}
if let Some(success_expr) = &success_expr {
match success_expr.func {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,11 @@ impl JavascriptParser<'_> {
.or_else(|| {
let info = self.get_variable_info(name);
if let Some(info) = info {
if let Some(FreeName::String(_)) = &info.free_name {
if let Some(FreeName::String(name)) = &info.free_name {
let mut eval =
BasicEvaluatedExpression::with_range(ident.span.real_lo(), ident.span.hi.0);
eval.set_identifier(
ident.sym.to_string(),
name.to_owned(),
ExportedVariableInfo::VariableInfo(info.id()),
None,
None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(function (module, exports, require) {
define(function (require, exports, module) {
const { foo, bar } = require('./constants');
exports.foo = foo;
exports.bar = bar;
Expand Down
2 changes: 1 addition & 1 deletion tests/webpack-test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const describeCases = config => {
}).not.toThrow();
optionsArr = [].concat(options);
optionsArr.forEach((options, idx) => {
if (options.amd === undefined) options.amd = {};
if (options.amd === undefined) options.amd = {}; // TODO(AMD): remove this
if (!options.context) options.context = testDirectory;
if (!options.mode) options.mode = "production";
if (!options.optimization) options.optimization = {};
Expand Down
1 change: 1 addition & 0 deletions tests/webpack-test/StatsTestCases.basictest.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe("StatsTestCases", () => {
}

(Array.isArray(options) ? options : [options]).forEach(options => {
if (options.amd === undefined) options.amd = {}; // TODO(AMD): remove this
if (!options.context) options.context = path.join(base, testName);
if (!options.output) options.output = options.output || {};
if (!options.output.path) options.output.path = outputDirectory;
Expand Down
2 changes: 1 addition & 1 deletion tests/webpack-test/TestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const describeCases = config => {
});
let options = {
...testConfig,
amd: {},
amd: {}, // TODO(AMD): remove this
context: casesPath,
entry: "./" + category.name + "/" + testName + "/",
target: config.target || "async-node",
Expand Down
111 changes: 41 additions & 70 deletions tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ cacheable modules 9.59 KiB (javascript) 14.6 KiB (asset)
Rspack x.x.x compiled successfully in X.23"
`;

exports[`StatsTestCases should print correct stats for async-commons-chunk 1`] = `
"chunk (runtime: main) 726.js 21 bytes <{909}> [rendered]
> ./index.js 9:4-13:6
./b.js 21 bytes [built] [code generated]
chunk (runtime: main) main.js (main) 557 bytes (javascript) 6.72 KiB (runtime) >{726}< [entry] [rendered]
> ./ main
dependent modules 42 bytes [dependent] 2 modules
./index.js 515 bytes [built] [code generated]
Rspack x.x.x compiled successfully"
`;

exports[`StatsTestCases should print correct stats for async-commons-chunk-auto 1`] = `
"disabled:
chunk (runtime: main) disabled/async-c.js (async-c) 196 bytes [rendered]
Expand Down Expand Up @@ -574,101 +585,61 @@ Rspack x.x.x compiled successfully in X.23"
exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin 1`] = `
"1 chunks:
asset bundle1.js 4.04 KiB [emitted] (name: main)
chunk (runtime: main) bundle1.js (main) 357 bytes (javascript) 1.84 KiB (runtime) <{909}> >{909}< [entry] [rendered]
asset bundle1.js 3.86 KiB [emitted] (name: main)
chunk (runtime: main) bundle1.js (main) 219 bytes (javascript) 1.84 KiB (runtime) <{909}> >{909}< [entry] [rendered]
./a.js 22 bytes [dependent] [built] [code generated]
./b.js 22 bytes [dependent] [built] [code generated]
./c.js 30 bytes [dependent] [built] [code generated]
./d.js 22 bytes [dependent] [built] [code generated]
./e.js 22 bytes [dependent] [built] [code generated]
./index.js 101 bytes [built] [code generated] [1 warning]
Xdir/limit-chunk-count-plugin|sync 160 bytes [dependent] [built] [code generated]
WARNING in ./index.js
⚠ Critical dependency: the request of a dependency is an expression
╭─[2:8]
1 │ require.ensure([\\"./a\\"], function() {});
2 │ require([\\"./b\\"]);
· ────────
3 │ import(/* webpackChunkName: \\"c\\" */ \\"./c\\");
╰────
1 chunks (Rspack x.x.x) compiled with 1 warning in X.23
./index.js 101 bytes [built] [code generated]
1 chunks (Rspack x.x.x) compiled successfully in X.23
2 chunks:
asset bundle2.js 10.6 KiB [emitted] (name: main)
asset 76.bundle2.js 545 bytes [emitted] (name: c)
chunk (runtime: main) 76.bundle2.js (c) 96 bytes <{76}> <{909}> >{76}< [rendered]
asset bundle2.js 10.3 KiB [emitted] (name: main)
asset 76.bundle2.js 599 bytes [emitted] (name: c)
chunk (runtime: main) 76.bundle2.js (c) 118 bytes <{76}> <{909}> >{76}< [rendered]
dependent modules 44 bytes [dependent]
./d.js 22 bytes [dependent] [built] [code generated]
./e.js 22 bytes [dependent] [built] [code generated]
./a.js 22 bytes [built] [code generated]
./b.js 22 bytes [built] [code generated]
./c.js 30 bytes [built] [code generated]
chunk (runtime: main) bundle2.js (main) 261 bytes (javascript) 8.46 KiB (runtime) >{76}< [entry] [rendered]
./index.js 101 bytes [built] [code generated] [1 warning]
Xdir/limit-chunk-count-plugin|sync 160 bytes [dependent] [built] [code generated]
WARNING in ./index.js
⚠ Critical dependency: the request of a dependency is an expression
╭─[2:8]
1 │ require.ensure([\\"./a\\"], function() {});
2 │ require([\\"./b\\"]);
· ────────
3 │ import(/* webpackChunkName: \\"c\\" */ \\"./c\\");
╰────
2 chunks (Rspack x.x.x) compiled with 1 warning in X.23
chunk (runtime: main) bundle2.js (main) 101 bytes (javascript) 8.46 KiB (runtime) >{76}< [entry] [rendered]
./index.js 101 bytes [built] [code generated]
2 chunks (Rspack x.x.x) compiled successfully in X.23
3 chunks:
asset bundle3.js 10.6 KiB [emitted] (name: main)
asset bundle3.js 10.3 KiB [emitted] (name: main)
asset 76.bundle3.js 385 bytes [emitted] (name: c)
asset 588.bundle3.js 236 bytes [emitted]
chunk (runtime: main) 588.bundle3.js 66 bytes <{76}> <{909}> [rendered]
asset 656.bundle3.js 290 bytes [emitted]
chunk (runtime: main) 656.bundle3.js 88 bytes <{76}> <{909}> [rendered]
./a.js 22 bytes [built] [code generated]
./b.js 22 bytes [built] [code generated]
./d.js 22 bytes [built] [code generated]
./e.js 22 bytes [built] [code generated]
chunk (runtime: main) 76.bundle3.js (c) 30 bytes <{909}> >{588}< [rendered]
chunk (runtime: main) 76.bundle3.js (c) 30 bytes <{909}> >{656}< [rendered]
./c.js 30 bytes [built] [code generated]
chunk (runtime: main) bundle3.js (main) 261 bytes (javascript) 8.46 KiB (runtime) >{588}< >{76}< [entry] [rendered]
./index.js 101 bytes [built] [code generated] [1 warning]
Xdir/limit-chunk-count-plugin|sync 160 bytes [dependent] [built] [code generated]
WARNING in ./index.js
⚠ Critical dependency: the request of a dependency is an expression
╭─[2:8]
1 │ require.ensure([\\"./a\\"], function() {});
2 │ require([\\"./b\\"]);
· ────────
3 │ import(/* webpackChunkName: \\"c\\" */ \\"./c\\");
╰────
3 chunks (Rspack x.x.x) compiled with 1 warning in X.23
chunk (runtime: main) bundle3.js (main) 101 bytes (javascript) 8.46 KiB (runtime) >{656}< >{76}< [entry] [rendered]
./index.js 101 bytes [built] [code generated]
3 chunks (Rspack x.x.x) compiled successfully in X.23
4 chunks:
asset bundle4.js 10.6 KiB [emitted] (name: main)
asset bundle4.js 10.3 KiB [emitted] (name: main)
asset 76.bundle4.js 385 bytes [emitted] (name: c)
asset 345.bundle4.js 182 bytes [emitted]
asset 272.bundle4.js 128 bytes [emitted]
chunk (runtime: main) 272.bundle4.js 22 bytes <{909}> [rendered]
asset 537.bundle4.js 236 bytes [emitted]
asset 697.bundle4.js 128 bytes [emitted]
chunk (runtime: main) 537.bundle4.js 66 bytes <{76}> <{909}> [rendered]
./a.js 22 bytes [built] [code generated]
chunk (runtime: main) 345.bundle4.js 44 bytes <{76}> [rendered]
./b.js 22 bytes [built] [code generated]
./d.js 22 bytes [built] [code generated]
chunk (runtime: main) 697.bundle4.js 22 bytes <{76}> [rendered]
./e.js 22 bytes [built] [code generated]
chunk (runtime: main) 76.bundle4.js (c) 30 bytes <{909}> >{345}< [rendered]
chunk (runtime: main) 76.bundle4.js (c) 30 bytes <{909}> >{537}< >{697}< [rendered]
./c.js 30 bytes [built] [code generated]
chunk (runtime: main) bundle4.js (main) 261 bytes (javascript) 8.46 KiB (runtime) >{272}< >{76}< [entry] [rendered]
./index.js 101 bytes [built] [code generated] [1 warning]
Xdir/limit-chunk-count-plugin|sync 160 bytes [dependent] [built] [code generated]
WARNING in ./index.js
⚠ Critical dependency: the request of a dependency is an expression
╭─[2:8]
1 │ require.ensure([\\"./a\\"], function() {});
2 │ require([\\"./b\\"]);
· ────────
3 │ import(/* webpackChunkName: \\"c\\" */ \\"./c\\");
╰────
4 chunks (Rspack x.x.x) compiled with 1 warning in X.23"
chunk (runtime: main) bundle4.js (main) 101 bytes (javascript) 8.46 KiB (runtime) >{537}< >{76}< [entry] [rendered]
./index.js 101 bytes [built] [code generated]
4 chunks (Rspack x.x.x) compiled successfully in X.23"
`;
exports[`StatsTestCases should print correct stats for logging-debug 1`] = `
Expand Down
22 changes: 11 additions & 11 deletions tests/webpack-test/cases/parsing/issue-2641/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ it("should call error callback on missing module", function(done) {
});
});

// it("should call error callback on missing module in context", function(done) {
// (function(module) {
// require(['./' + module], function(file){}, function(error) {
// try {
// expect(error).toBeInstanceOf(Error);
// expect(error.message).toBe("Cannot find module './missingModule'");
// done();
// } catch(e) { done(e); }
// });
// })('missingModule');
// });
it("should call error callback on missing module in context", function(done) {
(function(module) {
require(['./' + module], function(file){}, function(error) {
try {
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe("Cannot find module './missingModule'");
done();
} catch(e) { done(e); }
});
})('missingModule');
});

it("should call error callback on exception thrown in loading module", function(done) {
require(['./throwing'], function(){}, function(error) {
Expand Down
2 changes: 1 addition & 1 deletion tests/webpack-test/cases/parsing/issue-2641/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return "error callback on missing module in context"}
module.exports = () => {return "amd require context"}


3 changes: 0 additions & 3 deletions tests/webpack-test/cases/parsing/renaming/test.filter.js

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = () => {return "amd require ensure"}
module.exports = () => {return "Nested blocks"}

This file was deleted.

This file was deleted.

2 comments on commit e120d49

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on e120d49 Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
rsdoctor ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on e120d49 Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2025-01-26 f6401df) Current Change
10000_big_production-mode_disable-minimize + exec 37.6 s ± 670 ms 38.9 s ± 1.11 s +3.35 %
10000_development-mode + exec 1.82 s ± 29 ms 1.82 s ± 38 ms -0.08 %
10000_development-mode_hmr + exec 683 ms ± 5.7 ms 688 ms ± 42 ms +0.74 %
10000_production-mode + exec 2.4 s ± 65 ms 2.38 s ± 155 ms -0.73 %
10000_production-mode_persistent-cold + exec 2.61 s ± 135 ms 2.45 s ± 62 ms -6.25 %
10000_production-mode_persistent-hot + exec 1.75 s ± 40 ms 1.72 s ± 286 ms -1.67 %
arco-pro_development-mode + exec 1.74 s ± 120 ms 1.81 s ± 95 ms +4.17 %
arco-pro_development-mode_hmr + exec 388 ms ± 1.9 ms 386 ms ± 0.97 ms -0.29 %
arco-pro_production-mode + exec 3.72 s ± 170 ms 3.72 s ± 217 ms +0.02 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.86 s ± 246 ms 3.7 s ± 164 ms -4.09 %
arco-pro_production-mode_persistent-cold + exec 3.92 s ± 136 ms 3.88 s ± 69 ms -0.96 %
arco-pro_production-mode_persistent-hot + exec 2.49 s ± 117 ms 2.52 s ± 232 ms +1.23 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.78 s ± 55 ms 3.8 s ± 94 ms +0.42 %
large-dyn-imports_development-mode + exec 2.08 s ± 35 ms 2.12 s ± 100 ms +1.62 %
large-dyn-imports_production-mode + exec 2.15 s ± 34 ms 2.14 s ± 38 ms -0.21 %
threejs_development-mode_10x + exec 1.54 s ± 20 ms 1.53 s ± 24 ms -0.39 %
threejs_development-mode_10x_hmr + exec 782 ms ± 28 ms 785 ms ± 8.6 ms +0.38 %
threejs_production-mode_10x + exec 5.41 s ± 116 ms 5.35 s ± 205 ms -1.06 %
threejs_production-mode_10x_persistent-cold + exec 5.53 s ± 178 ms 5.43 s ± 291 ms -1.84 %
threejs_production-mode_10x_persistent-hot + exec 4.75 s ± 135 ms 4.62 s ± 160 ms -2.74 %
10000_big_production-mode_disable-minimize + rss memory 8715 MiB ± 76.3 MiB 8712 MiB ± 90.4 MiB -0.03 %
10000_development-mode + rss memory 639 MiB ± 17.5 MiB 654 MiB ± 25.9 MiB +2.45 %
10000_development-mode_hmr + rss memory 1249 MiB ± 202 MiB 1266 MiB ± 237 MiB +1.31 %
10000_production-mode + rss memory 632 MiB ± 22.6 MiB 642 MiB ± 20.4 MiB +1.64 %
10000_production-mode_persistent-cold + rss memory 748 MiB ± 38.6 MiB 750 MiB ± 17.7 MiB +0.24 %
10000_production-mode_persistent-hot + rss memory 725 MiB ± 12.5 MiB 730 MiB ± 29.7 MiB +0.66 %
arco-pro_development-mode + rss memory 556 MiB ± 33.9 MiB 569 MiB ± 20.3 MiB +2.44 %
arco-pro_development-mode_hmr + rss memory 643 MiB ± 65.9 MiB 658 MiB ± 58.9 MiB +2.24 %
arco-pro_production-mode + rss memory 718 MiB ± 26.6 MiB 718 MiB ± 29.6 MiB -0.01 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 723 MiB ± 47 MiB 716 MiB ± 17.9 MiB -0.98 %
arco-pro_production-mode_persistent-cold + rss memory 855 MiB ± 32.2 MiB 844 MiB ± 37.8 MiB -1.37 %
arco-pro_production-mode_persistent-hot + rss memory 713 MiB ± 21.8 MiB 707 MiB ± 31.2 MiB -0.84 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 720 MiB ± 46.1 MiB 718 MiB ± 14 MiB -0.35 %
large-dyn-imports_development-mode + rss memory 645 MiB ± 7.9 MiB 639 MiB ± 5.94 MiB -1.00 %
large-dyn-imports_production-mode + rss memory 525 MiB ± 3.31 MiB 525 MiB ± 2.33 MiB -0.09 %
threejs_development-mode_10x + rss memory 545 MiB ± 15.7 MiB 546 MiB ± 8.27 MiB +0.18 %
threejs_development-mode_10x_hmr + rss memory 1140 MiB ± 135 MiB 1181 MiB ± 79.3 MiB +3.58 %
threejs_production-mode_10x + rss memory 824 MiB ± 44.9 MiB 822 MiB ± 37 MiB -0.19 %
threejs_production-mode_10x_persistent-cold + rss memory 953 MiB ± 84.1 MiB 941 MiB ± 33.4 MiB -1.27 %
threejs_production-mode_10x_persistent-hot + rss memory 885 MiB ± 34.3 MiB 861 MiB ± 25.7 MiB -2.72 %

Please sign in to comment.