Skip to content

Commit

Permalink
add .js.js stubs for maximum compatible with requires using extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Apr 6, 2021
1 parent 897cf59 commit 1d20d88
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tools/build_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,28 @@ async function buildCJSIndex(name, languages) {
}

async function buildNodeLanguage(language, options) {
const EMIT = `function emitWarning() {
if (!emitWarning.warned) {
emitWarning.warned = true;
process.emitWarning(
'Using file extension in specifier is deprecated, use "highlight.js/lib/languages/%%%%" instead'
);
}
}
emitWarning();`;
const CJS_STUB = `${EMIT}
module.exports = require('./%%%%.js');`;
const ES_STUB = `${EMIT}
import lang from './%%%%.js';
export default lang;`;
const input = { ...config.rollup.node.input, input: language.path };
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/languages/${language.name}.js` };
await rollupWrite(input, output);
await fs.writeFile(`${process.env.BUILD_DIR}/lib/languages/${language.name}.js.js`,
CJS_STUB.replace(/%%%%/g, language.name));
if (options.esm) {
await fs.writeFile(`${process.env.BUILD_DIR}/es/languages/${language.name}.js.js`,
ES_STUB.replace(/%%%%/g, language.name));
await rollupWrite(input, {...output,
format: "es",
file: output.file.replace("/lib/", "/es/")
Expand Down

0 comments on commit 1d20d88

Please sign in to comment.