Skip to content

Commit

Permalink
Merge branch 'main' into feat-platform-target-properties-in-compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed May 7, 2024
2 parents 3a498e7 + a679033 commit 31ddbd2
Show file tree
Hide file tree
Showing 28 changed files with 165 additions and 46 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,21 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
id: calculate_architecture
with:
result-encoding: string
script: |
if ('${{ matrix.os }}' === 'macos-latest' && '${{ matrix['node-version'] }}' === '10.x') {
return "x64"
} else {
return ''
}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
architecture: ${{ steps.calculate_architecture.outputs.result }}
cache: "yarn"
# Install old `jest` version and deps for legacy node versions
- run: |
Expand Down
2 changes: 1 addition & 1 deletion lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ module.exports = webpackAsyncContext;`;
const thenFunction =
fakeMap !== 9
? `${arrow ? "id =>" : "function(id)"} {
${this.getReturnModuleObjectSource(fakeMap)}
${this.getReturnModuleObjectSource(fakeMap, true)}
}`
: RuntimeGlobals.require;
return `var map = ${JSON.stringify(map, null, "\t")};
Expand Down
1 change: 1 addition & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ const getResolveDefaults = ({
exportsFields: ["exports"],
roots: [context],
mainFields: ["main"],
importsFields: ["imports"],
byDependency: {
wasm: esmDeps(),
esm: esmDeps(),
Expand Down
85 changes: 47 additions & 38 deletions lib/javascript/JavascriptModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ class JavascriptModulesPlugin {
const lastInlinedModule = last(inlinedModules);
const startupSource = new ConcatSource();
startupSource.add(`var ${RuntimeGlobals.exports} = {};\n`);
const renamedInlinedModules = this.renamedRootModule(
const renamedInlinedModule = this.renameInlineModule(
allModules,
renderContext,
inlinedModules,
Expand All @@ -832,7 +832,7 @@ class JavascriptModulesPlugin {

for (const m of inlinedModules) {
const renderedModule =
renamedInlinedModules.get(m) ||
renamedInlinedModule.get(m) ||
this.renderModule(m, chunkRenderContext, hooks, false);

if (renderedModule) {
Expand Down Expand Up @@ -1400,7 +1400,7 @@ class JavascriptModulesPlugin {
* @param {CompilationHooks} hooks hooks
* @returns {Map<Module, Source>} renamed inlined modules
*/
renamedRootModule(
renameInlineModule(
allModules,
renderContext,
inlinedModules,
Expand All @@ -1409,14 +1409,12 @@ class JavascriptModulesPlugin {
) {
const { runtimeTemplate } = renderContext;

/** @type {Map<Module, { source: Source, ast: any, variables: Set<Variable> }>} */
/** @type {Map<Module, { source: Source, ast: any, variables: Set<Variable>, usedInNonInlined: Set<Variable>}>} */
const inlinedModulesToInfo = new Map();
/** @type {Set<string>} */
const nonInlinedModuleThroughIdentifiers = new Set();
/** @type {Map<Module, Source>} */
const renamedInlinedModules = new Map();
/** @type {Set<{ m: Module, variable: Variable}>} */
const usedIdentifiers = new Set();

for (const m of allModules) {
const isInlinedModule = inlinedModules && inlinedModules.has(m);
Expand All @@ -1443,55 +1441,66 @@ class JavascriptModulesPlugin {
const globalScope = scopeManager.acquire(ast);
if (inlinedModules && inlinedModules.has(m)) {
const moduleScope = globalScope.childScopes[0];
const variables = new Set();
for (const variable of moduleScope.variables) {
variables.add(variable);
}
inlinedModulesToInfo.set(m, { source: moduleSource, ast, variables });
inlinedModulesToInfo.set(m, {
source: moduleSource,
ast,
variables: new Set(moduleScope.variables),
usedInNonInlined: new Set()
});
} else {
for (const ref of globalScope.through) {
nonInlinedModuleThroughIdentifiers.add(ref.identifier.name);
}
}
}

for (const [module, { variables }] of inlinedModulesToInfo) {
for (const [, { variables, usedInNonInlined }] of inlinedModulesToInfo) {
for (const variable of variables) {
if (nonInlinedModuleThroughIdentifiers.has(variable.name)) {
usedIdentifiers.add({ m: module, variable });
usedInNonInlined.add(variable);
}
}
}

const usedName = new Set();
for (const { variable, m } of usedIdentifiers) {
const references = getAllReferences(variable);
const { ast, source: _source } = inlinedModulesToInfo.get(m);
for (const [m, moduleInfo] of inlinedModulesToInfo) {
const { ast, source: _source, usedInNonInlined } = moduleInfo;
const source = new ReplaceSource(_source);
const allIdentifiers = new Set(
references.map(r => r.identifier).concat(variable.identifiers)
);
if (usedInNonInlined.size === 0) {
renamedInlinedModules.set(m, source);
continue;
}

const newName = this.findNewName(
variable.name,
usedName,
m.readableIdentifier(runtimeTemplate.requestShortener)
const usedNames = new Set(
Array.from(inlinedModulesToInfo.get(m).variables).map(v => v.name)
);

for (const identifier of allIdentifiers) {
const r = identifier.range;
const path = getPathInAst(ast, identifier);
if (path && path.length > 1) {
const maybeProperty =
path[1].type === "AssignmentPattern" && path[1].left === path[0]
? path[2]
: path[1];
if (maybeProperty.type === "Property" && maybeProperty.shorthand) {
source.insert(r[1], `: ${newName}`);
continue;
for (const variable of usedInNonInlined) {
const references = getAllReferences(variable);
const allIdentifiers = new Set(
references.map(r => r.identifier).concat(variable.identifiers)
);

const newName = this.findNewName(
variable.name,
usedNames,
m.readableIdentifier(runtimeTemplate.requestShortener)
);
usedNames.add(newName);
for (const identifier of allIdentifiers) {
const r = identifier.range;
const path = getPathInAst(ast, identifier);
if (path && path.length > 1) {
const maybeProperty =
path[1].type === "AssignmentPattern" && path[1].left === path[0]
? path[2]
: path[1];
if (maybeProperty.type === "Property" && maybeProperty.shorthand) {
source.insert(r[1], `: ${newName}`);
continue;
}
}
source.replace(r[0], r[1] - 1, newName);
}
source.replace(r[0], r[1] - 1, newName);
}

renamedInlinedModules.set(m, source);
Expand Down Expand Up @@ -1524,10 +1533,10 @@ class JavascriptModulesPlugin {
}

let i = 0;
let nameWithNumber = Template.toIdentifier(`${oldName}_${i}`);
let nameWithNumber = Template.toIdentifier(`${name}_${i}`);
while (usedName.has(nameWithNumber)) {
i++;
nameWithNumber = Template.toIdentifier(`${oldName}_${i}`);
nameWithNumber = Template.toIdentifier(`${name}_${i}`);
}
return nameWithNumber;
}
Expand Down
3 changes: 3 additions & 0 deletions test/Defaults.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@ describe("snapshots", () => {
"exports",
],
"extensions": Array [],
"importsFields": Array [
"imports",
],
"mainFields": Array [
"main",
],
Expand Down
1 change: 1 addition & 0 deletions test/cases/mjs/namespace-object-lazy/cjs-dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = Promise.resolve(1);
10 changes: 10 additions & 0 deletions test/cases/mjs/namespace-object-lazy/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ it("should receive a namespace object when importing commonjs with __esModule",
.catch(done);
});

it("should resolve the promise returned by the imported dynamic commonjs", function (done) {
const post = "dynamic.js";
import(/* webpackMode: "eager" */ "./cjs-" + post) // context module
.then(function (result) {
expect(result).toBe(1);
done();
})
.catch(done);
});

function contextCJS(name) {
return Promise.all([
import(`./dir-cjs/${name}.js`),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = Promise.resolve(1);
10 changes: 10 additions & 0 deletions test/cases/mjs/non-mjs-namespace-object-lazy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ it("should receive a namespace object when importing commonjs with __esModule",
}).catch(done);
});

it("should resolve the promise returned by the imported dynamic commonjs", function (done) {
const post = "dynamic.js";
import(/* webpackMode: "eager" */ "./cjs-" + post) // context module
.then(function (result) {
expect(result).toBe(1);
done();
})
.catch(done);
});

function contextCJS(name) {
return Promise.all([
import(`./dir-cjs/${name}`),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { value as v1 } from "./module1";
const v2 = require("./module2")
const module3Inc = require("./module3")

var value = 42;
const index_value = 10;
let value = 42;

function inc() {
value++;
Expand All @@ -15,4 +16,5 @@ it("single inlined module should not be wrapped in IIFE", () => {
expect(module3Inc).toBe(undefined);
inc();
expect(value).toBe(43);
expect(index_value).toBe(10);
});
16 changes: 16 additions & 0 deletions test/configCases/output-module/multiple-inlined-module/index-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { value as v1 } from "./module1";
const v2 = require("./module2")

var value = 42;

function inc() {
value++;
}

it("multiple inlined modules should be wrapped in IIFE to isolate from other inlined modules and chunk modules", () => {
expect(value).toBe(42);
expect(v1).toBe(undefined);
expect(v2).toBe(undefined);
inc();
expect(value).toBe(43);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var value = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let value;

export { value };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let value

module.exports = value
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
entry: ["./index-1.js", "./index-2.js"],
output: {
module: true
},
optimization: {
concatenateModules: true
},
experiments: {
outputModule: true
},
target: "es2020"
};
8 changes: 8 additions & 0 deletions test/configCases/resolve-merging/imports-fields/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import d1 from "./pkg.mjs";
import d2 from "#internal";
import { d3, d4 } from "lib";

it("imports field to resolve to the same", () => {
expect(d2).toBe(d1);
expect(d4).toBe(d3);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/configCases/resolve-merging/imports-fields/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "pkg",
"imports": {
"#internal": "./pkg.mjs"
}
}
1 change: 1 addition & 0 deletions test/configCases/resolve-merging/imports-fields/pkg.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'pkg';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: "./index.js",
resolve: {
importsFields: ["hash-start", "..."]
}
};
2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5530,7 +5530,7 @@ declare class JavascriptModulesPlugin {
renderContext: RenderBootstrapContext,
hooks: CompilationHooksJavascriptModulesPlugin
): string;
renamedRootModule(
renameInlineModule(
allModules: Module[],
renderContext: MainRenderContext,
inlinedModules: Set<Module>,
Expand Down
14 changes: 9 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2557,9 +2557,9 @@ es-errors@^1.3.0:
integrity sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==

es-module-lexer@^1.2.1:
version "1.5.0"
resolved "https://registry.yarnpkg.com/es-module-lexer/-/es-module-lexer-1.5.0.tgz#4878fee3789ad99e065f975fdd3c645529ff0236"
integrity sha512-pqrTKmwEIgafsYZAGw9kszYzmagcE/n4dbgwGWLEXg7J4QFJVQRBld8j3Q3GNez79jzxZshq0bcT962QHOghjw==
version "1.5.2"
resolved "https://registry.yarnpkg.com/es-module-lexer/-/es-module-lexer-1.5.2.tgz#00b423304f2500ac59359cc9b6844951f372d497"
integrity sha512-l60ETUTmLqbVbVHv1J4/qj+M8nq7AwMzEcg3kmJDt9dCNrTk+yHcYFf/Kw75pMDwd9mPcIGCG5LcS20SxYRzFA==

es5-ext@^0.10.35, es5-ext@^0.10.46, es5-ext@^0.10.53, es5-ext@^0.10.62, es5-ext@^0.10.64, es5-ext@~0.10.14, es5-ext@~0.10.2, es5-ext@~0.10.46:
version "0.10.64"
Expand Down Expand Up @@ -5073,8 +5073,7 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w==

"prettier-2@npm:prettier@^2", prettier@^2.0.5:
name prettier-2
"prettier-2@npm:prettier@^2":
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
Expand All @@ -5086,6 +5085,11 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

prettier@^2.0.5:
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==

prettier@^3.2.1:
version "3.2.5"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368"
Expand Down

0 comments on commit 31ddbd2

Please sign in to comment.