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

Type-Only Imports make an error #66

Open
5ouma opened this issue Nov 7, 2024 · 1 comment
Open

Type-Only Imports make an error #66

5ouma opened this issue Nov 7, 2024 · 1 comment

Comments

@5ouma
Copy link

5ouma commented Nov 7, 2024

When I import only types from .astro files to the .stories.ts, it outputs an error and doesn't start.

Unexpected token (1:23)
  Stack trace:
    at pp$4.raise (node_modules/acorn/dist/acorn.mjs:3640:13)
    at pp$9.expect (node_modules/acorn/dist/acorn.mjs:760:26)
    at pp$8.parseImport (node_modules/acorn/dist/acorn.mjs:1845:28)
    at pp$8.parseTopLevel (node_modules/acorn/dist/acorn.mjs:823:21)
    at Function.parse (node_modules/acorn/dist/acorn.mjs:645:35)
error: script "astrobook" exited with code 1
@MicheMayer
Copy link

The problem is the acorn parser used during the consumption of story files.
It is only equipped to handle jsx. So an explicit type import or type syntax will cause a syntax error.

See here:

import { Parser } from 'acorn'
import jsx from 'acorn-jsx'
/**
* Parses the content of the given file and returns all its exports
*/
export function getExports(code: string): string[] {
// Parse the code into an AST
const parser = Parser.extend(jsx())
const ast = parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
allowImportExportEverywhere: true,
})

The only way to fix this is to transpile typescript first before consuming it or use a different parser.
As a quick fix you could avoid using that syntax and configure your project with:
tsconfig.json

{
  "verbatimModuleSyntax": false,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants