Skip to content

Commit

Permalink
fix: dynamic import warning with @vite-ignore (#7533)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 31, 2022
1 parent 41fa2f5 commit 29c1ec0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
Expand Up @@ -8,6 +8,10 @@ test('should load literal dynamic import', async () => {
test('should load full dynamic import from public', async () => {
await page.click('.qux')
await untilUpdated(() => page.textContent('.view'), 'Qux view', true)
// No warning should be logged as we are using @vite-ignore
expect(
serverLogs.some((log) => log.includes('cannot be analyzed by vite'))
).toBe(false)
})

test('should load data URL of `blob:`', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/dynamic-import/nested/index.js
Expand Up @@ -35,7 +35,7 @@ document.querySelector('.mxd2').addEventListener('click', async () => {
const test = { jss: '../mxd.js' }
const ttest = test
const view = 'mxd'
const { default: mxdDynamic } = await import(test.jss)
const { default: mxdDynamic } = await import(/*@vite-ignore*/ test.jss)
text('.view', mxdStatic === mxdDynamic)
})

Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -518,7 +518,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}
} else if (!importer.startsWith(clientDir) && !ssr) {
// check @vite-ignore which suppresses dynamic import warning
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(rawUrl)
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(
// complete expression inside parens
source.slice(dynamicIndex + 1, end)
)

const url = rawUrl
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
Expand Down

0 comments on commit 29c1ec0

Please sign in to comment.