@sveltejs/vite-plugin-svelte 1.0.0-next.37 → 1.0.0-next.38
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 +30 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +30 -39
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +6 -38
- package/src/utils/__tests__/dependencies.spec.ts +14 -3
- package/src/utils/dependencies.ts +17 -6
- package/src/utils/optimizer.ts +11 -4
- package/src/utils/resolve.ts +0 -2
package/src/index.ts
CHANGED
|
@@ -27,7 +27,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
27
27
|
}
|
|
28
28
|
validateInlineOptions(inlineOptions);
|
|
29
29
|
const cache = new VitePluginSvelteCache();
|
|
30
|
-
const pkg_resolve_errors = new Set();
|
|
31
30
|
// updated in configResolved hook
|
|
32
31
|
let requestParser: IdParser;
|
|
33
32
|
let options: ResolvedOptions;
|
|
@@ -136,36 +135,19 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
136
135
|
return resolvedSvelteSSR;
|
|
137
136
|
}
|
|
138
137
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return resolved;
|
|
144
|
-
}
|
|
145
|
-
} catch (err) {
|
|
146
|
-
pkg_resolve_errors.add(importee);
|
|
138
|
+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
|
|
139
|
+
if (resolved) {
|
|
140
|
+
log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
|
|
141
|
+
return resolved;
|
|
147
142
|
}
|
|
148
143
|
},
|
|
149
144
|
|
|
150
145
|
async transform(code, id, opts) {
|
|
151
146
|
const ssr = !!opts?.ssr;
|
|
152
147
|
const svelteRequest = requestParser(id, ssr);
|
|
153
|
-
if (!svelteRequest) {
|
|
148
|
+
if (!svelteRequest || svelteRequest.query.svelte) {
|
|
154
149
|
return;
|
|
155
150
|
}
|
|
156
|
-
const { filename, query } = svelteRequest;
|
|
157
|
-
|
|
158
|
-
if (query.svelte) {
|
|
159
|
-
if (query.type === 'style') {
|
|
160
|
-
const css = cache.getCSS(svelteRequest);
|
|
161
|
-
if (css) {
|
|
162
|
-
log.debug(`transform returns css for ${filename}`);
|
|
163
|
-
return css; // TODO return code arg instead? it's the code from load hook.
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
log.error('failed to transform tagged svelte request', svelteRequest);
|
|
167
|
-
throw new Error(`failed to transform tagged svelte request for id ${id}`);
|
|
168
|
-
}
|
|
169
151
|
let compileData;
|
|
170
152
|
try {
|
|
171
153
|
compileData = await compileSvelte(svelteRequest, code, options);
|
|
@@ -179,7 +161,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
179
161
|
ensureWatchedFile(options.server!.watcher, d, options.root);
|
|
180
162
|
});
|
|
181
163
|
}
|
|
182
|
-
log.debug(`transform returns compiled js for ${filename}`);
|
|
164
|
+
log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
|
|
183
165
|
return compileData.compiled.js;
|
|
184
166
|
},
|
|
185
167
|
|
|
@@ -191,20 +173,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
191
173
|
if (svelteRequest) {
|
|
192
174
|
return handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options);
|
|
193
175
|
}
|
|
194
|
-
},
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* All resolutions done; display warnings wrt `package.json` access.
|
|
198
|
-
*/
|
|
199
|
-
// TODO generateBundle isn't called by vite, is buildEnd enough or should it be logged once per violation in resolve
|
|
200
|
-
buildEnd() {
|
|
201
|
-
if (pkg_resolve_errors.size > 0) {
|
|
202
|
-
log.warn(
|
|
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, '')
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
176
|
}
|
|
209
177
|
};
|
|
210
178
|
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { findRootSvelteDependencies } from '../dependencies';
|
|
1
|
+
import { findRootSvelteDependencies, needsOptimization } from '../dependencies';
|
|
2
2
|
import * as path from 'path';
|
|
3
|
+
import { createRequire } from 'module';
|
|
3
4
|
|
|
4
5
|
describe('dependencies', () => {
|
|
5
6
|
describe('findRootSvelteDependencies', () => {
|
|
6
|
-
it('should find svelte dependencies in packages/e2e-test/hmr',
|
|
7
|
+
it('should find svelte dependencies in packages/e2e-test/hmr', () => {
|
|
7
8
|
const deps = findRootSvelteDependencies(path.resolve('packages/e2e-tests/hmr'));
|
|
8
9
|
expect(deps).toHaveLength(1);
|
|
9
10
|
expect(deps[0].name).toBe('e2e-test-dep-svelte-simple');
|
|
10
11
|
expect(deps[0].path).toEqual([]);
|
|
11
12
|
});
|
|
12
|
-
it('should find nested svelte dependencies in packages/e2e-test/package-json-svelte-field',
|
|
13
|
+
it('should find nested svelte dependencies in packages/e2e-test/package-json-svelte-field', () => {
|
|
13
14
|
const deps = findRootSvelteDependencies(
|
|
14
15
|
path.resolve('packages/e2e-tests/package-json-svelte-field')
|
|
15
16
|
);
|
|
@@ -26,4 +27,14 @@ describe('dependencies', () => {
|
|
|
26
27
|
expect(simple.path[0]).toBe('e2e-test-dep-svelte-nested');
|
|
27
28
|
});
|
|
28
29
|
});
|
|
30
|
+
describe('needsOptimization', () => {
|
|
31
|
+
it('should optimize cjs deps only', () => {
|
|
32
|
+
const localRequire = createRequire(path.resolve('packages/e2e-tests/dependencies'));
|
|
33
|
+
expect(needsOptimization('e2e-test-dep-cjs-and-esm', localRequire)).toBe(false);
|
|
34
|
+
expect(needsOptimization('e2e-test-dep-cjs-only', localRequire)).toBe(true);
|
|
35
|
+
expect(needsOptimization('e2e-test-dep-esm-only', localRequire)).toBe(false);
|
|
36
|
+
expect(needsOptimization('e2e-test-dep-index-only', localRequire)).toBe(true);
|
|
37
|
+
expect(needsOptimization('e2e-test-dep-types-only', localRequire)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
29
40
|
});
|
|
@@ -187,12 +187,23 @@ export function needsOptimization(dep: string, localRequire: NodeRequire): boole
|
|
|
187
187
|
const pkg = depData.pkg;
|
|
188
188
|
// only optimize if is cjs, using the below as heuristic
|
|
189
189
|
// see https://github.com/sveltejs/vite-plugin-svelte/issues/162
|
|
190
|
-
const
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
190
|
+
const hasEsmFields = pkg.module || pkg.exports;
|
|
191
|
+
if (hasEsmFields) return false;
|
|
192
|
+
if (pkg.main) {
|
|
193
|
+
// ensure entry is js so vite can prebundle it
|
|
194
|
+
// see https://github.com/sveltejs/vite-plugin-svelte/issues/233
|
|
195
|
+
const entryExt = path.extname(pkg.main);
|
|
196
|
+
return !entryExt || entryExt === '.js' || entryExt === '.cjs';
|
|
197
|
+
} else {
|
|
198
|
+
// check if has implicit index.js entrypoint
|
|
199
|
+
// https://github.com/sveltejs/vite-plugin-svelte/issues/281
|
|
200
|
+
try {
|
|
201
|
+
localRequire.resolve(`${dep}/index.js`);
|
|
202
|
+
return true;
|
|
203
|
+
} catch {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
196
207
|
}
|
|
197
208
|
|
|
198
209
|
interface DependencyData {
|
package/src/utils/optimizer.ts
CHANGED
|
@@ -16,11 +16,10 @@ const PREBUNDLE_SENSITIVE_OPTIONS: (keyof ResolvedOptions)[] = [
|
|
|
16
16
|
export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) {
|
|
17
17
|
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return;
|
|
18
18
|
|
|
19
|
-
const viteMetadataPath =
|
|
19
|
+
const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
|
|
20
|
+
if (!viteMetadataPath) return;
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const svelteMetadataPath = path.resolve(viteConfig.cacheDir, '_svelte_metadata.json');
|
|
22
|
+
const svelteMetadataPath = path.resolve(viteMetadataPath, '../_svelte_metadata.json');
|
|
24
23
|
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
|
|
25
24
|
return typeof value === 'function' ? value.toString() : value;
|
|
26
25
|
});
|
|
@@ -41,3 +40,11 @@ function generateSvelteMetadata(options: ResolvedOptions) {
|
|
|
41
40
|
}
|
|
42
41
|
return metadata;
|
|
43
42
|
}
|
|
43
|
+
|
|
44
|
+
function findViteMetadataPath(cacheDir: string) {
|
|
45
|
+
const metadataPaths = ['_metadata.json', 'deps/_metadata.json'];
|
|
46
|
+
for (const metadataPath of metadataPaths) {
|
|
47
|
+
const viteMetadataPath = path.resolve(cacheDir, metadataPath);
|
|
48
|
+
if (fs.existsSync(viteMetadataPath)) return viteMetadataPath;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/utils/resolve.ts
CHANGED