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(optimizer): support patch-package #10286

Merged
merged 1 commit into from Nov 25, 2022
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
5 changes: 3 additions & 2 deletions docs/guide/dep-pre-bundling.md
Expand Up @@ -77,9 +77,10 @@ Both `include` and `exclude` can be used to deal with this. If the dependency is

Vite caches the pre-bundled dependencies in `node_modules/.vite`. It determines whether it needs to re-run the pre-bundling step based on a few sources:

- The `dependencies` list in your `package.json`.
- Package manager lockfiles, e.g. `package-lock.json`, `yarn.lock`, or `pnpm-lock.yaml`.
- Package manager lockfile content, e.g. `package-lock.json`, `yarn.lock`, `pnpm-lock.yaml` or `bun.lockb`.
- Patches folder modification time.
- Relevant fields in your `vite.config.js`, if present.
- `NODE_ENV` value.

The pre-bundling step will only need to be re-run when one of the above has changed.

Expand Down
32 changes: 27 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -1042,14 +1042,36 @@ function isSingleDefaultExport(exports: readonly string[]) {
}

const lockfileFormats = [
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
'bun.lockb'
{ name: 'package-lock.json', patchesDirs: ['patches'] }, // Default of https://github.com/ds300/patch-package
{ name: 'yarn.lock', patchesDirs: ['patches', '.yarn/patches'] }, // .yarn/patches for v2+
Copy link
Contributor

@merceyz merceyz Nov 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be changed by the user but either way it's part of the lockfile.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the report, I didn't notice while I was testing. I will send a PR to fix this before the release

{ name: 'pnpm-lock.yaml', patchesDirs: [] }, // Included in lockfile
{ name: 'bun.lockb', patchesDirs: ['patches'] }
]

export function getDepHash(config: ResolvedConfig, ssr: boolean): string {
let content = lookupFile(config.root, lockfileFormats) || ''
const lockfilePath = lookupFile(
config.root,
lockfileFormats.map((l) => l.name),
{ pathOnly: true }
)
let content = lockfilePath ? fs.readFileSync(lockfilePath, 'utf-8') : ''
if (lockfilePath) {
const lockfileName = path.basename(lockfilePath)
const { patchesDirs } = lockfileFormats.find(
(f) => f.name === lockfileName
)!
const dependenciesRoot = path.dirname(lockfilePath)
for (const patchesDir of patchesDirs) {
const fullPath = path.join(dependenciesRoot, patchesDir)
if (fs.existsSync(fullPath)) {
const stats = fs.statSync(fullPath)
if (stats.isDirectory()) {
content += stats.mtimeMs.toString()
break
}
}
}
}
// also take config into account
// only a subset of config options that can affect dep optimization
const optimizeDeps = getDepOptimizationConfig(config, ssr)
Expand Down