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

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

Merged
merged 3 commits into from
Aug 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,33 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
compressedCount = 0
},

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) {
// 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.
const detectedIneffectiveDynamicImport = module.dynamicImporters.some(
(id) => !isInNodeModules(id) && chunk.moduleIds.includes(id),
)
if (detectedIneffectiveDynamicImport) {
this.warn(
`\n(!) ${
module.id
} is dynamically imported by ${module.dynamicImporters.join(
', ',
)} but also statically imported by ${module.importers.join(
', ',
)}, dynamic import will not move module into another chunk.\n`,
)
renderChunk(code, chunk, options) {
if (!options.inlineDynamicImports) {
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) {
// 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.
const detectedIneffectiveDynamicImport =
module.dynamicImporters.some(
(id) => !isInNodeModules(id) && chunk.moduleIds.includes(id),
)
if (detectedIneffectiveDynamicImport) {
this.warn(
`\n(!) ${
module.id
} is dynamically imported by ${module.dynamicImporters.join(
', ',
)} but also statically imported by ${module.importers.join(
', ',
)}, dynamic import will not move module into another chunk.\n`,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
)
},
)
1 change: 1 addition & 0 deletions playground/dynamic-import-inline/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script type="module" src="./src/index.js"></script>
12 changes: 12 additions & 0 deletions playground/dynamic-import-inline/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 3 additions & 0 deletions playground/dynamic-import-inline/src/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function foo() {
return 'foo'
}
9 changes: 9 additions & 0 deletions playground/dynamic-import-inline/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import foo from './foo'

const asyncImport = async () => {
const { foo } = await import('./foo.js')
foo()
}

foo()
asyncImport()
18 changes: 18 additions & 0 deletions playground/dynamic-import-inline/vite.config.js
Original file line number Diff line number Diff line change
@@ -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,
},
},
},
})