@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
package/package.json CHANGED
@@ -1,19 +1,17 @@
1
1
  {
2
2
  "name": "@sveltejs/vite-plugin-svelte",
3
- "version": "2.3.0",
3
+ "version": "2.4.1",
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
16
  "./package.json": "./package.json"
19
17
  },
@@ -42,23 +40,20 @@
42
40
  "magic-string": "^0.30.0",
43
41
  "svelte-hmr": "^0.15.1",
44
42
  "vitefu": "^0.2.4",
45
- "@sveltejs/vite-plugin-svelte-inspector": "^1.0.1"
43
+ "@sveltejs/vite-plugin-svelte-inspector": "^1.0.2"
46
44
  },
47
45
  "peerDependencies": {
48
- "svelte": "^3.54.0",
46
+ "svelte": "^3.54.0 || ^4.0.0-next.0",
49
47
  "vite": "^4.0.0"
50
48
  },
51
49
  "devDependencies": {
52
50
  "@types/debug": "^4.1.7",
53
51
  "esbuild": "^0.17.18",
54
- "rollup": "^3.21.6",
55
52
  "svelte": "^3.59.1",
56
- "tsup": "^6.7.0",
57
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<'esm'>;
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,77 +1,66 @@
1
1
  import fs from 'fs';
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
2
+ import { version as viteVersion } from 'vite';
3
+
12
4
  import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
13
- // eslint-disable-next-line node/no-missing-import
5
+
14
6
  import { isDepExcluded } from 'vitefu';
15
- import { handleHotUpdate } from './handle-hot-update';
16
- import { log, logCompilerWarnings } from './utils/log';
17
- import { type CompileSvelte, createCompileSvelte } from './utils/compile';
18
- import { buildIdParser, IdParser } from './utils/id';
7
+ import { handleHotUpdate } from './handle-hot-update.js';
8
+ import { log, logCompilerWarnings } from './utils/log.js';
9
+ import { createCompileSvelte } from './utils/compile.js';
10
+ import { buildIdParser } from './utils/id.js';
19
11
  import {
20
12
  buildExtraViteConfig,
21
13
  validateInlineOptions,
22
- Options,
23
- ResolvedOptions,
24
14
  resolveOptions,
25
15
  patchResolvedViteConfig,
26
16
  preResolveOptions
27
- } from './utils/options';
17
+ } from './utils/options.js';
28
18
 
29
- import { ensureWatchedFile, setupWatchers } from './utils/watch';
30
- import { resolveViaPackageJsonSvelte } from './utils/resolve';
31
- import { PartialResolvedId } from 'rollup';
32
- import { toRollupError } from './utils/error';
33
- import { saveSvelteMetadata } from './utils/optimizer';
34
- import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
35
- import { loadRaw } from './utils/load-raw';
36
- import { FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE } from './utils/constants';
19
+ import { ensureWatchedFile, setupWatchers } from './utils/watch.js';
20
+ import { resolveViaPackageJsonSvelte } from './utils/resolve.js';
37
21
 
38
- interface PluginAPI {
39
- /**
40
- * must not be modified, should not be used outside of vite-plugin-svelte repo
41
- * @internal
42
- * @experimental
43
- */
44
- options?: ResolvedOptions;
45
- // TODO expose compile cache here so other utility plugins can use it
46
- }
22
+ import { toRollupError } from './utils/error.js';
23
+ import { saveSvelteMetadata } from './utils/optimizer.js';
24
+ import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js';
25
+ import { loadRaw } from './utils/load-raw.js';
26
+ import { FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE } from './utils/constants.js';
27
+ import { isSvelte3 } from './utils/svelte-version.js';
47
28
 
48
29
  const isVite4_0 = viteVersion.startsWith('4.0');
49
- const isSvelte3 = svelteVersion.startsWith('3');
50
30
 
51
- export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
31
+ /** @type {import('./index.d.ts').svelte} */
32
+ export function svelte(inlineOptions) {
52
33
  if (process.env.DEBUG != null) {
53
34
  log.setLevel('debug');
54
35
  }
55
36
  validateInlineOptions(inlineOptions);
56
37
  const cache = new VitePluginSvelteCache();
57
38
  // updated in configResolved hook
58
- let requestParser: IdParser;
59
- let options: ResolvedOptions;
60
- let viteConfig: ResolvedConfig;
61
- /* eslint-disable no-unused-vars */
62
- let compileSvelte: CompileSvelte;
39
+ /** @type {import('./types/id.d.ts').IdParser} */
40
+ let requestParser;
41
+ /** @type {import('./types/options.d.ts').ResolvedOptions} */
42
+ let options;
43
+ /** @type {import('vite').ResolvedConfig} */
44
+ let viteConfig;
45
+
46
+ /** @type {import('./types/compile.d.ts').CompileSvelte} */
47
+ let compileSvelte;
63
48
  /* eslint-enable no-unused-vars */
64
49
 
65
- let resolvedSvelteSSR: Promise<PartialResolvedId | null>;
66
- let packagesWithResolveWarnings: Set<string>;
67
- const api: PluginAPI = {};
68
- const plugins: Plugin[] = [
50
+ /** @type {Promise<import('vite').Rollup.PartialResolvedId | null>} */
51
+ let resolvedSvelteSSR;
52
+ /** @type {Set<string>} */
53
+ let packagesWithResolveWarnings;
54
+ /** @type {import('./types/plugin-api.d.ts').PluginAPI} */
55
+ const api = {};
56
+ /** @type {import('vite').Plugin[]} */
57
+ const plugins = [
69
58
  {
70
59
  name: 'vite-plugin-svelte',
71
60
  // make sure our resolver runs before vite internal resolver to resolve svelte field correctly
72
61
  enforce: 'pre',
73
62
  api,
74
- async config(config, configEnv): Promise<Partial<UserConfig>> {
63
+ async config(config, configEnv) {
75
64
  // setup logger
76
65
  if (process.env.DEBUG) {
77
66
  log.setLevel('debug');
@@ -98,7 +87,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
98
87
  },
99
88
 
100
89
  async buildStart() {
101
- packagesWithResolveWarnings = new Set<string>();
90
+ packagesWithResolveWarnings = new Set();
102
91
  if (!options.prebundleSvelteLibraries) return;
103
92
  const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
104
93
  if (isSvelteMetadataChanged) {
@@ -109,7 +98,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
109
98
  },
110
99
 
111
100
  configureServer(server) {
112
- // eslint-disable-next-line no-unused-vars
113
101
  options.server = server;
114
102
  setupWatchers(options, cache, requestParser);
115
103
  },
@@ -258,7 +246,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
258
246
  };
259
247
  },
260
248
 
261
- handleHotUpdate(ctx: HmrContext): void | Promise<Array<ModuleNode> | void> {
249
+ handleHotUpdate(ctx) {
262
250
  if (!options.hot || !options.emitCss) {
263
251
  return;
264
252
  }
@@ -286,22 +274,5 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
286
274
  return plugins;
287
275
  }
288
276
 
289
- export { vitePreprocess } from './preprocess';
290
- export { loadSvelteConfig } from './utils/load-svelte-config';
291
-
292
- export {
293
- Options,
294
- PluginOptions,
295
- SvelteOptions,
296
- Preprocessor,
297
- PreprocessorGroup,
298
- CompileOptions,
299
- CssHashGetter,
300
- Arrayable,
301
- MarkupPreprocessor,
302
- ModuleFormat,
303
- Processed,
304
- Warning
305
- } from './utils/options';
306
-
307
- export { SvelteWarningsMessage } from './utils/log';
277
+ export { vitePreprocess } from './preprocess.js';
278
+ 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
  }
@@ -0,0 +1,48 @@
1
+ import type { Processed } from 'svelte/types/compiler/preprocess';
2
+ import type { SvelteRequest } from './id.d.ts';
3
+ import type { ResolvedOptions } from './options.d.ts';
4
+
5
+ export type CompileSvelte = (
6
+ svelteRequest: SvelteRequest,
7
+ code: string,
8
+ options: Partial<ResolvedOptions>
9
+ ) => Promise<CompileData>;
10
+
11
+ export interface Code {
12
+ code: string;
13
+ map?: any;
14
+ dependencies?: any[];
15
+ }
16
+
17
+ export interface Compiled {
18
+ js: Code;
19
+ css: Code;
20
+ ast: any; // TODO type
21
+ warnings: any[]; // TODO type
22
+ vars: {
23
+ name: string;
24
+ export_name: string;
25
+ injected: boolean;
26
+ module: boolean;
27
+ mutated: boolean;
28
+ reassigned: boolean;
29
+ referenced: boolean;
30
+ writable: boolean;
31
+ referenced_from_script: boolean;
32
+ }[];
33
+ stats: {
34
+ timings: {
35
+ total: number;
36
+ };
37
+ };
38
+ }
39
+
40
+ export interface CompileData {
41
+ filename: string;
42
+ normalizedFilename: string;
43
+ lang: string;
44
+ compiled: Compiled;
45
+ ssr: boolean | undefined;
46
+ dependencies: string[];
47
+ preprocessed: Processed;
48
+ }