fix(build): silence warn dynamic import module when inlineDynamicImports true (#13970)

This commit is contained in:
disservin 2023-08-01 10:58:39 +02:00 committed by GitHub
parent 2c73d10ed2
commit 7a77aaf28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 24 deletions

View File

@ -119,30 +119,33 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
compressedCount = 0 compressedCount = 0
}, },
renderChunk(code, chunk) { renderChunk(code, chunk, options) {
for (const id of chunk.moduleIds) { if (!options.inlineDynamicImports) {
const module = this.getModuleInfo(id) for (const id of chunk.moduleIds) {
if (!module) continue const module = this.getModuleInfo(id)
// When a dynamic importer shares a chunk with the imported module, if (!module) continue
// warn that the dynamic imported module will not be moved to another chunk (#12850). // When a dynamic importer shares a chunk with the imported module,
if (module.importers.length && module.dynamicImporters.length) { // warn that the dynamic imported module will not be moved to another chunk (#12850).
// Filter out the intersection of dynamic importers and sibling modules in if (module.importers.length && module.dynamicImporters.length) {
// the same chunk. The intersecting dynamic importers' dynamic import is not // Filter out the intersection of dynamic importers and sibling modules in
// expected to work. Note we're only detecting the direct ineffective // the same chunk. The intersecting dynamic importers' dynamic import is not
// dynamic import here. // expected to work. Note we're only detecting the direct ineffective
const detectedIneffectiveDynamicImport = module.dynamicImporters.some( // dynamic import here.
(id) => !isInNodeModules(id) && chunk.moduleIds.includes(id), const detectedIneffectiveDynamicImport =
) module.dynamicImporters.some(
if (detectedIneffectiveDynamicImport) { (id) => !isInNodeModules(id) && chunk.moduleIds.includes(id),
this.warn( )
`\n(!) ${ if (detectedIneffectiveDynamicImport) {
module.id this.warn(
} is dynamically imported by ${module.dynamicImporters.join( `\n(!) ${
', ', module.id
)} but also statically imported by ${module.importers.join( } is dynamically imported by ${module.dynamicImporters.join(
', ', ', ',
)}, dynamic import will not move module into another chunk.\n`, )} but also statically imported by ${module.importers.join(
) ', ',
)}, dynamic import will not move module into another chunk.\n`,
)
}
} }
} }
} }

View File

@ -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',
)
},
)

View File

@ -0,0 +1 @@
<script type="module" src="./src/index.js"></script>

View File

@ -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"
}
}

View File

@ -0,0 +1,3 @@
export default function foo() {
return 'foo'
}

View File

@ -0,0 +1,9 @@
import foo from './foo'
const asyncImport = async () => {
const { foo } = await import('./foo.js')
foo()
}
foo()
asyncImport()

View File

@ -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,
},
},
},
})