Skip to content

Commit

Permalink
fix: only warn when dynamic importer in same chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed May 17, 2023
1 parent 15b005e commit d16e2e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
30 changes: 19 additions & 11 deletions packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,28 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
for (const id of chunk.moduleIds) {
const module = this.getModuleInfo(id)
if (!module) continue
// warn if a module is imported both dynamically and statically in a chunk
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`,
)
for (const dynamicImporter of module.dynamicImporters) {
if (chunk.moduleIds.includes(dynamicImporter)) {
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) {
Expand Down
14 changes: 11 additions & 3 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,18 @@ test('should work with load ../ and contain itself directory', async () => {
test.runIf(isBuild)(
'should rollup warn when static and dynamic import a module in same chunk',
async () => {
await untilUpdated(
() => serverLogs.join('\n'),
const log = serverLogs.join('\n')
expect(log).toContain(
'dynamic import will not move module into another chunk',
true,
)
expect(log).toMatch(
/\(!\).*\/dynamic-import\/files\/mxd\.js is dynamically imported by/,
)
expect(log).toMatch(
/\(!\).*\/dynamic-import\/files\/mxd\.json is dynamically imported by/,
)
expect(log).not.toMatch(
/\(!\).*\/dynamic-import\/nested\/shared\.js is dynamically imported by/,
)
},
)

0 comments on commit d16e2e2

Please sign in to comment.