Skip to content

Commit 812f03f

Browse files
authored
Fix extra helper introduced with multiple async dependencies (#34)
1 parent 999e945 commit 812f03f

File tree

8 files changed

+43
-7
lines changed

8 files changed

+43
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"format": "prettier --cache --write .",
4545
"build": "tsc --build --verbose",
4646
"test": "mocha --config mocha.config.cjs",
47-
"clean": "tsc --build --clean && rm -rf test/dist",
47+
"snapshot": "CHAI_JEST_SNAPSHOT_UPDATE_ALL=true yarn run test",
48+
"clean": "tsc --build --clean && rm -rf dist test/dist",
4849
"prepublish": "yarn run lint",
4950
"prepack": "yarn run build",
5051
"postpack": "yarn run clean"

src/babel-plugin-runtime-dependency.cts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ export const runtimeDependencyPlugin = (): PluginObj<ThisPluginPass> => ({
3636
);
3737
}
3838
const localID = path.node.specifiers[0].local.name;
39+
const source = path.node.source.value;
3940
// In record mode, just save the request and exit.
4041
if (this.opts.record) {
41-
const dependencyID = ID_PREFIX + localID;
42-
this.opts.map.set(path.node.source.value, dependencyID);
42+
this.opts.map.set(source, ID_PREFIX + localID);
4343
return;
4444
}
4545
// Find the identifier that Webpack injected for this source.
46-
const dependencyID = this.opts.map.get(path.node.source.value);
46+
const dependencyID = this.opts.map.get(source);
4747
if (!dependencyID) {
4848
throw Error(
49-
`Encountered a dependency that was not recorded:\n${path.getSource()}`,
49+
`Encountered a dependency that was not recorded: ${source}`,
5050
);
5151
}
5252
// Check that the dependency was declared at the top scope, rename it,

src/index.cts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ const BABEL_DEFAULTS: Readonly<BabelOptions> = {
4141
compact: false,
4242
sourceMaps: true,
4343
presets: ["@babel/preset-env"],
44+
// A destructured assignment added by Webpack is also transpiled, which we can
45+
// safely assume is an array. Otherwise, Babel will introduce an extra
46+
// unrecorded helper whenever an async module has multiple async dependencies.
47+
// https://github.com/webpack/webpack/blob/be1d35eb02bbedb05ca6ac846fec38a563dcd47f/lib/async-modules/AwaitDependenciesInitFragment.js#L64-L69
48+
assumptions: { iterableIsArray: true },
4449
};
4550

4651
export class TransformAsyncModulesPlugin implements WebpackPluginInstance {

test/goodbye.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const require = createRequire(import.meta.url);
1212
const { devDependencies } =
1313
require("../package.json") as typeof import("../package.json");
1414
const context = fileURLToPath(new URL(".", import.meta.url));
15-
const ENTRY_NAMES = ["simple", "chained"] as const;
16-
const CHUNK_CHECKS = { goodbye: 1, parent: 2 } as const;
15+
const ENTRY_NAMES = ["simple", "chained", "multiple"] as const;
16+
const CHUNK_CHECKS = { goodbye: 1, parent: 2, with2: 3 } as const;
1717

1818
const numMatchesTest = (file: string, pattern: RegExp, nMatches: number) =>
1919
async function () {

test/goodbye.spec.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ exports[`Goodbye delay with node 0.12 target Compiles with correct debug log 1`]
55
Transforming module ./src/goodbye.js for chunk src_goodbye_js
66
Transforming module ./src/goodbye.js for chunk src_parent_js
77
Transforming module ./src/parent.js for chunk src_parent_js
8+
Transforming module ./src/another.js for chunk src_with2_js
9+
Transforming module ./src/goodbye.js for chunk src_with2_js
10+
Transforming module ./src/with2.js for chunk src_with2_js
811
912
goodbye-v0.12 compiled successfully"
1013
`;
1114

1215
exports[`Goodbye delay with node 0.12 target using runtime Compiles with correct debug log 1`] = `
1316
"DEBUG LOG from TransformAsyncModulesPlugin
1417
Adding dependencies for top level await parsed in ./src/goodbye.js
18+
Adding dependencies for top level await parsed in ./src/another.js
1519
Adding and reprocessing dependencies for ./src/parent.js
20+
Adding and reprocessing dependencies for ./src/with2.js
1621
Transforming module ./src/goodbye.js for chunk src_goodbye_js
1722
Transforming module ./src/goodbye.js for chunk src_parent_js
1823
Transforming module ./src/parent.js for chunk src_parent_js
24+
Transforming module ./src/another.js for chunk src_with2_js
25+
Transforming module ./src/goodbye.js for chunk src_with2_js
26+
Transforming module ./src/with2.js for chunk src_with2_js
1927
2028
goodbye-v0.12-runtime compiled successfully"
2129
`;
@@ -25,17 +33,25 @@ exports[`Goodbye delay with node 6 target Compiles with correct debug log 1`] =
2533
Transforming module ./src/goodbye.js for chunk src_goodbye_js
2634
Transforming module ./src/goodbye.js for chunk src_parent_js
2735
Transforming module ./src/parent.js for chunk src_parent_js
36+
Transforming module ./src/another.js for chunk src_with2_js
37+
Transforming module ./src/goodbye.js for chunk src_with2_js
38+
Transforming module ./src/with2.js for chunk src_with2_js
2839
2940
goodbye-v6 compiled successfully"
3041
`;
3142

3243
exports[`Goodbye delay with node 6 target using runtime Compiles with correct debug log 1`] = `
3344
"DEBUG LOG from TransformAsyncModulesPlugin
3445
Adding dependencies for top level await parsed in ./src/goodbye.js
46+
Adding dependencies for top level await parsed in ./src/another.js
3547
Adding and reprocessing dependencies for ./src/parent.js
48+
Adding and reprocessing dependencies for ./src/with2.js
3649
Transforming module ./src/goodbye.js for chunk src_goodbye_js
3750
Transforming module ./src/goodbye.js for chunk src_parent_js
3851
Transforming module ./src/parent.js for chunk src_parent_js
52+
Transforming module ./src/another.js for chunk src_with2_js
53+
Transforming module ./src/goodbye.js for chunk src_with2_js
54+
Transforming module ./src/with2.js for chunk src_with2_js
3955
4056
goodbye-v6-runtime compiled successfully"
4157
`;
@@ -45,6 +61,9 @@ exports[`Goodbye delay with node 8 target Compiles with correct debug log 1`] =
4561
Transforming module ./src/goodbye.js for chunk src_goodbye_js
4662
Transforming module ./src/goodbye.js for chunk src_parent_js
4763
Transforming module ./src/parent.js for chunk src_parent_js
64+
Transforming module ./src/another.js for chunk src_with2_js
65+
Transforming module ./src/goodbye.js for chunk src_with2_js
66+
Transforming module ./src/with2.js for chunk src_with2_js
4867
4968
goodbye-v8 compiled successfully"
5069
`;
@@ -54,6 +73,9 @@ exports[`Goodbye delay with node 8 target using runtime Compiles with correct de
5473
Transforming module ./src/goodbye.js for chunk src_goodbye_js
5574
Transforming module ./src/goodbye.js for chunk src_parent_js
5675
Transforming module ./src/parent.js for chunk src_parent_js
76+
Transforming module ./src/another.js for chunk src_with2_js
77+
Transforming module ./src/goodbye.js for chunk src_with2_js
78+
Transforming module ./src/with2.js for chunk src_with2_js
5779
5880
goodbye-v8-runtime compiled successfully"
5981
`;

test/src/another.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Just another simple TLA
2+
export const zero = await Promise.resolve(0);

test/src/multiple.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Same as chained but uses a parent with 2 dependencies.
2+
export const goodbye = import("./with2.js").then(({ pg }) => pg);

test/src/with2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This is the same as parent.js, except it also depends on another TLA.
2+
import * as pg from "./goodbye.js";
3+
import { zero } from "./another.js";
4+
export { pg, zero };

0 commit comments

Comments
 (0)