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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(optimizer): transpile before calling transformGlobImport #8343

Merged

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented May 26, 2022

Description

When import.meta.glob is used inside Vue SFC with TypeScript, optimizer fails with error.
Minimal reproduction (stackblitz). Run npx vite optimize to reproduce.

Before #7537, TypeScript was transpiled to JavaScript before passing to es-module-lexer.

if (contents.includes('import.meta.glob')) {
scripts[key] = {
// transformGlob already transforms to js
loader: 'js',
contents: await transformGlob(
contents,
path,
config.root,
loader,
resolve,
config.logger
)
}
} else {

async function transformGlob(
source: string,
importer: string,
root: string,
loader: Loader,
resolve: (url: string, importer?: string) => Promise<string | undefined>,
logger: Logger
) {
// transform the content first since es-module-lexer can't handle non-js
if (loader !== 'js') {
source = (await transform(source, { loader })).code
}
await init
const imports = parse(source)[0]

After #7537, the transpilation was removed.
scripts[key] = {
loader: 'js',
contents:
(
await transformGlobImport(
contents,
path,
config.root,
resolve
)
)?.s.toString() || contents
}

But parseImportGlob which is called from transformGlobImport assumes the code as JavaScript. So that transpilation is still needed.
try {
ast = parseExpressionAt(code, start, {
ecmaVersion: 'latest',
sourceType: 'module',
ranges: true
}) as any
} catch (e) {

Additional context

Found while testing alpha.4 with my project.馃槃


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label May 26, 2022
@sapphi-red sapphi-red marked this pull request as ready for review May 26, 2022 11:42
Comment on lines 302 to 305
// transpile because `transformGlobImport` only expects js
const transpiledContents = (
await transform(contents, { loader })
).code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only transpile if loader !== 'js'?

@antfu antfu merged commit 1dbc7cc into vitejs:main May 27, 2022
@sapphi-red sapphi-red deleted the fix/transpile-before-transformGlobImport branch May 27, 2022 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants