Coder Social home page Coder Social logo

Comments (23)

dawnmist avatar dawnmist commented on September 23, 2024 108

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.

JellyL avatar JellyL commented on September 23, 2024 19

I think --exclude 'nothing' should be the default setting

from patch-package.

dawnmist avatar dawnmist commented on September 23, 2024 18

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:

// don't commit package.json though
fs.unlinkSync(
path.join(tmpRepo.name, "node_modules", packageName, "package.json"),
)

and:
// remove package.json again
fs.unlinkSync(
path.join(tmpRepo.name, "node_modules", packageName, "package.json"),
)

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.

perrin4869 avatar perrin4869 commented on September 23, 2024 17

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 avatar dawnmist commented on September 23, 2024 7

@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.

karlhorky avatar karlhorky commented on September 23, 2024 7

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.

ds300 avatar ds300 commented on September 23, 2024 5

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.

ds300 avatar ds300 commented on September 23, 2024 4

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.

devinrhode2 avatar devinrhode2 commented on September 23, 2024 4

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.

DominikGuzei avatar DominikGuzei commented on September 23, 2024 3

@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.

dawnmist avatar dawnmist commented on September 23, 2024 2

@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.

DominikGuzei avatar DominikGuzei commented on September 23, 2024 1

@dawnmist makes sense, thanks for the detailed explanation! πŸ‘ sorry for the mistake πŸ˜…

from patch-package.

ds300 avatar ds300 commented on September 23, 2024

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.

ds300 avatar ds300 commented on September 23, 2024

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.

brunolemos avatar brunolemos commented on September 23, 2024

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.

ivikash avatar ivikash commented on September 23, 2024

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.

ivikash avatar ivikash commented on September 23, 2024

@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.

danieldunderfelt avatar danieldunderfelt commented on September 23, 2024

@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.

ds300 avatar ds300 commented on September 23, 2024

Closing this to tidy up, final 6.0.0 release will happen soon πŸŽ‰

from patch-package.

nerdaliciousCH avatar nerdaliciousCH commented on September 23, 2024

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.

xstable avatar xstable commented on September 23, 2024

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.

pkit avatar pkit commented on September 23, 2024

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.

pkit avatar pkit commented on September 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.