@sveltejs/vite-plugin-svelte 2.2.0 → 2.4.0

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.
Files changed (44) hide show
  1. package/package.json +12 -17
  2. package/src/{handle-hot-update.ts → handle-hot-update.js} +35 -20
  3. package/src/index.d.ts +215 -0
  4. package/src/{index.ts → index.js} +50 -71
  5. package/src/{preprocess.ts → preprocess.js} +37 -28
  6. package/src/types/compile.d.ts +48 -0
  7. package/src/types/id.d.ts +31 -0
  8. package/src/types/log.d.ts +24 -0
  9. package/src/types/options.d.ts +20 -0
  10. package/src/types/plugin-api.d.ts +11 -0
  11. package/src/types/vite-plugin-svelte-stats.d.ts +30 -0
  12. package/src/utils/{compile.ts → compile.js} +32 -66
  13. package/src/utils/{dependencies.ts → dependencies.js} +14 -11
  14. package/src/utils/{error.ts → error.js} +21 -14
  15. package/src/utils/{esbuild.ts → esbuild.js} +23 -17
  16. package/src/utils/{hash.ts → hash.js} +14 -3
  17. package/src/utils/{id.ts → id.js} +59 -60
  18. package/src/utils/{load-raw.ts → load-raw.js} +16 -16
  19. package/src/utils/{load-svelte-config.ts → load-svelte-config.js} +12 -10
  20. package/src/utils/{log.ts → log.js} +81 -48
  21. package/src/utils/{optimizer.ts → optimizer.js} +15 -7
  22. package/src/utils/{options.ts → options.js} +146 -295
  23. package/src/utils/{preprocess.ts → preprocess.js} +28 -12
  24. package/src/utils/{resolve.ts → resolve.js} +18 -9
  25. package/src/utils/{sourcemaps.ts → sourcemaps.js} +22 -14
  26. package/src/utils/{svelte-version.ts → svelte-version.js} +15 -7
  27. package/src/utils/vite-plugin-svelte-cache.js +253 -0
  28. package/src/utils/{vite-plugin-svelte-stats.ts → vite-plugin-svelte-stats.js} +66 -62
  29. package/src/utils/{watch.ts → watch.js} +30 -22
  30. package/dist/index.d.ts +0 -259
  31. package/dist/index.js +0 -2428
  32. package/dist/index.js.map +0 -1
  33. package/src/__tests__/fixtures/preprocess/foo.scss +0 -3
  34. package/src/__tests__/preprocess.spec.ts +0 -51
  35. package/src/ui/inspector/Inspector.svelte +0 -384
  36. package/src/ui/inspector/load-inspector.js +0 -15
  37. package/src/ui/inspector/options.ts +0 -131
  38. package/src/ui/inspector/plugin.ts +0 -97
  39. package/src/ui/inspector/utils.ts +0 -13
  40. package/src/utils/__tests__/compile.spec.ts +0 -49
  41. package/src/utils/__tests__/sourcemaps.spec.ts +0 -79
  42. package/src/utils/__tests__/svelte-version.spec.ts +0 -102
  43. package/src/utils/vite-plugin-svelte-cache.ts +0 -182
  44. /package/src/utils/{constants.ts → constants.js} +0 -0
@@ -1,27 +1,18 @@
1
1
  /* eslint-disable no-unused-vars */
2
- import { ConfigEnv, normalizePath, ResolvedConfig, UserConfig, ViteDevServer } from 'vite';
3
- import { isDebugNamespaceEnabled, log } from './log';
4
- import { loadSvelteConfig } from './load-svelte-config';
2
+ import { normalizePath } from 'vite';
3
+ import { isDebugNamespaceEnabled, log } from './log.js';
4
+ import { loadSvelteConfig } from './load-svelte-config.js';
5
5
  import {
6
6
  SVELTE_EXPORT_CONDITIONS,
7
7
  SVELTE_HMR_IMPORTS,
8
8
  SVELTE_IMPORTS,
9
9
  SVELTE_RESOLVE_MAIN_FIELDS,
10
10
  VITE_RESOLVE_MAIN_FIELDS
11
- } from './constants';
12
- // eslint-disable-next-line node/no-missing-import
13
- import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
14
- import type {
15
- MarkupPreprocessor,
16
- Preprocessor,
17
- PreprocessorGroup,
18
- Processed
19
- // eslint-disable-next-line node/no-missing-import
20
- } from 'svelte/types/compiler/preprocess';
11
+ } from './constants.js';
21
12
 
22
13
  import path from 'path';
23
- import { esbuildSveltePlugin, facadeEsbuildSveltePluginName } from './esbuild';
24
- import { addExtraPreprocessors } from './preprocess';
14
+ import { esbuildSveltePlugin, facadeEsbuildSveltePluginName } from './esbuild.js';
15
+ import { addExtraPreprocessors } from './preprocess.js';
25
16
  import deepmerge from 'deepmerge';
26
17
  import {
27
18
  crawlFrameworkPkgs,
@@ -29,13 +20,11 @@ import {
29
20
  isDepExternaled,
30
21
  isDepIncluded,
31
22
  isDepNoExternaled
32
- // eslint-disable-next-line node/no-missing-import
33
23
  } from 'vitefu';
34
24
 
35
- import { isCommonDepWithoutSvelteField } from './dependencies';
36
- import { VitePluginSvelteStats } from './vite-plugin-svelte-stats';
37
- import { VitePluginSvelteCache } from './vite-plugin-svelte-cache';
38
- import type { InspectorOptions } from '../ui/inspector/options';
25
+ import { isCommonDepWithoutSvelteField } from './dependencies.js';
26
+ import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
27
+ import { VitePluginSvelteCache } from './vite-plugin-svelte-cache.js';
39
28
 
40
29
  const allowedPluginOptions = new Set([
41
30
  'include',
@@ -53,7 +42,10 @@ const knownRootOptions = new Set(['extensions', 'compilerOptions', 'preprocess',
53
42
 
54
43
  const allowedInlineOptions = new Set(['configFile', ...allowedPluginOptions, ...knownRootOptions]);
55
44
 
56
- export function validateInlineOptions(inlineOptions?: Partial<Options>) {
45
+ /**
46
+ * @param {Partial<import('../index.d.ts').Options>} [inlineOptions]
47
+ */
48
+ export function validateInlineOptions(inlineOptions) {
57
49
  const invalidKeys = Object.keys(inlineOptions || {}).filter(
58
50
  (key) => !allowedInlineOptions.has(key)
59
51
  );
@@ -62,7 +54,11 @@ export function validateInlineOptions(inlineOptions?: Partial<Options>) {
62
54
  }
63
55
  }
64
56
 
65
- function convertPluginOptions(config?: Partial<SvelteOptions>): Partial<Options> | undefined {
57
+ /**
58
+ * @param {Partial<import('../index.d.ts').SvelteOptions>} [config]
59
+ * @returns {Partial<import('../index.d.ts').Options> | undefined}
60
+ */
61
+ function convertPluginOptions(config) {
66
62
  if (!config) {
67
63
  return;
68
64
  }
@@ -110,8 +106,8 @@ function convertPluginOptions(config?: Partial<SvelteOptions>): Partial<Options>
110
106
  delete pluginOptions[unkownOption];
111
107
  });
112
108
  }
113
-
114
- const result: Options = {
109
+ /** @type {import('../index.d.ts').Options} */
110
+ const result = {
115
111
  ...config,
116
112
  ...pluginOptions
117
113
  };
@@ -121,18 +117,25 @@ function convertPluginOptions(config?: Partial<SvelteOptions>): Partial<Options>
121
117
  return result;
122
118
  }
123
119
 
124
- // used in config phase, merges the default options, svelte config, and inline options
125
- export async function preResolveOptions(
126
- inlineOptions: Partial<Options> = {},
127
- viteUserConfig: UserConfig,
128
- viteEnv: ConfigEnv
129
- ): Promise<PreResolvedOptions> {
130
- const viteConfigWithResolvedRoot: UserConfig = {
120
+ /**
121
+ * used in config phase, merges the default options, svelte config, and inline options
122
+ * @param {Partial<import('../index.d.ts').Options> | undefined} inlineOptions
123
+ * @param {import('vite').UserConfig} viteUserConfig
124
+ * @param {import('vite').ConfigEnv} viteEnv
125
+ * @returns {Promise<import('../types/options.d.ts').PreResolvedOptions>}
126
+ */
127
+ export async function preResolveOptions(inlineOptions, viteUserConfig, viteEnv) {
128
+ if (!inlineOptions) {
129
+ inlineOptions = {};
130
+ }
131
+ /** @type {import('vite').UserConfig} */
132
+ const viteConfigWithResolvedRoot = {
131
133
  ...viteUserConfig,
132
134
  root: resolveViteRoot(viteUserConfig)
133
135
  };
134
136
  const isBuild = viteEnv.command === 'build';
135
- const defaultOptions: Partial<Options> = {
137
+ /** @type {Partial<import('../types/options.d.ts').PreResolvedOptions>} */
138
+ const defaultOptions = {
136
139
  extensions: ['.svelte'],
137
140
  emitCss: true,
138
141
  prebundleSvelteLibraries: !isBuild
@@ -140,18 +143,16 @@ export async function preResolveOptions(
140
143
  const svelteConfig = convertPluginOptions(
141
144
  await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions)
142
145
  );
143
-
144
- const extraOptions: Partial<PreResolvedOptions> = {
145
- root: viteConfigWithResolvedRoot.root!,
146
+ /** @type {Partial<import('../types/options.d.ts').PreResolvedOptions>} */
147
+ const extraOptions = {
148
+ root: viteConfigWithResolvedRoot.root,
146
149
  isBuild,
147
150
  isServe: viteEnv.command === 'serve',
148
151
  isDebug: process.env.DEBUG != null
149
152
  };
150
- const merged = mergeConfigs<PreResolvedOptions>(
151
- defaultOptions,
152
- svelteConfig,
153
- inlineOptions,
154
- extraOptions
153
+
154
+ const merged = /** @type {import('../types/options.d.ts').PreResolvedOptions} */ (
155
+ mergeConfigs(defaultOptions, svelteConfig, inlineOptions, extraOptions)
155
156
  );
156
157
  // configFile of svelteConfig contains the absolute path it was loaded from,
157
158
  // prefer it over the possibly relative inline path
@@ -161,26 +162,35 @@ export async function preResolveOptions(
161
162
  return merged;
162
163
  }
163
164
 
164
- function mergeConfigs<T>(...configs: (Partial<T> | undefined)[]): T {
165
- let result: Partial<T> = {};
165
+ /**
166
+ * @template T
167
+ * @param {(Partial<T> | undefined)[]} configs
168
+ * @returns T
169
+ */
170
+ function mergeConfigs(...configs) {
171
+ /** @type {Partial<T>} */
172
+ let result = {};
166
173
  for (const config of configs.filter((x) => x != null)) {
167
- result = deepmerge<T>(result, config!, {
174
+ result = deepmerge(result, /** @type {Partial<T>} */ (config), {
168
175
  // replace arrays
169
- arrayMerge: (target: any[], source: any[]) => source ?? target
176
+ arrayMerge: (target, source) => source ?? target
170
177
  });
171
178
  }
172
- return result as T;
179
+ return /** @type {T} */ result;
173
180
  }
174
181
 
175
- // used in configResolved phase, merges a contextual default config, pre-resolved options, and some preprocessors.
176
- // also validates the final config.
177
- export function resolveOptions(
178
- preResolveOptions: PreResolvedOptions,
179
- viteConfig: ResolvedConfig,
180
- cache: VitePluginSvelteCache
181
- ): ResolvedOptions {
182
+ /**
183
+ * used in configResolved phase, merges a contextual default config, pre-resolved options, and some preprocessors. also validates the final config.
184
+ *
185
+ * @param {import('../types/options.d.ts').PreResolvedOptions} preResolveOptions
186
+ * @param {import('vite').ResolvedConfig} viteConfig
187
+ * @param {VitePluginSvelteCache} cache
188
+ * @returns {import('../types/options.d.ts').ResolvedOptions}
189
+ */
190
+ export function resolveOptions(preResolveOptions, viteConfig, cache) {
182
191
  const css = preResolveOptions.emitCss ? 'external' : 'injected';
183
- const defaultOptions: Partial<Options> = {
192
+ /** @type {Partial<import('../index.d.ts').Options>} */
193
+ const defaultOptions = {
184
194
  hot: viteConfig.isProduction
185
195
  ? false
186
196
  : {
@@ -192,11 +202,14 @@ export function resolveOptions(
192
202
  dev: !viteConfig.isProduction
193
203
  }
194
204
  };
195
- const extraOptions: Partial<ResolvedOptions> = {
205
+ /** @type {Partial<import('../types/options.d.ts').ResolvedOptions>} */
206
+ const extraOptions = {
196
207
  root: viteConfig.root,
197
208
  isProduction: viteConfig.isProduction
198
209
  };
199
- const merged = mergeConfigs<ResolvedOptions>(defaultOptions, preResolveOptions, extraOptions);
210
+ const merged = /** @type {import('../types/options.d.ts').ResolvedOptions}*/ (
211
+ mergeConfigs(defaultOptions, preResolveOptions, extraOptions)
212
+ );
200
213
 
201
214
  removeIgnoredOptions(merged);
202
215
  handleDeprecatedOptions(merged);
@@ -210,7 +223,10 @@ export function resolveOptions(
210
223
  return merged;
211
224
  }
212
225
 
213
- function enforceOptionsForHmr(options: ResolvedOptions) {
226
+ /**
227
+ * @param {import('../types/options.d.ts').ResolvedOptions} options
228
+ */
229
+ function enforceOptionsForHmr(options) {
214
230
  if (options.hot) {
215
231
  if (!options.compilerOptions.dev) {
216
232
  log.warn('hmr is enabled but compilerOptions.dev is false, forcing it to true');
@@ -252,7 +268,10 @@ function enforceOptionsForHmr(options: ResolvedOptions) {
252
268
  }
253
269
  }
254
270
 
255
- function enforceOptionsForProduction(options: ResolvedOptions) {
271
+ /**
272
+ * @param {import('../types/options.d.ts').ResolvedOptions} options
273
+ */
274
+ function enforceOptionsForProduction(options) {
256
275
  if (options.isProduction) {
257
276
  if (options.hot) {
258
277
  log.warn('options.hot is enabled but does not work on production build, forcing it to false');
@@ -267,7 +286,10 @@ function enforceOptionsForProduction(options: ResolvedOptions) {
267
286
  }
268
287
  }
269
288
 
270
- function removeIgnoredOptions(options: ResolvedOptions) {
289
+ /**
290
+ * @param {import('../types/options.d.ts').ResolvedOptions} options
291
+ */
292
+ function removeIgnoredOptions(options) {
271
293
  const ignoredCompilerOptions = ['generate', 'format', 'filename'];
272
294
  if (options.hot && options.emitCss) {
273
295
  ignoredCompilerOptions.push('cssHash');
@@ -287,8 +309,11 @@ function removeIgnoredOptions(options: ResolvedOptions) {
287
309
  }
288
310
  }
289
311
 
290
- function handleDeprecatedOptions(options: ResolvedOptions) {
291
- const experimental = options.experimental as any;
312
+ /**
313
+ * @param {import('../types/options.d.ts').ResolvedOptions} options
314
+ */
315
+ function handleDeprecatedOptions(options) {
316
+ const experimental = /** @type {Record<string, any>} */ (options.experimental);
292
317
  if (experimental) {
293
318
  for (const promoted of ['prebundleSvelteLibraries', 'inspector']) {
294
319
  if (experimental[promoted]) {
@@ -306,17 +331,25 @@ function handleDeprecatedOptions(options: ResolvedOptions) {
306
331
  }
307
332
  }
308
333
 
309
- // vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
310
- // https://github.com/sveltejs/vite-plugin-svelte/issues/113
311
- // https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
312
- function resolveViteRoot(viteConfig: UserConfig): string | undefined {
334
+ /**
335
+ * vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
336
+ *
337
+ * @see https://github.com/sveltejs/vite-plugin-svelte/issues/113
338
+ * @see https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
339
+ *
340
+ * @param {import('vite').UserConfig} viteConfig
341
+ * @returns {string | undefined}
342
+ */
343
+ function resolveViteRoot(viteConfig) {
313
344
  return normalizePath(viteConfig.root ? path.resolve(viteConfig.root) : process.cwd());
314
345
  }
315
346
 
316
- export async function buildExtraViteConfig(
317
- options: PreResolvedOptions,
318
- config: UserConfig
319
- ): Promise<Partial<UserConfig>> {
347
+ /**
348
+ * @param {import('../types/options.d.ts').PreResolvedOptions} options
349
+ * @param {import('vite').UserConfig} config
350
+ * @returns {Promise<Partial<import('vite').UserConfig>>}
351
+ */
352
+ export async function buildExtraViteConfig(options, config) {
320
353
  // make sure we only readd vite default mainFields when no other plugin has changed the config already
321
354
  // see https://github.com/sveltejs/vite-plugin-svelte/issues/581
322
355
  if (!config.resolve) {
@@ -327,7 +360,8 @@ export async function buildExtraViteConfig(
327
360
  ...(config.resolve.mainFields ?? VITE_RESOLVE_MAIN_FIELDS)
328
361
  ];
329
362
 
330
- const extraViteConfig: Partial<UserConfig> = {
363
+ /** @type {Partial<import('vite').UserConfig>} */
364
+ const extraViteConfig = {
331
365
  resolve: {
332
366
  dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS],
333
367
  conditions: [...SVELTE_EXPORT_CONDITIONS]
@@ -401,16 +435,18 @@ export async function buildExtraViteConfig(
401
435
  return extraViteConfig;
402
436
  }
403
437
 
404
- function validateViteConfig(
405
- extraViteConfig: Partial<UserConfig>,
406
- config: UserConfig,
407
- options: PreResolvedOptions
408
- ) {
438
+ /**
439
+ * @param {Partial<import('vite').UserConfig>} extraViteConfig
440
+ * @param {import('vite').UserConfig} config
441
+ * @param {import('../types/options.d.ts').PreResolvedOptions} options
442
+ */
443
+ function validateViteConfig(extraViteConfig, config, options) {
409
444
  const { prebundleSvelteLibraries, isBuild } = options;
410
445
  if (prebundleSvelteLibraries) {
411
- const isEnabled = (option: 'dev' | 'build' | boolean) =>
412
- option !== true && option !== (isBuild ? 'build' : 'dev');
413
- const logWarning = (name: string, value: 'dev' | 'build' | boolean, recommendation: string) =>
446
+ /** @type {(option: 'dev' | 'build' | boolean)=> boolean} */
447
+ const isEnabled = (option) => option !== true && option !== (isBuild ? 'build' : 'dev');
448
+ /** @type {(name: string, value: 'dev' | 'build' | boolean, recommendation: string)=> void} */
449
+ const logWarning = (name, value, recommendation) =>
414
450
  log.warn.once(
415
451
  `Incompatible options: \`prebundleSvelteLibraries: true\` and vite \`${name}: ${JSON.stringify(
416
452
  value
@@ -424,7 +460,10 @@ function validateViteConfig(
424
460
  viteOptimizeDepsDisabled,
425
461
  'Forcing `optimizeDeps.disabled: "build"`. Disable prebundleSvelteLibraries or update your vite config to enable optimizeDeps during dev.'
426
462
  );
427
- extraViteConfig.optimizeDeps!.disabled = 'build';
463
+ if (!extraViteConfig.optimizeDeps) {
464
+ extraViteConfig.optimizeDeps = {};
465
+ }
466
+ extraViteConfig.optimizeDeps.disabled = 'build';
428
467
  } else if (isBuild && isOptimizeDepsEnabled) {
429
468
  logWarning(
430
469
  'optimizeDeps.disabled',
@@ -435,7 +474,12 @@ function validateViteConfig(
435
474
  }
436
475
  }
437
476
 
438
- async function buildExtraConfigForDependencies(options: PreResolvedOptions, config: UserConfig) {
477
+ /**
478
+ * @param {import('../types/options.d.ts').PreResolvedOptions} options
479
+ * @param {import('vite').UserConfig} config
480
+ * @returns {Promise<import('vitefu').CrawlFrameworkPkgsResult>}
481
+ */
482
+ async function buildExtraConfigForDependencies(options, config) {
439
483
  // extra handling for svelte dependencies in the project
440
484
  const depsConfig = await crawlFrameworkPkgs({
441
485
  root: options.root,
@@ -476,7 +520,7 @@ async function buildExtraConfigForDependencies(options: PreResolvedOptions, conf
476
520
  const userExclude = config.optimizeDeps?.exclude;
477
521
  depsConfig.optimizeDeps.include = !userExclude
478
522
  ? []
479
- : depsConfig.optimizeDeps.include.filter((dep: string) => {
523
+ : depsConfig.optimizeDeps.include.filter((dep) => {
480
524
  // reincludes look like this: foo > bar > baz
481
525
  // in case foo or bar are excluded, we have to retain the reinclude even with prebundling
482
526
  return (
@@ -506,10 +550,15 @@ async function buildExtraConfigForDependencies(options: PreResolvedOptions, conf
506
550
  return depsConfig;
507
551
  }
508
552
 
509
- function buildExtraConfigForSvelte(config: UserConfig) {
553
+ /**
554
+ * @param {import('vite').UserConfig} config
555
+ * @returns {import('vite').UserConfig & { optimizeDeps: { include: string[], exclude:string[] }, ssr: { noExternal:(string|RegExp)[], external: string[] } } }
556
+ */
557
+ function buildExtraConfigForSvelte(config) {
510
558
  // include svelte imports for optimization unless explicitly excluded
511
- const include: string[] = [];
512
- const exclude: string[] = ['svelte-hmr'];
559
+ /** @type {string[]} */
560
+ const include = [];
561
+ const exclude = ['svelte-hmr'];
513
562
  if (!isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
514
563
  const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== 'svelte/ssr'); // not used on clientside
515
564
  log.debug(
@@ -519,8 +568,10 @@ function buildExtraConfigForSvelte(config: UserConfig) {
519
568
  } else {
520
569
  log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
521
570
  }
522
- const noExternal: (string | RegExp)[] = [];
523
- const external: string[] = [];
571
+ /** @type {(string | RegExp)[]} */
572
+ const noExternal = [];
573
+ /** @type {string[]} */
574
+ const external = [];
524
575
  // add svelte to ssr.noExternal unless it is present in ssr.external
525
576
  // so we can resolve it with svelte/ssr
526
577
  if (!isDepExternaled('svelte', config.ssr?.external ?? [])) {
@@ -529,7 +580,11 @@ function buildExtraConfigForSvelte(config: UserConfig) {
529
580
  return { optimizeDeps: { include, exclude }, ssr: { noExternal, external } };
530
581
  }
531
582
 
532
- export function patchResolvedViteConfig(viteConfig: ResolvedConfig, options: ResolvedOptions) {
583
+ /**
584
+ * @param {import('vite').ResolvedConfig} viteConfig
585
+ * @param {import('../types/options.d.ts').ResolvedOptions} options
586
+ */
587
+ export function patchResolvedViteConfig(viteConfig, options) {
533
588
  if (options.preprocess) {
534
589
  for (const preprocessor of arraify(options.preprocess)) {
535
590
  if (preprocessor.style && '__resolvedConfig' in preprocessor.style) {
@@ -547,215 +602,11 @@ export function patchResolvedViteConfig(viteConfig: ResolvedConfig, options: Res
547
602
  }
548
603
  }
549
604
 
550
- function arraify<T>(value: T | T[]): T[] {
551
- return Array.isArray(value) ? value : [value];
552
- }
553
-
554
- export type Options = Omit<SvelteOptions, 'vitePlugin'> & PluginOptionsInline;
555
-
556
- interface PluginOptionsInline extends PluginOptions {
557
- /**
558
- * Path to a svelte config file, either absolute or relative to Vite root
559
- *
560
- * set to `false` to ignore the svelte config file
561
- *
562
- * @see https://vitejs.dev/config/#root
563
- */
564
- configFile?: string | false;
565
- }
566
-
567
- export interface PluginOptions {
568
- /**
569
- * A `picomatch` pattern, or array of patterns, which specifies the files the plugin should
570
- * operate on. By default, all svelte files are included.
571
- *
572
- * @see https://github.com/micromatch/picomatch
573
- */
574
- include?: Arrayable<string>;
575
-
576
- /**
577
- * A `picomatch` pattern, or array of patterns, which specifies the files to be ignored by the
578
- * plugin. By default, no files are ignored.
579
- *
580
- * @see https://github.com/micromatch/picomatch
581
- */
582
- exclude?: Arrayable<string>;
583
-
584
- /**
585
- * Emit Svelte styles as virtual CSS files for Vite and other plugins to process
586
- *
587
- * @default true
588
- */
589
- emitCss?: boolean;
590
-
591
- /**
592
- * Enable or disable Hot Module Replacement.
593
- *
594
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
595
- *
596
- * DO NOT CUSTOMIZE SVELTE-HMR OPTIONS UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING
597
- *
598
- * YOU HAVE BEEN WARNED
599
- *
600
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
601
- *
602
- * Set an object to pass custom options to svelte-hmr
603
- *
604
- * @see https://github.com/rixo/svelte-hmr#options
605
- * @default true for development, always false for production
606
- */
607
- hot?: boolean | { injectCss?: boolean; partialAccept?: boolean; [key: string]: any };
608
-
609
- /**
610
- * Some Vite plugins can contribute additional preprocessors by defining `api.sveltePreprocess`.
611
- * If you don't want to use them, set this to true to ignore them all or use an array of strings
612
- * with plugin names to specify which.
613
- *
614
- * @default false
615
- */
616
- ignorePluginPreprocessors?: boolean | string[];
617
-
618
- /**
619
- * vite-plugin-svelte automatically handles excluding svelte libraries and reinclusion of their dependencies
620
- * in vite.optimizeDeps.
621
- *
622
- * `disableDependencyReinclusion: true` disables all reinclusions
623
- * `disableDependencyReinclusion: ['foo','bar']` disables reinclusions for dependencies of foo and bar
624
- *
625
- * This should be used for hybrid packages that contain both node and browser dependencies, eg Routify
626
- *
627
- * @default false
628
- */
629
- disableDependencyReinclusion?: boolean | string[];
630
-
631
- /**
632
- * Enable support for Vite's dependency optimization to prebundle Svelte libraries.
633
- *
634
- * To disable prebundling for a specific library, add it to `optimizeDeps.exclude`.
635
- *
636
- * @default true for dev, false for build
637
- */
638
- prebundleSvelteLibraries?: boolean;
639
-
640
- /**
641
- * toggle/configure Svelte Inspector
642
- *
643
- * @default true
644
- */
645
- inspector?: InspectorOptions | boolean;
646
-
647
- /**
648
- * These options are considered experimental and breaking changes to them can occur in any release
649
- */
650
- experimental?: ExperimentalOptions;
651
- }
652
-
653
- export interface SvelteOptions {
654
- /**
655
- * A list of file extensions to be compiled by Svelte
656
- *
657
- * @default ['.svelte']
658
- */
659
- extensions?: string[];
660
-
661
- /**
662
- * An array of preprocessors to transform the Svelte source code before compilation
663
- *
664
- * @see https://svelte.dev/docs#svelte_preprocess
665
- */
666
- preprocess?: Arrayable<PreprocessorGroup>;
667
-
668
- /**
669
- * The options to be passed to the Svelte compiler. A few options are set by default,
670
- * including `dev` and `css`. However, some options are non-configurable, like
671
- * `filename`, `format`, `generate`, and `cssHash` (in dev).
672
- *
673
- * @see https://svelte.dev/docs#svelte_compile
674
- */
675
- compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
676
-
677
- /**
678
- * Handles warning emitted from the Svelte compiler
679
- */
680
- onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
681
-
682
- /**
683
- * Options for vite-plugin-svelte
684
- */
685
- vitePlugin?: PluginOptions;
686
- }
687
-
688
605
  /**
689
- * These options are considered experimental and breaking changes to them can occur in any release
606
+ * @template T
607
+ * @param {T | T[]} value
608
+ * @returns {T[]}
690
609
  */
691
- export interface ExperimentalOptions {
692
- /**
693
- * A function to update `compilerOptions` before compilation
694
- *
695
- * `data.filename` - The file to be compiled
696
- * `data.code` - The preprocessed Svelte code
697
- * `data.compileOptions` - The current compiler options
698
- *
699
- * To change part of the compiler options, return an object with the changes you need.
700
- *
701
- * @example
702
- * ```
703
- * ({ filename, compileOptions }) => {
704
- * // Dynamically set hydration per Svelte file
705
- * if (compileWithHydratable(filename) && !compileOptions.hydratable) {
706
- * return { hydratable: true };
707
- * }
708
- * }
709
- * ```
710
- */
711
- dynamicCompileOptions?: (data: {
712
- filename: string;
713
- code: string;
714
- compileOptions: Partial<CompileOptions>;
715
- }) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
716
-
717
- /**
718
- * send a websocket message with svelte compiler warnings during dev
719
- *
720
- */
721
- sendWarningsToBrowser?: boolean;
722
-
723
- /**
724
- * disable svelte field resolve warnings
725
- *
726
- * @default false
727
- */
728
- disableSvelteResolveWarnings?: boolean;
729
- }
730
-
731
- export interface PreResolvedOptions extends Options {
732
- // these options are non-nullable after resolve
733
- compilerOptions: CompileOptions;
734
- experimental?: ExperimentalOptions;
735
- // extra options
736
- root: string;
737
- isBuild: boolean;
738
- isServe: boolean;
739
- isDebug: boolean;
740
- }
741
-
742
- export interface ResolvedOptions extends PreResolvedOptions {
743
- isProduction: boolean;
744
- server?: ViteDevServer;
745
- stats?: VitePluginSvelteStats;
610
+ function arraify(value) {
611
+ return Array.isArray(value) ? value : [value];
746
612
  }
747
-
748
- export type {
749
- CompileOptions,
750
- Processed,
751
- MarkupPreprocessor,
752
- Preprocessor,
753
- PreprocessorGroup,
754
- Warning
755
- };
756
-
757
- export type ModuleFormat = NonNullable<CompileOptions['format']>;
758
-
759
- export type CssHashGetter = NonNullable<CompileOptions['cssHash']>;
760
-
761
- export type Arrayable<T> = T | T[];