From 82d3697e27d10853a59b840be351eb7521012e1f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 9 Feb 2023 09:52:33 -0500 Subject: [PATCH] fix package resolution --- index.js | 68 +++++++++-------------------------------------- package-lock.json | 13 +-------- package.json | 1 - 3 files changed, 13 insertions(+), 69 deletions(-) diff --git a/index.js b/index.js index 6e1b26a..a028921 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ const path = require('path'); const fs = require('fs'); -const relative = require('require-relative'); const { createFilter } = require('@rollup/pluginutils'); const { compile, preprocess } = require('svelte/compiler'); @@ -15,56 +14,6 @@ const plugin_options = new Set([ 'preprocess' ]); -const parse_pkg = function (dir) { - const pkg_file = path.join(dir, 'package.json'); - - try { - return JSON.parse(fs.readFileSync(pkg_file, 'utf-8')); - } catch (e) { - return false; - } -}; - -const get_dir = (file, importer) => - relative.resolve(file, path.dirname(importer)); - -const find_pkg = function (name, importer) { - let dir, pkg; - - const file = `${name}/package.json`; - - try { - const resolved = get_dir(file, importer); - dir = path.dirname(resolved); - pkg = require(resolved); - } catch (err) { - if (err.code === 'MODULE_NOT_FOUND') return { pkg: null, dir }; - if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') { - dir = path.dirname(get_dir(file, importer)); - - while (dir) { - pkg = parse_pkg(dir); - - if (pkg && pkg.name === name) { - return { pkg, dir }; - } - - const parent = path.dirname(dir); - if (parent === dir) { - break; - } - dir = parent; - } - - return { pkg: null, dir }; - } - - throw err; - } - - return { pkg, dir }; -}; - /** * @param [options] {Partial} * @returns {import('rollup').Plugin} @@ -120,11 +69,18 @@ module.exports = function (options = {}) { name += `/${parts.shift()}`; } - const { pkg, dir } = find_pkg(name, importer); - - // use pkg.svelte - if (parts.length === 0 && pkg && pkg.svelte) { - return path.resolve(dir, pkg.svelte); + if (parts.length > 0) return; + + let search_dir = importer; + while (search_dir !== (search_dir = path.dirname(search_dir))) { + const dir = path.join(search_dir, 'node_modules', name); + const file = path.join(dir, 'package.json'); + if (fs.existsSync(file)) { + const pkg = JSON.parse(fs.readFileSync(file, 'utf-8')); + if (pkg.svelte) { + return path.resolve(dir, pkg.svelte); + } + } } }, diff --git a/package-lock.json b/package-lock.json index 13c31ed..08778d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,7 @@ "version": "7.1.1", "license": "MIT", "dependencies": { - "@rollup/pluginutils": "^4.1.0", - "require-relative": "^0.8.7" + "@rollup/pluginutils": "^4.1.0" }, "devDependencies": { "eslint": "^7.14.0", @@ -1103,11 +1102,6 @@ "node": ">=0.10.0" } }, - "node_modules/require-relative": { - "version": "0.8.7", - "resolved": "https://registry.npmjs.org/require-relative/-/require-relative-0.8.7.tgz", - "integrity": "sha512-AKGr4qvHiryxRb19m3PsLRGuKVAbJLUD7E6eOaHkfKhwc+vSgVOCY5xNvm9EkolBKTOf0GrQAZKLimOCz81Khg==" - }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", @@ -2281,11 +2275,6 @@ "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", "dev": true }, - "require-relative": { - "version": "0.8.7", - "resolved": "https://registry.npmjs.org/require-relative/-/require-relative-0.8.7.tgz", - "integrity": "sha512-AKGr4qvHiryxRb19m3PsLRGuKVAbJLUD7E6eOaHkfKhwc+vSgVOCY5xNvm9EkolBKTOf0GrQAZKLimOCz81Khg==" - }, "resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", diff --git a/package.json b/package.json index a99ba77..ef44718 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "node": ">=10" }, "dependencies": { - "require-relative": "^0.8.7", "@rollup/pluginutils": "^4.1.0" }, "peerDependencies": {