Skip to content

Commit

Permalink
(chore) export named HighlightJS export in Node packages (#3307)
Browse files Browse the repository at this point in the history
Resolves #3333. Resolves #3295.

- exports a `HighlightJS` named export
- exports a `default` named export for CJS (TS compatibility)
  • Loading branch information
joshgoebel committed Oct 17, 2021
1 parent 7315fb1 commit 0f70132
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
@@ -1,5 +1,10 @@
## Version 11.3.0 (most likely)

Build:

- add `HighlightJS` named export (#3295) [Josh Goebel][]
- add `.default` named export to CJS builds (#3333) [Josh Goebel][]

Parser:

- add first rough performance testing script (#3280) [Austin Schick][]
Expand Down
7 changes: 5 additions & 2 deletions test/builds/package.js
Expand Up @@ -6,6 +6,9 @@ See .travis.yml
*/

import hljs from '../../build/lib/index.js';
import { HighlightJS } from '../../build/lib/index.js'

hljs.highlight("cpp","/* test */")
console.log("Rollup built package works.")
const language = "cpp";
hljs.highlight("/* test */", {language});
HighlightJS.highlight("/* test */", {language});
console.log("Rollup built package works.");
7 changes: 6 additions & 1 deletion tools/build_config.js
Expand Up @@ -28,7 +28,12 @@ module.exports = {
}
},
node: {
output: { format: "cjs", strict: false, exports: "auto" }
output: {
format: "cjs",
strict: false,
exports: "auto",
footer: ""
}
},
browser_iife: {
input: {
Expand Down
11 changes: 8 additions & 3 deletions tools/build_node.js
Expand Up @@ -12,14 +12,18 @@ const log = (...args) => console.log(...args);
async function buildESMStub(name) {
const code =
`// https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards\n` +
`import hljs from '../lib/${name}.js';\n` +
`export default hljs;\n`;
`import HighlightJS from '../lib/${name}.js';\n` +
`export { HighlightJS };\n` +
`export default HighlightJS;\n`;
await fs.writeFile(`${process.env.BUILD_DIR}/es/${name}.js`, code);
}

async function buildCJSIndex(name, languages) {
const header = "var hljs = require('./core');";
const footer = "module.exports = hljs;";
const footer =
`hljs.HighlightJS = hljs\n` +
`hljs.default = hljs\n` +
`module.exports = hljs;`;

const registration = languages.map((lang) => {
const require = `require('./languages/${lang.name}')`;
Expand Down Expand Up @@ -82,6 +86,7 @@ async function buildESMUtils() {
async function buildNodeHighlightJS(options) {
const input = { ...config.rollup.core.input, input: `src/highlight.js` };
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/core.js` };
output.footer = "highlight.HighlightJS = highlight;\nhighlight.default = highlight;";
await rollupWrite(input, output);
if (options.esm) {
buildESMStub("core");
Expand Down

0 comments on commit 0f70132

Please sign in to comment.