Skip to content

Commit 6c0c525

Browse files
committed
feat: support tree shaking in DllPlugin
This change introduces a new `entryOnly` flag to the DllPlugin options which limits the exposed modules to only those configured as entry points. This allows module concatenation and dead code removal optimizations to take place.
1 parent 6323b17 commit 6c0c525

File tree

11 files changed

+84
-1
lines changed

11 files changed

+84
-1
lines changed

lib/DllPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class DllPlugin {
3333
return true;
3434
});
3535
new LibManifestPlugin(this.options).apply(compiler);
36-
new FlagInitialModulesAsUsedPlugin("DllPlugin").apply(compiler);
36+
if (!this.options.entryOnly) {
37+
new FlagInitialModulesAsUsedPlugin("DllPlugin").apply(compiler);
38+
}
3739
}
3840
}
3941

schemas/plugins/DllPlugin.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"description": "Absolute path to the manifest json file (output)",
2424
"minLength": 1,
2525
"type": "string"
26+
},
27+
"entryOnly": {
28+
"description": "If true, only entry points will be exposed",
29+
"type": "boolean"
2630
}
2731
}
2832
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function foo() {
2+
console.log("foo");
3+
}
4+
5+
export const bar = "bar";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { bar } from "./dep";
2+
export default 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.noTests = true;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var path = require("path");
2+
var webpack = require("../../../../");
3+
4+
module.exports = {
5+
entry: ["."],
6+
output: {
7+
filename: "dll.js",
8+
chunkFilename: "[id].dll.js",
9+
libraryTarget: "commonjs2"
10+
},
11+
plugins: [
12+
new webpack.DllPlugin({
13+
path: path.resolve(
14+
__dirname,
15+
"../../../js/config/dll-plugin-entry/manifest0.json"
16+
),
17+
entryOnly: true
18+
})
19+
]
20+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Answer, { bar } from "dll/index";
2+
3+
it("should load a module from dll", function() {
4+
expect(require("dll/index")).toEqual({ bar: "bar", default: 42 });
5+
});
6+
7+
it("should load an harmony module from dll (default export)", function() {
8+
expect(Answer).toBe(42);
9+
});
10+
11+
it("should load an harmony module from dll (star export)", function() {
12+
expect(bar).toBe("bar");
13+
});
14+
15+
it("should give modules the correct ids", function() {
16+
expect(Object.keys(__webpack_modules__).filter(m => !m.startsWith("../.."))).toEqual([
17+
"./index.js",
18+
"dll-reference ../0-create-dll/dll.js",
19+
"dll/index.js"
20+
]);
21+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var webpack = require("../../../../");
2+
3+
module.exports = {
4+
plugins: [
5+
new webpack.DllReferencePlugin({
6+
manifest: require("../../../js/config/dll-plugin-entry/manifest0.json"), // eslint-disable-line node/no-missing-require
7+
name: "../0-create-dll/dll.js",
8+
scope: "dll",
9+
sourceType: "commonjs2"
10+
}),
11+
new webpack.NamedModulesPlugin()
12+
]
13+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [[/Can't resolve 'dll\/dep'/]];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("dll/dep");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var webpack = require("../../../../");
2+
3+
module.exports = {
4+
plugins: [
5+
new webpack.DllReferencePlugin({
6+
manifest: require("../../../js/config/dll-plugin-entry/manifest0.json"), // eslint-disable-line node/no-missing-require
7+
name: "../0-create-dll/dll.js",
8+
scope: "dll",
9+
sourceType: "commonjs2"
10+
}),
11+
new webpack.NamedModulesPlugin()
12+
]
13+
};

0 commit comments

Comments
 (0)