From 0f70132be18769c36b33740c99276e355b0b8702 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 17 Oct 2021 08:59:59 -0400 Subject: [PATCH] (chore) export named `HighlightJS` export in Node packages (#3307) Resolves #3333. Resolves #3295. - exports a `HighlightJS` named export - exports a `default` named export for CJS (TS compatibility) --- CHANGES.md | 5 +++++ test/builds/package.js | 7 +++++-- tools/build_config.js | 7 ++++++- tools/build_node.js | 11 ++++++++--- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f906cad1ec..ac98114f38 100644 --- a/CHANGES.md +++ b/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][] diff --git a/test/builds/package.js b/test/builds/package.js index 49376f43b2..eaff262ce0 100644 --- a/test/builds/package.js +++ b/test/builds/package.js @@ -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."); diff --git a/tools/build_config.js b/tools/build_config.js index 6d6557f4ab..02f91373ad 100644 --- a/tools/build_config.js +++ b/tools/build_config.js @@ -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: { diff --git a/tools/build_node.js b/tools/build_node.js index 6e8117e664..9dcc8579b5 100644 --- a/tools/build_node.js +++ b/tools/build_node.js @@ -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}')`; @@ -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");