mirror of https://github.com/webpack/webpack.git
Revert "fix unused modules in chunk when optimizing runtime-specific "
This commit is contained in:
parent
b1a11fc6b6
commit
2888c8a406
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1508,7 +1508,7 @@ ${defineGetters}`
|
|||
const orderedConcatenationList = this._createConcatenationList(
|
||||
this.rootModule,
|
||||
this._modules,
|
||||
runtime,
|
||||
undefined,
|
||||
moduleGraph
|
||||
);
|
||||
return orderedConcatenationList.map((info, index) => {
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ exports.getEntryRuntime = (compilation, name, options) => {
|
|||
result = mergeRuntimeOwned(result, runtime || name);
|
||||
}
|
||||
}
|
||||
return result || name;
|
||||
return result;
|
||||
} else {
|
||||
return runtime || name;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
import value from "package";
|
||||
|
||||
export function X() {
|
||||
return "X";
|
||||
}
|
||||
|
||||
export function Y() {
|
||||
return value;
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
exports.default = 42;
|
||||
module.exports = exports.default;
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"sideEffects": false
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
var supportsWorker = require("../../../helpers/supportsWorker");
|
||||
|
||||
module.exports = function (config) {
|
||||
return supportsWorker();
|
||||
};
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
import { Y } from "./module";
|
||||
import { parentPort } from "worker_threads";
|
||||
|
||||
parentPort.postMessage(Y());
|
||||
|
|
@ -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>;
|
||||
|
|
|
|||
Loading…
Reference in New Issue