From 7a77aaf28b7d2b384dff9f250cb886782b198166 Mon Sep 17 00:00:00 2001 From: disservin Date: Tue, 1 Aug 2023 10:58:39 +0200 Subject: [PATCH] fix(build): silence warn dynamic import module when inlineDynamicImports true (#13970) --- packages/vite/src/node/plugins/reporter.ts | 51 ++++++++++--------- .../__tests__/dynamic-import-inline.spec.ts | 12 +++++ playground/dynamic-import-inline/index.html | 1 + playground/dynamic-import-inline/package.json | 12 +++++ playground/dynamic-import-inline/src/foo.js | 3 ++ playground/dynamic-import-inline/src/index.js | 9 ++++ .../dynamic-import-inline/vite.config.js | 18 +++++++ 7 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 playground/dynamic-import-inline/__tests__/dynamic-import-inline.spec.ts create mode 100644 playground/dynamic-import-inline/index.html create mode 100644 playground/dynamic-import-inline/package.json create mode 100644 playground/dynamic-import-inline/src/foo.js create mode 100644 playground/dynamic-import-inline/src/index.js create mode 100644 playground/dynamic-import-inline/vite.config.js diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index e03bb5618..cbcb0409d 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -119,30 +119,33 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { compressedCount = 0 }, - renderChunk(code, chunk) { - for (const id of chunk.moduleIds) { - const module = this.getModuleInfo(id) - if (!module) continue - // When a dynamic importer shares a chunk with the imported module, - // warn that the dynamic imported module will not be moved to another chunk (#12850). - if (module.importers.length && module.dynamicImporters.length) { - // Filter out the intersection of dynamic importers and sibling modules in - // the same chunk. The intersecting dynamic importers' dynamic import is not - // expected to work. Note we're only detecting the direct ineffective - // dynamic import here. - const detectedIneffectiveDynamicImport = module.dynamicImporters.some( - (id) => !isInNodeModules(id) && chunk.moduleIds.includes(id), - ) - if (detectedIneffectiveDynamicImport) { - this.warn( - `\n(!) ${ - module.id - } is dynamically imported by ${module.dynamicImporters.join( - ', ', - )} but also statically imported by ${module.importers.join( - ', ', - )}, dynamic import will not move module into another chunk.\n`, - ) + renderChunk(code, chunk, options) { + if (!options.inlineDynamicImports) { + for (const id of chunk.moduleIds) { + const module = this.getModuleInfo(id) + if (!module) continue + // When a dynamic importer shares a chunk with the imported module, + // warn that the dynamic imported module will not be moved to another chunk (#12850). + if (module.importers.length && module.dynamicImporters.length) { + // Filter out the intersection of dynamic importers and sibling modules in + // the same chunk. The intersecting dynamic importers' dynamic import is not + // expected to work. Note we're only detecting the direct ineffective + // dynamic import here. + const detectedIneffectiveDynamicImport = + module.dynamicImporters.some( + (id) => !isInNodeModules(id) && chunk.moduleIds.includes(id), + ) + if (detectedIneffectiveDynamicImport) { + this.warn( + `\n(!) ${ + module.id + } is dynamically imported by ${module.dynamicImporters.join( + ', ', + )} but also statically imported by ${module.importers.join( + ', ', + )}, dynamic import will not move module into another chunk.\n`, + ) + } } } } diff --git a/playground/dynamic-import-inline/__tests__/dynamic-import-inline.spec.ts b/playground/dynamic-import-inline/__tests__/dynamic-import-inline.spec.ts new file mode 100644 index 000000000..6c2c0c447 --- /dev/null +++ b/playground/dynamic-import-inline/__tests__/dynamic-import-inline.spec.ts @@ -0,0 +1,12 @@ +import { expect, test } from 'vitest' +import { isBuild, serverLogs } from '~utils' + +test.runIf(isBuild)( + 'dont warn when inlineDynamicImports is set to true', + async () => { + const log = serverLogs.join('\n') + expect(log).not.toContain( + 'dynamic import will not move module into another chunk', + ) + }, +) diff --git a/playground/dynamic-import-inline/index.html b/playground/dynamic-import-inline/index.html new file mode 100644 index 000000000..d86d5c089 --- /dev/null +++ b/playground/dynamic-import-inline/index.html @@ -0,0 +1 @@ + diff --git a/playground/dynamic-import-inline/package.json b/playground/dynamic-import-inline/package.json new file mode 100644 index 000000000..32a989272 --- /dev/null +++ b/playground/dynamic-import-inline/package.json @@ -0,0 +1,12 @@ +{ + "name": "@vitejs/test-dynamic-import-inline", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../packages/vite/bin/vite", + "preview": "vite preview" + } +} diff --git a/playground/dynamic-import-inline/src/foo.js b/playground/dynamic-import-inline/src/foo.js new file mode 100644 index 000000000..483b987b3 --- /dev/null +++ b/playground/dynamic-import-inline/src/foo.js @@ -0,0 +1,3 @@ +export default function foo() { + return 'foo' +} diff --git a/playground/dynamic-import-inline/src/index.js b/playground/dynamic-import-inline/src/index.js new file mode 100644 index 000000000..a151a21a5 --- /dev/null +++ b/playground/dynamic-import-inline/src/index.js @@ -0,0 +1,9 @@ +import foo from './foo' + +const asyncImport = async () => { + const { foo } = await import('./foo.js') + foo() +} + +foo() +asyncImport() diff --git a/playground/dynamic-import-inline/vite.config.js b/playground/dynamic-import-inline/vite.config.js new file mode 100644 index 000000000..2d69e108f --- /dev/null +++ b/playground/dynamic-import-inline/vite.config.js @@ -0,0 +1,18 @@ +import path from 'node:path' +import { defineConfig } from 'vite' + +export default defineConfig({ + resolve: { + alias: { + '@': path.resolve(__dirname, 'alias'), + }, + }, + build: { + sourcemap: true, + rollupOptions: { + output: { + inlineDynamicImports: true, + }, + }, + }, +})