chore: remove patch file deps hack (#8138)

This commit is contained in:
Bjorn Lu 2022-05-12 15:18:59 +08:00 committed by GitHub
parent 8220ee5773
commit a8b22b63ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 60 deletions

View File

@ -145,18 +145,7 @@ test('?raw import', async () => {
## Note on Test Dependencies
In many test cases we need to mock dependencies using `link:` and `file:` protocols (which are supported by package managers like `yarn` and `pnpm`). However, `pnpm` treats `link:` and `file:` the same way and always use symlinks. This can be undesirable in cases where we want the dependency to be actually copied into `node_modules`.
To work around this, playground packages that uses the `file:` protocol should also include the following `postinstall` script:
```jsonc
"scripts": {
//...
"postinstall": "ts-node ../../scripts/patchFileDeps.ts"
}
```
This script patches the dependencies using `file:` protocol to match the copying behavior instead of linking.
In many test cases we need to mock dependencies using `link:` and `file:` protocols. `pnpm` treats `link:` as symlinks and `file:` as hardlinks. To test dependencies as if they are copied into `node_modules`, use the `file:` protocol, other cases should use the `link:` protocol.
## Debug Logging

View File

@ -6,8 +6,7 @@
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview",
"postinstall": "ts-node ../../scripts/patchFileDeps.ts"
"preview": "vite preview"
},
"dependencies": {
"aliased-module": "file:./dir/module",

View File

@ -6,8 +6,7 @@
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview",
"postinstall": "ts-node ../../scripts/patchFileDeps.ts"
"preview": "vite preview"
},
"dependencies": {
"pkg": "file:./pkg"

View File

@ -4,6 +4,6 @@
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"nested-include": "link:./nested-include"
"nested-include": "file:../nested-include"
}
}

View File

@ -6,8 +6,7 @@
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview",
"postinstall": "ts-node ../../scripts/patchFileDeps.ts"
"preview": "vite preview"
},
"dependencies": {
"axios": "^0.24.0",

View File

@ -3,8 +3,7 @@
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "node server",
"postinstall": "ts-node ../../scripts/patchFileDeps.ts"
"dev": "node server"
},
"dependencies": {
"missing-dep": "file:./missing-dep"

View File

@ -1,5 +1,8 @@
{
"name": "forwarded-export",
"private": true,
"version": "0.0.0"
"version": "0.0.0",
"dependencies": {
"object-assigned-exports": "file:../object-assigned-exports"
}
}

View File

@ -5,8 +5,7 @@
"scripts": {
"dev": "node server",
"serve": "cross-env NODE_ENV=production node server",
"debug": "node --inspect-brk server",
"postinstall": "ts-node ../../scripts/patchFileDeps.ts"
"debug": "node --inspect-brk server"
},
"dependencies": {
"bcrypt": "^5.0.1",

View File

@ -614,11 +614,11 @@ importers:
playground/optimize-deps/nested-exclude:
specifiers:
nested-include: link:./nested-include
nested-include: file:../nested-include
dependencies:
nested-include: link:nested-include
nested-include: file:playground/optimize-deps/nested-include
playground/optimize-deps/nested-exclude/nested-include:
playground/optimize-deps/nested-include:
specifiers: {}
playground/optimize-missing-deps:
@ -779,7 +779,10 @@ importers:
specifiers: {}
playground/ssr-deps/forwarded-export:
specifiers: {}
specifiers:
object-assigned-exports: file:../object-assigned-exports
dependencies:
object-assigned-exports: file:playground/ssr-deps/object-assigned-exports
playground/ssr-deps/object-assigned-exports:
specifiers: {}
@ -8295,7 +8298,13 @@ packages:
name: nested-exclude
version: 1.0.0
dependencies:
nested-include: link:nested-include
nested-include: file:playground/optimize-deps/nested-include
dev: false
file:playground/optimize-deps/nested-include:
resolution: {directory: playground/optimize-deps/nested-include, type: directory}
name: nested-include
version: 1.0.0
dev: false
file:playground/optimize-missing-deps/missing-dep:
@ -8328,6 +8337,8 @@ packages:
resolution: {directory: playground/ssr-deps/forwarded-export, type: directory}
name: forwarded-export
version: 0.0.0
dependencies:
object-assigned-exports: file:playground/ssr-deps/object-assigned-exports
dev: false
file:playground/ssr-deps/object-assigned-exports:

View File

@ -1,31 +0,0 @@
// pnpm treats file: protocols as link:, so it doesn't copy the actual files
// into node_modules, causing Vite to still consider those deps linked.
// This script is called from postinstall hooks in playground packages that
// uses the file: protocol, and copies the file: deps into node_modules.
import { join, resolve } from 'path'
import { copySync, removeSync } from 'fs-extra'
const root = process.cwd()
const pkg = require(join(root, 'package.json'))
let hasPatched: boolean = false
for (const [key, val] of Object.entries<string>(pkg.dependencies)) {
if (val.startsWith('file:')) {
hasPatched = true
const src = resolve(root, val.slice('file:'.length))
const dest = resolve(root, 'node_modules', key)
removeSync(dest)
copySync(src, dest, {
dereference: true
})
console.log(`patched ${val}`)
}
}
if (hasPatched) {
// remove node_modules/.ignored as pnpm will think our patched files are
// installed by another package manager and move them into this directory.
// On further installs it will error out if this directory is not empty.
removeSync(resolve(root, 'node_modules', '.ignored'))
}