shiki icon indicating copy to clipboard operation
shiki copied to clipboard

Error when building with SvelteKit

Open hxrriet02 opened this issue 1 year ago • 1 comments

Validations

  • [X] Follow our Code of Conduct
  • [X] Read the Contributing Guide.
  • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

Describe the bug

There appears to be an issue when trying to build with shiki as a dependency in sveltekit. I have not tried other frameworks but I believe this would be an issue throughout. Specifically its an issue with the oniguruma-to-js.

"rewrite" is not exported by "node_modules/regex/dist/regex.min.js", imported by "node_modules/oniguruma-to-js/dist/shared/oniguruma-to-js.2969e22d.mjs".
file: /usr/src/app/node_modules/oniguruma-to-js/dist/shared/oniguruma-to-js.2969e22d.mjs:1:9
1: import { rewrite } from 'regex';
            ^
2: 
3: var __defProp = Object.defineProperty;
[vite-plugin-sveltekit-compile] "rewrite" is not exported by "node_modules/regex/dist/regex.min.js", imported by "node_modules/oniguruma-to-js/dist/shared/oniguruma-to-js.2969e22d.mjs".
file: /usr/src/app/node_modules/oniguruma-to-js/dist/shared/oniguruma-to-js.2969e22d.mjs:1:9
1: import { rewrite } from 'regex';
            ^
2: 
3: var __defProp = Object.defineProperty;
✓ built in 17.67s
error during build:
RollupError: "rewrite" is not exported by "node_modules/regex/dist/regex.min.js", imported by "node_modules/oniguruma-to-js/dist/shared/oniguruma-to-js.2969e22d.mjs".
    at error (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:2287:30)
    at Module.error (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:13745:16)
    at Module.traceVariable (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:14175:29)
    at ModuleScope.findVariable (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:12615:39)
    at FunctionScope.findVariable (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at ChildScope.findVariable (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at TrackingScope.findVariable (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at BlockScope.findVariable (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at Identifier.bind (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:8319:40)
    at CallExpression.bind (file:///usr/src/app/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)

Reproduction

Install shiki into a basic sveltekit skeleton then run npm run build

Contributes

  • [ ] I am willing to submit a PR to fix this issue
  • [ ] I am willing to submit a PR with failing tests

hxrriet02 avatar Oct 16 '24 10:10 hxrriet02

I can't reproduce your error reporting. If it's convenient, can you make a modification on my demo and provide it to a smallest reproducible repository?

zhangmo8 avatar Oct 22 '24 09:10 zhangmo8

I was also having this issue using vanilla vite 4.x, but it resolved once I upgraded to vite 5.x.

romannurik avatar Oct 31 '24 14:10 romannurik

This is out of of date since Shiki 1.23.0+ no longer uses oniguruma-to-js, and the replacement JS library doesn't use the rewrite function highlighted in the error here.

Is this resolved when using the latest Shiki?

slevithan avatar Nov 18 '24 13:11 slevithan

For anyone else that stumbles upon this, I got a similar error after being on the latest version:

"toRegExp" is not exported by "node_modules/oniguruma-to-es/dist/index.min.js", imported by "node_modules/@shikijs/engine-javascript/dist/index.mjs".
file: /app/node_modules/@shikijs/engine-javascript/dist/index.mjs:1:9
1: import { toRegExp } from 'oniguruma-to-es';
            ^
2:
3: var __defProp = Object.defineProperty;
[vite-plugin-sveltekit-compile] "toRegExp" is not exported by "node_modules/oniguruma-to-es/dist/index.min.js", imported by "node_modules/@shikijs/engine-javascript/dist/index.mjs".
file: /app/node_modules/@shikijs/engine-javascript/dist/index.mjs:1:9
1: import { toRegExp } from 'oniguruma-to-es';
            ^
2:
3: var __defProp = Object.defineProperty;
✓ built in 7.38s

I ended up solving it by upgrading to vite 5.0.3.

Axelwickm avatar Dec 03 '24 14:12 Axelwickm

@Axelwickm it sounds like some kind of issue/bug in a bundler or build tool, since it makes no sense to try to import from node_modules/oniguruma-to-es/dist/index.min.js. That file is a browser bundle that makes the library available as a global variable. It doesn't export anything. And yes, this seems like the same issue as when @hxrriet02 was seeing an error about importing from node_modules/regex/dist/regex.min.js since that's also a browser bundle with a global variable, and regex uses a similar package.json configuration as oniguruma-to-es.

Thanks for mentioning that upgrading to vite 5.0.3 solves it.

If you have ideas for how to change oniguruma-to-es's package.json to avoid / work around this with tools that are getting it wrong, that will probably be what's needed.

slevithan avatar Dec 03 '24 14:12 slevithan

I managed to fix this by upgrading svelte to the latest svelte (4) version, turns out it needed more than just an npm i. I wasn't able to update shiki without this, so I assume that was the issue.

hxrriet02 avatar Dec 03 '24 16:12 hxrriet02