From e0ecb809b1fff2686d37467fd4211750c294757a Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 19 Aug 2022 18:31:53 +0800 Subject: [PATCH] feat(ssr): warn if cant analyze dynamic import (#9738) --- .../vite/src/node/plugins/importAnalysis.ts | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 99b7016264b2bc..77b24c2a35da68 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -553,42 +553,46 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // for pre-transforming staticImportedUrls.add({ url: urlWithoutBase, id: resolvedId }) } - } else if (!importer.startsWith(clientDir) && !ssr) { - // check @vite-ignore which suppresses dynamic import warning - const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test( - // complete expression inside parens - source.slice(dynamicIndex + 1, end) - ) - - const url = rawUrl - .replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '') - .trim() - if (!hasViteIgnore) { - this.warn( - `\n` + - colors.cyan(importerModule.file) + - `\n` + - generateCodeFrame(source, start) + - `\nThe above dynamic import cannot be analyzed by vite.\n` + - `See ${colors.blue( - `https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations` - )} ` + - `for supported dynamic import formats. ` + - `If this is intended to be left as-is, you can use the ` + - `/* @vite-ignore */ comment inside the import() call to suppress this warning.\n` + } else if (!importer.startsWith(clientDir)) { + if (!importer.includes('node_modules')) { + // check @vite-ignore which suppresses dynamic import warning + const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test( + // complete expression inside parens + source.slice(dynamicIndex + 1, end) ) + if (!hasViteIgnore) { + this.warn( + `\n` + + colors.cyan(importerModule.file) + + `\n` + + generateCodeFrame(source, start) + + `\nThe above dynamic import cannot be analyzed by Vite.\n` + + `See ${colors.blue( + `https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations` + )} ` + + `for supported dynamic import formats. ` + + `If this is intended to be left as-is, you can use the ` + + `/* @vite-ignore */ comment inside the import() call to suppress this warning.\n` + ) + } } - if ( - !/^('.*'|".*"|`.*`)$/.test(url) || - isExplicitImportRequired(url.slice(1, -1)) - ) { - needQueryInjectHelper = true - str().overwrite( - start, - end, - `__vite__injectQuery(${url}, 'import')`, - { contentOnly: true } - ) + + if (!ssr) { + const url = rawUrl + .replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '') + .trim() + if ( + !/^('.*'|".*"|`.*`)$/.test(url) || + isExplicitImportRequired(url.slice(1, -1)) + ) { + needQueryInjectHelper = true + str().overwrite( + start, + end, + `__vite__injectQuery(${url}, 'import')`, + { contentOnly: true } + ) + } } } }