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

ESM / CJS syntax detection #11

Closed
pi0 opened this issue Oct 21, 2021 · 2 comments · Fixed by #12
Closed

ESM / CJS syntax detection #11

pi0 opened this issue Oct 21, 2021 · 2 comments · Fixed by #12
Assignees

Comments

@pi0
Copy link
Member

pi0 commented Oct 21, 2021

Provide utilities to use regex based static analyzes detecting if potentially ESM or CommonJS syntax is used in a code

  • hasESMSyntax(code: string, id?: string) (note: dynamic import is not esm syntax`
  • hasCJSSyntax(code: string, id?: string)
  • detectSyntax(code: string, id?: string): esm | cjs | mixed | unknown

Some reference: https://github.com/unjs/jiti/blob/main/src/utils.ts#L27

@danielroe danielroe self-assigned this Oct 22, 2021
@danielroe
Copy link
Member

For interest, there is also this more rigorous method that uses acorn to detect syntax.

@pi0
Copy link
Member Author

pi0 commented Oct 22, 2021

Indeed AST parsing is most reliable but costs performance impact that we can not simply use it for DX purpose anymore in many places. These utilities are mainly needed to have a quick guess on file format and inline to use vite or rollup's AST parser and transforms.

For reference rollup commonjs plugin is also regex based for first phase https://github.com/rollup/plugins/blob/master/packages/commonjs/src/parse.js

@pi0 pi0 closed this as completed in #12 Oct 27, 2021
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

Successfully merging a pull request may close this issue.

2 participants