|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const webpack = require("../../../../"); |
| 6 | + |
| 7 | +/** @type {import("../../../../").Configuration[]} */ |
| 8 | +module.exports = [false, true].map((includeJavaScript) => { |
| 9 | + const name = includeJavaScript ? "mixed" : "virtual"; |
| 10 | + return { |
| 11 | + mode: "production", |
| 12 | + target: "web", |
| 13 | + entry: { [name]: "./entry.js" }, |
| 14 | + experiments: { html: true }, |
| 15 | + output: { |
| 16 | + filename: "[name].[contenthash].js", |
| 17 | + htmlFilename: `${name}.html`, |
| 18 | + publicPath: "", |
| 19 | + crossOriginLoading: "anonymous", |
| 20 | + html: { integrity: true } |
| 21 | + }, |
| 22 | + optimization: { |
| 23 | + minimize: false, |
| 24 | + runtimeChunk: { name: `${name}-runtime` }, |
| 25 | + splitChunks: { |
| 26 | + cacheGroups: { |
| 27 | + shared: { |
| 28 | + /** |
| 29 | + * @param {import("../../../../").Module} module module |
| 30 | + * @returns {boolean} whether the module belongs to the shared chunk |
| 31 | + */ |
| 32 | + test: (module) => |
| 33 | + module.type === "consume-shared-module" || |
| 34 | + (includeJavaScript && |
| 35 | + module.nameForCondition() === |
| 36 | + path.join(__dirname, "vendor.js")), |
| 37 | + chunks: "all", |
| 38 | + enforce: true, |
| 39 | + name: `${name}-shared` |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + }, |
| 44 | + plugins: [ |
| 45 | + new webpack.container.ModuleFederationPlugin({ |
| 46 | + name, |
| 47 | + shared: { |
| 48 | + "./first": { eager: true, requiredVersion: false }, |
| 49 | + "./second": { eager: true, requiredVersion: false } |
| 50 | + } |
| 51 | + }), |
| 52 | + { |
| 53 | + /** |
| 54 | + * @param {import("../../../../").Compiler} compiler compiler |
| 55 | + * @returns {void} |
| 56 | + */ |
| 57 | + apply(compiler) { |
| 58 | + compiler.hooks.afterEmit.tap("CheckSharedChunks", (compilation) => { |
| 59 | + const chunk = /** @type {import("../../../../").Chunk} */ ( |
| 60 | + compilation.namedChunks.get(`${name}-shared`) |
| 61 | + ); |
| 62 | + expect(chunk).toBeDefined(); |
| 63 | + expect(chunk.files.size).toBe(includeJavaScript ? 1 : 0); |
| 64 | + expect(compilation.chunkGraph.getNumberOfChunkModules(chunk)).toBe( |
| 65 | + includeJavaScript ? 3 : 2 |
| 66 | + ); |
| 67 | + }); |
| 68 | + compiler.hooks.thisCompilation.tap("CopyTest", (compilation) => { |
| 69 | + compilation.hooks.processAssets.tap( |
| 70 | + { |
| 71 | + name: "CopyTest", |
| 72 | + stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL |
| 73 | + }, |
| 74 | + () => { |
| 75 | + compilation.emitAsset( |
| 76 | + "test.js", |
| 77 | + new webpack.sources.RawSource( |
| 78 | + fs.readFileSync(path.join(__dirname, "test.js")) |
| 79 | + ) |
| 80 | + ); |
| 81 | + } |
| 82 | + ); |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + ] |
| 87 | + }; |
| 88 | +}); |
0 commit comments