|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** @import { Compilation, Compiler } from "../../../../" */ |
| 4 | + |
| 5 | +// A CommonJS entry module adds `export default __webpack_exports__` so that the |
| 6 | +// library stays importable, which only holds while no merged module exports a |
| 7 | +// real `default` — two exports of that name are not a module any engine loads. |
| 8 | +/** |
| 9 | + * @param {boolean} synthetic whether the synthetic default export is expected |
| 10 | + * @returns {(this: Compiler) => void} plugin |
| 11 | + */ |
| 12 | +const expectOneDefaultExport = (synthetic) => |
| 13 | + function expectOneDefaultExportPlugin() { |
| 14 | + /** |
| 15 | + * @param {Compilation} compilation compilation |
| 16 | + */ |
| 17 | + const handler = (compilation) => { |
| 18 | + compilation.hooks.afterProcessAssets.tap("testcase", (assets) => { |
| 19 | + const source = assets["main.mjs"].source().toString(); |
| 20 | + const [, names = ""] = /export \{([^}]*)\};/.exec(source) || []; |
| 21 | + expect(names).toContain(" as fromB"); |
| 22 | + if (synthetic) { |
| 23 | + expect(source).toContain("export default __webpack_exports__;"); |
| 24 | + expect(names).not.toContain(" as default"); |
| 25 | + expect(names).toContain(" as fromC"); |
| 26 | + } else { |
| 27 | + expect(source).not.toContain("export default "); |
| 28 | + expect(names.match(/ as default\b/g)).toHaveLength(1); |
| 29 | + expect(names).toContain(" as fromA"); |
| 30 | + } |
| 31 | + }); |
| 32 | + }; |
| 33 | + this.hooks.compilation.tap("testcase", handler); |
| 34 | + }; |
| 35 | + |
| 36 | +/** |
| 37 | + * @param {string[]} entry entry modules, the last one CommonJS |
| 38 | + * @param {boolean} synthetic whether the synthetic default export is expected |
| 39 | + * @returns {import("../../../../").Configuration} config |
| 40 | + */ |
| 41 | +const config = (entry, synthetic) => ({ |
| 42 | + entry: { main: entry }, |
| 43 | + target: "node14", |
| 44 | + output: { |
| 45 | + filename: "[name].mjs", |
| 46 | + module: true, |
| 47 | + library: { entryExports: "all", type: "module" } |
| 48 | + }, |
| 49 | + experiments: { outputModule: true }, |
| 50 | + optimization: { concatenateModules: false }, |
| 51 | + plugins: [expectOneDefaultExport(synthetic)] |
| 52 | +}); |
| 53 | + |
| 54 | +/** @type {import("../../../../").Configuration[]} */ |
| 55 | +module.exports = [ |
| 56 | + config(["./a.js", "./b.js"], false), |
| 57 | + config(["./c.js", "./b.js"], true) |
| 58 | +]; |
0 commit comments