Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(chore) export named HighlightJS export in Node packages #3307

Merged
merged 6 commits into from Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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