@sveltejs/vite-plugin-svelte 2.3.0 → 2.4.1

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