Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
renderChunk (code) {
const str = new MagicString(code);
str.replace(
/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])+)\/\s*\.\s*source\b/g,

Check warning on line 253 in scripts/build.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \/
(m, /** @type {string} */ source) => {
// escape backslashes
source = source.replace(
Expand Down Expand Up @@ -297,13 +297,32 @@
renderChunk (code) {
const str = new MagicString(code);
str.replace(
/^(?<indent>[ \t]+)grammar: (\{[\s\S]*?^\k<indent>\})/m,

Check warning on line 300 in scripts/build.js

View workflow job for this annotation

GitHub Actions / lint

The quantifier '[\s\S]*?' is always entered despite having a minimum of 0. This is because the assertion '^' contradicts with the element(s) after the quantifier. Either set the minimum to 1 (+?) or change the assertion
(m, _, /** @type {string} */ grammar) => `\tgrammar: () => (${grammar})`
);
return toRenderedChunk(str);
},
};

/**
* 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 }}
Expand Down Expand Up @@ -411,16 +430,20 @@
const bundles = {
esm: {
rollupOptions: defaultRollupOptions,
outputOptions: defaultOutputOptions,
outputOptions: {
...defaultOutputOptions,
format: 'es',
},
},
cjs: {
rollupOptions: {
...defaultRollupOptions,
plugins: [...defaultRollupOptions.plugins, commonjs()],
plugins: [...defaultRollupOptions.plugins, commonjs(), cjsExportPlugin],
},
outputOptions: {
...defaultOutputOptions,
dir: './dist/cjs',
format: 'cjs',
},
},
};
Expand All @@ -430,6 +453,14 @@
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)) {
Expand Down
Loading