Skip to content

Commit

Permalink
fix: don't add ?import for @vite-ignoreed import (#14851)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 2, 2023
1 parent ab5bb40 commit 42fee9e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
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

0 comments on commit 42fee9e

Please sign in to comment.