-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect and warn against imports to transitively optimized deps
close #1543
- Loading branch information
Showing
3 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
import { Plugin } from 'rollup' | ||
import { DepOptimizationMetadata } from '.' | ||
|
||
const moduleIdRE = /node_modules\/([^@.][^/]*|@[^/]+\/[^/]+)\// | ||
|
||
export function recordCjsEntryPlugin(data: DepOptimizationMetadata): Plugin { | ||
return { | ||
name: 'vite:cjs-entry-named-export', | ||
async generateBundle(options, bundle) { | ||
async generateBundle(_, bundle) { | ||
Object.values(bundle).forEach((chunk) => { | ||
if (chunk.type === 'chunk' && chunk.isEntry) { | ||
data.optimized[chunk.name] = chunk.fileName | ||
if (chunk.facadeModuleId) { | ||
const facadeInfo = this.getModuleInfo(chunk.facadeModuleId) | ||
// this info is exposed by rollup commonjs plugin | ||
if (facadeInfo?.meta?.commonjs?.isCommonJS) { | ||
data.cjsEntries[chunk.fileName] = true | ||
if (chunk.type === 'chunk') { | ||
if (chunk.isEntry) { | ||
data.optimized[chunk.name] = chunk.fileName | ||
if (chunk.facadeModuleId) { | ||
const facadeInfo = this.getModuleInfo(chunk.facadeModuleId) | ||
// this info is exposed by rollup commonjs plugin | ||
if (facadeInfo?.meta?.commonjs?.isCommonJS) { | ||
data.cjsEntries[chunk.fileName] = true | ||
} | ||
} | ||
} | ||
for (const id in chunk.modules) { | ||
const depId = id.match(moduleIdRE)?.[1] | ||
if (depId) { | ||
data.transitiveOptimized[depId] = true | ||
} | ||
} | ||
} | ||
}) | ||
|
||
for (const key in data.transitiveOptimized) { | ||
if (key in data.optimized) { | ||
delete data.transitiveOptimized[key] | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters