Revert "fix unused modules in chunk when optimizing runtime-specific "

This commit is contained in:
Tobias Koppers 2020-10-15 20:17:26 +02:00 committed by GitHub
parent b1a11fc6b6
commit 2888c8a406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 124 deletions

View File

@ -416,7 +416,7 @@ class ExportsInfo {
/**
* @param {RuntimeSpec} runtime the runtime
* @returns {boolean} true, when the module exports are used in any way
* @returns {boolean} true, when the module is used in any way
*/
isUsed(runtime) {
if (this._redirectTo !== undefined) {
@ -436,17 +436,6 @@ class ExportsInfo {
return false;
}
/**
* @param {RuntimeSpec} runtime the runtime
* @returns {boolean} true, when the module is used in any way
*/
isModuleUsed(runtime) {
if (this.isUsed(runtime)) return true;
if (this._sideEffectsOnlyInfo.getUsed(runtime) !== UsageState.Unused)
return true;
return false;
}
/**
* @param {RuntimeSpec} runtime the runtime
* @returns {SortableSet<string> | boolean | null} set of used exports, or true (when namespace object is used), or false (when unused), or null (when unknown)

View File

@ -63,15 +63,9 @@ class FlagDependencyUsagePlugin {
* @param {Module} module module to process
* @param {(string[] | ReferencedExport)[]} usedExports list of used exports
* @param {RuntimeSpec} runtime part of which runtime
* @param {boolean} forceSideEffects always apply side effects
* @returns {void}
*/
const processReferencedModule = (
module,
usedExports,
runtime,
forceSideEffects
) => {
const processReferencedModule = (module, usedExports, runtime) => {
const exportsInfo = moduleGraph.getExportsInfo(module);
if (usedExports.length > 0) {
if (!module.buildMeta || !module.buildMeta.exportsType) {
@ -149,12 +143,10 @@ class FlagDependencyUsagePlugin {
// This module won't be evaluated in this case
// TODO webpack 6 remove this check
if (
!forceSideEffects &&
module.factoryMeta !== undefined &&
module.factoryMeta.sideEffectFree
) {
)
return;
}
if (exportsInfo.setUsedForSideEffectsOnly(runtime)) {
queue.enqueue(module, runtime);
}
@ -250,18 +242,12 @@ class FlagDependencyUsagePlugin {
for (const [module, referencedExports] of map) {
if (Array.isArray(referencedExports)) {
processReferencedModule(
module,
referencedExports,
runtime,
false
);
processReferencedModule(module, referencedExports, runtime);
} else {
processReferencedModule(
module,
Array.from(referencedExports.values()),
runtime,
false
runtime
);
}
}
@ -284,12 +270,8 @@ class FlagDependencyUsagePlugin {
const processEntryDependency = (dep, runtime) => {
const module = moduleGraph.getModule(dep);
if (module) {
processReferencedModule(
module,
NO_EXPORTS_REFERENCED,
runtime,
true
);
processReferencedModule(module, NO_EXPORTS_REFERENCED, runtime);
queue.enqueue(module, runtime);
}
};
/** @type {RuntimeSpec} */
@ -325,59 +307,28 @@ class FlagDependencyUsagePlugin {
);
if (!this.global) {
compilation.hooks.afterChunks.tap("FlagDependencyUsagePlugin", () => {
const processEntrypoint = entrypoint => {
const runtime = getEntryRuntime(
compilation,
entrypoint.name,
entrypoint.options
/** @type {Map<Chunk, string>} */
const runtimeChunks = new Map();
for (const entrypoint of compilation.entrypoints.values()) {
runtimeChunks.set(
entrypoint.getRuntimeChunk(),
entrypoint.options.runtime
);
for (const chunk of entrypoint
.getEntrypointChunk()
.getAllReferencedChunks()) {
}
for (const entrypoint of compilation.asyncEntrypoints) {
runtimeChunks.set(
entrypoint.getRuntimeChunk(),
entrypoint.options.runtime
);
}
for (const [runtimeChunk, runtimeName] of runtimeChunks) {
const runtime = runtimeName || runtimeChunk.name;
for (const chunk of runtimeChunk.getAllReferencedChunks()) {
chunk.runtime = mergeRuntime(chunk.runtime, runtime);
}
};
for (const entrypoint of compilation.entrypoints.values()) {
processEntrypoint(entrypoint);
}
for (const asyncEntry of compilation.asyncEntrypoints) {
processEntrypoint(asyncEntry);
}
});
compilation.hooks.optimizeChunkModules.tap(
"FlagDependencyUsagePlugin",
(chunks, modules) => {
for (const module of modules) {
const exportsInfo = compilation.moduleGraph.getExportsInfo(
module
);
let removeFromRuntimes = undefined;
for (const runtime of compilation.chunkGraph.getModuleRuntimes(
module
)) {
if (!exportsInfo.isModuleUsed(runtime)) {
if (removeFromRuntimes === undefined) {
removeFromRuntimes = new Set();
}
removeFromRuntimes.add(runtime);
}
}
if (removeFromRuntimes !== undefined) {
for (const chunk of compilation.chunkGraph.getModuleChunksIterable(
module
)) {
if (removeFromRuntimes.has(chunk.runtime)) {
compilation.chunkGraph.disconnectChunkAndModule(
chunk,
module
);
}
}
}
}
}
);
}
});
}

View File

@ -1508,7 +1508,7 @@ ${defineGetters}`
const orderedConcatenationList = this._createConcatenationList(
this.rootModule,
this._modules,
runtime,
undefined,
moduleGraph
);
return orderedConcatenationList.map((info, index) => {

View File

@ -44,7 +44,7 @@ exports.getEntryRuntime = (compilation, name, options) => {
result = mergeRuntimeOwned(result, runtime || name);
}
}
return result || name;
return result;
} else {
return runtime || name;
}

View File

@ -287,7 +287,7 @@ const describeCases = config => {
if (module.substr(0, 2) === "./") {
const p = path.join(outputDirectory, module);
const fn = vm.runInThisContext(
"(function(require, module, exports, __dirname, __filename, it, expect) {" +
"(function(require, module, exports, __dirname, it, expect) {" +
"global.expect = expect;" +
'function nsObj(m) { Object.defineProperty(m, Symbol.toStringTag, { value: "Module" }); return m; }' +
fs.readFileSync(p, "utf-8") +
@ -304,7 +304,6 @@ const describeCases = config => {
m,
m.exports,
outputDirectory,
p,
_it,
expect
);

View File

@ -1,12 +0,0 @@
import { Worker } from "worker_threads";
import { X } from "./module";
// test
it("should compile", done => {
expect(X()).toBe("X");
const worker = new Worker(new URL("worker.js", import.meta.url));
worker.once("message", value => {
expect(value).toBe(42);
done();
});
});

View File

@ -1,9 +0,0 @@
import value from "package";
export function X() {
return "X";
}
export function Y() {
return value;
}

View File

@ -1,2 +0,0 @@
exports.default = 42;
module.exports = exports.default;

View File

@ -1,3 +0,0 @@
{
"sideEffects": false
}

View File

@ -1,5 +0,0 @@
var supportsWorker = require("../../../helpers/supportsWorker");
module.exports = function (config) {
return supportsWorker();
};

View File

@ -1,4 +0,0 @@
import { Y } from "./module";
import { parentPort } from "worker_threads";
parentPort.postMessage(Y());

1
types.d.ts vendored
View File

@ -3076,7 +3076,6 @@ declare abstract class ExportsInfo {
setAllKnownExportsUsed(runtime: string | SortableSet<string>): boolean;
setUsedForSideEffectsOnly(runtime: string | SortableSet<string>): boolean;
isUsed(runtime: string | SortableSet<string>): boolean;
isModuleUsed(runtime: string | SortableSet<string>): boolean;
getUsedExports(
runtime: string | SortableSet<string>
): boolean | SortableSet<string>;