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

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

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/

const templateLiteralRE = /^\s*`(.*)`\s*$/

interface UrlPosition {
url: string
start: number
Expand Down Expand Up @@ -423,12 +425,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
ss: expStart,
se: expEnd,
d: dynamicIndex,
// #2083 User may use escape path,
// so use imports[index].n to get the unescaped string
n: specifier,
a: assertIndex,
} = importSpecifier

// #2083 User may use escape path,
// so use imports[index].n to get the unescaped string
let specifier = importSpecifier.n

const rawUrl = source.slice(start, end)

// check import.meta usage
Expand Down Expand Up @@ -466,6 +469,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
hasEnv = true
}
return
} 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
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
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
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 👍

}

const isDynamicImport = dynamicIndex > -1
Expand Down
2 changes: 2 additions & 0 deletions playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

<div class="dynamic-import-self"></div>

<div class="dynamic-import-static"></div>

<div class="dynamic-import-nested-self"></div>

<script type="module" src="./nested/index.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@ import(`../nested/nested/${base}.js`).then((mod) => {
text('.dynamic-import-nested-self', mod.self)
})

import(`../nested/static.js`).then((mod) => {
text('.dynamic-import-static', mod.self)
})

console.log('index.js')
1 change: 1 addition & 0 deletions playground/dynamic-import/nested/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const self = 'dynamic-import-static'