Skip to content

Commit a2d7417

Browse files
committed
add --no-esm build option
1 parent ea763f7 commit a2d7417

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

tools/build.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const dir = {};
7171
commander
7272
.usage('[options] [<language>...]')
7373
.option('-n, --no-minify', 'Disable minification')
74+
.option('-ne, --no-esm', 'Disable building ESM')
7475
.option('-t, --target <name>',
7576
'Build for target ' +
7677
'[all, browser, cdn, node]',
@@ -86,7 +87,7 @@ async function doTarget(target, buildDir) {
8687
const build = require(`./build_${target}`);
8788
process.env.BUILD_DIR = buildDir;
8889
await clean(buildDir);
89-
await build.build({ languages: commander.args, minify: commander.opts().minify });
90+
await build.build({ languages: commander.args, minify: commander.opts().minify, esm: commander.opts().esm });
9091
}
9192

9293
async function doBuild() {

tools/build_node.js

+35-27
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ async function buildCJSIndex(name, languages) {
4040
await fs.writeFile(`${process.env.BUILD_DIR}/lib/${name}.js`, index);
4141
}
4242

43-
async function buildNodeLanguage(language) {
43+
async function buildNodeLanguage(language, options) {
4444
const input = { ...config.rollup.node.input, input: language.path };
4545
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/languages/${language.name}.js` };
4646
await rollupWrite(input, output);
47-
await rollupWrite(input, {...output,
48-
format: "es",
49-
file: output.file.replace("/lib/", "/es/")
50-
});
47+
if (options.esm) {
48+
await rollupWrite(input, {...output,
49+
format: "es",
50+
file: output.file.replace("/lib/", "/es/")
51+
});
52+
}
5153
}
5254

5355
const EXCLUDE = ["join"];
@@ -62,23 +64,24 @@ async function buildESMUtils() {
6264
return code;
6365
}
6466
}];
65-
const output = {
67+
await rollupWrite(input, {
6668
...config.rollup.node.output,
6769
format: "es",
6870
file: `${process.env.BUILD_DIR}/es/utils/regex.js`
69-
};
70-
await rollupWrite(input, output);
71+
});
7172
}
7273

73-
74-
async function buildNodeHighlightJS() {
74+
async function buildNodeHighlightJS(options) {
7575
const input = { ...config.rollup.node.input, input: `src/highlight.js` };
7676
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/core.js` };
7777
await rollupWrite(input, output);
78-
await rollupWrite(input, { ...output,
79-
format: "es",
80-
file: `${process.env.BUILD_DIR}/es/core.js`
81-
});
78+
if (options.esm) {
79+
await rollupWrite(input, {
80+
...output,
81+
format: "es",
82+
file: `${process.env.BUILD_DIR}/es/core.js`
83+
});
84+
}
8285
}
8386

8487
function dual(file) {
@@ -88,7 +91,7 @@ function dual(file) {
8891
};
8992
}
9093

91-
async function buildPackageJSON() {
94+
async function buildPackageJSON(options) {
9295
const packageJson = require("../package");
9396

9497
const exports = {
@@ -98,16 +101,16 @@ async function buildPackageJSON() {
98101
"./lib/core": dual("./lib/core.js"),
99102
"./lib/languages/*": dual("./lib/languages/*.js"),
100103
};
101-
packageJson.exports = exports;
104+
if (options.esm) packageJson.exports = exports;
102105

103106
await fs.writeFile(`${process.env.BUILD_DIR}/package.json`, JSON.stringify(packageJson, null, 2));
104107
}
105108

106-
async function buildLanguages(languages) {
109+
async function buildLanguages(languages, options) {
107110
log("Writing languages.");
108111
await Promise.all(
109112
languages.map(async(lang) => {
110-
await buildNodeLanguage(lang);
113+
await buildNodeLanguage(lang, options);
111114
process.stdout.write(".");
112115
})
113116
);
@@ -126,7 +129,6 @@ const CORE_FILES = [
126129

127130
async function buildNode(options) {
128131
mkdir("lib/languages");
129-
mkdir("es/languages");
130132
mkdir("scss");
131133
mkdir("styles");
132134
mkdir("types");
@@ -136,9 +138,13 @@ async function buildNode(options) {
136138
install(`./${file}`, file);
137139
});
138140
install("./src/core.d.ts", "lib/core.d.ts");
139-
install("./src/core.d.ts", "es/core.d.ts");
140141
install("./src/core.d.ts", "lib/common.d.ts");
141-
install("./src/core.d.ts", "es/common.d.ts");
142+
143+
if (options.esm) {
144+
mkdir("es/languages");
145+
install("./src/core.d.ts", "es/core.d.ts");
146+
install("./src/core.d.ts", "es/common.d.ts");
147+
}
142148

143149
log("Writing styles.");
144150
const styles = await fs.readdir("./src/styles/");
@@ -147,22 +153,24 @@ async function buildNode(options) {
147153
install(`./src/styles/${file}`, `scss/${file.replace(".css", ".scss")}`);
148154
});
149155
log("Writing package.json.");
150-
await buildPackageJSON();
156+
await buildPackageJSON(options);
151157

152158
let languages = await getLanguages();
153159
// filter languages for inclusion in the highlight.js bundle
154160
languages = filter(languages, options.languages);
155161

156162
const common = languages.filter(l => l.categories.includes("common"));
157-
await buildESMIndex("index", languages);
158-
await buildESMIndex("common", common);
159-
await buildESMUtils();
163+
if (options.esm) {
164+
await buildESMIndex("index", languages);
165+
await buildESMIndex("common", common);
166+
await buildESMUtils();
167+
}
160168
await buildCJSIndex("index", languages);
161169
await buildCJSIndex("common", common);
162-
await buildLanguages(languages);
170+
await buildLanguages(languages, options);
163171

164172
log("Writing highlight.js");
165-
await buildNodeHighlightJS();
173+
await buildNodeHighlightJS(options);
166174
}
167175

168176
module.exports.build = buildNode;

0 commit comments

Comments
 (0)