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: don't add ?import for @vite-ignoreed import #14851

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
server.warmupRequest(url, { ssr })
}
} else if (!importer.startsWith(withTrailingSlash(clientDir))) {
// check @vite-ignore which suppresses dynamic import warning
const hasViteIgnore = hasViteIgnoreRE.test(
// complete expression inside parens
source.slice(dynamicIndex + 1, end),
)
if (!isInNodeModules(importer)) {
// check @vite-ignore which suppresses dynamic import warning
const hasViteIgnore = hasViteIgnoreRE.test(
// complete expression inside parens
source.slice(dynamicIndex + 1, end),
)
if (!hasViteIgnore) {
this.warn(
`\n` +
Expand All @@ -652,8 +652,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
if (!ssr) {
const url = rawUrl.replace(cleanUpRawUrlRE, '').trim()
if (
!urlIsStringRE.test(url) ||
isExplicitImportRequired(url.slice(1, -1))
!hasViteIgnore &&
(!urlIsStringRE.test(url) ||
isExplicitImportRequired(url.slice(1, -1)))
) {
needQueryInjectHelper = true
str().overwrite(
Expand Down
22 changes: 16 additions & 6 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { expect, test } from 'vitest'
import { getColor, isBuild, page, serverLogs, untilUpdated } from '~utils'
import {
getColor,
isBuild,
isServe,
page,
serverLogs,
untilUpdated,
} from '~utils'

test('should load literal dynamic import', async () => {
await page.click('.baz')
Expand Down Expand Up @@ -30,11 +37,14 @@ test('should have same reference on static and dynamic js import, .mxd', async (
await untilUpdated(() => page.textContent('.view'), 'true', true)
})

// in this case, it is not possible to detect the correct module
test('should have same reference on static and dynamic js import, .mxd2', async () => {
await page.click('.mxd2')
await untilUpdated(() => page.textContent('.view'), 'false', true)
})
// in this case, it is not possible to detect the correct module in build
test.runIf(isServe)(
'should have same reference on static and dynamic js import, .mxd2',
async () => {
await page.click('.mxd2')
await untilUpdated(() => page.textContent('.view'), 'true')
},
)

test('should have same reference on static and dynamic js import, .mxdjson', async () => {
await page.click('.mxdjson')
Expand Down
2 changes: 0 additions & 2 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ document.querySelector('.mxd').addEventListener('click', async () => {

document.querySelector('.mxd2').addEventListener('click', async () => {
const test = { jss: '../files/mxd.js' }
const ttest = test
const view = 'mxd'
const { default: mxdDynamic } = await import(/*@vite-ignore*/ test.jss)
text('.view', mxdStatic === mxdDynamic)
})
Expand Down