Skip to content

Commit b2eecd0

Browse files
committed
Replace pkg-up by an inlined solution
1 parent 24eb007 commit b2eecd0

File tree

5 files changed

+100
-38
lines changed

5 files changed

+100
-38
lines changed

lib/config/parse-runtime.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <fabien@symfony.com>
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), which depends on "find-up-simple" (ESM only too).
18+
*/
19+
module.exports = function({ cwd }) {
20+
return findUpSync('package.json', {cwd, type: 'file'});
21+
}
22+
23+
24+
function toPath(urlOrPath) {
25+
return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
26+
}
27+
28+
/**
29+
* Inlined version of the package "find-up-simple" (ESM only).
30+
*/
31+
function findUpSync(name, {
32+
cwd = process.cwd(),
33+
type = 'file',
34+
stopAt,
35+
} = {}) {
36+
let directory = path.resolve(toPath(cwd) ?? '');
37+
const {root} = path.parse(directory);
38+
stopAt = path.resolve(directory, toPath(stopAt) ?? root);
39+
40+
while (directory && directory !== stopAt && directory !== root) {
41+
const filePath = path.isAbsolute(name) ? name : path.join(directory, name);
42+
43+
try {
44+
const stats = fs.statSync(filePath, {throwIfNoEntry: false});
45+
if ((type === 'file' && stats?.isFile()) || (type === 'directory' && stats?.isDirectory())) {
46+
return filePath;
47+
}
48+
} catch {}
49+
50+
directory = path.dirname(directory);
51+
}
52+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"css-minimizer-webpack-plugin": "^5.0.0",
3737
"fast-levenshtein": "^3.0.0",
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

Lines changed: 45 additions & 0 deletions
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 <fabien@symfony.com>
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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,13 +3793,6 @@ find-up@5.0.0, find-up@^5.0.0:
37933793
locate-path "^6.0.0"
37943794
path-exists "^4.0.0"
37953795

3796-
find-up@^3.0.0:
3797-
version "3.0.0"
3798-
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
3799-
integrity sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==
3800-
dependencies:
3801-
locate-path "^3.0.0"
3802-
38033796
find-up@^4.0.0:
38043797
version "4.1.0"
38053798
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
@@ -4932,14 +4925,6 @@ locate-character@^3.0.0:
49324925
resolved "https://registry.yarnpkg.com/locate-character/-/locate-character-3.0.0.tgz#0305c5b8744f61028ef5d01f444009e00779f974"
49334926
integrity sha512-SW13ws7BjaeJ6p7Q6CO2nchbYEc3X3J6WrmTTDto7yMPqVSZTUyY5Tjbid+Ab8gLnATtygYtiDIJGQRRn2ZOiA==
49344927

4935-
locate-path@^3.0.0:
4936-
version "3.0.0"
4937-
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
4938-
integrity sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==
4939-
dependencies:
4940-
p-locate "^3.0.0"
4941-
path-exists "^3.0.0"
4942-
49434928
locate-path@^5.0.0:
49444929
version "5.0.0"
49454930
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
@@ -5451,7 +5436,7 @@ optionator@^0.9.3:
54515436
prelude-ls "^1.2.1"
54525437
type-check "^0.4.0"
54535438

5454-
p-limit@^2.0.0, p-limit@^2.2.0:
5439+
p-limit@^2.2.0:
54555440
version "2.3.0"
54565441
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1"
54575442
integrity sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==
@@ -5472,13 +5457,6 @@ p-limit@^4.0.0:
54725457
dependencies:
54735458
yocto-queue "^1.0.0"
54745459

5475-
p-locate@^3.0.0:
5476-
version "3.0.0"
5477-
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4"
5478-
integrity sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==
5479-
dependencies:
5480-
p-limit "^2.0.0"
5481-
54825460
p-locate@^4.1.0:
54835461
version "4.1.0"
54845462
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
@@ -5550,11 +5528,6 @@ parseurl@~1.3.2, parseurl@~1.3.3:
55505528
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
55515529
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==
55525530

5553-
path-exists@^3.0.0:
5554-
version "3.0.0"
5555-
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"
5556-
integrity sha512-bpC7GYwiDYQ4wYLe+FA8lhRjhQCMcQGuSgGGqDkg/QerRWw9CmGRT0iSOVRSZJ29NMLZgIzqaljJ63oaL4NIJQ==
5557-
55585531
path-exists@^4.0.0:
55595532
version "4.0.0"
55605533
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
@@ -5670,13 +5643,6 @@ pkg-dir@^7.0.0:
56705643
dependencies:
56715644
find-up "^6.3.0"
56725645

5673-
pkg-up@^3.1.0:
5674-
version "3.1.0"
5675-
resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-3.1.0.tgz#100ec235cc150e4fd42519412596a28512a0def5"
5676-
integrity sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==
5677-
dependencies:
5678-
find-up "^3.0.0"
5679-
56805646
pn@^1.1.0:
56815647
version "1.1.0"
56825648
resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"

0 commit comments

Comments
 (0)