Skip to content

Commit 3cb4349

Browse files
committed
test: move filter dispatch coverage into minify-option suite
Drop the standalone `test/minimizer-filter.test.js` and re-express the same coverage as snapshot tests in `test/minify-option.test.js`, matching the surrounding style (real fixtures, `readsAssets` / `getErrors` / `getWarnings` snapshots). The four new cases cover: - a single TerserPlugin instance dispatching to terser + html-minifier via the array-of-minimizers form - a single minimizer whose `filter` returns false (asset stays as-is) - a single minimizer whose `filter` returns undefined (treated as accept) - an array of minimizers whose filters all reject the asset
1 parent 324e369 commit 3cb4349

3 files changed

Lines changed: 219 additions & 287 deletions

File tree

test/__snapshots__/minify-option.test.js.snap

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,68 @@ The 'extractComments' option for 'swcMinify' only supports booleans, \\"some\\",
7777

7878
exports[`minify option should report an error when the \`extractComments\` option for \`swcMinify\` uses a function condition: warnings 1`] = `Array []`;
7979

80+
exports[`minify option should skip assets when every minimizer in the \`minify\` array rejects them via \`filter\`: assets 1`] = `
81+
Object {
82+
"main.js": "/******/ (() => { // webpackBootstrap
83+
/******/ \\"use strict\\";
84+
class Point {
85+
constructor(x, y) {
86+
this.x = x;
87+
this.y = y;
88+
}
89+
90+
static distance(a, b) {
91+
const dx = a.x - b.x;
92+
const dy = a.y - b.y;
93+
94+
return Math.hypot(dx, dy);
95+
}
96+
}
97+
98+
console.log('HERE');
99+
100+
/* unused harmony default export */ var __WEBPACK_DEFAULT_EXPORT__ = ((/* unused pure expression or super */ null && (Point)));
101+
102+
/******/ })()
103+
;",
104+
}
105+
`;
106+
107+
exports[`minify option should skip assets when every minimizer in the \`minify\` array rejects them via \`filter\`: errors 1`] = `Array []`;
108+
109+
exports[`minify option should skip assets when every minimizer in the \`minify\` array rejects them via \`filter\`: warnings 1`] = `Array []`;
110+
111+
exports[`minify option should skip assets when the only minimizer's \`filter\` returns \`false\`: assets 1`] = `
112+
Object {
113+
"main.js": "/******/ (() => { // webpackBootstrap
114+
/******/ \\"use strict\\";
115+
class Point {
116+
constructor(x, y) {
117+
this.x = x;
118+
this.y = y;
119+
}
120+
121+
static distance(a, b) {
122+
const dx = a.x - b.x;
123+
const dy = a.y - b.y;
124+
125+
return Math.hypot(dx, dy);
126+
}
127+
}
128+
129+
console.log('HERE');
130+
131+
/* unused harmony default export */ var __WEBPACK_DEFAULT_EXPORT__ = ((/* unused pure expression or super */ null && (Point)));
132+
133+
/******/ })()
134+
;",
135+
}
136+
`;
137+
138+
exports[`minify option should skip assets when the only minimizer's \`filter\` returns \`false\`: errors 1`] = `Array []`;
139+
140+
exports[`minify option should skip assets when the only minimizer's \`filter\` returns \`false\`: warnings 1`] = `Array []`;
141+
80142
exports[`minify option should snapshot with extracting comments: assets 1`] = `
81143
Object {
82144
"main.js": "/*! For license information please see main.js.LICENSE.txt */
@@ -117,6 +179,37 @@ Error",
117179

118180
exports[`minify option should throw an error when an error: warnings 1`] = `Array []`;
119181

182+
exports[`minify option should treat a \`filter\` returning \`undefined\` as accept: assets 1`] = `
183+
Object {
184+
"main.js": "/* undef-filter *//******/ (() => { // webpackBootstrap
185+
/******/ \\"use strict\\";
186+
class Point {
187+
constructor(x, y) {
188+
this.x = x;
189+
this.y = y;
190+
}
191+
192+
static distance(a, b) {
193+
const dx = a.x - b.x;
194+
const dy = a.y - b.y;
195+
196+
return Math.hypot(dx, dy);
197+
}
198+
}
199+
200+
console.log('HERE');
201+
202+
/* unused harmony default export */ var __WEBPACK_DEFAULT_EXPORT__ = ((/* unused pure expression or super */ null && (Point)));
203+
204+
/******/ })()
205+
;",
206+
}
207+
`;
208+
209+
exports[`minify option should treat a \`filter\` returning \`undefined\` as accept: errors 1`] = `Array []`;
210+
211+
exports[`minify option should treat a \`filter\` returning \`undefined\` as accept: warnings 1`] = `Array []`;
212+
120213
exports[`minify option should work using when the \`minify\` option is \`esbuildMinify\` and ECMA modules output: assets 1`] = `
121214
Object {
122215
"main.js": "var t={};t.d=(_,e)=>{for(var o in e)t.o(e,o)&&!t.o(_,o)&&Object.defineProperty(_,o,{enumerable:!0,get:e[o]})},t.o=(_,e)=>Object.prototype.hasOwnProperty.call(_,e);var r={};t.d(r,{A:()=>p});const c=4;function a(){console.log(7+c)}a();const p=a,s=r.A;export{s as default};
@@ -895,6 +988,17 @@ exports[`minify option should work when \`minify\` and \`terserOptions\` are bot
895988
896989
exports[`minify option should work when \`minify\` and \`terserOptions\` are both arrays: warnings 1`] = `Array []`;
897990
991+
exports[`minify option should work when \`minify\` is an array of functions and dispatches by \`filter\`: assets 1`] = `
992+
Object {
993+
"1d477fcae4c6c3852830.html": "<!doctype html> <html lang=\\"en\\"> <head> <meta charset=\\"utf-8\\"/> <title>Hello</title> </head> <body> <h1>Hello, World!</h1> <p> Hello there </p> </body> </html> ",
994+
"main.js": "(()=>{var t={740(t,r,e){\\"use strict\\";t.exports=e.p+\\"1d477fcae4c6c3852830.html\\"}},r={};function e(o){var n=r[o];if(void 0!==n)return n.exports;var c=r[o]={exports:{}};return t[o](c,c.exports,e),c.exports}e.m=t,e.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(t){if(\\"object\\"==typeof window)return window}}(),e.o=(t,r)=>Object.prototype.hasOwnProperty.call(t,r),(()=>{var t;e.g.importScripts&&(t=e.g.location+\\"\\");var r=e.g.document;if(!t&&r&&(r.currentScript&&\\"SCRIPT\\"===r.currentScript.tagName.toUpperCase()&&(t=r.currentScript.src),!t)){var o=r.getElementsByTagName(\\"script\\");if(o.length)for(var n=o.length-1;n>-1&&(!t||!/^http(s?):/.test(t));)t=o[n--].src}if(!t)throw new Error(\\"Automatic publicPath is not supported in this browser\\");t=t.replace(/^blob:/,\\"\\").replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),e.p=t})(),e.b=\\"undefined\\"!=typeof document&&document.baseURI||self.location.href,console.log(new URL(e(740),e.b))})();",
995+
}
996+
`;
997+
998+
exports[`minify option should work when \`minify\` is an array of functions and dispatches by \`filter\`: errors 1`] = `Array []`;
999+
1000+
exports[`minify option should work when \`minify\` is an array of functions and dispatches by \`filter\`: warnings 1`] = `Array []`;
1001+
8981002
exports[`minify option should work when \`minify\` is an array of functions using \`htmlMinifierTerser\`: assets 1`] = `
8991003
Object {
9001004
"1d477fcae4c6c3852830.html": "<!doctype html> <html lang=\\"en\\"> <head> <meta charset=\\"utf-8\\"/> <title>Hello</title> </head> <body> <h1>Hello, World!</h1> <p> Hello there </p> </body> </html> ",

test/minify-option.test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,4 +1301,119 @@ describe("minify option", () => {
13011301
expect(getErrors(stats)).toMatchSnapshot("errors");
13021302
expect(getWarnings(stats)).toMatchSnapshot("warnings");
13031303
});
1304+
1305+
it("should work when `minify` is an array of functions and dispatches by `filter`", async () => {
1306+
const compiler = getCompiler({
1307+
entry: path.resolve(__dirname, "./fixtures/html.js"),
1308+
});
1309+
1310+
new TerserPlugin({
1311+
test: /\.(?:[cm]?js|html?)(\?.*)?$/i,
1312+
minify: [TerserPlugin.terserMinify, TerserPlugin.htmlMinifierTerser],
1313+
}).apply(compiler);
1314+
1315+
const stats = await compile(compiler);
1316+
1317+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
1318+
expect(getErrors(stats)).toMatchSnapshot("errors");
1319+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1320+
});
1321+
1322+
it("should skip assets when the only minimizer's `filter` returns `false`", async () => {
1323+
const compiler = getCompiler({
1324+
entry: path.resolve(__dirname, "./fixtures/minify/es6.js"),
1325+
output: {
1326+
path: path.resolve(__dirname, "./dist-terser"),
1327+
filename: "[name].js",
1328+
chunkFilename: "[id].[name].js",
1329+
},
1330+
});
1331+
1332+
const minify = (file) => {
1333+
const [[, code]] = Object.entries(file);
1334+
1335+
return { code: `/* minified */${code}` };
1336+
};
1337+
1338+
minify.filter = () => false;
1339+
1340+
new TerserPlugin({
1341+
parallel: false,
1342+
minify,
1343+
}).apply(compiler);
1344+
1345+
const stats = await compile(compiler);
1346+
1347+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
1348+
expect(getErrors(stats)).toMatchSnapshot("errors");
1349+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1350+
});
1351+
1352+
it("should treat a `filter` returning `undefined` as accept", async () => {
1353+
const compiler = getCompiler({
1354+
entry: path.resolve(__dirname, "./fixtures/minify/es6.js"),
1355+
output: {
1356+
path: path.resolve(__dirname, "./dist-terser"),
1357+
filename: "[name].js",
1358+
chunkFilename: "[id].[name].js",
1359+
},
1360+
});
1361+
1362+
const minify = (file) => {
1363+
const [[, code]] = Object.entries(file);
1364+
1365+
return { code: `/* undef-filter */${code}` };
1366+
};
1367+
1368+
minify.filter = () => undefined;
1369+
1370+
new TerserPlugin({
1371+
parallel: false,
1372+
minify,
1373+
}).apply(compiler);
1374+
1375+
const stats = await compile(compiler);
1376+
1377+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
1378+
expect(getErrors(stats)).toMatchSnapshot("errors");
1379+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1380+
});
1381+
1382+
it("should skip assets when every minimizer in the `minify` array rejects them via `filter`", async () => {
1383+
const compiler = getCompiler({
1384+
entry: path.resolve(__dirname, "./fixtures/minify/es6.js"),
1385+
output: {
1386+
path: path.resolve(__dirname, "./dist-terser"),
1387+
filename: "[name].js",
1388+
chunkFilename: "[id].[name].js",
1389+
},
1390+
});
1391+
1392+
const cssOnly = (file) => {
1393+
const [[, code]] = Object.entries(file);
1394+
1395+
return { code: `/* css */${code}` };
1396+
};
1397+
1398+
cssOnly.filter = (name) => /\.css(\?.*)?$/i.test(name);
1399+
1400+
const htmlOnly = (file) => {
1401+
const [[, code]] = Object.entries(file);
1402+
1403+
return { code: `/* html */${code}` };
1404+
};
1405+
1406+
htmlOnly.filter = (name) => /\.html?(\?.*)?$/i.test(name);
1407+
1408+
new TerserPlugin({
1409+
parallel: false,
1410+
minify: [cssOnly, htmlOnly],
1411+
}).apply(compiler);
1412+
1413+
const stats = await compile(compiler);
1414+
1415+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
1416+
expect(getErrors(stats)).toMatchSnapshot("errors");
1417+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1418+
});
13041419
});

0 commit comments

Comments
 (0)