Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): warn dynamic import module with a static import alongside #12850

Merged
merged 5 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,35 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
compressedCount = 0
},

renderChunk() {
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) {
bluwy marked this conversation as resolved.
Show resolved Hide resolved
// 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.
if (
module.dynamicImporters.some((m) => chunk.moduleIds.includes(m))
) {
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
19 changes: 19 additions & 0 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,22 @@ test('should work with load ../ and contain itself directory', async () => {
true,
)
})

test.runIf(isBuild)(
'should rollup warn when static and dynamic import a module in same chunk',
async () => {
const log = serverLogs.join('\n')
expect(log).toContain(
'dynamic import will not move module into another chunk',
)
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/,
)
},
)