Skip to content

Commit 127c334

Browse files
authoredJun 10, 2023
feat(build): warn dynamic import module with a static import alongside (#12850)
1 parent ff3ce31 commit 127c334

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,35 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
111111
compressedCount = 0
112112
},
113113

114-
renderChunk() {
114+
renderChunk(code, chunk) {
115+
for (const id of chunk.moduleIds) {
116+
const module = this.getModuleInfo(id)
117+
if (!module) continue
118+
// When a dynamic importer shares a chunk with the imported module,
119+
// warn that the dynamic imported module will not be moved to another chunk (#12850).
120+
if (module.importers.length && module.dynamicImporters.length) {
121+
// Filter out the intersection of dynamic importers and sibling modules in
122+
// the same chunk. The intersecting dynamic importers' dynamic import is not
123+
// expected to work. Note we're only detecting the direct ineffective
124+
// dynamic import here.
125+
if (
126+
module.dynamicImporters.some((m) => chunk.moduleIds.includes(m))
127+
) {
128+
this.warn(
129+
`\n(!) ${
130+
module.id
131+
} is dynamically imported by ${module.dynamicImporters
132+
.map((m) => m)
133+
.join(', ')} but also statically imported by ${module.importers
134+
.map((m) => m)
135+
.join(
136+
', ',
137+
)}, dynamic import will not move module into another chunk.\n`,
138+
)
139+
}
140+
}
141+
}
142+
115143
chunkCount++
116144
if (shouldLogInfo) {
117145
if (!tty) {

‎playground/dynamic-import/__tests__/dynamic-import.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,22 @@ test('should work with load ../ and contain itself directory', async () => {
129129
true,
130130
)
131131
})
132+
133+
test.runIf(isBuild)(
134+
'should rollup warn when static and dynamic import a module in same chunk',
135+
async () => {
136+
const log = serverLogs.join('\n')
137+
expect(log).toContain(
138+
'dynamic import will not move module into another chunk',
139+
)
140+
expect(log).toMatch(
141+
/\(!\).*\/dynamic-import\/files\/mxd\.js is dynamically imported by/,
142+
)
143+
expect(log).toMatch(
144+
/\(!\).*\/dynamic-import\/files\/mxd\.json is dynamically imported by/,
145+
)
146+
expect(log).not.toMatch(
147+
/\(!\).*\/dynamic-import\/nested\/shared\.js is dynamically imported by/,
148+
)
149+
},
150+
)

0 commit comments

Comments
 (0)
Please sign in to comment.