From 2aa209b9ab6342e7185dee0232c8de839508c97f Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 7 Apr 2023 15:51:05 +0530 Subject: [PATCH 1/2] fix: support [contenthash] template in DllPlugin --- lib/LibManifestPlugin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/LibManifestPlugin.js b/lib/LibManifestPlugin.js index 398e2261205..e93adce5223 100644 --- a/lib/LibManifestPlugin.js +++ b/lib/LibManifestPlugin.js @@ -49,7 +49,8 @@ class LibManifestPlugin { const name = this.options.name && compilation.getPath(this.options.name, { - chunk + chunk, + contentHashType: "javascript" }); const content = Object.create(null); for (const module of chunkGraph.getOrderedChunkModulesIterable( From fc3bba63cee8cdd8780e10d34d7f2a6956b2ee47 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 7 Apr 2023 15:53:25 +0530 Subject: [PATCH 2/2] test: add cases for DllPlugin usage with [contenthash] --- .../0-create-dll-with-contenthash/_d.js | 1 + .../0-create-dll-with-contenthash/_e.js | 3 + .../0-create-dll-with-contenthash/a.js | 1 + .../0-create-dll-with-contenthash/b.js | 3 + .../0-create-dll-with-contenthash/c.js | 1 + .../0-create-dll-with-contenthash/d.js | 1 + .../0-create-dll-with-contenthash/e.js | 4 ++ .../0-create-dll-with-contenthash/e1.js | 3 + .../0-create-dll-with-contenthash/e2.js | 3 + .../0-create-dll-with-contenthash/ee1.js | 2 + .../0-create-dll-with-contenthash/ee2.js | 2 + .../0-create-dll-with-contenthash/f.jsx | 1 + .../0-create-dll-with-contenthash/g-loader.js | 4 ++ .../0-create-dll-with-contenthash/g.abc.js | 1 + .../0-create-dll-with-contenthash/h.js | 1 + .../0-create-dll-with-contenthash/h1.js | 2 + .../0-create-dll-with-contenthash/ha.js | 1 + .../0-create-dll-with-contenthash/hb.js | 1 + .../test.config.js | 1 + .../webpack.config.js | 44 ++++++++++++++ .../4-use-dll-with-contenthash/e.js | 2 + .../4-use-dll-with-contenthash/index.js | 60 +++++++++++++++++++ .../webpack.config.js | 17 ++++++ 23 files changed, 159 insertions(+) create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/_d.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/_e.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/a.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/b.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/c.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/d.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/e.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/e1.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/e2.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/ee1.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/ee2.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/f.jsx create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/g-loader.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/g.abc.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/h.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/h1.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/ha.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/hb.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/test.config.js create mode 100644 test/configCases/dll-plugin/0-create-dll-with-contenthash/webpack.config.js create mode 100644 test/configCases/dll-plugin/4-use-dll-with-contenthash/e.js create mode 100644 test/configCases/dll-plugin/4-use-dll-with-contenthash/index.js create mode 100644 test/configCases/dll-plugin/4-use-dll-with-contenthash/webpack.config.js diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/_d.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/_d.js new file mode 100644 index 00000000000..d108c9a3722 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/_d.js @@ -0,0 +1 @@ +import "./d"; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/_e.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/_e.js new file mode 100644 index 00000000000..586eb3aa06b --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/_e.js @@ -0,0 +1,3 @@ +import "./e1"; +import "./e2"; +import "./e"; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/a.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/a.js new file mode 100644 index 00000000000..6cd1d0075d4 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/a.js @@ -0,0 +1 @@ +module.exports = "a"; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/b.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/b.js new file mode 100644 index 00000000000..58a90d8f841 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/b.js @@ -0,0 +1,3 @@ +module.exports = function() { + return import("./c"); +} diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/c.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/c.js new file mode 100644 index 00000000000..b2091de76d6 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/c.js @@ -0,0 +1 @@ +export default "c"; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/d.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/d.js new file mode 100644 index 00000000000..987d6d7e401 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/d.js @@ -0,0 +1 @@ +export default "d"; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/e.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/e.js new file mode 100644 index 00000000000..9fbe80f85cf --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/e.js @@ -0,0 +1,4 @@ +export * from "./e1"; +export * from "./ee2"; + +console.log.bind(console); // side effect to avoid removing module diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/e1.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/e1.js new file mode 100644 index 00000000000..23709cd95ff --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/e1.js @@ -0,0 +1,3 @@ +export * from "./ee1"; + +console.log.bind(console); // side effect to avoid removing module diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/e2.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/e2.js new file mode 100644 index 00000000000..25612746b57 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/e2.js @@ -0,0 +1,3 @@ +export * from "./ee2"; + +console.log.bind(console); // side effect to avoid removing module diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/ee1.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/ee1.js new file mode 100644 index 00000000000..359c69fe3e7 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/ee1.js @@ -0,0 +1,2 @@ +export var x1 = 123; +export var y1 = 456; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/ee2.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/ee2.js new file mode 100644 index 00000000000..634e1a91947 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/ee2.js @@ -0,0 +1,2 @@ +export var x2 = 123; +export var y2 = 456; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/f.jsx b/test/configCases/dll-plugin/0-create-dll-with-contenthash/f.jsx new file mode 100644 index 00000000000..61445975b07 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/f.jsx @@ -0,0 +1 @@ +module.exports = 'f'; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/g-loader.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/g-loader.js new file mode 100644 index 00000000000..c6d8a635121 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/g-loader.js @@ -0,0 +1,4 @@ +/** @type {import("../../../../").LoaderDefinition} */ +module.exports = function (source) { + return source; +}; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/g.abc.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/g.abc.js new file mode 100644 index 00000000000..483352ffbff --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/g.abc.js @@ -0,0 +1 @@ +module.exports = typeof module.id; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/h.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/h.js new file mode 100644 index 00000000000..1fa89a4fb1c --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/h.js @@ -0,0 +1 @@ +export { B } from "./h1.js"; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/h1.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/h1.js new file mode 100644 index 00000000000..a392743d956 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/h1.js @@ -0,0 +1,2 @@ +export { A } from "./ha.js"; +export { B } from "./hb.js"; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/ha.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/ha.js new file mode 100644 index 00000000000..6506d8d86b2 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/ha.js @@ -0,0 +1 @@ +export const A = "A"; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/hb.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/hb.js new file mode 100644 index 00000000000..f3c1f2c5d79 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/hb.js @@ -0,0 +1 @@ +export const B = "B"; \ No newline at end of file diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/test.config.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/test.config.js new file mode 100644 index 00000000000..08ea6c319c8 --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/test.config.js @@ -0,0 +1 @@ +exports.noTests = true; diff --git a/test/configCases/dll-plugin/0-create-dll-with-contenthash/webpack.config.js b/test/configCases/dll-plugin/0-create-dll-with-contenthash/webpack.config.js new file mode 100644 index 00000000000..124c663928e --- /dev/null +++ b/test/configCases/dll-plugin/0-create-dll-with-contenthash/webpack.config.js @@ -0,0 +1,44 @@ +var path = require("path"); +var webpack = require("../../../../"); + +/** @type {import("../../../../").Configuration} */ +module.exports = { + entry: ["./a", "./b", "./_d", "./_e", "./f", "./g.abc", "./h"], + resolve: { + extensions: [".js", ".jsx"] + }, + output: { + filename: "dll.js", + chunkFilename: "[id].dll.js", + libraryTarget: "commonjs2" + }, + module: { + rules: [ + { + test: /\.abc\.js$/, + loader: "./g-loader.js", + options: { + test: 1 + } + }, + { + test: /0-create-dll.h/, + sideEffects: false + } + ] + }, + optimization: { + usedExports: true, + sideEffects: true + }, + plugins: [ + new webpack.DllPlugin({ + path: path.resolve( + __dirname, + "../../../js/config/dll-plugin/manifest0.json" + ), + name: "[name]_[contenthash]", + entryOnly: false + }) + ] +}; diff --git a/test/configCases/dll-plugin/4-use-dll-with-contenthash/e.js b/test/configCases/dll-plugin/4-use-dll-with-contenthash/e.js new file mode 100644 index 00000000000..f490fc4645b --- /dev/null +++ b/test/configCases/dll-plugin/4-use-dll-with-contenthash/e.js @@ -0,0 +1,2 @@ +export * from "dll/e1"; +export * from "dll/e2"; diff --git a/test/configCases/dll-plugin/4-use-dll-with-contenthash/index.js b/test/configCases/dll-plugin/4-use-dll-with-contenthash/index.js new file mode 100644 index 00000000000..d771fcdc8c0 --- /dev/null +++ b/test/configCases/dll-plugin/4-use-dll-with-contenthash/index.js @@ -0,0 +1,60 @@ +import d from "dll/d"; +import { x1, y2 } from "./e"; +import { x2, y1 } from "dll/e"; +import { B } from "dll/h"; + +it("should load a module from dll", function() { + expect(require("dll/a")).toBe("a"); +}); + +it("should load a module of non-default type without extension from dll", function() { + expect(require("dll/f")).toBe("f"); +}); + +it("should load an async module from dll", function(done) { + require("dll/b")() + .then(function(c) { + expect(c).toEqual(nsObj({ default: "c" })); + done(); + }) + .catch(done); +}); + +it("should load an harmony module from dll (default export)", function() { + expect(d).toBe("d"); +}); + +it("should load an harmony module from dll (star export)", function() { + expect(x1).toBe(123); + expect(x2).toBe(123); + expect(y1).toBe(456); + expect(y2).toBe(456); +}); + +it("should load a module with loader applied", function() { + expect(require("dll/g.abc.js")).toBe("number"); +}); + +it("should give modules the correct ids", function() { + expect( + Object.keys(__webpack_modules__) + .filter(m => !m.startsWith("../..")) + .sort() + ).toEqual([ + "./index.js", + "dll-reference ../0-create-dll-with-contenthash/dll.js", + "dll/a.js", + "dll/b.js", + "dll/d.js", + "dll/e.js", + "dll/e1.js", + "dll/e2.js", + "dll/f.jsx", + "dll/g.abc.js", + "dll/h.js" + ]); +}); + +it("should not crash on side-effect-free modules", function() { + expect(B).toBe("B"); +}); diff --git a/test/configCases/dll-plugin/4-use-dll-with-contenthash/webpack.config.js b/test/configCases/dll-plugin/4-use-dll-with-contenthash/webpack.config.js new file mode 100644 index 00000000000..6ea85deee54 --- /dev/null +++ b/test/configCases/dll-plugin/4-use-dll-with-contenthash/webpack.config.js @@ -0,0 +1,17 @@ +var webpack = require("../../../../"); + +/** @type {import("../../../../").Configuration} */ +module.exports = { + optimization: { + moduleIds: "named" + }, + plugins: [ + new webpack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll-with-contenthash/dll.js", + scope: "dll", + sourceType: "commonjs2", + extensions: [".js", ".jsx"] + }) + ] +};