Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"format": "prettier --cache --write .",
"build": "tsc --build --verbose",
"test": "mocha --config mocha.config.cjs",
"clean": "tsc --build --clean && rm -rf test/dist",
"snapshot": "CHAI_JEST_SNAPSHOT_UPDATE_ALL=true yarn run test",
"clean": "tsc --build --clean && rm -rf dist test/dist",
"prepublish": "yarn run lint",
"prepack": "yarn run build",
"postpack": "yarn run clean"
Expand Down
8 changes: 4 additions & 4 deletions src/babel-plugin-runtime-dependency.cts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ export const runtimeDependencyPlugin = (): PluginObj<ThisPluginPass> => ({
);
}
const localID = path.node.specifiers[0].local.name;
const source = path.node.source.value;
// In record mode, just save the request and exit.
if (this.opts.record) {
const dependencyID = ID_PREFIX + localID;
this.opts.map.set(path.node.source.value, dependencyID);
this.opts.map.set(source, ID_PREFIX + localID);
return;
}
// Find the identifier that Webpack injected for this source.
const dependencyID = this.opts.map.get(path.node.source.value);
const dependencyID = this.opts.map.get(source);
if (!dependencyID) {
throw Error(
`Encountered a dependency that was not recorded:\n${path.getSource()}`,
`Encountered a dependency that was not recorded: ${source}`,
);
}
// Check that the dependency was declared at the top scope, rename it,
Expand Down
5 changes: 5 additions & 0 deletions src/index.cts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const BABEL_DEFAULTS: Readonly<BabelOptions> = {
compact: false,
sourceMaps: true,
presets: ["@babel/preset-env"],
// A destructured assignment added by Webpack is also transpiled, which we can
// safely assume is an array. Otherwise, Babel will introduce an extra
// unrecorded helper whenever an async module has multiple async dependencies.
// https://github.com/webpack/webpack/blob/be1d35eb02bbedb05ca6ac846fec38a563dcd47f/lib/async-modules/AwaitDependenciesInitFragment.js#L64-L69
assumptions: { iterableIsArray: true },
};

export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
Expand Down
4 changes: 2 additions & 2 deletions test/goodbye.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const require = createRequire(import.meta.url);
const { devDependencies } =
require("../package.json") as typeof import("../package.json");
const context = fileURLToPath(new URL(".", import.meta.url));
const ENTRY_NAMES = ["simple", "chained"] as const;
const CHUNK_CHECKS = { goodbye: 1, parent: 2 } as const;
const ENTRY_NAMES = ["simple", "chained", "multiple"] as const;
const CHUNK_CHECKS = { goodbye: 1, parent: 2, with2: 3 } as const;

const numMatchesTest = (file: string, pattern: RegExp, nMatches: number) =>
async function () {
Expand Down
22 changes: 22 additions & 0 deletions test/goodbye.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ exports[`Goodbye delay with node 0.12 target Compiles with correct debug log 1`]
Transforming module ./src/goodbye.js for chunk src_goodbye_js
Transforming module ./src/goodbye.js for chunk src_parent_js
Transforming module ./src/parent.js for chunk src_parent_js
Transforming module ./src/another.js for chunk src_with2_js
Transforming module ./src/goodbye.js for chunk src_with2_js
Transforming module ./src/with2.js for chunk src_with2_js

goodbye-v0.12 compiled successfully"
`;

exports[`Goodbye delay with node 0.12 target using runtime Compiles with correct debug log 1`] = `
"DEBUG LOG from TransformAsyncModulesPlugin
Adding dependencies for top level await parsed in ./src/goodbye.js
Adding dependencies for top level await parsed in ./src/another.js
Adding and reprocessing dependencies for ./src/parent.js
Adding and reprocessing dependencies for ./src/with2.js
Transforming module ./src/goodbye.js for chunk src_goodbye_js
Transforming module ./src/goodbye.js for chunk src_parent_js
Transforming module ./src/parent.js for chunk src_parent_js
Transforming module ./src/another.js for chunk src_with2_js
Transforming module ./src/goodbye.js for chunk src_with2_js
Transforming module ./src/with2.js for chunk src_with2_js

goodbye-v0.12-runtime compiled successfully"
`;
Expand All @@ -25,17 +33,25 @@ exports[`Goodbye delay with node 6 target Compiles with correct debug log 1`] =
Transforming module ./src/goodbye.js for chunk src_goodbye_js
Transforming module ./src/goodbye.js for chunk src_parent_js
Transforming module ./src/parent.js for chunk src_parent_js
Transforming module ./src/another.js for chunk src_with2_js
Transforming module ./src/goodbye.js for chunk src_with2_js
Transforming module ./src/with2.js for chunk src_with2_js

goodbye-v6 compiled successfully"
`;

exports[`Goodbye delay with node 6 target using runtime Compiles with correct debug log 1`] = `
"DEBUG LOG from TransformAsyncModulesPlugin
Adding dependencies for top level await parsed in ./src/goodbye.js
Adding dependencies for top level await parsed in ./src/another.js
Adding and reprocessing dependencies for ./src/parent.js
Adding and reprocessing dependencies for ./src/with2.js
Transforming module ./src/goodbye.js for chunk src_goodbye_js
Transforming module ./src/goodbye.js for chunk src_parent_js
Transforming module ./src/parent.js for chunk src_parent_js
Transforming module ./src/another.js for chunk src_with2_js
Transforming module ./src/goodbye.js for chunk src_with2_js
Transforming module ./src/with2.js for chunk src_with2_js

goodbye-v6-runtime compiled successfully"
`;
Expand All @@ -45,6 +61,9 @@ exports[`Goodbye delay with node 8 target Compiles with correct debug log 1`] =
Transforming module ./src/goodbye.js for chunk src_goodbye_js
Transforming module ./src/goodbye.js for chunk src_parent_js
Transforming module ./src/parent.js for chunk src_parent_js
Transforming module ./src/another.js for chunk src_with2_js
Transforming module ./src/goodbye.js for chunk src_with2_js
Transforming module ./src/with2.js for chunk src_with2_js

goodbye-v8 compiled successfully"
`;
Expand All @@ -54,6 +73,9 @@ exports[`Goodbye delay with node 8 target using runtime Compiles with correct de
Transforming module ./src/goodbye.js for chunk src_goodbye_js
Transforming module ./src/goodbye.js for chunk src_parent_js
Transforming module ./src/parent.js for chunk src_parent_js
Transforming module ./src/another.js for chunk src_with2_js
Transforming module ./src/goodbye.js for chunk src_with2_js
Transforming module ./src/with2.js for chunk src_with2_js

goodbye-v8-runtime compiled successfully"
`;
2 changes: 2 additions & 0 deletions test/src/another.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Just another simple TLA
export const zero = await Promise.resolve(0);
2 changes: 2 additions & 0 deletions test/src/multiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Same as chained but uses a parent with 2 dependencies.
export const goodbye = import("./with2.js").then(({ pg }) => pg);
4 changes: 4 additions & 0 deletions test/src/with2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This is the same as parent.js, except it also depends on another TLA.
import * as pg from "./goodbye.js";
import { zero } from "./another.js";
export { pg, zero };