|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | +const MinimizerPlugin = require("minimizer-webpack-plugin"); |
| 5 | +const { canMinifyByPath } = /** @type {EXPECTED_ANY} */ ( |
| 6 | + // @ts-expect-error -- the plugin does not publish declarations for this test hook |
| 7 | + require("minimizer-webpack-plugin/dist/implementation") |
| 8 | +); |
| 9 | +const webpack = require("../.."); |
| 10 | + |
| 11 | +describe("default minimizer module paths", () => { |
| 12 | + /** |
| 13 | + * @param {import("../..").Configuration} config configuration |
| 14 | + * @returns {EXPECTED_ANY} raw minimizer options |
| 15 | + */ |
| 16 | + const getRawOptions = (config) => { |
| 17 | + const originalApply = MinimizerPlugin.prototype.apply; |
| 18 | + /** @type {EXPECTED_ANY} */ |
| 19 | + let rawOptions; |
| 20 | + MinimizerPlugin.prototype.apply = function () { |
| 21 | + rawOptions = /** @type {EXPECTED_ANY} */ (this).rawOptions; |
| 22 | + }; |
| 23 | + try { |
| 24 | + webpack({ ...config, mode: "production" }); |
| 25 | + } finally { |
| 26 | + MinimizerPlugin.prototype.apply = originalApply; |
| 27 | + } |
| 28 | + return rawOptions; |
| 29 | + }; |
| 30 | + |
| 31 | + const expectModulePathDispatch = (/** @type {EXPECTED_ANY} */ rawOptions) => { |
| 32 | + expect(rawOptions).toBeDefined(); |
| 33 | + const implementations = Array.isArray(rawOptions.minify) |
| 34 | + ? rawOptions.minify |
| 35 | + : [rawOptions.minify]; |
| 36 | + expect( |
| 37 | + implementations.every( |
| 38 | + (/** @type {EXPECTED_ANY} */ implementation) => |
| 39 | + typeof implementation === "string" || |
| 40 | + (implementation && typeof implementation.path === "string") |
| 41 | + ) |
| 42 | + ).toBe(true); |
| 43 | + // This is the plugin's exact guard for worker.minify. A function, |
| 44 | + // function-valued option, or mixed implementation would make it false. |
| 45 | + expect(canMinifyByPath({ |
| 46 | + minimizer: { |
| 47 | + implementation: rawOptions.minify, |
| 48 | + options: rawOptions.minimizerOptions || {} |
| 49 | + } |
| 50 | + })).toBe(true); |
| 51 | + }; |
| 52 | + |
| 53 | + it("keeps the JavaScript task on the module-path dispatch", () => { |
| 54 | + expectModulePathDispatch( |
| 55 | + getRawOptions({ |
| 56 | + entry: path.resolve(__dirname, "../../package.json") |
| 57 | + }) |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it("keeps the multi-language task on the module-path dispatch", () => { |
| 62 | + expectModulePathDispatch( |
| 63 | + getRawOptions({ |
| 64 | + entry: path.resolve(__dirname, "../../package.json"), |
| 65 | + experiments: { css: true, html: true } |
| 66 | + }) |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
0 commit comments