@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
package/package.json CHANGED
@@ -1,22 +1,19 @@
1
1
  {
2
2
  "name": "@sveltejs/vite-plugin-svelte",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
7
- "dist",
8
- "src",
9
- "*.d.ts"
7
+ "src"
10
8
  ],
11
9
  "type": "module",
12
- "types": "dist/index.d.ts",
10
+ "types": "src/index.d.ts",
13
11
  "exports": {
14
12
  ".": {
15
- "types": "./dist/index.d.ts",
16
- "import": "./dist/index.js"
13
+ "types": "./src/index.d.ts",
14
+ "import": "./src/index.js"
17
15
  },
18
- "./package.json": "./package.json",
19
- "./src/ui/*": "./src/ui/*"
16
+ "./package.json": "./package.json"
20
17
  },
21
18
  "engines": {
22
19
  "node": "^14.18.0 || >= 16"
@@ -42,7 +39,8 @@
42
39
  "kleur": "^4.1.5",
43
40
  "magic-string": "^0.30.0",
44
41
  "svelte-hmr": "^0.15.1",
45
- "vitefu": "^0.2.4"
42
+ "vitefu": "^0.2.4",
43
+ "@sveltejs/vite-plugin-svelte-inspector": "^1.0.1"
46
44
  },
47
45
  "peerDependencies": {
48
46
  "svelte": "^3.54.0",
@@ -51,14 +49,11 @@
51
49
  "devDependencies": {
52
50
  "@types/debug": "^4.1.7",
53
51
  "esbuild": "^0.17.18",
54
- "rollup": "^3.21.2",
55
- "svelte": "^3.58.0",
56
- "tsup": "^6.7.0",
57
- "vite": "^4.3.3"
52
+ "svelte": "^3.59.1",
53
+ "vite": "^4.3.5"
58
54
  },
59
55
  "scripts": {
60
- "dev": "pnpm build:ci --sourcemap --watch src",
61
- "build:ci": "rimraf dist && tsup-node src/index.ts --format esm",
62
- "build": "pnpm build:ci --dts --sourcemap"
56
+ "check:publint": "publint --strict",
57
+ "check:types": "tsc --noEmit"
63
58
  }
64
59
  }
@@ -1,21 +1,17 @@
1
- import { ModuleNode, HmrContext } from 'vite';
2
- import { Code, CompileData } from './utils/compile';
3
- import { log, logCompilerWarnings } from './utils/log';
4
- import { SvelteRequest } from './utils/id';
5
- import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
6
- import { ResolvedOptions } from './utils/options';
7
- import { toRollupError } from './utils/error';
1
+ import { log, logCompilerWarnings } from './utils/log.js';
2
+ import { toRollupError } from './utils/error.js';
8
3
 
9
4
  /**
10
5
  * Vite-specific HMR handling
6
+ *
7
+ * @param {Function} compileSvelte
8
+ * @param {import('vite').HmrContext} ctx
9
+ * @param {import('./types/id.d.ts').SvelteRequest} svelteRequest
10
+ * @param {import('./utils/vite-plugin-svelte-cache').VitePluginSvelteCache} cache
11
+ * @param {import('./types/options.d.ts').ResolvedOptions} options
12
+ * @returns {Promise<import('vite').ModuleNode[] | void>}
11
13
  */
12
- export async function handleHotUpdate(
13
- compileSvelte: Function,
14
- ctx: HmrContext,
15
- svelteRequest: SvelteRequest,
16
- cache: VitePluginSvelteCache,
17
- options: ResolvedOptions
18
- ): Promise<ModuleNode[] | void> {
14
+ export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options) {
19
15
  if (!cache.has(svelteRequest)) {
20
16
  // file hasn't been requested yet (e.g. async component)
21
17
  log.debug(`handleHotUpdate called before initial transform for ${svelteRequest.id}`);
@@ -27,7 +23,8 @@ export async function handleHotUpdate(
27
23
  const cachedCss = cache.getCSS(svelteRequest);
28
24
 
29
25
  const content = await read();
30
- let compileData: CompileData;
26
+ /** @type {import('./types/compile.d.ts').CompileData} */
27
+ let compileData;
31
28
  try {
32
29
  compileData = await compileSvelte(svelteRequest, content, options);
33
30
  cache.update(compileData);
@@ -73,11 +70,22 @@ export async function handleHotUpdate(
73
70
  return affectedModules;
74
71
  }
75
72
 
76
- function cssChanged(prev?: Code, next?: Code): boolean {
73
+ /**
74
+ * @param {import('./types/compile.d.ts').Code} [prev]
75
+ * @param {import('./types/compile.d.ts').Code} [next]
76
+ * @returns {boolean}
77
+ */
78
+ function cssChanged(prev, next) {
77
79
  return !isCodeEqual(prev?.code, next?.code);
78
80
  }
79
81
 
80
- function jsChanged(prev?: Code, next?: Code, filename?: string): boolean {
82
+ /**
83
+ * @param {import('./types/compile.d.ts').Code} [prev]
84
+ * @param {import('./types/compile.d.ts').Code} [next]
85
+ * @param {string} [filename]
86
+ * @returns {boolean}
87
+ */
88
+ function jsChanged(prev, next, filename) {
81
89
  const prevJs = prev?.code;
82
90
  const nextJs = next?.code;
83
91
  const isStrictEqual = isCodeEqual(prevJs, nextJs);
@@ -93,7 +101,12 @@ function jsChanged(prev?: Code, next?: Code, filename?: string): boolean {
93
101
  return !isLooseEqual;
94
102
  }
95
103
 
96
- function isCodeEqual(prev?: string, next?: string): boolean {
104
+ /**
105
+ * @param {string} [prev]
106
+ * @param {string} [next]
107
+ * @returns {boolean}
108
+ */
109
+ function isCodeEqual(prev, next) {
97
110
  if (!prev && !next) {
98
111
  return true;
99
112
  }
@@ -108,9 +121,11 @@ function isCodeEqual(prev?: string, next?: string): boolean {
108
121
  *
109
122
  * 1) add_location() calls. These add location metadata to elements, only used by some dev tools
110
123
  * 2) ... maybe more (or less) in the future
111
- * @param code
124
+ *
125
+ * @param {string} [code]
126
+ * @returns {string | undefined}
112
127
  */
113
- function normalizeJsCode(code?: string): string | undefined {
128
+ function normalizeJsCode(code) {
114
129
  if (!code) {
115
130
  return code;
116
131
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,215 @@
1
+ import type { InlineConfig, ResolvedConfig, UserConfig, Plugin } from 'vite';
2
+ import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
3
+ import type { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
4
+ import type { Options as InspectorOptions } from '@sveltejs/vite-plugin-svelte-inspector';
5
+
6
+ type Options = Omit<SvelteOptions, 'vitePlugin'> & PluginOptionsInline;
7
+
8
+ interface PluginOptionsInline extends PluginOptions {
9
+ /**
10
+ * Path to a svelte config file, either absolute or relative to Vite root
11
+ *
12
+ * set to `false` to ignore the svelte config file
13
+ *
14
+ * @see https://vitejs.dev/config/#root
15
+ */
16
+ configFile?: string | false;
17
+ }
18
+
19
+ interface PluginOptions {
20
+ /**
21
+ * A `picomatch` pattern, or array of patterns, which specifies the files the plugin should
22
+ * operate on. By default, all svelte files are included.
23
+ *
24
+ * @see https://github.com/micromatch/picomatch
25
+ */
26
+ include?: Arrayable<string>;
27
+ /**
28
+ * A `picomatch` pattern, or array of patterns, which specifies the files to be ignored by the
29
+ * plugin. By default, no files are ignored.
30
+ *
31
+ * @see https://github.com/micromatch/picomatch
32
+ */
33
+ exclude?: Arrayable<string>;
34
+ /**
35
+ * Emit Svelte styles as virtual CSS files for Vite and other plugins to process
36
+ *
37
+ * @default true
38
+ */
39
+ emitCss?: boolean;
40
+ /**
41
+ * Enable or disable Hot Module Replacement.
42
+ *
43
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
44
+ *
45
+ * DO NOT CUSTOMIZE SVELTE-HMR OPTIONS UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING
46
+ *
47
+ * YOU HAVE BEEN WARNED
48
+ *
49
+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50
+ *
51
+ * Set an object to pass custom options to svelte-hmr
52
+ *
53
+ * @see https://github.com/rixo/svelte-hmr#options
54
+ * @default true for development, always false for production
55
+ */
56
+ hot?:
57
+ | boolean
58
+ | {
59
+ injectCss?: boolean;
60
+ partialAccept?: boolean;
61
+ [key: string]: any;
62
+ };
63
+ /**
64
+ * Some Vite plugins can contribute additional preprocessors by defining `api.sveltePreprocess`.
65
+ * If you don't want to use them, set this to true to ignore them all or use an array of strings
66
+ * with plugin names to specify which.
67
+ *
68
+ * @default false
69
+ */
70
+ ignorePluginPreprocessors?: boolean | string[];
71
+ /**
72
+ * vite-plugin-svelte automatically handles excluding svelte libraries and reinclusion of their dependencies
73
+ * in vite.optimizeDeps.
74
+ *
75
+ * `disableDependencyReinclusion: true` disables all reinclusions
76
+ * `disableDependencyReinclusion: ['foo','bar']` disables reinclusions for dependencies of foo and bar
77
+ *
78
+ * This should be used for hybrid packages that contain both node and browser dependencies, eg Routify
79
+ *
80
+ * @default false
81
+ */
82
+ disableDependencyReinclusion?: boolean | string[];
83
+ /**
84
+ * Enable support for Vite's dependency optimization to prebundle Svelte libraries.
85
+ *
86
+ * To disable prebundling for a specific library, add it to `optimizeDeps.exclude`.
87
+ *
88
+ * @default true for dev, false for build
89
+ */
90
+ prebundleSvelteLibraries?: boolean;
91
+ /**
92
+ * toggle/configure Svelte Inspector
93
+ *
94
+ * @default unset for dev, always false for build
95
+ */
96
+ inspector?: InspectorOptions | boolean;
97
+ /**
98
+ * These options are considered experimental and breaking changes to them can occur in any release
99
+ */
100
+ experimental?: ExperimentalOptions;
101
+ }
102
+
103
+ interface SvelteOptions {
104
+ /**
105
+ * A list of file extensions to be compiled by Svelte
106
+ *
107
+ * @default ['.svelte']
108
+ */
109
+ extensions?: string[];
110
+ /**
111
+ * An array of preprocessors to transform the Svelte source code before compilation
112
+ *
113
+ * @see https://svelte.dev/docs#svelte_preprocess
114
+ */
115
+ preprocess?: Arrayable<PreprocessorGroup>;
116
+ /**
117
+ * The options to be passed to the Svelte compiler. A few options are set by default,
118
+ * including `dev` and `css`. However, some options are non-configurable, like
119
+ * `filename`, `format`, `generate`, and `cssHash` (in dev).
120
+ *
121
+ * @see https://svelte.dev/docs#svelte_compile
122
+ */
123
+ compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
124
+ /**
125
+ * Handles warning emitted from the Svelte compiler
126
+ */
127
+ onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
128
+ /**
129
+ * Options for vite-plugin-svelte
130
+ */
131
+ vitePlugin?: PluginOptions;
132
+ }
133
+
134
+ /**
135
+ * These options are considered experimental and breaking changes to them can occur in any release
136
+ */
137
+ interface ExperimentalOptions {
138
+ /**
139
+ * A function to update `compilerOptions` before compilation
140
+ *
141
+ * `data.filename` - The file to be compiled
142
+ * `data.code` - The preprocessed Svelte code
143
+ * `data.compileOptions` - The current compiler options
144
+ *
145
+ * To change part of the compiler options, return an object with the changes you need.
146
+ *
147
+ * @example
148
+ * ```
149
+ * ({ filename, compileOptions }) => {
150
+ * // Dynamically set hydration per Svelte file
151
+ * if (compileWithHydratable(filename) && !compileOptions.hydratable) {
152
+ * return { hydratable: true };
153
+ * }
154
+ * }
155
+ * ```
156
+ */
157
+ dynamicCompileOptions?: (data: {
158
+ filename: string;
159
+ code: string;
160
+ compileOptions: Partial<CompileOptions>;
161
+ }) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
162
+ /**
163
+ * send a websocket message with svelte compiler warnings during dev
164
+ *
165
+ */
166
+ sendWarningsToBrowser?: boolean;
167
+ /**
168
+ * disable svelte field resolve warnings
169
+ *
170
+ * @default false
171
+ */
172
+ disableSvelteResolveWarnings?: boolean;
173
+ }
174
+
175
+ type ModuleFormat = NonNullable<CompileOptions['format']>;
176
+ type CssHashGetter = NonNullable<CompileOptions['cssHash']>;
177
+ type Arrayable<T> = T | T[];
178
+
179
+ interface VitePreprocessOptions {
180
+ script?: boolean;
181
+ style?: boolean | InlineConfig | ResolvedConfig;
182
+ }
183
+
184
+ declare function vitePreprocess(opts?: VitePreprocessOptions): PreprocessorGroup;
185
+
186
+ declare function loadSvelteConfig(
187
+ viteConfig?: UserConfig,
188
+ inlineOptions?: Partial<Options>
189
+ ): Promise<Partial<SvelteOptions> | undefined>;
190
+
191
+ declare function svelte(inlineOptions?: Partial<Options>): Plugin[];
192
+
193
+ export {
194
+ Arrayable,
195
+ CssHashGetter,
196
+ ModuleFormat,
197
+ Options,
198
+ PluginOptions,
199
+ SvelteOptions,
200
+ loadSvelteConfig,
201
+ svelte,
202
+ VitePreprocessOptions,
203
+ vitePreprocess
204
+ };
205
+
206
+ // reexported types
207
+
208
+ export { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
209
+
210
+ export {
211
+ MarkupPreprocessor,
212
+ Preprocessor,
213
+ PreprocessorGroup,
214
+ Processed
215
+ } from 'svelte/types/compiler/preprocess';
@@ -1,76 +1,67 @@
1
1
  import fs from 'fs';
2
2
  import { VERSION as svelteVersion } from 'svelte/compiler';
3
- import {
4
- HmrContext,
5
- ModuleNode,
6
- Plugin,
7
- ResolvedConfig,
8
- UserConfig,
9
- version as viteVersion
10
- } from 'vite';
11
- // eslint-disable-next-line node/no-missing-import
3
+ import { version as viteVersion } from 'vite';
4
+
5
+ import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
6
+
12
7
  import { isDepExcluded } from 'vitefu';
13
- import { handleHotUpdate } from './handle-hot-update';
14
- import { log, logCompilerWarnings } from './utils/log';
15
- import { type CompileSvelte, createCompileSvelte } from './utils/compile';
16
- import { buildIdParser, IdParser } from './utils/id';
8
+ import { handleHotUpdate } from './handle-hot-update.js';
9
+ import { log, logCompilerWarnings } from './utils/log.js';
10
+ import { createCompileSvelte } from './utils/compile.js';
11
+ import { buildIdParser } from './utils/id.js';
17
12
  import {
18
13
  buildExtraViteConfig,
19
14
  validateInlineOptions,
20
- Options,
21
- ResolvedOptions,
22
15
  resolveOptions,
23
16
  patchResolvedViteConfig,
24
17
  preResolveOptions
25
- } from './utils/options';
18
+ } from './utils/options.js';
26
19
 
27
- import { ensureWatchedFile, setupWatchers } from './utils/watch';
28
- import { resolveViaPackageJsonSvelte } from './utils/resolve';
29
- import { PartialResolvedId } from 'rollup';
30
- import { toRollupError } from './utils/error';
31
- import { saveSvelteMetadata } from './utils/optimizer';
32
- import { svelteInspector } from './ui/inspector/plugin';
33
- import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
34
- import { loadRaw } from './utils/load-raw';
35
- import { FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE } from './utils/constants';
20
+ import { ensureWatchedFile, setupWatchers } from './utils/watch.js';
21
+ import { resolveViaPackageJsonSvelte } from './utils/resolve.js';
36
22
 
37
- interface PluginAPI {
38
- /**
39
- * must not be modified, should not be used outside of vite-plugin-svelte repo
40
- * @internal
41
- * @experimental
42
- */
43
- options?: ResolvedOptions;
44
- // TODO expose compile cache here so other utility plugins can use it
45
- }
23
+ import { toRollupError } from './utils/error.js';
24
+ import { saveSvelteMetadata } from './utils/optimizer.js';
25
+ import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js';
26
+ import { loadRaw } from './utils/load-raw.js';
27
+ import { FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE } from './utils/constants.js';
46
28
 
47
29
  const isVite4_0 = viteVersion.startsWith('4.0');
48
30
  const isSvelte3 = svelteVersion.startsWith('3');
49
31
 
50
- export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
32
+ /** @type {import('./index.d.ts').svelte} */
33
+ export function svelte(inlineOptions) {
51
34
  if (process.env.DEBUG != null) {
52
35
  log.setLevel('debug');
53
36
  }
54
37
  validateInlineOptions(inlineOptions);
55
38
  const cache = new VitePluginSvelteCache();
56
39
  // updated in configResolved hook
57
- let requestParser: IdParser;
58
- let options: ResolvedOptions;
59
- let viteConfig: ResolvedConfig;
60
- /* eslint-disable no-unused-vars */
61
- let compileSvelte: CompileSvelte;
40
+ /** @type {import('./types/id.d.ts').IdParser} */
41
+ let requestParser;
42
+ /** @type {import('./types/options.d.ts').ResolvedOptions} */
43
+ let options;
44
+ /** @type {import('vite').ResolvedConfig} */
45
+ let viteConfig;
46
+
47
+ /** @type {import('./types/compile.d.ts').CompileSvelte} */
48
+ let compileSvelte;
62
49
  /* eslint-enable no-unused-vars */
63
50
 
64
- let resolvedSvelteSSR: Promise<PartialResolvedId | null>;
65
- let packagesWithResolveWarnings: Set<string>;
66
- const api: PluginAPI = {};
67
- const plugins: Plugin[] = [
51
+ /** @type {Promise<import('vite').Rollup.PartialResolvedId | null>} */
52
+ let resolvedSvelteSSR;
53
+ /** @type {Set<string>} */
54
+ let packagesWithResolveWarnings;
55
+ /** @type {import('./types/plugin-api.d.ts').PluginAPI} */
56
+ const api = {};
57
+ /** @type {import('vite').Plugin[]} */
58
+ const plugins = [
68
59
  {
69
60
  name: 'vite-plugin-svelte',
70
61
  // make sure our resolver runs before vite internal resolver to resolve svelte field correctly
71
62
  enforce: 'pre',
72
63
  api,
73
- async config(config, configEnv): Promise<Partial<UserConfig>> {
64
+ async config(config, configEnv) {
74
65
  // setup logger
75
66
  if (process.env.DEBUG) {
76
67
  log.setLevel('debug');
@@ -97,7 +88,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
97
88
  },
98
89
 
99
90
  async buildStart() {
100
- packagesWithResolveWarnings = new Set<string>();
91
+ packagesWithResolveWarnings = new Set();
101
92
  if (!options.prebundleSvelteLibraries) return;
102
93
  const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
103
94
  if (isSvelteMetadataChanged) {
@@ -108,7 +99,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
108
99
  },
109
100
 
110
101
  configureServer(server) {
111
- // eslint-disable-next-line no-unused-vars
112
102
  options.server = server;
113
103
  setupWatchers(options, cache, requestParser);
114
104
  },
@@ -235,10 +225,16 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
235
225
  }
236
226
  logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
237
227
  cache.update(compileData);
238
- if (compileData.dependencies?.length && options.server) {
239
- compileData.dependencies.forEach((d) => {
240
- ensureWatchedFile(options.server!.watcher, d, options.root);
241
- });
228
+ if (compileData.dependencies?.length) {
229
+ if (options.server) {
230
+ for (const dep of compileData.dependencies) {
231
+ ensureWatchedFile(options.server.watcher, dep, options.root);
232
+ }
233
+ } else if (options.isBuild && viteConfig.build.watch) {
234
+ for (const dep of compileData.dependencies) {
235
+ this.addWatchFile(dep);
236
+ }
237
+ }
242
238
  }
243
239
  log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
244
240
  return {
@@ -251,7 +247,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
251
247
  };
252
248
  },
253
249
 
254
- handleHotUpdate(ctx: HmrContext): void | Promise<Array<ModuleNode> | void> {
250
+ handleHotUpdate(ctx) {
255
251
  if (!options.hot || !options.emitCss) {
256
252
  return;
257
253
  }
@@ -279,22 +275,5 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
279
275
  return plugins;
280
276
  }
281
277
 
282
- export { vitePreprocess } from './preprocess';
283
- export { loadSvelteConfig } from './utils/load-svelte-config';
284
-
285
- export {
286
- Options,
287
- PluginOptions,
288
- SvelteOptions,
289
- Preprocessor,
290
- PreprocessorGroup,
291
- CompileOptions,
292
- CssHashGetter,
293
- Arrayable,
294
- MarkupPreprocessor,
295
- ModuleFormat,
296
- Processed,
297
- Warning
298
- } from './utils/options';
299
-
300
- export { SvelteWarningsMessage } from './utils/log';
278
+ export { vitePreprocess } from './preprocess.js';
279
+ export { loadSvelteConfig } from './utils/load-svelte-config.js';
@@ -1,19 +1,19 @@
1
1
  import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
2
- import type { ESBuildOptions, InlineConfig, ResolvedConfig } from 'vite';
3
- // eslint-disable-next-line node/no-missing-import
4
- import type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/preprocess';
5
- import { mapToRelative, removeLangSuffix } from './utils/sourcemaps';
2
+ import { mapToRelative, removeLangSuffix } from './utils/sourcemaps.js';
3
+
4
+ /**
5
+ * @typedef {(code: string, filename: string) => Promise<{ code: string; map?: any; deps?: Set<string> }>} CssTransform
6
+ */
6
7
 
7
8
  const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];
8
9
  const supportedScriptLangs = ['ts'];
9
10
 
10
11
  export const lang_sep = '.vite-preprocess.';
11
12
 
12
- export function vitePreprocess(opts?: {
13
- script?: boolean;
14
- style?: boolean | InlineConfig | ResolvedConfig;
15
- }) {
16
- const preprocessor: PreprocessorGroup = {};
13
+ /** @type {import('./index.d.ts').vitePreprocess} */
14
+ export function vitePreprocess(opts) {
15
+ /** @type {import('svelte/types/compiler/preprocess').PreprocessorGroup} */
16
+ const preprocessor = {};
17
17
  if (opts?.script !== false) {
18
18
  preprocessor.script = viteScript().script;
19
19
  }
@@ -24,13 +24,16 @@ export function vitePreprocess(opts?: {
24
24
  return preprocessor;
25
25
  }
26
26
 
27
- function viteScript(): { script: Preprocessor } {
27
+ /**
28
+ * @returns {{ script: import('svelte/types/compiler/preprocess').Preprocessor }}
29
+ */
30
+ function viteScript() {
28
31
  return {
29
32
  async script({ attributes, content, filename = '' }) {
30
- const lang = attributes.lang as string;
33
+ const lang = /** @type {string} */ (attributes.lang);
31
34
  if (!supportedScriptLangs.includes(lang)) return;
32
35
  const { code, map } = await transformWithEsbuild(content, filename, {
33
- loader: lang as ESBuildOptions['loader'],
36
+ loader: /** @type {import('vite').ESBuildOptions['loader']} */ (lang),
34
37
  target: 'esnext',
35
38
  tsconfigRaw: {
36
39
  compilerOptions: {
@@ -51,15 +54,20 @@ function viteScript(): { script: Preprocessor } {
51
54
  };
52
55
  }
53
56
 
54
- function viteStyle(config: InlineConfig | ResolvedConfig = {}): {
55
- style: Preprocessor;
56
- } {
57
- let transform: CssTransform;
58
- const style: Preprocessor = async ({ attributes, content, filename = '' }) => {
59
- const lang = attributes.lang as string;
57
+ /**
58
+ * @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config
59
+ * @returns {{ style: import('svelte/types/compiler/preprocess').Preprocessor }}
60
+ */
61
+ function viteStyle(config = {}) {
62
+ /** @type {CssTransform} */
63
+ let transform;
64
+ /** @type {import('svelte/types/compiler/preprocess').Preprocessor} */
65
+ const style = async ({ attributes, content, filename = '' }) => {
66
+ const lang = /** @type {string} */ (attributes.lang);
60
67
  if (!supportedStyleLangs.includes(lang)) return;
61
68
  if (!transform) {
62
- let resolvedConfig: ResolvedConfig;
69
+ /** @type {import('vite').ResolvedConfig} */
70
+ let resolvedConfig;
63
71
  // @ts-expect-error special prop added if running in v-p-s
64
72
  if (style.__resolvedConfig) {
65
73
  // @ts-expect-error
@@ -91,19 +99,20 @@ function viteStyle(config: InlineConfig | ResolvedConfig = {}): {
91
99
  return { style };
92
100
  }
93
101
 
94
- type CssTransform = (
95
- // eslint-disable-next-line no-unused-vars
96
- code: string,
97
- // eslint-disable-next-line no-unused-vars
98
- filename: string
99
- ) => Promise<{ code: string; map?: any; deps?: Set<string> }>;
100
-
101
- function getCssTransformFn(config: ResolvedConfig): CssTransform {
102
+ /**
103
+ * @param {import('vite').ResolvedConfig} config
104
+ * @returns {CssTransform}
105
+ */
106
+ function getCssTransformFn(config) {
102
107
  return async (code, filename) => {
103
108
  return preprocessCSS(code, filename, config);
104
109
  };
105
110
  }
106
111
 
107
- function isResolvedConfig(config: any): config is ResolvedConfig {
112
+ /**
113
+ * @param {any} config
114
+ * @returns {config is import('vite').ResolvedConfig}
115
+ */
116
+ function isResolvedConfig(config) {
108
117
  return !!config.inlineConfig;
109
118
  }