Skip to content

Commit 4385103

Browse files
fix: bundle schema-utils without errors or warnings
precompile required the ajv helpers it inlines with a request built from their name and webpack with a variable request. A bundler that bundles schema-utils, as webpack's own test cases do, turned the first into a context over ajv/dist/runtime and failed to parse its .d.ts and .map files, and warned about the second. The helpers are now required by name and webpack through an alias of module, so bundling schema-utils reports nothing. The minified output is byte-identical. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01QYPkbvwyRWt2SHgXDoYRdo
1 parent b4b44eb commit 4385103

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎.changeset/precompile-bundle.md‎

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+
Bundle `schema-utils` without errors or warnings: `precompile` requires the `ajv` helpers it inlines by name and loads webpack's minifier without a request a bundler parses.

‎src/precompile.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ function addKeywords(ajv, hoist) {
130130
});
131131
}
132132

133+
// Each helper is required by its own name: a request built from the name would make a bundler
134+
// that bundles `schema-utils` parse every file in `ajv/dist/runtime`
135+
const INLINE_HELPERS = {
136+
equal: () => require("ajv/dist/runtime/equal").default,
137+
ucs2length: () => require("ajv/dist/runtime/ucs2length").default,
138+
};
139+
133140
/**
134141
* @param {string} code generated code
135142
* @param {boolean} esm whether the code is an ES module
@@ -147,7 +154,7 @@ function useRuntime(code, esm, inline) {
147154
// WHY: loading a helper through `schema-utils` costs about 1ms, more than the check itself
148155
// takes to run for most options - both functions are small and self-contained, `uri` is not
149156
if (inline && name !== "uri") {
150-
return `(${require(`ajv/dist/runtime/${name}`).default.toString()})`;
157+
return `(${INLINE_HELPERS[/** @type {"equal" | "ucs2length"} */ (name)]().toString()})`;
151158
}
152159

153160
// ajv keeps `require` for its helpers in an ES module too
@@ -262,12 +269,13 @@ async function minify(code, esm) {
262269
let minifier;
263270

264271
// webpack's own minifier, which writes what terser writes without loading it - the packages
265-
// shipping a check already have webpack. Required by name so neither TypeScript nor a bundler
266-
// reaches for it.
267-
const WEBPACK = "webpack";
272+
// shipping a check already have webpack. Required through an alias of `module` so a bundler
273+
// that bundles `schema-utils` neither bundles webpack nor warns about a request it cannot
274+
// resolve, while Node.js resolves it like any `require` here.
275+
const self = module;
268276

269277
try {
270-
minifier = await require(WEBPACK).javascript.syntax.printer.load();
278+
minifier = await self.require("webpack").javascript.syntax.printer.load();
271279
} catch (err) {
272280
throw new Error(
273281
"Install webpack 5.112.0 or later to minify a precompiled check",

‎test/precompile.test.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,46 @@ describe("precompile", () => {
598598
});
599599
});
600600
});
601+
602+
describe("bundled", () => {
603+
// `schema-utils` is bundled by tools that ship it inside their own code, which reaches
604+
// `precompile` through the `precompile` export even though it only runs at build time
605+
it("should bundle without errors or warnings", (done) => {
606+
const webpack = require("webpack");
607+
608+
const outputPath = fs.mkdtempSync(path.join(os.tmpdir(), "schema-utils-"));
609+
610+
webpack(
611+
{
612+
mode: "none",
613+
target: "node",
614+
context: path.resolve(__dirname, ".."),
615+
entry: "./src/index.js",
616+
output: { path: outputPath },
617+
},
618+
(err, stats) => {
619+
remove(outputPath);
620+
621+
if (err) {
622+
done(err);
623+
624+
return;
625+
}
626+
627+
try {
628+
const { errors, warnings } = stats.toJson({
629+
all: false,
630+
errors: true,
631+
warnings: true,
632+
});
633+
634+
expect(errors.map(({ message }) => message)).toEqual([]);
635+
expect(warnings.map(({ message }) => message)).toEqual([]);
636+
done();
637+
} catch (error) {
638+
done(error);
639+
}
640+
},
641+
);
642+
}, 60000);
643+
});

0 commit comments

Comments
 (0)