@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.
@@ -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
+ }
@@ -112,6 +112,7 @@ export function resolveOptions(
112
112
  ...defaultOptions.compilerOptions,
113
113
  ...preResolveOptions.compilerOptions
114
114
  },
115
+ root: viteConfig.root,
115
116
  isProduction: viteConfig.isProduction
116
117
  };
117
118
  addExtraPreprocessors(merged, viteConfig);
@@ -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
  });
@@ -54,7 +54,7 @@ export function setupWatchers(
54
54
  });
55
55
  } else {
56
56
  log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
57
- server.restart(!!options.experimental?.prebundleSvelteLibraries);
57
+ server.restart();
58
58
  }
59
59
  };
60
60