Skip to content

Commit

Permalink
Merge pull request #10478 from webpack/bugfix/all-modules-dll
Browse files Browse the repository at this point in the history
flag all modules as used for Dll
  • Loading branch information
sokra committed Feb 29, 2020
2 parents d147689 + c1aa9d4 commit 07a4b28
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/DllPlugin.js
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions lib/FlagAllModulesAsUsedPlugin.js
@@ -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;
2 changes: 1 addition & 1 deletion lib/FlagDependencyUsagePlugin.js
Expand Up @@ -93,7 +93,7 @@ class FlagDependencyUsagePlugin {
};

for (const module of modules) {
module.used = false;
if (!module.used) module.used = false;
}

/** @type {[Module, DependenciesBlock, UsedExports][]} */
Expand Down
@@ -0,0 +1,3 @@
export default function createB() {
return "b";
}
@@ -0,0 +1,3 @@
export default function createC() {
return "c";
}
@@ -0,0 +1,3 @@
import { a } from "./module";

export default a();
12 changes: 12 additions & 0 deletions test/configCases/dll-plugin-side-effects/0-create-dll/module.js
@@ -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();
}
@@ -0,0 +1 @@
exports.noTests = true;
@@ -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"
)
})
]
};
9 changes: 9 additions & 0 deletions test/configCases/dll-plugin-side-effects/1-use-dll/index.js
@@ -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");
});
@@ -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"
})
]
};
1 change: 1 addition & 0 deletions test/configCases/dll-plugin/0-create-dll/webpack.config.js
Expand Up @@ -27,6 +27,7 @@ module.exports = {
]
},
optimization: {
usedExports: true,
sideEffects: true
},
plugins: [
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/dll-plugin/0-issue-10475/index.js
@@ -0,0 +1,3 @@
import { constants } from "test-package";

var x = constants;

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.

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.

1 change: 1 addition & 0 deletions test/configCases/dll-plugin/0-issue-10475/test.config.js
@@ -0,0 +1 @@
exports.noTests = true;
19 changes: 19 additions & 0 deletions test/configCases/dll-plugin/0-issue-10475/webpack.config.js
@@ -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"
)
})
]
};
3 changes: 3 additions & 0 deletions test/configCases/dll-plugin/1-issue-10475/index.js
@@ -0,0 +1,3 @@
it("should have all modules", () => {
require("dll/index.js");
});
12 changes: 12 additions & 0 deletions test/configCases/dll-plugin/1-issue-10475/webpack.config.js
@@ -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"
})
]
};

0 comments on commit 07a4b28

Please sign in to comment.