wooorm / starry-night

Syntax highlighting, like GitHub

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot import grammar from default export

muuvmuuv opened this issue · comments

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",

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!

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.

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:

starry-night/lib/all.js

Lines 815 to 816 in 98556ed

// @ts-expect-error: TS is wrong, `.json` does not mean JSON.
sourceJson,
.

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.

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

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

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

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

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

starry-night/lib/all.js

Lines 815 to 816 in 98556ed

// @ts-expect-error: TS is wrong, `.json` does not mean JSON.
sourceJson,

It’s something TS should solve.