Skip to content

Commit e498789

Browse files
refactor(precompile): minify with webpack's SourceProcessor where webpack has one
webpack's `javascript.syntax.printer.load()` is an experimental shim over the one `SourceProcessor` minify pass and is going away; webpack 5.112.x, which has no `SourceProcessor`, still minifies through it. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 17b5696 commit e498789

3 files changed

Lines changed: 71 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"schema-utils": patch
3+
---
4+
5+
Minify a precompiled check with webpack's `SourceProcessor` where webpack has one.

‎src/precompile.js‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ async function loadFileSchema(uri) {
265265
* @returns {Promise<string>} code without error objects, minified
266266
*/
267267
async function minify(code, esm) {
268-
/** @type {{ minify: (code: string, options: Record<string, EXPECTED_ANY>) => Promise<{ code?: string }> }} */
269-
let minifier;
268+
/** @type {(code: string, options: Record<string, EXPECTED_ANY>) => Promise<string> | string} */
269+
let minifyWith;
270270

271271
// webpack's own minifier, which writes what terser writes without loading it - the packages
272272
// shipping a check already have webpack. A bundler that bundles `schema-utils` must neither
@@ -280,9 +280,25 @@ async function minify(code, esm) {
280280
const self = module;
281281

282282
try {
283-
minifier = await (nodeRequire || self.require.bind(self))(
283+
const { syntax } = (nodeRequire || self.require.bind(self))(
284284
"webpack",
285-
).javascript.syntax.printer.load();
285+
).javascript;
286+
287+
if (syntax.SourceProcessor) {
288+
// One `SourceProcessor` run reads, mangles, compresses and prints it.
289+
minifyWith = (source, options) =>
290+
new syntax.SourceProcessor().process(source, {
291+
...options,
292+
sourceType: esm ? "module" : "script",
293+
mode: "minify",
294+
}).code;
295+
} else {
296+
// TODO in the next major release: require the webpack whose `SourceProcessor` minifies and drop `printer.load()`
297+
const minifier = await syntax.printer.load();
298+
299+
minifyWith = async (source, options) =>
300+
/** @type {string} */ ((await minifier.minify(source, options)).code);
301+
}
286302
} catch (err) {
287303
throw new Error(
288304
"Install webpack 5.112.0 or later to minify a precompiled check",
@@ -296,15 +312,14 @@ async function minify(code, esm) {
296312
/(=\s*\[?)\{instancePath(?::instancePath\b[^{}]*?)?,schemaPath:"(?:[^"\\]|\\.)*",keyword:"\w+",/g,
297313
"$1{",
298314
);
299-
const result = await minifier.minify(stripped, {
315+
316+
return minifyWith(stripped, {
300317
compress: { passes: 3 },
301318
mangle: true,
302319
ecma: 2015,
303320
module: esm,
304321
toplevel: true,
305322
});
306-
307-
return /** @type {string} */ (result.code);
308323
}
309324

310325
/**

‎test/precompile.test.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,50 @@ describe("precompile", () => {
212212
expect(code).not.toMatch(/require\(|module\.exports/);
213213
});
214214

215+
it("should minify with webpack's SourceProcessor where it has one", async () => {
216+
/** @type {Record<string, EXPECTED_ANY>[]} */
217+
const runs = [];
218+
219+
jest.doMock("webpack", () => ({
220+
javascript: {
221+
syntax: {
222+
SourceProcessor: class {
223+
/**
224+
* @param {string} code code
225+
* @param {Record<string, EXPECTED_ANY>} options options
226+
* @returns {{ code: string }} the code
227+
*/
228+
process(code, options) {
229+
runs.push(options);
230+
return { code: `/* minified */${code.length}` };
231+
}
232+
},
233+
},
234+
},
235+
}));
236+
237+
try {
238+
const code = await precompile(schema, { esm: true, minify: true });
239+
240+
expect(code).toMatch(/\/\* minified \*\/\d+$/);
241+
expect(runs).toEqual([
242+
{
243+
compress: { passes: 3 },
244+
mangle: true,
245+
ecma: 2015,
246+
module: true,
247+
toplevel: true,
248+
sourceType: "module",
249+
mode: "minify",
250+
},
251+
]);
252+
} finally {
253+
jest.dontMock("webpack");
254+
// A mock once required stays in the registry for the next `doMock`.
255+
jest.resetModules();
256+
}
257+
});
258+
215259
it("should ask for webpack when it has no minifier", async () => {
216260
// webpack before 5.112.0
217261
jest.doMock("webpack", () => ({}));

0 commit comments

Comments
 (0)