Skip to content

Commit 71fcada

Browse files
SukkaWJounQin
andauthored
feat: reduce installation size (#1)
Co-authored-by: JounQin <[email protected]>
1 parent 31e6f7e commit 71fcada

28 files changed

+2482
-2174
lines changed

Diff for: .changeset/slow-llamas-fetch.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@unts/patch-package": minor
3+
---
4+
5+
feat: reduce installation size by replacing `json-stable-stringify` to
6+
`@nolyfill/json-stable-stringify`

Diff for: integration-tests/dev-only-patches/__snapshots__/dev-only-patches.test.ts.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`Test dev-only-patches: 00: patch-package happily ignores slash on CI be
55
patch-package 0.0.0
66
Applying patches...
77
8-
Skipping dev-only slash@3.0.0 ✔
8+
Skipping dev-only slash@2.0.0 ✔
99
END SNAPSHOT"
1010
`;
1111

@@ -14,12 +14,12 @@ exports[`Test dev-only-patches: 01: patch-package fails to find fake-package 1`]
1414
patch-package 0.0.0
1515
Applying patches...
1616
17-
Skipping dev-only slash@3.0.0 ✔
17+
Skipping dev-only slash@2.0.0 ✔
1818
Error: Patch file found for package fake-package which is not present at node_modules/fake-package
1919
2020
If this package is a dev dependency, rename the patch file to
2121
22-
fake-package+3.0.0.dev.patch
22+
fake-package+2.0.0.dev.patch
2323
2424
---
2525
patch-package finished with 1 error(s).
@@ -30,8 +30,8 @@ exports[`Test dev-only-patches: 02: fake-package should be skipped 1`] = `
3030
"SNAPSHOT: fake-package should be skipped
3131
patch-package 0.0.0
3232
Applying patches...
33-
Skipping dev-only fake-package@3.0.0 ✔
33+
Skipping dev-only fake-package@2.0.0 ✔
3434
35-
Skipping dev-only slash@3.0.0 ✔
35+
Skipping dev-only slash@2.0.0 ✔
3636
END SNAPSHOT"
3737
`;

Diff for: integration-tests/dev-only-patches/dev-only-patches.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ echo "SNAPSHOT: patch-package happily ignores slash on CI because it's a dev dep
1313
patch-package
1414
echo "END SNAPSHOT"
1515

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

1919
echo "SNAPSHOT: patch-package fails to find fake-package"
2020
if patch-package
@@ -24,7 +24,7 @@ fi
2424
echo "END SNAPSHOT"
2525

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

2929
echo "SNAPSHOT: fake-package should be skipped"
3030
patch-package

Diff for: integration-tests/dev-only-patches/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"rimraf": "3.0.0"
1111
},
1212
"devDependencies": {
13-
"slash": "3.0.0"
13+
"slash": "2.0.0"
1414
}
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
diff --git a/node_modules/slash/index.js b/node_modules/slash/index.js
2+
index 75d7250..bc742e7 100644
3+
--- a/node_modules/slash/index.js
4+
+++ b/node_modules/slash/index.js
5+
@@ -1,5 +1,5 @@
6+
'use strict';
7+
-module.exports = input => {
8+
+module.patchPackage = input => {
9+
const isExtendedLengthPath = /^\\\\\?\\/.test(input);
10+
const hasNonAscii = /[^\u0000-\u0080]+/.test(input); // eslint-disable-line no-control-regex
11+

Diff for: integration-tests/dev-only-patches/patches/slash+3.0.0.patch

-11
This file was deleted.

Diff for: integration-tests/dev-only-patches/yarn.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inherits@2:
5050
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
5151
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
5252

53-
left-pad@^1.3.0:
53+
5454
version "1.3.0"
5555
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
5656
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==
@@ -74,17 +74,17 @@ path-is-absolute@^1.0.0:
7474
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
7575
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
7676

77-
rimraf@^3.0.0:
77+
7878
version "3.0.0"
7979
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
8080
integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
8181
dependencies:
8282
glob "^7.1.3"
8383

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

8989
wrappy@1:
9090
version "1.0.2"

Diff for: integration-tests/unexpected-patch-creation-failure/unexpected-patch-creation-failure.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ echo "modify left-pad"
99
npx replace leftPad patchPackage node_modules/left-pad/index.js
1010

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

1414
echo "there is no error log file"
1515
if ls ./patch-package-error.json.gz

Diff for: package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@
5959
"@types/cross-spawn": "^6.0.0",
6060
"@types/fs-extra": "^9.0.0",
6161
"@types/jest": "^24.0.11",
62-
"@types/json-stable-stringify": "^1.0.34",
62+
"@types/klaw-sync": "^6.0.5",
6363
"@types/minimist": "^1.2.2",
6464
"@types/node": "^12.0.0",
65+
"@types/randomstring": "^1.3.0",
6566
"@types/rimraf": "^2.0.2",
6667
"@types/semver": "^7.5.0",
6768
"@types/tmp": "^0.0.34",
@@ -78,18 +79,18 @@
7879
"typescript": "^4.2.2"
7980
},
8081
"dependencies": {
82+
"@nolyfill/json-stable-stringify": "^1.0.30",
8183
"@yarnpkg/lockfile": "^1.1.0",
8284
"chalk": "^4.1.2",
8385
"ci-info": "^3.7.0",
8486
"cross-spawn": "^7.0.3",
8587
"find-yarn-workspace-root": "^2.0.0",
8688
"fs-extra": "^10.0.0",
87-
"json-stable-stringify": "^1.0.2",
8889
"klaw-sync": "^6.0.0",
8990
"minimist": "^1.2.6",
9091
"open": "^7.4.2",
9192
"semver": "^7.5.3",
92-
"slash": "^2.0.0",
93+
"slash": "^3.0.0",
9394
"tmp": "^0.0.33",
9495
"yaml": "^2.2.2"
9596
},

Diff for: src/applyPatches.ts

+32-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import chalk from "chalk"
2-
import { writeFileSync } from "fs"
3-
import { existsSync } from "fs-extra"
1+
import colors from "chalk"
2+
import { existsSync, writeFileSync } from "fs-extra"
43
import { posix } from "path"
54
import semver from "semver"
65
import { hashFile } from "./hash"
@@ -45,7 +44,7 @@ function getInstalledPackageVersion({
4544
}
4645

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

@@ -54,7 +53,7 @@ function getInstalledPackageVersion({
5453
5554
If this package is a dev dependency, rename the patch file to
5655
57-
${chalk.bold(patchFilename.replace(".patch", ".dev.patch"))}
56+
${colors.bold(patchFilename.replace(".patch", ".dev.patch"))}
5857
`
5958
}
6059
throw new PatchApplicationError(err)
@@ -65,7 +64,7 @@ function getInstalledPackageVersion({
6564
const result = semver.valid(version)
6665
if (result === null) {
6766
throw new PatchApplicationError(
68-
`${chalk.red(
67+
`${colors.red(
6968
"Error:",
7069
)} Version string '${version}' cannot be parsed from ${join(
7170
packageDir,
@@ -85,9 +84,9 @@ function logPatchApplication(patchDetails: PatchedPackageDetails) {
8584
})`
8685
: ""
8786
console.log(
88-
`${chalk.bold(patchDetails.pathSpecifier)}@${
87+
`${colors.bold(patchDetails.pathSpecifier)}@${
8988
patchDetails.version
90-
}${sequenceString} ${chalk.green("✔")}`,
89+
}${sequenceString} ${colors.green("✔")}`,
9190
)
9291
}
9392

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

112111
if (groupedPatches.numPatchFiles === 0) {
113-
console.log(chalk.blueBright("No patch files found"))
112+
console.log(colors.blue("No patch files found"))
114113
return
115114
}
116115

@@ -140,10 +139,10 @@ export function applyPatchesForApp({
140139

141140
const problemsSummary = []
142141
if (warnings.length) {
143-
problemsSummary.push(chalk.yellow(`${warnings.length} warning(s)`))
142+
problemsSummary.push(colors.yellow(`${warnings.length} warning(s)`))
144143
}
145144
if (errors.length) {
146-
problemsSummary.push(chalk.red(`${errors.length} error(s)`))
145+
problemsSummary.push(colors.red(`${errors.length} error(s)`))
147146
}
148147

149148
if (problemsSummary.length) {
@@ -201,8 +200,8 @@ export function applyPatchesForPackage({
201200
appliedPatches.push(unappliedPatches.shift()!)
202201
} else {
203202
console.log(
204-
chalk.red("Error:"),
205-
`The patches for ${chalk.bold(pathSpecifier)} have changed.`,
203+
colors.red("Error:"),
204+
`The patches for ${colors.bold(pathSpecifier)} have changed.`,
206205
`You should reinstall your node_modules folder to make sure the package is up to date`,
207206
)
208207
process.exit(1)
@@ -249,9 +248,9 @@ export function applyPatchesForPackage({
249248
if (!installedPackageVersion) {
250249
// it's ok we're in production mode and this is a dev only package
251250
console.log(
252-
`Skipping dev-only ${chalk.bold(
251+
`Skipping dev-only ${colors.bold(
253252
pathSpecifier,
254-
)}@${version} ${chalk.blue("✔")}`,
253+
)}@${version} ${colors.blue("✔")}`,
255254
)
256255
continue
257256
}
@@ -429,7 +428,7 @@ export function applyPatch({
429428
if (errors?.length) {
430429
console.log(
431430
"Saving errors to",
432-
chalk.cyan.bold("./patch-package-errors.log"),
431+
colors.cyan(colors.bold("./patch-package-errors.log")),
433432
)
434433
writeFileSync("patch-package-errors.log", errors.join("\n\n"))
435434
process.exit(0)
@@ -464,18 +463,20 @@ function createVersionMismatchWarning({
464463
path: string
465464
}) {
466465
return `
467-
${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
466+
${colors.yellow(
467+
"Warning:",
468+
)} patch-package detected a patch file version mismatch
468469
469470
Don't worry! This is probably fine. The patch was still applied
470471
successfully. Here's the deets:
471472
472473
Patch file created for
473474
474-
${packageName}@${chalk.bold(originalVersion)}
475+
${packageName}@${colors.bold(originalVersion)}
475476
476477
applied to
477478
478-
${packageName}@${chalk.bold(actualVersion)}
479+
${packageName}@${colors.bold(actualVersion)}
479480
480481
At path
481482
@@ -485,7 +486,7 @@ ${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
485486
breakage even though the patch was applied successfully. Make sure the package
486487
still behaves like you expect (you wrote tests, right?) and then run
487488
488-
${chalk.bold(`patch-package ${pathSpecifier}`)}
489+
${colors.bold(`patch-package ${pathSpecifier}`)}
489490
490491
to update the version in the patch file name and make this warning go away.
491492
`
@@ -503,8 +504,8 @@ function createBrokenPatchFileError({
503504
pathSpecifier: string
504505
}) {
505506
return `
506-
${chalk.red.bold("**ERROR**")} ${chalk.red(
507-
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
507+
${colors.red(colors.bold("**ERROR**"))} ${colors.red(
508+
`Failed to apply patch for package ${colors.bold(packageName)} at path`,
508509
)}
509510
510511
${path}
@@ -543,13 +544,15 @@ function createPatchApplicationFailureError({
543544
pathSpecifier: string
544545
}) {
545546
return `
546-
${chalk.red.bold("**ERROR**")} ${chalk.red(
547-
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
547+
${colors.red(colors.bold("**ERROR**"))} ${colors.red(
548+
`Failed to apply patch for package ${colors.bold(packageName)} at path`,
548549
)}
549550
550551
${path}
551552
552-
This error was caused because ${chalk.bold(packageName)} has changed since you
553+
This error was caused because ${colors.bold(
554+
packageName,
555+
)} has changed since you
553556
made the patch file for it. This introduced conflicts with your patch,
554557
just like a merge conflict in Git when separate incompatible changes are
555558
made to the same piece of code.
@@ -568,8 +571,8 @@ ${chalk.red.bold("**ERROR**")} ${chalk.red(
568571
569572
Info:
570573
Patch file: patches/${patchFilename}
571-
Patch was made for version: ${chalk.green.bold(originalVersion)}
572-
Installed version: ${chalk.red.bold(actualVersion)}
574+
Patch was made for version: ${colors.green(colors.bold(originalVersion))}
575+
Installed version: ${colors.red(colors.bold(actualVersion))}
573576
`
574577
}
575578

@@ -581,8 +584,8 @@ function createUnexpectedError({
581584
error: Error
582585
}) {
583586
return `
584-
${chalk.red.bold("**ERROR**")} ${chalk.red(
585-
`Failed to apply patch file ${chalk.bold(filename)}`,
587+
${colors.red(colors.bold("**ERROR**"))} ${colors.red(
588+
`Failed to apply patch file ${colors.bold(filename)}`,
586589
)}
587590
588591
${error.stack}

Diff for: src/createIssue.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chalk from "chalk"
1+
import colors from "chalk"
22
import open from "open"
33
import { stringify } from "querystring"
44
import { PackageManager } from "./detectPackageManager"
@@ -105,7 +105,7 @@ export function maybePrintIssueCreationPrompt(
105105
packageManager: PackageManager,
106106
) {
107107
if (vcs) {
108-
console.log(`💡 ${chalk.bold(packageDetails.name)} is on ${
108+
console.log(`💡 ${colors.bold(packageDetails.name)} is on ${
109109
vcs.provider
110110
}! To draft an issue based on your patch run
111111

0 commit comments

Comments
 (0)