Skip to content

fix: replace relative-require with our own resolveDependencyData #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"svelte": "^3.46.2",
"ts-jest": "^27.1.3",
"typescript": "^4.5.4",
"vite": "^2.7.12"
"vite": "^2.7.13"
},
"lint-staged": {
"*.{js,ts,svelte,html,md,svx}": "eslint --fix",
Expand All @@ -74,7 +74,7 @@
"pnpm": {
"overrides": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"vite": "^2.7.10",
"vite": "^2.7.13",
"ansi-regex@>2.1.1 <5.0.1": "^5.0.1"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/autoprefixer-browerslist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"postcss-load-config": "^3.1.1",
"svelte": "^3.46.2",
"svelte-preprocess": "^4.10.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/configfile-custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/configfile-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"svelte-preprocess": "^4.10.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/custom-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/hmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"@sveltejs/vite-plugin-svelte": "workspace:*",
"node-fetch": "^2.6.6",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/package-json-svelte-field/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/preprocess-with-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"sass": "^1.48.0",
"stylus": "^0.56.0",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/svelte-preprocess/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"svelte": "^3.46.2",
"svelte-preprocess": "^4.10.2",
"typescript": "^4.5.4",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/ts-type-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"@tsconfig/svelte": "^3.0.0",
"@types/node": "^17.0.7",
"svelte-preprocess": "^4.10.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/vite-ssr-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"npm-run-all": "^4.1.5",
"serve-static": "^1.14.2",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/vite-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"express": "^4.17.2",
"serve-static": "^1.14.2",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/playground/big-component-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"@sveltejs/vite-plugin-svelte": "workspace:*",
"carbon-components-svelte": "^0.52.0",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/playground/big/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/playground/optimizedeps-include/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.46.2",
"tinro": "^0.6.10",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
2 changes: 1 addition & 1 deletion packages/playground/windicss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@sveltejs/vite-plugin-svelte": "workspace:*",
"diff-match-patch": "^1.0.5",
"svelte": "^3.46.2",
"vite": "^2.7.12",
"vite": "^2.7.13",
"vite-plugin-windicss": "^1.6.3"
}
}
3 changes: 1 addition & 2 deletions packages/vite-plugin-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"debug": "^4.3.3",
"kleur": "^4.1.4",
"magic-string": "^0.25.7",
"require-relative": "^0.8.7",
"svelte-hmr": "^0.14.9"
},
"peerDependencies": {
Expand All @@ -70,6 +69,6 @@
"rollup": "^2.64.0",
"svelte": "^3.46.2",
"tsup": "^5.11.11",
"vite": "^2.7.12"
"vite": "^2.7.13"
}
}
19 changes: 6 additions & 13 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
}
validateInlineOptions(inlineOptions);
const cache = new VitePluginSvelteCache();
const pkg_export_errors = new Set();
const pkg_resolve_errors = new Set();
// updated in configResolved hook
let requestParser: IdParser;
let options: ResolvedOptions;
Expand Down Expand Up @@ -143,15 +143,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
return resolved;
}
} catch (err) {
switch (err.code) {
case 'ERR_PACKAGE_PATH_NOT_EXPORTED':
pkg_export_errors.add(importee);
return null;
case 'MODULE_NOT_FOUND':
return null;
default:
throw err;
}
pkg_resolve_errors.add(importee);
}
},

Expand Down Expand Up @@ -206,10 +198,11 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
*/
// TODO generateBundle isn't called by vite, is buildEnd enough or should it be logged once per violation in resolve
buildEnd() {
if (pkg_export_errors.size > 0) {
if (pkg_resolve_errors.size > 0) {
log.warn(
`The following packages did not export their \`package.json\` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.`,
Array.from(pkg_export_errors, (s) => `- ${s}`).join('\n')
`vite-plugin-svelte was unable to find package.json of the following packages and wasn't able to resolve via their "svelte" field.
If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join('\n')}`.replace(/\t/g, '')
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/vite-plugin-svelte/src/utils/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ function getSvelteDependencies(
return result;
}

function resolveDependencyData(dep: string, localRequire: NodeRequire): DependencyData | void {
export function resolveDependencyData(
dep: string,
localRequire: NodeRequire
): DependencyData | void {
try {
const pkgJson = `${dep}/package.json`;
const pkg = localRequire(pkgJson);
Expand Down Expand Up @@ -166,7 +169,7 @@ const COMMON_PREFIXES_WITHOUT_SVELTE_FIELD = [
* @param dependency {string}
* @returns {boolean} true if it is a dependency without a svelte field
*/
function is_common_without_svelte_field(dependency: string): boolean {
export function is_common_without_svelte_field(dependency: string): boolean {
return (
COMMON_DEPENDENCIES_WITHOUT_SVELTE_FIELD.includes(dependency) ||
COMMON_PREFIXES_WITHOUT_SVELTE_FIELD.some(
Expand Down
20 changes: 12 additions & 8 deletions packages/vite-plugin-svelte/src/utils/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import path from 'path';
import fs from 'fs';
// @ts-ignore
import relative from 'require-relative';
import { createRequire } from 'module';
import { is_common_without_svelte_field, resolveDependencyData } from './dependencies';

export function resolveViaPackageJsonSvelte(importee: string, importer?: string): string | void {
if (importer && isBareImport(importee)) {
const importeePkgFile = relative.resolve(`${importee}/package.json`, path.dirname(importer));
const importeePkg = JSON.parse(fs.readFileSync(importeePkgFile, { encoding: 'utf-8' }));
if (importeePkg.svelte) {
return path.resolve(path.dirname(importeePkgFile), importeePkg.svelte);
if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
const localRequire = createRequire(importer);
const pkgData = resolveDependencyData(importee, localRequire);
if (pkgData) {
const { pkg, dir } = pkgData;
if (pkg.svelte) {
return path.resolve(dir, pkg.svelte);
}
} else {
throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
}
}
}
Expand Down
Loading