Comments (23)
The --include
and --exclude
options are there - but they're used when creating patches for other packages, not during postinstall (where you're applying patches to the various npm packages). You also need to be providing the --exclude
option to override the default setting if you want to include changes to the package.json in the patch.
So to use it, you would first:
- Modify the files inside the
node_modules/package-to-be-patched
so that they contained what you needed, then - Create the patch for the package using:
patch-package --exclude 'nothing' package-to-be-patched
- Check that the patch file generated contains what you expected it to. If not, return to step 1 to update the contents of the package that you want to patch so that it reflects the correct information and redo the patch creation again.
- Then set up postinstall so it applies the generated patches after each installation:
"postinstall": "patch-package && jetify"
You won't be able to change the dependencies in another project's package.json, because the patch is only applied after the dependencies have been installed. But you can do things like add a typescript type definition file to a project and update its package.json file to point to your new types.
from patch-package.
I think --exclude 'nothing'
should be the default setting
from patch-package.
If you can add an example for disabling the default exclude, that'd be fantastic.
The other time that being able to patch package.json is desirable is when adding typescript type definitions to a package that doesn't have its own type definitions, so that you can add the "types" field to the package's package.json file pointing to your new type definition file.
Edit: It doesn't seem to matter what the exclude regex is, package.json is hardcoded to be excluded in the diff inside makePatch at:
patch-package/src/makePatch.ts
Lines 123 to 126 in 10c0a51
and:
patch-package/src/makePatch.ts
Lines 135 to 138 in 10c0a51
At present, the only way to be able to include package.json in the diff is to first patch patch-package to comment out those two lines in the compiled code. There's a bit of humour there in using patch-package to patch patch-package itself...
Patch: patch-package+5.1.1.patch - to allow enabling patching of package.json if you also use a regex that doesn't include it (e.g. --exclude '\*.txt'
).
patch-package
--- a/node_modules/patch-package/dist/makePatch.js
+++ b/node_modules/patch-package/dist/makePatch.js
@@ -66,14 +66,14 @@ function makePatch(packageName, appPath, packageManager, includePaths, excludePa
fs.writeFileSync(path.join(tmpRepo.name, ".gitignore"), "!/node_modules\n\n");
tmpExec_1("git", ["init"]);
// don't commit package.json though
- fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
+ // fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
tmpExec_1("git", ["add", "-f", slash(path.join("node_modules", packageName))]);
tmpExec_1("git", ["commit", "-m", "init"]);
// replace package with user's version
rimraf.sync(tmpRepoPackagePath);
fsExtra.copySync(packagePath, tmpRepoPackagePath, { recursive: true });
// remove package.json again
- fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
+ // fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
// stage all files
tmpExec_1("git", ["add", "-f", slash(path.join("node_modules", packageName))]);
// unstage any ignored files so they don't show up in the diff
from patch-package.
I think the biggest case for --exclude 'nothing'
is that one common requirement is to modify the exports
field of a package.json
file - due to many of them lacking exports for esm modules yet
from patch-package.
@dawnmist why not remove the default for
--exclude
? If we don't make changes to the package.json it wouldn't be picked up anyway, right?
@DominikGuzei You'd probably be better asking the project maintainer that question. I am just someone who uses patch-package (and highly recommends it for certain tasks) who also had to work out how to make changes to the package.json work (for adding typescript types to a few packages).
That said, my suspicion is that it's because any patch you apply to the npm package's package.json is only applied after all its dependencies have already been installed - so most changes you could potentially make to the npm package's package.json file would be applied too late for them to actually do what you intend them to do. If those changes were included by default, many people would try to use them in situations where they do not work and the maintainer would forever be answering issues about things like "I modified the package's dependencies using patch-package, but npm/yarn are still installing the old ones!?" due to people not understanding that the modification only gets applied after all packages are installed.
from patch-package.
To really exclude everything without choosing a filename that doesn't exist (which could lead to weird unexpected edge cases), use a regular expression pattern containing an empty string to exclude everything:
$ yarn patch-package react --exclude '^$'
from patch-package.
Hi @brunolemos π !
Unfortunately what you want to do is not possible π’ This is a common question about patch-package. I don't know of a way to get absolute control over which transient dependencies get installed. The best thing I've found is manually editing your own lock file, but that's only useful for making sure a specific version gets installed.
from patch-package.
Just published a fix for this in [email protected] β Thanks for pointing this out @dawnmist and @davidlampon π β€οΈ
I'll close this once v6 leaves beta.
from patch-package.
I don't think anyone mentioned this idea yet
But as typically we have 1000's of npm packages, this means most individual npm packages are not that huge.
If we tweak our gitignore to ignore all node_module
sub-folders like so:
node_nodules/*
We can then INCLUDE a certain node_module
sub-folder in our git repo:
!node_modules/rc-slider
I think this may altogether be a better approach than patch-package, unless the package you are touching is huge. By simply checking the node module into git directly, there's a lot of nice simple benefits
from patch-package.
@dawnmist why not remove the default for --exclude
? If we don't make changes to the package.json it wouldn't be picked up anyway, right?
from patch-package.
@nerdaliciousCH try making up a string that might be a filename but isn't.
e.g. --exclude 'IAmNotARealFile.txt'
The '$' in a regexp string means "at the end of the line" (or "end of the string" for something like filename strings). By having nothing before the $, you're actually matching every filename because every filename will have an end of the string.
from patch-package.
@dawnmist makes sense, thanks for the detailed explanation! π sorry for the mistake π
from patch-package.
Hi! π Thanks for the thorough explanation :-)
The first thing I noticed is that you're using JS-style regexp literals with the --exclude override. Instead you should use regular strings. See the examples in include-exclude-paths.sh β particularly this one which uses interesting regexp syntax. Imagine you were passing the string to new RegExp(...)
Thanks for bringing this issue to my attention. I'll try to improve that section of the readme.
from patch-package.
Oh yes, you're right! Really sorry about the mixup. I think those lines should have been removed when I added the --include and --exclude options.
from patch-package.
I'm using 6.0.0-7
and I'm not able to patch a package.json
from another package, how can I do that?
EDIT: Adding a --exclude '\*.txt'
made it work.
I removed a package from a third party package.json
but when I run yarn
it gets added to yarn.lock
anyway :( It makes sense, as it run a postinstall
script.
Tried this but did not work: "preinstall": "patch-package --include 'package\\.json$'"
from patch-package.
I am trying to patch a dependency of a dependency in the package.json
. It does generate the patch file with a little noise (using npm) but is unable to apply the patches -> **ERROR** Failed to apply patch for package somePackage
. Can you include a short summary or documentation on the usage, like how to generate the patch file and especially what should be done in prepare
step. I am currently using 6.0.0-v5
due to some constraints.
from patch-package.
@ds300 Any update on this? Also, as I am using npm
so should the patch-package
be applied on prepare
or postinstall
? While trying with preinstall
its throwing an error saying patch-package is not found.
from patch-package.
@ds300 so when will v6 leave beta? At least the beta is installable with npm, so it's not a huge issue, but people who are looking to patch package.json need to be aware about the beta and actively install it.
And yes, the beta works very well, so far as this issue is concerned!
from patch-package.
Closing this to tidy up, final 6.0.0 release will happen soon π
from patch-package.
How to make an empty --exclude that doesn't match anything?? so --include 'package\.json$' --exclude '$'. That doesn't work for me :D
from patch-package.
I use npx to run patch-package.
If I check for the Version I got:
npx patch-package -v
patch-package 6.2.0
So I'm right, that the include && exclude funcitonality still not implemented?
I use this:
postinstall": "patch-package --include 'package.json$' && jetify"
or double-escaped like this:
"postinstall": "patch-package --include 'package\\.json$' && jetify"
Both don't recognize the changes in package.json.
from patch-package.
For people who want to actually change dependencies: use yarn "resolutions" although it's not perfect (cannot install a new package that didn't exist before), it covers a lot of cases.
from patch-package.
By simply checking the node module into git directly, there's a lot of nice simple benefits
Dunno, looks like that approach gives zero new features compared to patch-package
, but pollutes the repo with a bunch of garbage.
Any examples of the benefits?
from patch-package.
Related Issues (20)
- Is there a way to convert pnpm patches to patch-package patches? Or do they use the same format? HOT 2
- -
- Uniterpretable Error during makePatch HOT 11
- react-native-otp-verify+1.1.6.patch
- Confusing syntax error message when pointing to a package that does not exist
- Patch not applied after package upgrade
- inflight - Missing Release of Resource after Effective Lifetime HOT 2
- how get the post-install to run when installing cosmos (or storybook and the like)
- Patch cannot be applied when it simultaneously renames and modifies the file HOT 5
- npx patch-package [email protected] error HOT 1
- Not sure if I'm clear on how to re-apply updated patch...
- Can patch-package detect content changes in package.json? Let's say I changed a dependency versionοΌ HOT 3
- Failed to apply patch for package react-native at path node_modules/react-native HOT 1
- Failed to apply patch for package react-native-ui-lib at path node_modules/react-native-ui-lib
- Installing a dependency with its own local patches doesn't apply them HOT 1
- Support for yarn berry HOT 1
- ERROR with RN 0.74.x HOT 1
- Respect `copy from`/`copy to` extended headers
- How to exclude android\build folder?
- node_modules/@rbmweb/canvas This error was caused because patch-package cannot apply the following patch file: patches/@rbmweb+canvas+1.0.0.patch
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from patch-package.