flag all modules as used for Dll

fix problem that modules were removed before they were flagged as used

fixes #10189

backport of #10205

fixes #10475
This commit is contained in:
Tobias Koppers 2020-02-28 16:18:02 +01:00
parent d147689836
commit c1aa9d4506
22 changed files with 175 additions and 3 deletions

View File

@ -5,8 +5,8 @@
"use strict";
const DllEntryPlugin = require("./DllEntryPlugin");
const FlagAllModulesAsUsedPlugin = require("./FlagAllModulesAsUsedPlugin");
const LibManifestPlugin = require("./LibManifestPlugin");
const FlagInitialModulesAsUsedPlugin = require("./FlagInitialModulesAsUsedPlugin");
const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/DllPlugin.json");
@ -41,7 +41,7 @@ class DllPlugin {
});
new LibManifestPlugin(this.options).apply(compiler);
if (!this.options.entryOnly) {
new FlagInitialModulesAsUsedPlugin("DllPlugin").apply(compiler);
new FlagAllModulesAsUsedPlugin("DllPlugin").apply(compiler);
}
}
}

View File

@ -0,0 +1,38 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/** @typedef {import("./Compiler")} Compiler */
class FlagAllModulesAsUsedPlugin {
constructor(explanation) {
this.explanation = explanation;
}
/**
* @param {Compiler} compiler webpack compiler
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"FlagAllModulesAsUsedPlugin",
compilation => {
compilation.hooks.optimizeDependencies.tap(
"FlagAllModulesAsUsedPlugin",
modules => {
for (const module of modules) {
module.used = true;
module.usedExports = true;
module.addReason(null, null, this.explanation);
}
}
);
}
);
}
}
module.exports = FlagAllModulesAsUsedPlugin;

View File

@ -93,7 +93,7 @@ class FlagDependencyUsagePlugin {
};
for (const module of modules) {
module.used = false;
if (!module.used) module.used = false;
}
/** @type {[Module, DependenciesBlock, UsedExports][]} */

View File

@ -0,0 +1,3 @@
export default function createB() {
return "b";
}

View File

@ -0,0 +1,3 @@
export default function createC() {
return "c";
}

View File

@ -0,0 +1,3 @@
import { a } from "./module";
export default a();

View File

@ -0,0 +1,12 @@
import createB from "./dependency";
import createC from "./dependency2";
export function a() {
return "a";
}
export { createB as b };
export function c() {
return createC();
}

View File

@ -0,0 +1 @@
exports.noTests = true;

View File

@ -0,0 +1,31 @@
var path = require("path");
var webpack = require("../../../../");
module.exports = {
entry: ["./index"],
output: {
filename: "dll.js",
chunkFilename: "[id].dll.js",
libraryTarget: "commonjs2"
},
module: {
rules: [
{
test: /0-create-dll.(module|dependency)/,
sideEffects: false
}
]
},
optimization: {
usedExports: true,
sideEffects: true
},
plugins: [
new webpack.DllPlugin({
path: path.resolve(
__dirname,
"../../../js/config/dll-plugin-side-effects/manifest0.json"
)
})
]
};

View File

@ -0,0 +1,9 @@
it("should include all exports and modules in the dll", function() {
const { a, b, c } = require("dll/module");
expect(typeof a).toBe("function");
expect(a()).toBe("a");
expect(typeof b).toBe("function");
expect(b()).toBe("b");
expect(typeof c).toBe("function");
expect(c()).toBe("c");
});

View File

@ -0,0 +1,12 @@
var webpack = require("../../../../");
module.exports = {
plugins: [
new webpack.DllReferencePlugin({
manifest: require("../../../js/config/dll-plugin-side-effects/manifest0.json"), // eslint-disable-line node/no-missing-require
name: "../0-create-dll/dll.js",
scope: "dll",
sourceType: "commonjs2"
})
]
};

View File

@ -27,6 +27,7 @@ module.exports = {
]
},
optimization: {
usedExports: true,
sideEffects: true
},
plugins: [

View File

@ -0,0 +1,3 @@
import { constants } from "test-package";
var x = constants;

View File

@ -0,0 +1,2 @@
export const constant1 = 'constant1';
export const constant2 = 'constant2';

View File

@ -0,0 +1,5 @@
import * as _constants from './constants';
export var constants = _constants;
export { default as someFunction } from './someFunction';
console.log(constants);

View File

@ -0,0 +1,4 @@
{
"main": "index.js",
"sideEffects": false
}

View File

@ -0,0 +1,3 @@
export default function someFunction() {
console.log('This is some function');
}

View File

@ -0,0 +1,7 @@
export const constant1 = 'constant1';
export const constant2 = 'constant2';
export default {
constant1,
constant2,
};

View File

@ -0,0 +1 @@
exports.noTests = true;

View File

@ -0,0 +1,19 @@
var path = require("path");
var webpack = require("../../../../");
module.exports = {
entry: ["./index.js"],
output: {
filename: "dll.js",
chunkFilename: "[id].dll.js",
libraryTarget: "commonjs2"
},
plugins: [
new webpack.DllPlugin({
path: path.resolve(
__dirname,
"../../../js/config/dll-plugin/issue-10475.json"
)
})
]
};

View File

@ -0,0 +1,3 @@
it("should have all modules", () => {
require("dll/index.js");
});

View File

@ -0,0 +1,12 @@
var webpack = require("../../../../");
module.exports = {
plugins: [
new webpack.DllReferencePlugin({
manifest: require("../../../js/config/dll-plugin/issue-10475.json"), // eslint-disable-line node/no-missing-require
name: "../0-issue-10475/dll.js",
scope: "dll",
sourceType: "commonjs2"
})
]
};