@sveltejs/vite-plugin-svelte 2.3.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.
- package/package.json +7 -12
- package/src/{handle-hot-update.ts → handle-hot-update.js} +35 -20
- package/src/index.d.ts +215 -0
- package/src/{index.ts → index.js} +39 -67
- package/src/{preprocess.ts → preprocess.js} +37 -28
- package/src/types/compile.d.ts +48 -0
- package/src/types/id.d.ts +31 -0
- package/src/types/log.d.ts +24 -0
- package/src/types/options.d.ts +20 -0
- package/src/types/plugin-api.d.ts +11 -0
- package/src/types/vite-plugin-svelte-stats.d.ts +30 -0
- package/src/utils/{compile.ts → compile.js} +32 -63
- package/src/utils/{dependencies.ts → dependencies.js} +14 -11
- package/src/utils/{error.ts → error.js} +21 -14
- package/src/utils/{esbuild.ts → esbuild.js} +23 -17
- package/src/utils/{hash.ts → hash.js} +14 -3
- package/src/utils/{id.ts → id.js} +59 -60
- package/src/utils/{load-raw.ts → load-raw.js} +16 -16
- package/src/utils/{load-svelte-config.ts → load-svelte-config.js} +12 -10
- package/src/utils/{log.ts → log.js} +81 -48
- package/src/utils/{optimizer.ts → optimizer.js} +15 -7
- package/src/utils/{options.ts → options.js} +146 -296
- package/src/utils/{preprocess.ts → preprocess.js} +28 -12
- package/src/utils/{resolve.ts → resolve.js} +18 -9
- package/src/utils/{sourcemaps.ts → sourcemaps.js} +22 -14
- package/src/utils/{svelte-version.ts → svelte-version.js} +15 -7
- package/src/utils/vite-plugin-svelte-cache.js +253 -0
- package/src/utils/{vite-plugin-svelte-stats.ts → vite-plugin-svelte-stats.js} +66 -62
- package/src/utils/{watch.ts → watch.js} +30 -22
- package/dist/index.d.ts +0 -193
- package/dist/index.js +0 -2272
- package/dist/index.js.map +0 -1
- package/src/__tests__/fixtures/preprocess/foo.scss +0 -3
- package/src/__tests__/preprocess.spec.ts +0 -51
- package/src/utils/__tests__/compile.spec.ts +0 -49
- package/src/utils/__tests__/sourcemaps.spec.ts +0 -79
- package/src/utils/__tests__/svelte-version.spec.ts +0 -102
- package/src/utils/vite-plugin-svelte-cache.ts +0 -182
- /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
|
+
"version": "2.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"src",
|
|
9
|
-
"*.d.ts"
|
|
7
|
+
"src"
|
|
10
8
|
],
|
|
11
9
|
"type": "module",
|
|
12
|
-
"types": "
|
|
10
|
+
"types": "src/index.d.ts",
|
|
13
11
|
"exports": {
|
|
14
12
|
".": {
|
|
15
|
-
"types": "./
|
|
16
|
-
"import": "./
|
|
13
|
+
"types": "./src/index.d.ts",
|
|
14
|
+
"import": "./src/index.js"
|
|
17
15
|
},
|
|
18
16
|
"./package.json": "./package.json"
|
|
19
17
|
},
|
|
@@ -51,14 +49,11 @@
|
|
|
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
|
-
"
|
|
61
|
-
"
|
|
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 {
|
|
2
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
124
|
+
*
|
|
125
|
+
* @param {string} [code]
|
|
126
|
+
* @returns {string | undefined}
|
|
112
127
|
*/
|
|
113
|
-
function normalizeJsCode(code
|
|
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,77 +1,67 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { VERSION as svelteVersion } from 'svelte/compiler';
|
|
3
|
-
import {
|
|
4
|
-
|
|
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
|
+
|
|
12
5
|
import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
|
|
13
|
-
|
|
6
|
+
|
|
14
7
|
import { isDepExcluded } from 'vitefu';
|
|
15
|
-
import { handleHotUpdate } from './handle-hot-update';
|
|
16
|
-
import { log, logCompilerWarnings } from './utils/log';
|
|
17
|
-
import {
|
|
18
|
-
import { buildIdParser
|
|
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';
|
|
19
12
|
import {
|
|
20
13
|
buildExtraViteConfig,
|
|
21
14
|
validateInlineOptions,
|
|
22
|
-
Options,
|
|
23
|
-
ResolvedOptions,
|
|
24
15
|
resolveOptions,
|
|
25
16
|
patchResolvedViteConfig,
|
|
26
17
|
preResolveOptions
|
|
27
|
-
} from './utils/options';
|
|
18
|
+
} from './utils/options.js';
|
|
28
19
|
|
|
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';
|
|
20
|
+
import { ensureWatchedFile, setupWatchers } from './utils/watch.js';
|
|
21
|
+
import { resolveViaPackageJsonSvelte } from './utils/resolve.js';
|
|
37
22
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
*/
|
|
44
|
-
options?: ResolvedOptions;
|
|
45
|
-
// TODO expose compile cache here so other utility plugins can use it
|
|
46
|
-
}
|
|
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';
|
|
47
28
|
|
|
48
29
|
const isVite4_0 = viteVersion.startsWith('4.0');
|
|
49
30
|
const isSvelte3 = svelteVersion.startsWith('3');
|
|
50
31
|
|
|
51
|
-
|
|
32
|
+
/** @type {import('./index.d.ts').svelte} */
|
|
33
|
+
export function svelte(inlineOptions) {
|
|
52
34
|
if (process.env.DEBUG != null) {
|
|
53
35
|
log.setLevel('debug');
|
|
54
36
|
}
|
|
55
37
|
validateInlineOptions(inlineOptions);
|
|
56
38
|
const cache = new VitePluginSvelteCache();
|
|
57
39
|
// updated in configResolved hook
|
|
58
|
-
|
|
59
|
-
let
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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;
|
|
63
49
|
/* eslint-enable no-unused-vars */
|
|
64
50
|
|
|
65
|
-
|
|
66
|
-
let
|
|
67
|
-
|
|
68
|
-
|
|
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 = [
|
|
69
59
|
{
|
|
70
60
|
name: 'vite-plugin-svelte',
|
|
71
61
|
// make sure our resolver runs before vite internal resolver to resolve svelte field correctly
|
|
72
62
|
enforce: 'pre',
|
|
73
63
|
api,
|
|
74
|
-
async config(config, configEnv)
|
|
64
|
+
async config(config, configEnv) {
|
|
75
65
|
// setup logger
|
|
76
66
|
if (process.env.DEBUG) {
|
|
77
67
|
log.setLevel('debug');
|
|
@@ -98,7 +88,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
98
88
|
},
|
|
99
89
|
|
|
100
90
|
async buildStart() {
|
|
101
|
-
packagesWithResolveWarnings = new Set
|
|
91
|
+
packagesWithResolveWarnings = new Set();
|
|
102
92
|
if (!options.prebundleSvelteLibraries) return;
|
|
103
93
|
const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
|
|
104
94
|
if (isSvelteMetadataChanged) {
|
|
@@ -109,7 +99,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
109
99
|
},
|
|
110
100
|
|
|
111
101
|
configureServer(server) {
|
|
112
|
-
// eslint-disable-next-line no-unused-vars
|
|
113
102
|
options.server = server;
|
|
114
103
|
setupWatchers(options, cache, requestParser);
|
|
115
104
|
},
|
|
@@ -258,7 +247,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
258
247
|
};
|
|
259
248
|
},
|
|
260
249
|
|
|
261
|
-
handleHotUpdate(ctx
|
|
250
|
+
handleHotUpdate(ctx) {
|
|
262
251
|
if (!options.hot || !options.emitCss) {
|
|
263
252
|
return;
|
|
264
253
|
}
|
|
@@ -286,22 +275,5 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
286
275
|
return plugins;
|
|
287
276
|
}
|
|
288
277
|
|
|
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';
|
|
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
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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
|
|
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:
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
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
|
+
}
|