Skip to content

fix(analysis): warnings for dynamic imports that use static template literals#14458

Merged
bluwy merged 6 commits intovitejs:mainfrom
Dunqing:fix/issue/14101
Sep 28, 2023
Merged

fix(analysis): warnings for dynamic imports that use static template literals#14458
bluwy merged 6 commits intovitejs:mainfrom
Dunqing:fix/issue/14101

Conversation

@Dunqing
Copy link
Copy Markdown
Contributor

@Dunqing Dunqing commented Sep 24, 2023

Description

When I finished this PR, I noticed that there was another one #14103 being processed. But I think we shouldn't hide warnings.

Fixes: #14101
Closes: #14103

Additional context


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 PR Title 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.

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

bluwy
bluwy previously approved these changes Sep 25, 2023
Copy link
Copy Markdown
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I like this change!

Comment on lines +472 to +475
} else if (templateLiteralRE.test(rawUrl)) {
// Only static template literal will into this branch.
// It has variables will processed in importMetaGlob.ts
specifier = rawUrl.replace(templateLiteralRE, '$1')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is correct.
Dynamic import with /* @vite-ignore */ or dynamic imports that cannot be handled with the dynamic-import plugin (e.g. import(`${startsWithVariable}/foo`)) will be still there.
https://stackblitz.com/edit/vitejs-vite-fnln8a?file=main.js&terminal=dev
image

So templateLiteralRE.test(rawUrl) will match `foo${va}`
https://stackblitz.com/edit/node-ptwy2i?file=index.js

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, I realize that. A dynamic import that doesn't conform to the rules will still get in here. So do I just need to exclude paths containing variables here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So do I just need to exclude paths containing variables here?

I think so 👍

Comment thread packages/vite/src/node/plugins/importAnalysis.ts Outdated
sapphi-red
sapphi-red previously approved these changes Sep 26, 2023
Copy link
Copy Markdown
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

import(`foo\${bar}.js`) will still cause a warning but I guess that's fine.

@bluwy bluwy enabled auto-merge (squash) September 28, 2023 07:19
@bluwy bluwy added the p3-minor-bug An edge case that only affects very specific usage (priority) label Sep 28, 2023
@bluwy bluwy merged commit ec7ee22 into vitejs:main Sep 28, 2023
bluwy pushed a commit that referenced this pull request Oct 3, 2023
…literals (#14458)

Co-authored-by: 翠 / green <green@sapphi.red>
@bluwy bluwy mentioned this pull request Oct 3, 2023
bluwy pushed a commit that referenced this pull request Oct 3, 2023
…literals (#14458)

Co-authored-by: 翠 / green <green@sapphi.red>
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.

Import Analysis - warns on imports that use a template literal but have no variables

3 participants