diff --git a/CHANGELOG.md b/CHANGELOG.md index 541510f5a..e06fc6528 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,8 @@ module.exports = { * #1304 Replace `fast-levenshtein` by `fastest-levenshtein` (@Kocal) +* #1303 Replace `pkg-up` by an inlined solution (@Kocal) + ## [v4.6.1](https://github.com/symfony/webpack-encore/releases/tag/v4.6.1) * #1256 Re-adding node 18 support (@weaverryan) diff --git a/lib/config/parse-runtime.js b/lib/config/parse-runtime.js index 689b5f586..afa58cacc 100644 --- a/lib/config/parse-runtime.js +++ b/lib/config/parse-runtime.js @@ -10,7 +10,7 @@ 'use strict'; const RuntimeConfig = require('./RuntimeConfig'); -const pkgUp = require('pkg-up'); +const packageUp = require('../utils/package-up'); const path = require('path'); const babel = require('@babel/core'); @@ -59,7 +59,7 @@ module.exports = function(argv, cwd) { runtimeConfig.context = argv.context; if (typeof runtimeConfig.context === 'undefined') { - const packagesPath = pkgUp.sync({ cwd }); + const packagesPath = packageUp({ cwd }); if (null === packagesPath) { throw new Error('Cannot determine webpack context. (Are you executing webpack from a directory outside of your project?). Try passing the --context option.'); diff --git a/lib/utils/package-up.js b/lib/utils/package-up.js new file mode 100644 index 000000000..b641efb53 --- /dev/null +++ b/lib/utils/package-up.js @@ -0,0 +1,54 @@ +/* + * This file is part of the Symfony Webpack Encore package. + * + * (c) Fabien Potencier + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const { fileURLToPath } = require('url'); + +/** + * Inlined version of the package "package-up" (ESM only). + * + * @param {string} cwd The directory to start searching from. + * @returns {string|undefined} The path to the nearest package.json file or undefined if not found. + */ +module.exports = function({ cwd }) { + return findUpSync('package.json', { cwd, type: 'file' }); +}; + +function toPath(urlOrPath) { + return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; +} + +/** + * Inlined and simplified version of the package "find-up-simple" (ESM only). + * + * @param {string} name The name of the file to find + * @param {Object} options + * @param {string} options.cwd The directory to start searching from. + * @returns {string|undefined} The path to the file found or undefined if not found. + */ +function findUpSync(name, { cwd = process.cwd() } = {}) { + let directory = path.resolve(toPath(cwd) || ''); + const { root } = path.parse(directory); + + while (directory && directory !== root) { + const filePath = path.isAbsolute(name) ? name : path.join(directory, name); + + try { + const stats = fs.statSync(filePath, { throwIfNoEntry: false }); + if (stats && stats.isFile()) { + return filePath; + } + } catch (e) {} + + directory = path.dirname(directory); + } +} diff --git a/package.json b/package.json index abbc9c71c..a9951b5ab 100755 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ "css-minimizer-webpack-plugin": "^5.0.0", "fastest-levenshtein": "^1.0.16", "mini-css-extract-plugin": "^2.6.0", - "pkg-up": "^3.1.0", "pretty-error": "^4.0.0", "resolve-url-loader": "^5.0.0", "semver": "^7.3.2", diff --git a/test/utils/package-up.js b/test/utils/package-up.js new file mode 100644 index 000000000..06d96a84c --- /dev/null +++ b/test/utils/package-up.js @@ -0,0 +1,45 @@ +/* + * This file is part of the Symfony Webpack Encore package. + * + * (c) Fabien Potencier + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +'use strict'; + +const { resolve: resolvePath } = require('path'); +const expect = require('chai').expect; +const packageUp = require('../../lib/utils/package-up'); + +describe('package-up', () => { + const test = { + 'package.json from Encore': { + cwd: __dirname, + expectedPath: resolvePath(__dirname, '../../package.json'), + }, + 'package.json from a subdirectory': { + cwd: resolvePath(__dirname, '../../fixtures/stimulus/mock-module'), + expectedPath: resolvePath(__dirname, '../../fixtures/stimulus/mock-module/package.json'), + }, + 'package.json from Encore when no package.json exists in the current directory': { + cwd: resolvePath(__dirname, '../../fixtures'), + expectedPath: resolvePath(__dirname, '../../package.json'), + }, + 'package.json from Encore when no package.json exists in the current directory (subdirectory)': { + cwd: resolvePath(__dirname, '../../fixtures/copy'), + expectedPath: resolvePath(__dirname, '../../package.json'), + }, + }; + + Object.entries(test).forEach(([description, { cwd, expectedPath }]) => { + it(description, () => { + expect(expectedPath).to.be.a('string'); + + const path = packageUp({ cwd }); + + expect(path).to.equal(expectedPath); + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index 1aef26f2a..0d3da9f28 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3786,13 +3786,6 @@ find-up@5.0.0, find-up@^5.0.0: locate-path "^6.0.0" path-exists "^4.0.0" -find-up@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73" - integrity sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg== - dependencies: - locate-path "^3.0.0" - find-up@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19" @@ -4925,14 +4918,6 @@ locate-character@^3.0.0: resolved "https://registry.yarnpkg.com/locate-character/-/locate-character-3.0.0.tgz#0305c5b8744f61028ef5d01f444009e00779f974" integrity sha512-SW13ws7BjaeJ6p7Q6CO2nchbYEc3X3J6WrmTTDto7yMPqVSZTUyY5Tjbid+Ab8gLnATtygYtiDIJGQRRn2ZOiA== -locate-path@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e" - integrity sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A== - dependencies: - p-locate "^3.0.0" - path-exists "^3.0.0" - locate-path@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0" @@ -5444,7 +5429,7 @@ optionator@^0.9.3: prelude-ls "^1.2.1" type-check "^0.4.0" -p-limit@^2.0.0, p-limit@^2.2.0: +p-limit@^2.2.0: version "2.3.0" resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1" integrity sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w== @@ -5465,13 +5450,6 @@ p-limit@^4.0.0: dependencies: yocto-queue "^1.0.0" -p-locate@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4" - integrity sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ== - dependencies: - p-limit "^2.0.0" - p-locate@^4.1.0: version "4.1.0" 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: resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4" integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ== -path-exists@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515" - integrity sha512-bpC7GYwiDYQ4wYLe+FA8lhRjhQCMcQGuSgGGqDkg/QerRWw9CmGRT0iSOVRSZJ29NMLZgIzqaljJ63oaL4NIJQ== - path-exists@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3" @@ -5663,13 +5636,6 @@ pkg-dir@^7.0.0: dependencies: find-up "^6.3.0" -pkg-up@^3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-3.1.0.tgz#100ec235cc150e4fd42519412596a28512a0def5" - integrity sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA== - dependencies: - find-up "^3.0.0" - pn@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"