Skip to content

Commit

Permalink
feat(node): add config option of warnExternalChunkRender
Browse files Browse the repository at this point in the history
  • Loading branch information
chaejunlee committed Jul 18, 2023
1 parent e0f49a7 commit 135e9d6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
7 changes: 7 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ export interface BuildOptions {
* @default null
*/
watch?: WatcherOptions | null
/**
* Whether to log warnings emitted by ineffective dynamic imports.
* Ineffective dynamic imports are imports that do not split chunks (#12850).
* @default false
*/
warnExternalChunkRender: boolean
}

export interface LibraryOptions {
Expand Down Expand Up @@ -350,6 +356,7 @@ export function resolveBuildOptions(
reportCompressedSize: true,
chunkSizeWarningLimit: 500,
watch: null,
warnExternalChunkRender: false,
}

const userBuildOptions = raw
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export async function resolveConfig(
optimizeDeps: { disabled: false },
ssr: { optimizeDeps: { disabled: false } },
})
config.build ??= {}
config.build ??= {} as BuildOptions
config.build.commonjsOptions = { include: [] }
}

Expand Down
22 changes: 11 additions & 11 deletions packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
// 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 (hasStaticImport && hasDynamicImport) {
// Check if the module is imported by an external dependency, because
// warning ineffective dynamic import of an external dependency is not helpful.
const isExternalDependency = (module: string) =>
module.includes('/node_modules/')
const isInTheSameChunk = (module: string) =>
chunk.moduleIds.includes(module)
const detectedDirectIneffectiveDynamicImport =
module.dynamicImporters.some(
(m) => !isExternalDependency(m) && isInTheSameChunk(m),
)
const warningCondition = (module: string) => {
const isExternalDependency = module.includes('/node_modules/')
const isInSameChunk = chunk.moduleIds.includes(module)
if (!isInSameChunk) return false
if (!isExternalDependency) return true
return config.build.warnExternalChunkRender
}

const detectedIneffectiveDynamicImport =
module.dynamicImporters.some(warningCondition)

// 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 (detectedDirectIneffectiveDynamicImport) {
if (detectedIneffectiveDynamicImport) {
this.warn(
`\n(!) ${
module.id
Expand Down

0 comments on commit 135e9d6

Please sign in to comment.