Skip to content

Commit f22c114

Browse files
committed
minor #1303 Replace pkg-up by an inlined solution (Kocal)
This PR was squashed before being merged into the main branch. Discussion ---------- Replace `pkg-up` by an inlined solution [`pkg-up`](https://www.npmjs.com/package/pkg-up) is deprecated, and has been replaced by [`package-up`](https://www.npmjs.com/package/package-up). This one is more lightweight, but is only available as ESM format, which is not compatible with Encore. So, I've dropped `pkg-up` and inlined `package-up` and its single dependency [`find-up-simple`](https://www.npmjs.com/package/find-up-simple), with some tests. Removing `pkg-up` remove a lot of sub-dependencies aswell, which is better in term of security, but also in term of bandwidth and size on disk. Here, you can see the impact without `pkg-up` and with `package-up`, we reduce the bandwidth consumption by ~500 GB (but note that our inlined solution is smaller than `package-up`): ``` Package size report =================== Package info for "`@symfony`/[email protected]": 61 MB Released: 2024-01-25 17:18:00.95 +0000 UTC (30w1d ago) Downloads last week: 48,107 (32.02%) Estimated traffic last week: 2.9 TB Subdependencies: 634 Removed dependencies: - [email protected]: 36 kB (0.05%) Downloads last week: 6,317,196 (N/A% from 3.1.0) Downloads last week from "`@symfony`/[email protected]": 48,107 (N/A%) Traffic last week: N/A Traffic from "`@symfony`/[email protected]": 2.9 TB (N/A%) Subdependencies: 6 (0.94%) Added dependencies: + [email protected]: 13 kB (0.02%) Downloads last week: 31,071 (N/A% from 5.0.0) Estimated traffic last week: N/A Subdependencies: 1 (0.15%) Estimated new statistics: Package size: 61 MB → 51 MB (83.07%) Subdependencies: 634 → 486 (-148) Traffic with last week's downloads: For current version: 2.9 TB → 2.4 TB (498 GB saved) For all versions: 9.2 TB → 7.6 TB (1.6 TB saved) ``` Commits ------- d9cfde3 Replace `pkg-up` by an inlined solution
2 parents cc268db + d9cfde3 commit f22c114

File tree

6 files changed

+104
-38
lines changed

6 files changed

+104
-38
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ module.exports = {
5454

5555
* #1304 Replace `fast-levenshtein` by `fastest-levenshtein` (@Kocal)
5656

57+
* #1303 Replace `pkg-up` by an inlined solution (@Kocal)
58+
5759
## [v4.6.1](https://github.com/symfony/webpack-encore/releases/tag/v4.6.1)
5860

5961
* #1256 Re-adding node 18 support (@weaverryan)

lib/config/parse-runtime.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const RuntimeConfig = require('./RuntimeConfig');
13-
const pkgUp = require('pkg-up');
13+
const packageUp = require('../utils/package-up');
1414
const path = require('path');
1515
const babel = require('@babel/core');
1616

@@ -59,7 +59,7 @@ module.exports = function(argv, cwd) {
5959

6060
runtimeConfig.context = argv.context;
6161
if (typeof runtimeConfig.context === 'undefined') {
62-
const packagesPath = pkgUp.sync({ cwd });
62+
const packagesPath = packageUp({ cwd });
6363

6464
if (null === packagesPath) {
6565
throw new Error('Cannot determine webpack context. (Are you executing webpack from a directory outside of your project?). Try passing the --context option.');

lib/utils/package-up.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const fs = require('fs');
13+
const path = require('path');
14+
const { fileURLToPath } = require('url');
15+
16+
/**
17+
* Inlined version of the package "package-up" (ESM only).
18+
*
19+
* @param {string} cwd The directory to start searching from.
20+
* @returns {string|undefined} The path to the nearest package.json file or undefined if not found.
21+
*/
22+
module.exports = function({ cwd }) {
23+
return findUpSync('package.json', { cwd, type: 'file' });
24+
};
25+
26+
function toPath(urlOrPath) {
27+
return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
28+
}
29+
30+
/**
31+
* Inlined and simplified version of the package "find-up-simple" (ESM only).
32+
*
33+
* @param {string} name The name of the file to find
34+
* @param {Object} options
35+
* @param {string} options.cwd The directory to start searching from.
36+
* @returns {string|undefined} The path to the file found or undefined if not found.
37+
*/
38+
function findUpSync(name, { cwd = process.cwd() } = {}) {
39+
let directory = path.resolve(toPath(cwd) || '');
40+
const { root } = path.parse(directory);
41+
42+
while (directory && directory !== root) {
43+
const filePath = path.isAbsolute(name) ? name : path.join(directory, name);
44+
45+
try {
46+
const stats = fs.statSync(filePath, { throwIfNoEntry: false });
47+
if (stats && stats.isFile()) {
48+
return filePath;
49+
}
50+
} catch (e) {}
51+
52+
directory = path.dirname(directory);
53+
}
54+
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"css-minimizer-webpack-plugin": "^5.0.0",
3737
"fastest-levenshtein": "^1.0.16",
3838
"mini-css-extract-plugin": "^2.6.0",
39-
"pkg-up": "^3.1.0",
4039
"pretty-error": "^4.0.0",
4140
"resolve-url-loader": "^5.0.0",
4241
"semver": "^7.3.2",

test/utils/package-up.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const { resolve: resolvePath } = require('path');
13+
const expect = require('chai').expect;
14+
const packageUp = require('../../lib/utils/package-up');
15+
16+
describe('package-up', () => {
17+
const test = {
18+
'package.json from Encore': {
19+
cwd: __dirname,
20+
expectedPath: resolvePath(__dirname, '../../package.json'),
21+
},
22+
'package.json from a subdirectory': {
23+
cwd: resolvePath(__dirname, '../../fixtures/stimulus/mock-module'),
24+
expectedPath: resolvePath(__dirname, '../../fixtures/stimulus/mock-module/package.json'),
25+
},
26+
'package.json from Encore when no package.json exists in the current directory': {
27+
cwd: resolvePath(__dirname, '../../fixtures'),
28+
expectedPath: resolvePath(__dirname, '../../package.json'),
29+
},
30+
'package.json from Encore when no package.json exists in the current directory (subdirectory)': {
31+
cwd: resolvePath(__dirname, '../../fixtures/copy'),
32+
expectedPath: resolvePath(__dirname, '../../package.json'),
33+
},
34+
};
35+
36+
Object.entries(test).forEach(([description, { cwd, expectedPath }]) => {
37+
it(description, () => {
38+
expect(expectedPath).to.be.a('string');
39+
40+
const path = packageUp({ cwd });
41+
42+
expect(path).to.equal(expectedPath);
43+
});
44+
});
45+
});

yarn.lock

+1-35
Original file line numberDiff line numberDiff line change
@@ -3786,13 +3786,6 @@ [email protected], find-up@^5.0.0:
37863786
locate-path "^6.0.0"
37873787
path-exists "^4.0.0"
37883788

3789-
find-up@^3.0.0:
3790-
version "3.0.0"
3791-
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
3792-
integrity sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==
3793-
dependencies:
3794-
locate-path "^3.0.0"
3795-
37963789
find-up@^4.0.0:
37973790
version "4.1.0"
37983791
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
@@ -4925,14 +4918,6 @@ locate-character@^3.0.0:
49254918
resolved "https://registry.yarnpkg.com/locate-character/-/locate-character-3.0.0.tgz#0305c5b8744f61028ef5d01f444009e00779f974"
49264919
integrity sha512-SW13ws7BjaeJ6p7Q6CO2nchbYEc3X3J6WrmTTDto7yMPqVSZTUyY5Tjbid+Ab8gLnATtygYtiDIJGQRRn2ZOiA==
49274920

4928-
locate-path@^3.0.0:
4929-
version "3.0.0"
4930-
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
4931-
integrity sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==
4932-
dependencies:
4933-
p-locate "^3.0.0"
4934-
path-exists "^3.0.0"
4935-
49364921
locate-path@^5.0.0:
49374922
version "5.0.0"
49384923
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
@@ -5444,7 +5429,7 @@ optionator@^0.9.3:
54445429
prelude-ls "^1.2.1"
54455430
type-check "^0.4.0"
54465431

5447-
p-limit@^2.0.0, p-limit@^2.2.0:
5432+
p-limit@^2.2.0:
54485433
version "2.3.0"
54495434
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1"
54505435
integrity sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==
@@ -5465,13 +5450,6 @@ p-limit@^4.0.0:
54655450
dependencies:
54665451
yocto-queue "^1.0.0"
54675452

5468-
p-locate@^3.0.0:
5469-
version "3.0.0"
5470-
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4"
5471-
integrity sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==
5472-
dependencies:
5473-
p-limit "^2.0.0"
5474-
54755453
p-locate@^4.1.0:
54765454
version "4.1.0"
54775455
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
@@ -5543,11 +5521,6 @@ parseurl@~1.3.2, parseurl@~1.3.3:
55435521
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
55445522
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==
55455523

5546-
path-exists@^3.0.0:
5547-
version "3.0.0"
5548-
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"
5549-
integrity sha512-bpC7GYwiDYQ4wYLe+FA8lhRjhQCMcQGuSgGGqDkg/QerRWw9CmGRT0iSOVRSZJ29NMLZgIzqaljJ63oaL4NIJQ==
5550-
55515524
path-exists@^4.0.0:
55525525
version "4.0.0"
55535526
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
@@ -5663,13 +5636,6 @@ pkg-dir@^7.0.0:
56635636
dependencies:
56645637
find-up "^6.3.0"
56655638

5666-
pkg-up@^3.1.0:
5667-
version "3.1.0"
5668-
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-3.1.0.tgz#100ec235cc150e4fd42519412596a28512a0def5"
5669-
integrity sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==
5670-
dependencies:
5671-
find-up "^3.0.0"
5672-
56735639
pn@^1.1.0:
56745640
version "1.1.0"
56755641
resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"

0 commit comments

Comments
 (0)