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

Error when using rollup with highlight.js? #4116

Open
fireflysemantics opened this issue Sep 11, 2024 · 13 comments
Open

Error when using rollup with highlight.js? #4116

fireflysemantics opened this issue Sep 11, 2024 · 13 comments
Labels
bug help welcome Could use help from community parser

Comments

@fireflysemantics
Copy link

Pasting in the below description from this issue on SO:
https://stackoverflow.com/questions/78971938/using-rollup-with-highlight-js

I'm trying to use rollup to build a bundle containing highlight.js.

This is the starter project I'm using to build the bundle. If we clone it and run:

git clone [email protected]:fireflysemantics/fs-lit-element-starter-ts.git b
cd b
npm i 
npm run rollup

We can see that the bundle is built without any errors.

And if we add highlight.js and import the default into src/my-dispatcher.component.ts, the following error is created.

npm i highlight.js

Import the default.

import hljs from "highlight.js";

Reference hljs on a property so that we don't get any linting errors.

h:any = hljs;

If we now run npm run rollup again the following error is produced.

Thoughts?

./build/index.js → ./build/index.bundle.js...
(!) [plugin replace] @rollup/plugin-replace: 'preventAssignment' currently defaults to false. It is recommended to set this option to `true`, as the next major version will default this option to `true`.
[!] RollupError: node_modules/highlight.js/es/index.js (2:7): "default" is not exported by "node_modules/highlight.js/lib/index.js", imported by "node_modules/highlight.js/es/index.js".
https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module
node_modules/highlight.js/es/index.js (2:7)
1: // https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards
2: import HighlightJS from '../lib/index.js';
          ^
3: export { HighlightJS };
4: export default HighlightJS;
    at getRollupError (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/parseAst.js:282:41)
    at Object.error (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/parseAst.js:278:42)
    at Module.error (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:15321:28)
    at Module.traceVariable (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:15769:29)
    at ModuleScope.findVariable (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:13480:39)
    at Identifier.bind (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:8386:40)
    at ExportDefaultDeclaration.bind (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:6255:23)
    at Program.bind (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:6251:28)
    at Module.bindReferences (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:15302:18)
    at Graph.sortModules (/Users/oleersoy/Temp/b/node_modules/rollup/dist/shared/rollup.js:20535:20)

Just for reference this is the rollup configuration.

/**
 * @license
 * Copyright 2018 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */

import summary from 'rollup-plugin-summary';
import terser from '@rollup/plugin-terser';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';

export default {
  input: './build/index.js',
  output: {
    file: './build/index.bundle.js',
    format: 'esm',
  },
  onwarn(warning) {
    if (warning.code !== 'THIS_IS_UNDEFINED') {
      console.error(`(!) ${warning.message}`);
    }
  },
  plugins: [
    replace({'Reflect.decorate': 'undefined'}),
    resolve(),
    /**
     * This minification setup serves the static site generation.
     * For bundling and minification, check the README.md file.
     */
    terser({
      ecma: 2021,
      module: true,
      warnings: true,
      mangle: {
        properties: {
          regex: /^__/,
        },
      },
    }),
    summary(),
  ],
};
@fireflysemantics fireflysemantics added bug help welcome Could use help from community parser labels Sep 11, 2024
@joshgoebel
Copy link
Member

For now I'd try using the cdn-release instead which should have "real" ES6 modules...

@fireflysemantics
Copy link
Author

@joshgoebel the build is for an open source web component that uses highlight.js to do what it does ... but with a custom element ... and rollup is being used to bundle highlight.js into the component ... So crossing my fingers for a fix

@joshgoebel
Copy link
Member

Repeating: For now I'd suggest using the cdn-release instead which should have "real" [independent] ES6 modules...

...which is exactly what you are asking for. We purposely bundled the npm library the way we did (per the docs) and at this late point in the game that's probably not changing. Version 12 will be ESM only, until then the cdn-assets build should suffice for anyone wanting to re-bundle ES6.

https://www.npmjs.com/package/@highlightjs/cdn-assets/v/11.10.0

@fireflysemantics
Copy link
Author

@joshgoebel I tried this, and it looks like Typescript does not have a way of importing from CDN assets ...

https://stackoverflow.com/questions/78983373/importing-highlight-js-as-a-cdn-resource-in-a-typescript-project?noredirect=1#comment139262477_78983373

@joshgoebel
Copy link
Member

joshgoebel commented Sep 13, 2024

I linked you to the NPM package where we publish all the assets as an npm package. You don't need to actually import them from a CDN - you just need the files. Importing should be the same as with any other npm package.

@fireflysemantics
Copy link
Author

fireflysemantics commented Sep 14, 2024

@joshgoebel ooooh - OK - It's looking more realistic now :). I tried this import:

import hljs from '@highlightjs/cdn-assets/highlight.js';

But Typescript creates this error ( Looks like @types/highlightjs ) doesn't understand the CDN assets) .

Could not find a declaration file for module '@highlightjs/cdn-assets/highlight.js'. '/Users/oleersoy/Temp/b/node_modules/@highlightjs/cdn-assets/highlight.js' implicitly has an 'any' type.
Try npm i --save-dev @types/highlightjs__cdn-assets if it exists or add a new declaration (.d.ts) file containing declare module '@highlightjs/cdn-assets/highlight.js';ts(7016)

Do you by chance have a workaround for this to? Thanks for all your help this.

@joshgoebel
Copy link
Member

implicitly has an 'any' type.

That is a warning, not an error... but I really don't know further, sorry.

@taufik-nurrohman
Copy link
Member

May be related to the assert part:

https://v8.dev/features/import-assertions

@fireflysemantics
Copy link
Author

@joshgoebel for me it shows up as an error.

src/my-dispatcher.component.ts:4:18 - error TS7016: Could not find a declaration file for module '@highlightjs/cdn-assets/highlight.js'. '/Users/oleersoy/Temp/b/node_modules/@highlightjs/cdn-assets/highlight.js' implicitly has an 'any' type.
Try npm i --save-dev @types/highlightjs__cdn-assets if it exists or add a new declaration (.d.ts) file containing declare module '@highlightjs/cdn-assets/highlight.js';

4 import hljs from '@highlightjs/cdn-assets/highlight.js';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Found 1 error in src/my-dispatcher.component.ts:4

I"ll keep plugging away at it though. I think I may just have to forgo using Typescript for this project, and do pure Javascript instead.

Thanks again for all the input.

@HoikanChan
Copy link

Got same error with rollup

[!] Error: 'default' is not exported by ../../node_modules/highlight.js/lib/index.js, imported by ../../node_modules/highlight.js/es/index.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
../../node_modules/highlight.js/es/index.js (2:7)
1: // https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards
2: import HighlightJS from '../lib/index.js';
^
3: export { HighlightJS };
4: export default HighlightJS;
Error: 'default' is not exported by ../../node_modules/highlight.js/lib/index.js, imported by ../../node_modules/highlight.js/es/index.js
at error (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:198:30)
at Module.error (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:12560:16)
at Module.traceVariable (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:12919:29)
at ModuleScope.findVariable (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:11571:39)
at Identifier.bind (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:7570:40)
at ExportDefaultDeclaration.bind (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:5400:23)
at Program.bind (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:5396:73)
at Module.bindReferences (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:12556:18)
at Graph.sortModules (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:23193:20)
at Graph.build (D:\workspace\ai-dsl-engine\node_modules\rollup\dist\shared\rollup.js:23071:14)

error Command failed with exit code 1.

@joshgoebel
Copy link
Member

joshgoebel commented Sep 19, 2024

A simple build against our ESM CDN builds (and cdn_release) with rollup should work just fine: (just built from main)

❯ ./tools/build.js -t cdn
Starting build.
Finished build.
Writing style files.
Building language files.
...................................................................................................................................................................................................
Building extra/highlightjs-1c-enterprise/dist/1c-enterprise.min.js.
Building extra/highlightjs-robots-txt/dist/robots-txt.min.js.

Building highlight.js.
Building es/core.js.
Building es/highlight.js.
-----
Embedded Lang           : 0 bytes
All Lang                : 1226701 bytes
highlight.js            : 80686 bytes
highlight.min.js        : 20546 bytes
highlight.min.js.gz     : 8183 bytes
es/core.js              : 76000 bytes
es/highlight.js         : 76000 bytes
es/core.min.js          : 20444 bytes
es/core.min.js.gz       : 8145 bytes
es/highlight.min.js     : 20444 bytes
es/highlight.min.js.gz  : 8145 bytes
-----
❯ rollup -c test/builds/rollup_import_cdn_build_esm.mjs

test/builds/cdn_build_as_esm.mjs → build/bundle.js...
created build/bundle.js in 120ms

@joshgoebel
Copy link
Member

4 import hljs from '@highlightjs/cdn-assets/highlight.js';

Probably not what you want, the es modules are in the es folder. Top-level is CJS.

@joshgoebel
Copy link
Member

joshgoebel commented Sep 19, 2024

And using the Node ESM build:

❯ ./tools/build.js -t node
Starting build.
Finished build.
Writing styles.
Writing package.json.
Writing languages.
...................................................................................................................................................................................................
Writing highlight.js
❯ rollup -c test/builds/rollup_import_node_build_esm.mjs

test/builds/node_build_as_esm.mjs → build/bundle.js...
created build/bundle.js in 1s
❯ node build/bundle.js
Pass: browser build works with Node.js just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community parser
Projects
None yet
Development

No branches or pull requests

4 participants