From c60204a6c35df9875ff347ae09ddb399498bbbcf Mon Sep 17 00:00:00 2001 From: Dmitry Sharabin Date: Tue, 2 Jun 2026 22:57:34 +0200 Subject: [PATCH 1/2] Fix CJS build to actually emit CommonJS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CJS bundle was identical to the ESM bundle because the Rollup config did not specify `format: 'cjs'`. This caused `require("prismjs")` to throw a SyntaxError on Node 18–21, and return an unexpected ESM namespace object on Node 22+. - Add `format: 'cjs'` to the CJS output options - Add `format: 'es'` to the ESM output options for clarity - Write `dist/cjs/package.json` with `{"type":"commonjs"}` so Node parses the CJS files correctly despite the root `"type": "module"` Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/build.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/build.js b/scripts/build.js index 0dc11ce06b..b228a710f8 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -411,7 +411,10 @@ async function buildJS () { const bundles = { esm: { rollupOptions: defaultRollupOptions, - outputOptions: defaultOutputOptions, + outputOptions: { + ...defaultOutputOptions, + format: 'es', + }, }, cjs: { rollupOptions: { @@ -421,6 +424,7 @@ async function buildJS () { outputOptions: { ...defaultOutputOptions, dir: './dist/cjs', + format: 'cjs', }, }, }; @@ -430,6 +434,14 @@ async function buildJS () { bundle.build = await rollup(bundle.rollupOptions); await bundle.build.write(bundle.outputOptions); } + // The root package.json has "type": "module", so Node treats all .js files as ESM. + // This overrides that for the CJS directory, since Node uses the nearest parent + // package.json to determine the module system: https://nodejs.org/api/packages.html#type + await writeFile( + path.join(DIST_DIR, 'cjs', 'package.json'), + JSON.stringify({ type: 'commonjs' }), + 'utf-8' + ); } finally { for (const bundle of Object.values(bundles)) { From 033ac248110fbf4c760621018b48d40135da50a1 Mon Sep 17 00:00:00 2001 From: Dmitry Sharabin Date: Tue, 2 Jun 2026 23:23:54 +0200 Subject: [PATCH 2/2] Make CJS exports idiomatic so require() works without .default Add a Rollup plugin that appends a one-liner to CJS chunks with mixed default + named exports, making module.exports the default with named exports as properties (the Axios pattern). Without this, CJS consumers would need require("prismjs").default. Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/build.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/scripts/build.js b/scripts/build.js index b228a710f8..873706f2ed 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -304,6 +304,25 @@ const lazyGrammarPlugin = { }, }; +/** + * Makes `require("prismjs")` return the default export directly with named + * exports as properties, so CJS consumers don't need `.default`. + * + * @type {Plugin} + */ +const cjsExportPlugin = { + name: 'cjs-default-export', + renderChunk (code) { + if (!code.includes('exports.default=')) { + return null; + } + + code += 'module.exports=Object.assign(exports.default,exports);'; + + return { code, map: null }; + }, +}; + /** * @param {MagicString} s * @returns {{ code: string; map: SourceMapInput }} @@ -419,7 +438,7 @@ async function buildJS () { cjs: { rollupOptions: { ...defaultRollupOptions, - plugins: [...defaultRollupOptions.plugins, commonjs()], + plugins: [...defaultRollupOptions.plugins, commonjs(), cjsExportPlugin], }, outputOptions: { ...defaultOutputOptions,