@sveltejs/vite-plugin-svelte 1.0.0-next.33 → 1.0.0-next.34
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 +112 -64
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +74 -36
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +5 -0
- package/src/utils/optimizer.ts +43 -0
- package/src/utils/options.ts +1 -0
- package/src/utils/preprocess.ts +2 -1
- package/src/utils/watch.ts +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { optimizeDeps, ResolvedConfig } from 'vite';
|
|
4
|
+
import { ResolvedOptions } from './options';
|
|
5
|
+
|
|
6
|
+
// List of options that changes the prebundling result
|
|
7
|
+
const PREBUNDLE_SENSITIVE_OPTIONS: (keyof ResolvedOptions)[] = [
|
|
8
|
+
'compilerOptions',
|
|
9
|
+
'configFile',
|
|
10
|
+
'experimental',
|
|
11
|
+
'extensions',
|
|
12
|
+
'ignorePluginPreprocessors',
|
|
13
|
+
'preprocess'
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) {
|
|
17
|
+
if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return;
|
|
18
|
+
|
|
19
|
+
const viteMetadataPath = path.resolve(viteConfig.cacheDir, '_metadata.json');
|
|
20
|
+
|
|
21
|
+
if (!fs.existsSync(viteMetadataPath)) return;
|
|
22
|
+
|
|
23
|
+
const svelteMetadataPath = path.resolve(viteConfig.cacheDir, '_svelte_metadata.json');
|
|
24
|
+
const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
|
|
25
|
+
return typeof value === 'function' ? value.toString() : value;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (fs.existsSync(svelteMetadataPath)) {
|
|
29
|
+
const existingSvelteMetadata = fs.readFileSync(svelteMetadataPath, 'utf8');
|
|
30
|
+
if (existingSvelteMetadata === currentSvelteMetadata) return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await optimizeDeps(viteConfig, true);
|
|
34
|
+
fs.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function generateSvelteMetadata(options: ResolvedOptions) {
|
|
38
|
+
const metadata: Record<string, any> = {};
|
|
39
|
+
for (const key of PREBUNDLE_SENSITIVE_OPTIONS) {
|
|
40
|
+
metadata[key] = options[key];
|
|
41
|
+
}
|
|
42
|
+
return metadata;
|
|
43
|
+
}
|
package/src/utils/options.ts
CHANGED
package/src/utils/preprocess.ts
CHANGED
|
@@ -25,7 +25,8 @@ function createViteScriptPreprocessor(): Preprocessor {
|
|
|
25
25
|
tsconfigRaw: {
|
|
26
26
|
compilerOptions: {
|
|
27
27
|
// svelte typescript needs this flag to work with type imports
|
|
28
|
-
importsNotUsedAsValues: 'preserve'
|
|
28
|
+
importsNotUsedAsValues: 'preserve',
|
|
29
|
+
preserveValueImports: true
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
});
|
package/src/utils/watch.ts
CHANGED