diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index 147090ac3faf9d..8b8970b55c00a3 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -112,7 +112,24 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { compressedCount = 0 }, - renderChunk() { + renderChunk(code, chunk) { + for (const id of chunk.moduleIds) { + const module = this.getModuleInfo(id) + if (!module) continue + if (module.importers.length && module.dynamicImporters.length) { + this.warn( + `\n(!) ${ + module.id + } is dynamically imported by ${module.dynamicImporters + .map((m) => m) + .join(', ')} but also statically imported by ${module.importers + .map((m) => m) + .join( + ', ', + )}, dynamic import will not move module into another chunk.\n`, + ) + } + } chunkCount++ if (shouldLogInfo) { if (!tty) { diff --git a/playground/dynamic-import/__tests__/dynamic-import.spec.ts b/playground/dynamic-import/__tests__/dynamic-import.spec.ts index 91e7ae35d0afaf..dbd6adb72f642a 100644 --- a/playground/dynamic-import/__tests__/dynamic-import.spec.ts +++ b/playground/dynamic-import/__tests__/dynamic-import.spec.ts @@ -129,3 +129,14 @@ test('should work with load ../ and contain itself directory', async () => { true, ) }) + +test.runIf(isBuild)( + 'should rollup warn when static and dynamic import a module in same chunk', + async () => { + await untilUpdated( + () => serverLogs.join('\n'), + 'dynamic import will not move module into another chunk', + true, + ) + }, +)