Skip to content

Reduce installation size #1

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

Merged
merged 13 commits into from
Mar 1, 2025
6 changes: 6 additions & 0 deletions .changeset/slow-llamas-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@unts/patch-package": minor
---

feat: reduce installation size by replacing `json-stable-stringify` to
`@nolyfill/json-stable-stringify`
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`Test dev-only-patches: 00: patch-package happily ignores slash on CI be
patch-package 0.0.0
Applying patches...
[email protected]
Skipping dev-only slash@3.0.0 ✔
Skipping dev-only slash@2.0.0 ✔
END SNAPSHOT"
`;

Expand All @@ -14,12 +14,12 @@ exports[`Test dev-only-patches: 01: patch-package fails to find fake-package 1`]
patch-package 0.0.0
Applying patches...
[email protected]
Skipping dev-only slash@3.0.0 ✔
Skipping dev-only slash@2.0.0 ✔
Error: Patch file found for package fake-package which is not present at node_modules/fake-package

If this package is a dev dependency, rename the patch file to

fake-package+3.0.0.dev.patch
fake-package+2.0.0.dev.patch

---
patch-package finished with 1 error(s).
Expand All @@ -30,8 +30,8 @@ exports[`Test dev-only-patches: 02: fake-package should be skipped 1`] = `
"SNAPSHOT: fake-package should be skipped
patch-package 0.0.0
Applying patches...
Skipping dev-only fake-package@3.0.0 ✔
Skipping dev-only fake-package@2.0.0 ✔
[email protected]
Skipping dev-only slash@3.0.0 ✔
Skipping dev-only slash@2.0.0 ✔
END SNAPSHOT"
`;
6 changes: 3 additions & 3 deletions integration-tests/dev-only-patches/dev-only-patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ echo "SNAPSHOT: patch-package happily ignores slash on CI because it's a dev dep
patch-package
echo "END SNAPSHOT"

echo "create fake-package+3.0.0.patch"
cp patches/slash+3.0.0.patch patches/fake-package+3.0.0.patch
echo "create fake-package+2.0.0.patch"
cp patches/slash+2.0.0.patch patches/fake-package+2.0.0.patch

echo "SNAPSHOT: patch-package fails to find fake-package"
if patch-package
Expand All @@ -24,7 +24,7 @@ fi
echo "END SNAPSHOT"

echo "rename fake-package patch file to .dev.patch"
mv patches/fake-package+3.0.0.patch patches/fake-package+3.0.0.dev.patch
mv patches/fake-package+2.0.0.patch patches/fake-package+2.0.0.dev.patch

echo "SNAPSHOT: fake-package should be skipped"
patch-package
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/dev-only-patches/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"rimraf": "3.0.0"
},
"devDependencies": {
"slash": "3.0.0"
"slash": "2.0.0"
}
}
11 changes: 11 additions & 0 deletions integration-tests/dev-only-patches/patches/slash+2.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
diff --git a/node_modules/slash/index.js b/node_modules/slash/index.js
index 75d7250..bc742e7 100644
--- a/node_modules/slash/index.js
+++ b/node_modules/slash/index.js
@@ -1,5 +1,5 @@
'use strict';
-module.exports = input => {
+module.patchPackage = input => {
const isExtendedLengthPath = /^\\\\\?\\/.test(input);
const hasNonAscii = /[^\u0000-\u0080]+/.test(input); // eslint-disable-line no-control-regex

11 changes: 0 additions & 11 deletions integration-tests/dev-only-patches/patches/slash+3.0.0.patch

This file was deleted.

12 changes: 6 additions & 6 deletions integration-tests/dev-only-patches/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ inherits@2:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

left-pad@^1.3.0:
[email protected]:
version "1.3.0"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==
Expand All @@ -74,17 +74,17 @@ path-is-absolute@^1.0.0:
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=

rimraf@^3.0.0:
[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
dependencies:
glob "^7.1.3"

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
slash@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==

wrappy@1:
version "1.0.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ echo "modify left-pad"
npx replace leftPad patchPackage node_modules/left-pad/index.js

echo "force patch-package to fail"
npx replace 'parsePatchFile\(' 'blarseBlatchBlile(' node_modules/@unts/patch-package/dist/makePatch.js
npx replace 'parsePatchFile\)\(' 'blarseBlatchBlile)(' node_modules/@unts/patch-package/dist/makePatch.js

echo "there is no error log file"
if ls ./patch-package-error.json.gz
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@
"@types/cross-spawn": "^6.0.0",
"@types/fs-extra": "^9.0.0",
"@types/jest": "^24.0.11",
"@types/json-stable-stringify": "^1.0.34",
"@types/klaw-sync": "^6.0.5",
"@types/minimist": "^1.2.2",
"@types/node": "^12.0.0",
"@types/randomstring": "^1.3.0",
"@types/rimraf": "^2.0.2",
"@types/semver": "^7.5.0",
"@types/tmp": "^0.0.34",
Expand All @@ -78,18 +79,18 @@
"typescript": "^4.2.2"
},
"dependencies": {
"@nolyfill/json-stable-stringify": "^1.0.30",
"@yarnpkg/lockfile": "^1.1.0",
"chalk": "^4.1.2",
"ci-info": "^3.7.0",
"cross-spawn": "^7.0.3",
"find-yarn-workspace-root": "^2.0.0",
"fs-extra": "^10.0.0",
"json-stable-stringify": "^1.0.2",
"klaw-sync": "^6.0.0",
"minimist": "^1.2.6",
"open": "^7.4.2",
"semver": "^7.5.3",
"slash": "^2.0.0",
"slash": "^3.0.0",
"tmp": "^0.0.33",
"yaml": "^2.2.2"
},
Expand Down
61 changes: 32 additions & 29 deletions src/applyPatches.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import chalk from "chalk"
import { writeFileSync } from "fs"
import { existsSync } from "fs-extra"
import colors from "chalk"
import { existsSync, writeFileSync } from "fs-extra"
import { posix } from "path"
import semver from "semver"
import { hashFile } from "./hash"
Expand Down Expand Up @@ -45,7 +44,7 @@ function getInstalledPackageVersion({
}

let err =
`${chalk.red("Error:")} Patch file found for package ${posix.basename(
`${colors.red("Error:")} Patch file found for package ${posix.basename(
pathSpecifier,
)}` + ` which is not present at ${relative(".", packageDir)}`

Expand All @@ -54,7 +53,7 @@ function getInstalledPackageVersion({

If this package is a dev dependency, rename the patch file to

${chalk.bold(patchFilename.replace(".patch", ".dev.patch"))}
${colors.bold(patchFilename.replace(".patch", ".dev.patch"))}
`
}
throw new PatchApplicationError(err)
Expand All @@ -65,7 +64,7 @@ function getInstalledPackageVersion({
const result = semver.valid(version)
if (result === null) {
throw new PatchApplicationError(
`${chalk.red(
`${colors.red(
"Error:",
)} Version string '${version}' cannot be parsed from ${join(
packageDir,
Expand All @@ -85,9 +84,9 @@ function logPatchApplication(patchDetails: PatchedPackageDetails) {
})`
: ""
console.log(
`${chalk.bold(patchDetails.pathSpecifier)}@${
`${colors.bold(patchDetails.pathSpecifier)}@${
patchDetails.version
}${sequenceString} ${chalk.green("✔")}`,
}${sequenceString} ${colors.green("✔")}`,
)
}

Expand All @@ -110,7 +109,7 @@ export function applyPatchesForApp({
const groupedPatches = getGroupedPatches(patchesDirectory)

if (groupedPatches.numPatchFiles === 0) {
console.log(chalk.blueBright("No patch files found"))
console.log(colors.blue("No patch files found"))
return
}

Expand Down Expand Up @@ -140,10 +139,10 @@ export function applyPatchesForApp({

const problemsSummary = []
if (warnings.length) {
problemsSummary.push(chalk.yellow(`${warnings.length} warning(s)`))
problemsSummary.push(colors.yellow(`${warnings.length} warning(s)`))
}
if (errors.length) {
problemsSummary.push(chalk.red(`${errors.length} error(s)`))
problemsSummary.push(colors.red(`${errors.length} error(s)`))
}

if (problemsSummary.length) {
Expand Down Expand Up @@ -201,8 +200,8 @@ export function applyPatchesForPackage({
appliedPatches.push(unappliedPatches.shift()!)
} else {
console.log(
chalk.red("Error:"),
`The patches for ${chalk.bold(pathSpecifier)} have changed.`,
colors.red("Error:"),
`The patches for ${colors.bold(pathSpecifier)} have changed.`,
`You should reinstall your node_modules folder to make sure the package is up to date`,
)
process.exit(1)
Expand Down Expand Up @@ -249,9 +248,9 @@ export function applyPatchesForPackage({
if (!installedPackageVersion) {
// it's ok we're in production mode and this is a dev only package
console.log(
`Skipping dev-only ${chalk.bold(
`Skipping dev-only ${colors.bold(
pathSpecifier,
)}@${version} ${chalk.blue("✔")}`,
)}@${version} ${colors.blue("✔")}`,
)
continue
}
Expand Down Expand Up @@ -429,7 +428,7 @@ export function applyPatch({
if (errors?.length) {
console.log(
"Saving errors to",
chalk.cyan.bold("./patch-package-errors.log"),
colors.cyan(colors.bold("./patch-package-errors.log")),
)
writeFileSync("patch-package-errors.log", errors.join("\n\n"))
process.exit(0)
Expand Down Expand Up @@ -464,18 +463,20 @@ function createVersionMismatchWarning({
path: string
}) {
return `
${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
${colors.yellow(
"Warning:",
)} patch-package detected a patch file version mismatch

Don't worry! This is probably fine. The patch was still applied
successfully. Here's the deets:

Patch file created for

${packageName}@${chalk.bold(originalVersion)}
${packageName}@${colors.bold(originalVersion)}

applied to

${packageName}@${chalk.bold(actualVersion)}
${packageName}@${colors.bold(actualVersion)}

At path

Expand All @@ -485,7 +486,7 @@ ${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
breakage even though the patch was applied successfully. Make sure the package
still behaves like you expect (you wrote tests, right?) and then run

${chalk.bold(`patch-package ${pathSpecifier}`)}
${colors.bold(`patch-package ${pathSpecifier}`)}

to update the version in the patch file name and make this warning go away.
`
Expand All @@ -503,8 +504,8 @@ function createBrokenPatchFileError({
pathSpecifier: string
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
${colors.red(colors.bold("**ERROR**"))} ${colors.red(
`Failed to apply patch for package ${colors.bold(packageName)} at path`,
)}

${path}
Expand Down Expand Up @@ -543,13 +544,15 @@ function createPatchApplicationFailureError({
pathSpecifier: string
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
${colors.red(colors.bold("**ERROR**"))} ${colors.red(
`Failed to apply patch for package ${colors.bold(packageName)} at path`,
)}

${path}

This error was caused because ${chalk.bold(packageName)} has changed since you
This error was caused because ${colors.bold(
packageName,
)} has changed since you
made the patch file for it. This introduced conflicts with your patch,
just like a merge conflict in Git when separate incompatible changes are
made to the same piece of code.
Expand All @@ -568,8 +571,8 @@ ${chalk.red.bold("**ERROR**")} ${chalk.red(

Info:
Patch file: patches/${patchFilename}
Patch was made for version: ${chalk.green.bold(originalVersion)}
Installed version: ${chalk.red.bold(actualVersion)}
Patch was made for version: ${colors.green(colors.bold(originalVersion))}
Installed version: ${colors.red(colors.bold(actualVersion))}
`
}

Expand All @@ -581,8 +584,8 @@ function createUnexpectedError({
error: Error
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch file ${chalk.bold(filename)}`,
${colors.red(colors.bold("**ERROR**"))} ${colors.red(
`Failed to apply patch file ${colors.bold(filename)}`,
)}

${error.stack}
Expand Down
4 changes: 2 additions & 2 deletions src/createIssue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from "chalk"
import colors from "chalk"
import open from "open"
import { stringify } from "querystring"
import { PackageManager } from "./detectPackageManager"
Expand Down Expand Up @@ -105,7 +105,7 @@ export function maybePrintIssueCreationPrompt(
packageManager: PackageManager,
) {
if (vcs) {
console.log(`💡 ${chalk.bold(packageDetails.name)} is on ${
console.log(`💡 ${colors.bold(packageDetails.name)} is on ${
vcs.provider
}! To draft an issue based on your patch run

Expand Down
Loading
Loading