Skip to content

Commit 3bb0b1d

Browse files
committed
dual build
1 parent 1a2845d commit 3bb0b1d

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"type": "git",
3333
"url": "git://github.com/highlightjs/highlight.js.git"
3434
},
35-
"main": "./lib/index.mjs",
35+
"type": "commonjs",
36+
"main": "./lib/index.js",
3637
"types": "./types/index.d.ts",
3738
"scripts": {
3839
"mocha": "mocha",

tools/build_config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
clean_css: {},
99
rollup: {
1010
node: {
11-
output: { format: "es", strict: false, exports: "auto" },
11+
output: { format: "cjs", strict: false, exports: "auto" },
1212
input: {
1313
plugins: [
1414
cjsPlugin(),

tools/build_node.js

+72-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const { filter } = require("./lib/dependencies");
66
const { rollupWrite } = require("./lib/bundling.js");
77
const log = (...args) => console.log(...args);
88

9-
async function buildNodeIndex(name, languages) {
9+
async function buildESMIndex(name, languages) {
1010
const header = `import hljs from './core.mjs';`;
1111
const footer = "export default hljs;";
1212

1313
const registration = languages.map((lang) => {
1414
let out = '';
1515
const importName = "L_" + lang.name.replace("-","_")
16-
let require = `import ${importName} from './languages/${lang.name}.mjs';`;
16+
let require = `import ${importName} from './languages/${lang.name}.js';`;
1717
// TODO: break this with v11? All modules must export default?
1818
if (lang.loader) {
1919
require = require += `.${lang.loader}`;
@@ -25,23 +25,84 @@ async function buildNodeIndex(name, languages) {
2525
});
2626

2727
const index = `${header}\n\n${registration.join("\n")}\n\n${footer}`;
28-
await fs.writeFile(`${process.env.BUILD_DIR}/lib/${name}.mjs`, index);
28+
await fs.writeFile(`${process.env.BUILD_DIR}/es/${name}.js`, index);
29+
}
30+
31+
async function buildCJSIndex(name, languages) {
32+
const header = "var hljs = require('./core');";
33+
const footer = "module.exports = hljs;";
34+
35+
const registration = languages.map((lang) => {
36+
let require = `require('./languages/${lang.name}')`;
37+
if (lang.loader) {
38+
require = require += `.${lang.loader}`;
39+
}
40+
return `hljs.registerLanguage('${lang.name}', ${require});`;
41+
});
42+
43+
const index = `${header}\n\n${registration.join("\n")}\n\n${footer}`;
44+
await fs.writeFile(`${process.env.BUILD_DIR}/lib/${name}.js`, index);
2945
}
3046

3147
async function buildNodeLanguage(language) {
3248
const input = { ...config.rollup.node.input, input: language.path };
33-
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/languages/${language.name}.mjs` };
49+
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/languages/${language.name}.js` };
50+
await rollupWrite(input, output);
51+
await rollupWrite(input, {...output,
52+
format: "es",
53+
file: output.file.replace("/lib/", "/es/")
54+
});
55+
}
56+
57+
const EXCLUDE = ["join"];
58+
59+
async function buildESMUtils() {
60+
const input = { ...config.rollup.node.input, input: `src/lib/regex.js` };
61+
input.plugins = [...input.plugins, {
62+
transform: (code) => {
63+
EXCLUDE.forEach((fn) => {
64+
code = code.replace(`export function ${fn}(`, `function ${fn}(`);
65+
});
66+
return code;
67+
}
68+
}];
69+
const output = {
70+
...config.rollup.node.output,
71+
format: "es",
72+
file: `${process.env.BUILD_DIR}/es/utils/regex.js`
73+
};
3474
await rollupWrite(input, output);
3575
}
3676

77+
3778
async function buildNodeHighlightJS() {
3879
const input = { ...config.rollup.node.input, input: `src/highlight.js` };
39-
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/core.mjs` };
80+
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/core.js` };
4081
await rollupWrite(input, output);
82+
await rollupWrite(input, { ...output,
83+
format: "es",
84+
file: `${process.env.BUILD_DIR}/es/core.js`
85+
});
86+
}
87+
88+
function dual(file) {
89+
return {
90+
require: file,
91+
import: file.replace("/lib/", "/es/")
92+
};
4193
}
4294

4395
async function buildPackageJSON() {
4496
const packageJson = require("../package");
97+
98+
const exports = {
99+
".": dual("./lib/index.js"),
100+
"./lib/common": dual("./lib/common.js"),
101+
"./lib/core": dual("./lib/core.js"),
102+
"./lib/languages/*": dual("./lib/languages/*.js"),
103+
};
104+
packageJson.exports = exports;
105+
45106
await fs.writeFile(`${process.env.BUILD_DIR}/package.json`, JSON.stringify(packageJson, null, 2));
46107
}
47108

@@ -58,6 +119,7 @@ async function buildLanguages(languages) {
58119

59120
async function buildNode(options) {
60121
mkdir("lib/languages");
122+
mkdir("es/languages");
61123
mkdir("scss");
62124
mkdir("styles");
63125
mkdir("types");
@@ -79,8 +141,11 @@ async function buildNode(options) {
79141
// filter languages for inclusion in the highlight.js bundle
80142
languages = filter(languages, options.languages);
81143

82-
await buildNodeIndex("index", languages);
83-
await buildNodeIndex("common", languages.filter(l => l.categories.includes("common")));
144+
await buildESMIndex("index", languages);
145+
await buildESMIndex("common", languages.filter(l => l.categories.includes("common")));
146+
await buildESMUtils();
147+
await buildCJSIndex("index", languages);
148+
await buildCJSIndex("common", languages.filter(l => l.categories.includes("common")));
84149
await buildLanguages(languages);
85150

86151
log("Writing highlight.js");

0 commit comments

Comments
 (0)