Skip to content

Commit e0ecb80

Browse files
authoredAug 19, 2022
feat(ssr): warn if cant analyze dynamic import (#9738)
1 parent 1d6a1eb commit e0ecb80

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed
 

‎packages/vite/src/node/plugins/importAnalysis.ts

+38-34
Original file line numberDiff line numberDiff line change
@@ -553,42 +553,46 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
553553
// for pre-transforming
554554
staticImportedUrls.add({ url: urlWithoutBase, id: resolvedId })
555555
}
556-
} else if (!importer.startsWith(clientDir) && !ssr) {
557-
// check @vite-ignore which suppresses dynamic import warning
558-
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(
559-
// complete expression inside parens
560-
source.slice(dynamicIndex + 1, end)
561-
)
562-
563-
const url = rawUrl
564-
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
565-
.trim()
566-
if (!hasViteIgnore) {
567-
this.warn(
568-
`\n` +
569-
colors.cyan(importerModule.file) +
570-
`\n` +
571-
generateCodeFrame(source, start) +
572-
`\nThe above dynamic import cannot be analyzed by vite.\n` +
573-
`See ${colors.blue(
574-
`https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations`
575-
)} ` +
576-
`for supported dynamic import formats. ` +
577-
`If this is intended to be left as-is, you can use the ` +
578-
`/* @vite-ignore */ comment inside the import() call to suppress this warning.\n`
556+
} else if (!importer.startsWith(clientDir)) {
557+
if (!importer.includes('node_modules')) {
558+
// check @vite-ignore which suppresses dynamic import warning
559+
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(
560+
// complete expression inside parens
561+
source.slice(dynamicIndex + 1, end)
579562
)
563+
if (!hasViteIgnore) {
564+
this.warn(
565+
`\n` +
566+
colors.cyan(importerModule.file) +
567+
`\n` +
568+
generateCodeFrame(source, start) +
569+
`\nThe above dynamic import cannot be analyzed by Vite.\n` +
570+
`See ${colors.blue(
571+
`https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations`
572+
)} ` +
573+
`for supported dynamic import formats. ` +
574+
`If this is intended to be left as-is, you can use the ` +
575+
`/* @vite-ignore */ comment inside the import() call to suppress this warning.\n`
576+
)
577+
}
580578
}
581-
if (
582-
!/^('.*'|".*"|`.*`)$/.test(url) ||
583-
isExplicitImportRequired(url.slice(1, -1))
584-
) {
585-
needQueryInjectHelper = true
586-
str().overwrite(
587-
start,
588-
end,
589-
`__vite__injectQuery(${url}, 'import')`,
590-
{ contentOnly: true }
591-
)
579+
580+
if (!ssr) {
581+
const url = rawUrl
582+
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
583+
.trim()
584+
if (
585+
!/^('.*'|".*"|`.*`)$/.test(url) ||
586+
isExplicitImportRequired(url.slice(1, -1))
587+
) {
588+
needQueryInjectHelper = true
589+
str().overwrite(
590+
start,
591+
end,
592+
`__vite__injectQuery(${url}, 'import')`,
593+
{ contentOnly: true }
594+
)
595+
}
592596
}
593597
}
594598
}

0 commit comments

Comments
 (0)
Please sign in to comment.