Skip to content

Commit 7a77aaf

Browse files
authoredAug 1, 2023
fix(build): silence warn dynamic import module when inlineDynamicImports true (#13970)
1 parent 2c73d10 commit 7a77aaf

File tree

7 files changed

+82
-24
lines changed

7 files changed

+82
-24
lines changed
 

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

+27-24
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,33 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
119119
compressedCount = 0
120120
},
121121

122-
renderChunk(code, chunk) {
123-
for (const id of chunk.moduleIds) {
124-
const module = this.getModuleInfo(id)
125-
if (!module) continue
126-
// When a dynamic importer shares a chunk with the imported module,
127-
// warn that the dynamic imported module will not be moved to another chunk (#12850).
128-
if (module.importers.length && module.dynamicImporters.length) {
129-
// Filter out the intersection of dynamic importers and sibling modules in
130-
// the same chunk. The intersecting dynamic importers' dynamic import is not
131-
// expected to work. Note we're only detecting the direct ineffective
132-
// dynamic import here.
133-
const detectedIneffectiveDynamicImport = module.dynamicImporters.some(
134-
(id) => !isInNodeModules(id) && chunk.moduleIds.includes(id),
135-
)
136-
if (detectedIneffectiveDynamicImport) {
137-
this.warn(
138-
`\n(!) ${
139-
module.id
140-
} is dynamically imported by ${module.dynamicImporters.join(
141-
', ',
142-
)} but also statically imported by ${module.importers.join(
143-
', ',
144-
)}, dynamic import will not move module into another chunk.\n`,
145-
)
122+
renderChunk(code, chunk, options) {
123+
if (!options.inlineDynamicImports) {
124+
for (const id of chunk.moduleIds) {
125+
const module = this.getModuleInfo(id)
126+
if (!module) continue
127+
// When a dynamic importer shares a chunk with the imported module,
128+
// warn that the dynamic imported module will not be moved to another chunk (#12850).
129+
if (module.importers.length && module.dynamicImporters.length) {
130+
// Filter out the intersection of dynamic importers and sibling modules in
131+
// the same chunk. The intersecting dynamic importers' dynamic import is not
132+
// expected to work. Note we're only detecting the direct ineffective
133+
// dynamic import here.
134+
const detectedIneffectiveDynamicImport =
135+
module.dynamicImporters.some(
136+
(id) => !isInNodeModules(id) && chunk.moduleIds.includes(id),
137+
)
138+
if (detectedIneffectiveDynamicImport) {
139+
this.warn(
140+
`\n(!) ${
141+
module.id
142+
} is dynamically imported by ${module.dynamicImporters.join(
143+
', ',
144+
)} but also statically imported by ${module.importers.join(
145+
', ',
146+
)}, dynamic import will not move module into another chunk.\n`,
147+
)
148+
}
146149
}
147150
}
148151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test } from 'vitest'
2+
import { isBuild, serverLogs } from '~utils'
3+
4+
test.runIf(isBuild)(
5+
'dont warn when inlineDynamicImports is set to true',
6+
async () => {
7+
const log = serverLogs.join('\n')
8+
expect(log).not.toContain(
9+
'dynamic import will not move module into another chunk',
10+
)
11+
},
12+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script type="module" src="./src/index.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@vitejs/test-dynamic-import-inline",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
10+
"preview": "vite preview"
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function foo() {
2+
return 'foo'
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import foo from './foo'
2+
3+
const asyncImport = async () => {
4+
const { foo } = await import('./foo.js')
5+
foo()
6+
}
7+
8+
foo()
9+
asyncImport()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import path from 'node:path'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
resolve: {
6+
alias: {
7+
'@': path.resolve(__dirname, 'alias'),
8+
},
9+
},
10+
build: {
11+
sourcemap: true,
12+
rollupOptions: {
13+
output: {
14+
inlineDynamicImports: true,
15+
},
16+
},
17+
},
18+
})

0 commit comments

Comments
 (0)
Please sign in to comment.