Skip to content

Commit 1d20d88

Browse files
committed
add .js.js stubs for maximum compatible with requires using extensions
1 parent 897cf59 commit 1d20d88

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tools/build_node.js

+18
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,28 @@ async function buildCJSIndex(name, languages) {
4141
}
4242

4343
async function buildNodeLanguage(language, options) {
44+
const EMIT = `function emitWarning() {
45+
if (!emitWarning.warned) {
46+
emitWarning.warned = true;
47+
process.emitWarning(
48+
'Using file extension in specifier is deprecated, use "highlight.js/lib/languages/%%%%" instead'
49+
);
50+
}
51+
}
52+
emitWarning();`;
53+
const CJS_STUB = `${EMIT}
54+
module.exports = require('./%%%%.js');`;
55+
const ES_STUB = `${EMIT}
56+
import lang from './%%%%.js';
57+
export default lang;`;
4458
const input = { ...config.rollup.node.input, input: language.path };
4559
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/languages/${language.name}.js` };
4660
await rollupWrite(input, output);
61+
await fs.writeFile(`${process.env.BUILD_DIR}/lib/languages/${language.name}.js.js`,
62+
CJS_STUB.replace(/%%%%/g, language.name));
4763
if (options.esm) {
64+
await fs.writeFile(`${process.env.BUILD_DIR}/es/languages/${language.name}.js.js`,
65+
ES_STUB.replace(/%%%%/g, language.name));
4866
await rollupWrite(input, {...output,
4967
format: "es",
5068
file: output.file.replace("/lib/", "/es/")

0 commit comments

Comments
 (0)