Skip to content

Commit 1901b42

Browse files
authored
fix: replace relative-require with our own resolveDependencyData (#254)
* fix: replace relative-require with our own resolveDependencyData to avoid errors when package.json isn't exported * feat: implement simple cache for resolved svelte fields * ci: use windows-2019 label to see if windows fail is caused by new github default * ci: remove esbuild install calls, relax timeouts for ci and windows, add logging to globalSetup * chore: add changeset
1 parent a31f6c6 commit 1901b42

File tree

28 files changed

+239
-192
lines changed

28 files changed

+239
-192
lines changed

.changeset/five-houses-serve.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
do not use require-relative to resolve svelte field of libraries and cache resolved values (fixes #244)

.github/workflows/ci.yml

+3-7
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ jobs:
3434
cache: 'pnpm'
3535
cache-dependency-path: '**/pnpm-lock.yaml'
3636
- name: install
37-
run: |
38-
pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
39-
node node_modules/esbuild/install.js
37+
run: pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
4038
- name: build
4139
id: build
4240
run: pnpm run build
@@ -58,7 +56,7 @@ jobs:
5856
fail-fast: false
5957
matrix:
6058
node: [16]
61-
os: [ubuntu-latest, macos-latest, windows-latest]
59+
os: [ubuntu-latest, macos-latest, windows-2019]
6260
include:
6361
- node: 14
6462
os: ubuntu-latest
@@ -74,9 +72,7 @@ jobs:
7472
cache: 'pnpm'
7573
cache-dependency-path: '**/pnpm-lock.yaml'
7674
- name: install
77-
run: |
78-
pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
79-
node node_modules/esbuild/install.js
75+
run: pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
8076
- name: build
8177
run: pnpm run build:ci
8278
- name: run tests

.github/workflows/release.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ jobs:
3232
cache: 'pnpm'
3333
cache-dependency-path: '**/pnpm-lock.yaml'
3434
- name: install
35-
run: |
36-
pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
37-
node node_modules/esbuild/install.js
35+
run: pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
3836

3937
- name: Creating .npmrc
4038
run: |

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"svelte": "^3.46.2",
5858
"ts-jest": "^27.1.3",
5959
"typescript": "^4.5.4",
60-
"vite": "^2.7.12"
60+
"vite": "^2.7.13"
6161
},
6262
"lint-staged": {
6363
"*.{js,ts,svelte,html,md,svx}": "eslint --fix",
@@ -74,7 +74,7 @@
7474
"pnpm": {
7575
"overrides": {
7676
"@sveltejs/vite-plugin-svelte": "workspace:*",
77-
"vite": "^2.7.10",
77+
"vite": "^2.7.13",
7878
"ansi-regex@>2.1.1 <5.0.1": "^5.0.1"
7979
}
8080
}

packages/e2e-tests/autoprefixer-browerslist/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
"postcss-load-config": "^3.1.1",
1818
"svelte": "^3.46.2",
1919
"svelte-preprocess": "^4.10.2",
20-
"vite": "^2.7.12"
20+
"vite": "^2.7.13"
2121
}
2222
}

packages/e2e-tests/configfile-custom/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"devDependencies": {
1414
"@sveltejs/vite-plugin-svelte": "workspace:*",
1515
"svelte": "^3.46.2",
16-
"vite": "^2.7.12"
16+
"vite": "^2.7.13"
1717
}
1818
}

packages/e2e-tests/configfile-esm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@sveltejs/vite-plugin-svelte": "workspace:*",
1515
"svelte": "^3.46.2",
1616
"svelte-preprocess": "^4.10.2",
17-
"vite": "^2.7.12"
17+
"vite": "^2.7.13"
1818
},
1919
"type": "module"
2020
}

packages/e2e-tests/custom-extensions/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"devDependencies": {
1111
"@sveltejs/vite-plugin-svelte": "workspace:*",
1212
"svelte": "^3.46.2",
13-
"vite": "^2.7.12"
13+
"vite": "^2.7.13"
1414
}
1515
}

packages/e2e-tests/env/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"devDependencies": {
1111
"@sveltejs/vite-plugin-svelte": "workspace:*",
1212
"svelte": "^3.46.2",
13-
"vite": "^2.7.12"
13+
"vite": "^2.7.13"
1414
},
1515
"type": "module"
1616
}

packages/e2e-tests/hmr/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"@sveltejs/vite-plugin-svelte": "workspace:*",
1515
"node-fetch": "^2.6.6",
1616
"svelte": "^3.46.2",
17-
"vite": "^2.7.12"
17+
"vite": "^2.7.13"
1818
}
1919
}

packages/e2e-tests/package-json-svelte-field/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"devDependencies": {
1515
"@sveltejs/vite-plugin-svelte": "workspace:*",
1616
"svelte": "^3.46.2",
17-
"vite": "^2.7.12"
17+
"vite": "^2.7.13"
1818
}
1919
}

packages/e2e-tests/preprocess-with-vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"sass": "^1.48.0",
1313
"stylus": "^0.56.0",
1414
"svelte": "^3.46.2",
15-
"vite": "^2.7.12"
15+
"vite": "^2.7.13"
1616
}
1717
}

packages/e2e-tests/svelte-preprocess/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"svelte": "^3.46.2",
1313
"svelte-preprocess": "^4.10.2",
1414
"typescript": "^4.5.4",
15-
"vite": "^2.7.12"
15+
"vite": "^2.7.13"
1616
}
1717
}

packages/e2e-tests/ts-type-import/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"@tsconfig/svelte": "^3.0.0",
1313
"@types/node": "^17.0.7",
1414
"svelte-preprocess": "^4.10.2",
15-
"vite": "^2.7.12"
15+
"vite": "^2.7.13"
1616
}
1717
}

packages/e2e-tests/vite-ssr-esm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"npm-run-all": "^4.1.5",
2222
"serve-static": "^1.14.2",
2323
"svelte": "^3.46.2",
24-
"vite": "^2.7.12"
24+
"vite": "^2.7.13"
2525
}
2626
}

packages/e2e-tests/vite-ssr/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
"express": "^4.17.2",
1919
"serve-static": "^1.14.2",
2020
"svelte": "^3.46.2",
21-
"vite": "^2.7.12"
21+
"vite": "^2.7.13"
2222
}
2323
}

packages/playground/big-component-library/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"@sveltejs/vite-plugin-svelte": "workspace:*",
1313
"carbon-components-svelte": "^0.52.0",
1414
"svelte": "^3.46.2",
15-
"vite": "^2.7.12"
15+
"vite": "^2.7.13"
1616
}
1717
}

packages/playground/big/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"devDependencies": {
1111
"@sveltejs/vite-plugin-svelte": "workspace:*",
1212
"svelte": "^3.46.2",
13-
"vite": "^2.7.12"
13+
"vite": "^2.7.13"
1414
}
1515
}

packages/playground/optimizedeps-include/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"@sveltejs/vite-plugin-svelte": "workspace:*",
1313
"svelte": "^3.46.2",
1414
"tinro": "^0.6.10",
15-
"vite": "^2.7.12"
15+
"vite": "^2.7.13"
1616
}
1717
}

packages/playground/windicss/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@sveltejs/vite-plugin-svelte": "workspace:*",
1515
"diff-match-patch": "^1.0.5",
1616
"svelte": "^3.46.2",
17-
"vite": "^2.7.12",
17+
"vite": "^2.7.13",
1818
"vite-plugin-windicss": "^1.6.3"
1919
}
2020
}

packages/vite-plugin-svelte/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"debug": "^4.3.3",
5050
"kleur": "^4.1.4",
5151
"magic-string": "^0.25.7",
52-
"require-relative": "^0.8.7",
5352
"svelte-hmr": "^0.14.9"
5453
},
5554
"peerDependencies": {
@@ -70,6 +69,6 @@
7069
"rollup": "^2.64.0",
7170
"svelte": "^3.46.2",
7271
"tsup": "^5.11.11",
73-
"vite": "^2.7.12"
72+
"vite": "^2.7.13"
7473
}
7574
}

packages/vite-plugin-svelte/src/index.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
2727
}
2828
validateInlineOptions(inlineOptions);
2929
const cache = new VitePluginSvelteCache();
30-
const pkg_export_errors = new Set();
30+
const pkg_resolve_errors = new Set();
3131
// updated in configResolved hook
3232
let requestParser: IdParser;
3333
let options: ResolvedOptions;
@@ -137,21 +137,13 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
137137
}
138138

139139
try {
140-
const resolved = resolveViaPackageJsonSvelte(importee, importer);
140+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
141141
if (resolved) {
142142
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
143143
return resolved;
144144
}
145145
} catch (err) {
146-
switch (err.code) {
147-
case 'ERR_PACKAGE_PATH_NOT_EXPORTED':
148-
pkg_export_errors.add(importee);
149-
return null;
150-
case 'MODULE_NOT_FOUND':
151-
return null;
152-
default:
153-
throw err;
154-
}
146+
pkg_resolve_errors.add(importee);
155147
}
156148
},
157149

@@ -206,10 +198,11 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
206198
*/
207199
// TODO generateBundle isn't called by vite, is buildEnd enough or should it be logged once per violation in resolve
208200
buildEnd() {
209-
if (pkg_export_errors.size > 0) {
201+
if (pkg_resolve_errors.size > 0) {
210202
log.warn(
211-
`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.`,
212-
Array.from(pkg_export_errors, (s) => `- ${s}`).join('\n')
203+
`vite-plugin-svelte was unable to find package.json of the following packages and wasn't able to resolve via their "svelte" field.
204+
If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
205+
${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join('\n')}`.replace(/\t/g, '')
213206
);
214207
}
215208
}

packages/vite-plugin-svelte/src/utils/dependencies.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ function getSvelteDependencies(
6666
return result;
6767
}
6868

69-
function resolveDependencyData(dep: string, localRequire: NodeRequire): DependencyData | void {
69+
export function resolveDependencyData(
70+
dep: string,
71+
localRequire: NodeRequire
72+
): DependencyData | void {
7073
try {
7174
const pkgJson = `${dep}/package.json`;
7275
const pkg = localRequire(pkgJson);
@@ -166,7 +169,7 @@ const COMMON_PREFIXES_WITHOUT_SVELTE_FIELD = [
166169
* @param dependency {string}
167170
* @returns {boolean} true if it is a dependency without a svelte field
168171
*/
169-
function is_common_without_svelte_field(dependency: string): boolean {
172+
export function is_common_without_svelte_field(dependency: string): boolean {
170173
return (
171174
COMMON_DEPENDENCIES_WITHOUT_SVELTE_FIELD.includes(dependency) ||
172175
COMMON_PREFIXES_WITHOUT_SVELTE_FIELD.some(

packages/vite-plugin-svelte/src/utils/resolve.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import path from 'path';
2-
import fs from 'fs';
3-
// @ts-ignore
4-
import relative from 'require-relative';
2+
import { createRequire } from 'module';
3+
import { is_common_without_svelte_field, resolveDependencyData } from './dependencies';
4+
import { VitePluginSvelteCache } from './vite-plugin-svelte-cache';
55

6-
export function resolveViaPackageJsonSvelte(importee: string, importer?: string): string | void {
7-
if (importer && isBareImport(importee)) {
8-
const importeePkgFile = relative.resolve(`${importee}/package.json`, path.dirname(importer));
9-
const importeePkg = JSON.parse(fs.readFileSync(importeePkgFile, { encoding: 'utf-8' }));
10-
if (importeePkg.svelte) {
11-
return path.resolve(path.dirname(importeePkgFile), importeePkg.svelte);
6+
export function resolveViaPackageJsonSvelte(
7+
importee: string,
8+
importer: string | undefined,
9+
cache: VitePluginSvelteCache
10+
): string | void {
11+
if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
12+
const cached = cache.getResolvedSvelteField(importee, importer);
13+
if (cached) {
14+
return cached;
15+
}
16+
const localRequire = createRequire(importer);
17+
const pkgData = resolveDependencyData(importee, localRequire);
18+
if (pkgData) {
19+
const { pkg, dir } = pkgData;
20+
if (pkg.svelte) {
21+
const result = path.resolve(dir, pkg.svelte);
22+
cache.setResolvedSvelteField(importee, importer, result);
23+
return result;
24+
}
25+
} else {
26+
throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
1227
}
1328
}
1429
}

packages/vite-plugin-svelte/src/utils/vite-plugin-svelte-cache.ts

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class VitePluginSvelteCache {
66
private _js = new Map<string, Code>();
77
private _dependencies = new Map<string, string[]>();
88
private _dependants = new Map<string, Set<string>>();
9+
private _resolvedSvelteFields = new Map<string, string>();
910

1011
public update(compileData: CompileData) {
1112
this.updateCSS(compileData);
@@ -80,4 +81,23 @@ export class VitePluginSvelteCache {
8081
const dependants = this._dependants.get(path);
8182
return dependants ? [...dependants] : [];
8283
}
84+
85+
public getResolvedSvelteField(name: string, importer?: string): string | void {
86+
return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
87+
}
88+
89+
public setResolvedSvelteField(
90+
importee: string,
91+
importer: string | undefined = undefined,
92+
resolvedSvelte: string
93+
) {
94+
this._resolvedSvelteFields.set(
95+
this._getResolvedSvelteFieldKey(importee, importer),
96+
resolvedSvelte
97+
);
98+
}
99+
100+
private _getResolvedSvelteFieldKey(importee: string, importer?: string): string {
101+
return importer ? `${importer} > ${importee}` : importee;
102+
}
83103
}

0 commit comments

Comments
 (0)