@sveltejs/vite-plugin-svelte 1.0.0-next.34 → 1.0.0-next.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +43 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +42 -28
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
- package/src/index.ts +7 -14
- package/src/utils/dependencies.ts +5 -2
- package/src/utils/resolve.ts +24 -9
- package/src/utils/vite-plugin-svelte-cache.ts +20 -0
package/src/index.ts
CHANGED
|
@@ -27,7 +27,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
27
27
|
}
|
|
28
28
|
validateInlineOptions(inlineOptions);
|
|
29
29
|
const cache = new VitePluginSvelteCache();
|
|
30
|
-
const
|
|
30
|
+
const pkg_resolve_errors = new Set();
|
|
31
31
|
// updated in configResolved hook
|
|
32
32
|
let requestParser: IdParser;
|
|
33
33
|
let options: ResolvedOptions;
|
|
@@ -137,21 +137,13 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
try {
|
|
140
|
-
const resolved = resolveViaPackageJsonSvelte(importee, importer);
|
|
140
|
+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
|
|
141
141
|
if (resolved) {
|
|
142
142
|
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
|
|
143
143
|
return resolved;
|
|
144
144
|
}
|
|
145
145
|
} catch (err) {
|
|
146
|
-
|
|
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);
|
|
155
147
|
}
|
|
156
148
|
},
|
|
157
149
|
|
|
@@ -206,10 +198,11 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
206
198
|
*/
|
|
207
199
|
// TODO generateBundle isn't called by vite, is buildEnd enough or should it be logged once per violation in resolve
|
|
208
200
|
buildEnd() {
|
|
209
|
-
if (
|
|
201
|
+
if (pkg_resolve_errors.size > 0) {
|
|
210
202
|
log.warn(
|
|
211
|
-
`
|
|
212
|
-
|
|
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, '')
|
|
213
206
|
);
|
|
214
207
|
}
|
|
215
208
|
}
|
|
@@ -66,7 +66,10 @@ function getSvelteDependencies(
|
|
|
66
66
|
return result;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
function resolveDependencyData(
|
|
69
|
+
export function resolveDependencyData(
|
|
70
|
+
dep: string,
|
|
71
|
+
localRequire: NodeRequire
|
|
72
|
+
): DependencyData | void {
|
|
70
73
|
try {
|
|
71
74
|
const pkgJson = `${dep}/package.json`;
|
|
72
75
|
const pkg = localRequire(pkgJson);
|
|
@@ -166,7 +169,7 @@ const COMMON_PREFIXES_WITHOUT_SVELTE_FIELD = [
|
|
|
166
169
|
* @param dependency {string}
|
|
167
170
|
* @returns {boolean} true if it is a dependency without a svelte field
|
|
168
171
|
*/
|
|
169
|
-
function is_common_without_svelte_field(dependency: string): boolean {
|
|
172
|
+
export function is_common_without_svelte_field(dependency: string): boolean {
|
|
170
173
|
return (
|
|
171
174
|
COMMON_DEPENDENCIES_WITHOUT_SVELTE_FIELD.includes(dependency) ||
|
|
172
175
|
COMMON_PREFIXES_WITHOUT_SVELTE_FIELD.some(
|
package/src/utils/resolve.ts
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
import { is_common_without_svelte_field, resolveDependencyData } from './dependencies';
|
|
4
|
+
import { VitePluginSvelteCache } from './vite-plugin-svelte-cache';
|
|
5
5
|
|
|
6
|
-
export function resolveViaPackageJsonSvelte(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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}`);
|
|
12
27
|
}
|
|
13
28
|
}
|
|
14
29
|
}
|
|
@@ -6,6 +6,7 @@ export class VitePluginSvelteCache {
|
|
|
6
6
|
private _js = new Map<string, Code>();
|
|
7
7
|
private _dependencies = new Map<string, string[]>();
|
|
8
8
|
private _dependants = new Map<string, Set<string>>();
|
|
9
|
+
private _resolvedSvelteFields = new Map<string, string>();
|
|
9
10
|
|
|
10
11
|
public update(compileData: CompileData) {
|
|
11
12
|
this.updateCSS(compileData);
|
|
@@ -80,4 +81,23 @@ export class VitePluginSvelteCache {
|
|
|
80
81
|
const dependants = this._dependants.get(path);
|
|
81
82
|
return dependants ? [...dependants] : [];
|
|
82
83
|
}
|
|
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
|
+
}
|
|
83
103
|
}
|