wooorm / starry-night

Syntax highlighting, like GitHub
MIT License
1.45k stars 30 forks source link

Cannot import grammar from default export #35

Closed muuvmuuv closed 10 months ago

muuvmuuv commented 10 months ago

There is no export at the root of starry-night:

import mdx from '@wooorm/starry-night/source.mdx'

show this error:

✘ [ERROR] TS2307: Cannot find module '@wooorm/starry-night/source.json' or its corresponding type declarations. [plugin angular-compiler]

    src/app/components/code/code.service.ts:3:24:
      3 │ import jsonGrammar from '@wooorm/starry-night/source.json'

but in /lang/ are all grammars exported. Unfortunately when using lang I get this error:

✘ [ERROR] Could not resolve "@wooorm/starry-night/lang/source.json"

    src/app/components/code/code.service.ts:3:24:
      3 │ import jsonGrammar from '@wooorm/starry-night/lang/source.json';
        ╵                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  The module "./lang/lang/source.json.js" was not found on the file system:

    node_modules/@wooorm/starry-night/package.json:37:11:
      37 │     "./*": "./lang/*.js",
wooorm commented 10 months ago

Your current setup works. It's just that typescript doesn't understand that .json does not mean json. You might want to raise it with TS folks!

muuvmuuv commented 10 months ago

Is that really a TS issue? From what I see TS gets the path correct (second error), just has issue with ./*. Maybe a explicit ./*.json would help. In general that shouldn't be a problem since Angular e.g. uses xxx.component.ts where component is the same as json here.

wooorm commented 10 months ago

The second error is how things are supposed to work. Because the second code is not allowed. See the docs or the export map in package.json for what is allowed.

The first code is allowed. But TS fails. Other tools don’t fail. Here TS fails too: https://github.com/wooorm/starry-night/blob/98556ed2274d13fbc10925e4a9b084a47ee62d8d/lib/all.js#L815-L816.

If you use other tools that fail, that might be in those tools too. But you never said which tools you use, so I can’t debug something I don’t know.

muuvmuuv commented 10 months ago

Yes, the second was just a try to fix the conflict.

I use VS Code, my project is an Angular app. Here is a repl (didn't had time yet): https://codesandbox.io/p/sandbox/elegant-surf-dft8c3?file=%2Fsrc%2Fapp%2Fapp.component.ts%3A12%2C3-12%2C13

wooorm commented 10 months ago

Add a // @ts-expect-error above it and it works?

Again, TS just doesn’t get .json in export maps.

muuvmuuv commented 10 months ago

Works! Hm, is this special to .json? Because other potential extensions seems to work. Anyway worth to add to the docs I think.

wooorm commented 10 months ago

Yes, TS has a bug with .json. See the code I linked before:

https://github.com/wooorm/starry-night/blob/98556ed2274d13fbc10925e4a9b084a47ee62d8d/lib/all.js#L815-L816

It’s something TS should solve.