@sveltejs/vite-plugin-svelte 3.1.0 → 4.0.0-next.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 +9 -10
- package/src/handle-hot-update.js +4 -4
- package/src/index.js +9 -22
- package/src/preprocess.js +1 -1
- package/src/public.d.ts +25 -20
- package/src/types/id.d.ts +2 -2
- package/src/types/log.d.ts +1 -1
- package/src/types/options.d.ts +1 -1
- package/src/utils/compile.js +19 -55
- package/src/utils/constants.js +3 -14
- package/src/utils/dependencies.js +0 -1
- package/src/utils/error.js +3 -32
- package/src/utils/esbuild.js +5 -9
- package/src/utils/load-raw.js +6 -14
- package/src/utils/log.js +10 -17
- package/src/utils/options.js +20 -78
- package/src/utils/preprocess.js +2 -2
- package/src/utils/svelte-version.js +0 -5
- package/src/utils/vite-plugin-svelte-cache.js +4 -4
- package/types/index.d.ts +25 -21
- package/types/index.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/vite-plugin-svelte",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-next.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"engines": {
|
|
21
|
-
"node": "^18.0.0 || >=
|
|
21
|
+
"node": "^18.0.0 || ^20.0.0 || >=22"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -36,24 +36,23 @@
|
|
|
36
36
|
},
|
|
37
37
|
"homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@sveltejs/vite-plugin-svelte-inspector": "^2.0.0",
|
|
40
39
|
"debug": "^4.3.4",
|
|
41
40
|
"deepmerge": "^4.3.1",
|
|
42
41
|
"kleur": "^4.1.5",
|
|
43
|
-
"magic-string": "^0.30.
|
|
44
|
-
"
|
|
45
|
-
"
|
|
42
|
+
"magic-string": "^0.30.10",
|
|
43
|
+
"vitefu": "^0.2.5",
|
|
44
|
+
"@sveltejs/vite-plugin-svelte-inspector": "^3.0.0-next.0"
|
|
46
45
|
},
|
|
47
46
|
"peerDependencies": {
|
|
48
|
-
"svelte": "^
|
|
47
|
+
"svelte": "^5.0.0-next.96 || ^5.0.0",
|
|
49
48
|
"vite": "^5.0.0"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
51
|
"@types/debug": "^4.1.12",
|
|
53
52
|
"esbuild": "^0.20.2",
|
|
54
|
-
"sass": "^1.
|
|
55
|
-
"svelte": "^
|
|
56
|
-
"vite": "^5.2.
|
|
53
|
+
"sass": "^1.76.0",
|
|
54
|
+
"svelte": "^5.0.0-next.123",
|
|
55
|
+
"vite": "^5.2.11"
|
|
57
56
|
},
|
|
58
57
|
"scripts": {
|
|
59
58
|
"check:publint": "publint --strict",
|
package/src/handle-hot-update.js
CHANGED
|
@@ -81,8 +81,8 @@ export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache,
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
|
-
* @param {import('./types/compile.d.ts').Code} [prev]
|
|
85
|
-
* @param {import('./types/compile.d.ts').Code} [next]
|
|
84
|
+
* @param {import('./types/compile.d.ts').Code | null} [prev]
|
|
85
|
+
* @param {import('./types/compile.d.ts').Code | null} [next]
|
|
86
86
|
* @returns {boolean}
|
|
87
87
|
*/
|
|
88
88
|
function cssChanged(prev, next) {
|
|
@@ -90,8 +90,8 @@ function cssChanged(prev, next) {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
* @param {import('./types/compile.d.ts').Code} [prev]
|
|
94
|
-
* @param {import('./types/compile.d.ts').Code} [next]
|
|
93
|
+
* @param {import('./types/compile.d.ts').Code | null} [prev]
|
|
94
|
+
* @param {import('./types/compile.d.ts').Code | null} [next]
|
|
95
95
|
* @param {string} [filename]
|
|
96
96
|
* @returns {boolean}
|
|
97
97
|
*/
|
package/src/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
-
|
|
3
2
|
import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
|
|
4
|
-
|
|
5
3
|
import { handleHotUpdate } from './handle-hot-update.js';
|
|
6
|
-
import { log, logCompilerWarnings
|
|
4
|
+
import { log, logCompilerWarnings } from './utils/log.js';
|
|
7
5
|
import { createCompileSvelte } from './utils/compile.js';
|
|
8
6
|
import { buildIdParser, buildModuleIdParser } from './utils/id.js';
|
|
9
7
|
import {
|
|
@@ -13,13 +11,11 @@ import {
|
|
|
13
11
|
patchResolvedViteConfig,
|
|
14
12
|
preResolveOptions
|
|
15
13
|
} from './utils/options.js';
|
|
16
|
-
|
|
17
14
|
import { ensureWatchedFile, setupWatchers } from './utils/watch.js';
|
|
18
15
|
import { toRollupError } from './utils/error.js';
|
|
19
16
|
import { saveSvelteMetadata } from './utils/optimizer.js';
|
|
20
17
|
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js';
|
|
21
18
|
import { loadRaw } from './utils/load-raw.js';
|
|
22
|
-
import { isSvelte5 } from './utils/svelte-version.js';
|
|
23
19
|
import * as svelteCompiler from 'svelte/compiler';
|
|
24
20
|
|
|
25
21
|
/**
|
|
@@ -72,7 +68,7 @@ export function svelte(inlineOptions) {
|
|
|
72
68
|
options = resolveOptions(options, config, cache);
|
|
73
69
|
patchResolvedViteConfig(config, options);
|
|
74
70
|
requestParser = buildIdParser(options);
|
|
75
|
-
compileSvelte = createCompileSvelte(
|
|
71
|
+
compileSvelte = createCompileSvelte();
|
|
76
72
|
viteConfig = config;
|
|
77
73
|
// TODO deep clone to avoid mutability from outside?
|
|
78
74
|
api.options = options;
|
|
@@ -178,8 +174,7 @@ export function svelte(inlineOptions) {
|
|
|
178
174
|
},
|
|
179
175
|
|
|
180
176
|
handleHotUpdate(ctx) {
|
|
181
|
-
|
|
182
|
-
if ((!options.hot && !options.compilerOptions.hmr) || !options.emitCss) {
|
|
177
|
+
if (!options.compilerOptions.hmr || !options.emitCss) {
|
|
183
178
|
return;
|
|
184
179
|
}
|
|
185
180
|
const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
|
|
@@ -190,12 +185,8 @@ export function svelte(inlineOptions) {
|
|
|
190
185
|
async buildEnd() {
|
|
191
186
|
await options.stats?.finishAll();
|
|
192
187
|
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (isSvelte5) {
|
|
196
|
-
logSvelte5Warning();
|
|
197
|
-
// TODO move to separate file
|
|
198
|
-
plugins.push({
|
|
188
|
+
},
|
|
189
|
+
{
|
|
199
190
|
name: 'vite-plugin-svelte-module',
|
|
200
191
|
enforce: 'post',
|
|
201
192
|
async configResolved() {
|
|
@@ -208,8 +199,7 @@ export function svelte(inlineOptions) {
|
|
|
208
199
|
return;
|
|
209
200
|
}
|
|
210
201
|
try {
|
|
211
|
-
|
|
212
|
-
const compileResult = await svelteCompiler.compileModule(code, {
|
|
202
|
+
const compileResult = svelteCompiler.compileModule(code, {
|
|
213
203
|
generate: ssr ? 'server' : 'client',
|
|
214
204
|
filename: moduleRequest.filename
|
|
215
205
|
});
|
|
@@ -219,12 +209,9 @@ export function svelte(inlineOptions) {
|
|
|
219
209
|
throw toRollupError(e, options);
|
|
220
210
|
}
|
|
221
211
|
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
// TODO reenable once svelte5 has support and update utils/log.js#logSvelte5Warning
|
|
226
|
-
plugins.push(svelteInspector());
|
|
227
|
-
}
|
|
212
|
+
},
|
|
213
|
+
svelteInspector()
|
|
214
|
+
];
|
|
228
215
|
return plugins;
|
|
229
216
|
}
|
|
230
217
|
|
package/src/preprocess.js
CHANGED
|
@@ -16,7 +16,7 @@ export const lang_sep = '.vite-preprocess';
|
|
|
16
16
|
export function vitePreprocess(opts) {
|
|
17
17
|
/** @type {import('svelte/compiler').PreprocessorGroup} */
|
|
18
18
|
const preprocessor = { name: 'vite-preprocess' };
|
|
19
|
-
if (opts?.script
|
|
19
|
+
if (opts?.script === true) {
|
|
20
20
|
preprocessor.script = viteScript().script;
|
|
21
21
|
}
|
|
22
22
|
if (opts?.style !== false) {
|
package/src/public.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { InlineConfig, ResolvedConfig } from 'vite';
|
|
2
2
|
import type { CompileOptions } from 'svelte/compiler';
|
|
3
|
-
import type { Warning } from 'svelte/
|
|
3
|
+
import type { Warning } from 'svelte/compiler';
|
|
4
4
|
import type { PreprocessorGroup } from 'svelte/compiler';
|
|
5
5
|
import type { Options as InspectorOptions } from '@sveltejs/vite-plugin-svelte-inspector';
|
|
6
6
|
|
|
@@ -40,27 +40,13 @@ interface PluginOptions {
|
|
|
40
40
|
emitCss?: boolean;
|
|
41
41
|
/**
|
|
42
42
|
* Enable or disable Hot Module Replacement.
|
|
43
|
+
* Deprecated, use compilerOptions.hmr instead!
|
|
43
44
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* DO NOT CUSTOMIZE SVELTE-HMR OPTIONS UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING
|
|
47
|
-
*
|
|
48
|
-
* YOU HAVE BEEN WARNED
|
|
49
|
-
*
|
|
50
|
-
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
51
|
-
*
|
|
52
|
-
* Set an object to pass custom options to svelte-hmr
|
|
53
|
-
*
|
|
54
|
-
* @see https://github.com/rixo/svelte-hmr#options
|
|
45
|
+
* @deprecated
|
|
55
46
|
* @default true for development, always false for production
|
|
56
47
|
*/
|
|
57
|
-
hot?:
|
|
58
|
-
|
|
59
|
-
| {
|
|
60
|
-
injectCss?: boolean;
|
|
61
|
-
partialAccept?: boolean;
|
|
62
|
-
[key: string]: any;
|
|
63
|
-
};
|
|
48
|
+
hot?: boolean;
|
|
49
|
+
|
|
64
50
|
/**
|
|
65
51
|
* Some Vite plugins can contribute additional preprocessors by defining `api.sveltePreprocess`.
|
|
66
52
|
* If you don't want to use them, set this to true to ignore them all or use an array of strings
|
|
@@ -151,8 +137,18 @@ export interface SvelteConfig {
|
|
|
151
137
|
|
|
152
138
|
/**
|
|
153
139
|
* Handles warning emitted from the Svelte compiler
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```
|
|
143
|
+
* (warning, defaultHandler) => {
|
|
144
|
+
* // ignore some warnings
|
|
145
|
+
* if (!['foo','bar'].includes(warning.code)) {
|
|
146
|
+
* defaultHandler(warning);
|
|
147
|
+
* }
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
154
150
|
*/
|
|
155
|
-
onwarn?: (warning: Warning, defaultHandler
|
|
151
|
+
onwarn?: (warning: Warning, defaultHandler: (warning: Warning) => void) => void;
|
|
156
152
|
/**
|
|
157
153
|
* Options for vite-plugin-svelte
|
|
158
154
|
*/
|
|
@@ -187,7 +183,16 @@ interface CompileModuleOptions {
|
|
|
187
183
|
type Arrayable<T> = T | T[];
|
|
188
184
|
|
|
189
185
|
export interface VitePreprocessOptions {
|
|
186
|
+
/**
|
|
187
|
+
* preprocess script block with vite pipeline.
|
|
188
|
+
* Since svelte5 this is not needed for typescript anymore
|
|
189
|
+
*
|
|
190
|
+
* @default false
|
|
191
|
+
*/
|
|
190
192
|
script?: boolean;
|
|
193
|
+
/**
|
|
194
|
+
* preprocess style blocks with vite pipeline
|
|
195
|
+
*/
|
|
191
196
|
style?: boolean | InlineConfig | ResolvedConfig;
|
|
192
197
|
}
|
|
193
198
|
|
package/src/types/id.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CompileOptions } from 'svelte/
|
|
1
|
+
import type { CompileOptions } from 'svelte/compiler';
|
|
2
2
|
|
|
3
3
|
export type SvelteQueryTypes = 'style' | 'script' | 'preprocessed' | 'all';
|
|
4
4
|
|
|
@@ -9,7 +9,7 @@ export interface RequestQuery {
|
|
|
9
9
|
sourcemap?: boolean;
|
|
10
10
|
compilerOptions?: Pick<
|
|
11
11
|
CompileOptions,
|
|
12
|
-
'generate' | 'dev' | 'css' | '
|
|
12
|
+
'generate' | 'dev' | 'css' | 'customElement' | 'immutable'
|
|
13
13
|
>;
|
|
14
14
|
// vite specific
|
|
15
15
|
url?: boolean;
|
package/src/types/log.d.ts
CHANGED
package/src/types/options.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CompileOptions } from 'svelte/
|
|
1
|
+
import type { CompileOptions } from 'svelte/compiler';
|
|
2
2
|
import type { ViteDevServer } from 'vite';
|
|
3
3
|
import { VitePluginSvelteStats } from '../utils/vite-plugin-svelte-stats.js';
|
|
4
4
|
import type { Options } from '../public.d.ts';
|
package/src/utils/compile.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as svelte from 'svelte/compiler';
|
|
2
|
-
|
|
3
|
-
import { createMakeHot } from 'svelte-hmr';
|
|
2
|
+
|
|
4
3
|
import { safeBase64Hash } from './hash.js';
|
|
5
4
|
import { log } from './log.js';
|
|
6
5
|
|
|
@@ -10,7 +9,6 @@ import {
|
|
|
10
9
|
} from './preprocess.js';
|
|
11
10
|
import { mapToRelative } from './sourcemaps.js';
|
|
12
11
|
import { enhanceCompileError } from './error.js';
|
|
13
|
-
import { isSvelte5 } from './svelte-version.js';
|
|
14
12
|
|
|
15
13
|
// TODO this is a patched version of https://github.com/sveltejs/vite-plugin-svelte/pull/796/files#diff-3bce0b33034aad4b35ca094893671f7e7ddf4d27254ae7b9b0f912027a001b15R10
|
|
16
14
|
// which is closer to the other regexes in at least not falling into commented script
|
|
@@ -19,10 +17,9 @@ const scriptLangRE =
|
|
|
19
17
|
/<!--[^]*?-->|<script (?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/g;
|
|
20
18
|
|
|
21
19
|
/**
|
|
22
|
-
* @param {Function} [makeHot]
|
|
23
20
|
* @returns {import('../types/compile.d.ts').CompileSvelte}
|
|
24
21
|
*/
|
|
25
|
-
export
|
|
22
|
+
export function createCompileSvelte() {
|
|
26
23
|
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined} */
|
|
27
24
|
let stats;
|
|
28
25
|
const devStylePreprocessor = createInjectScopeEverythingRulePreprocessorGroup();
|
|
@@ -32,7 +29,7 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
32
29
|
const { emitCss = true } = options;
|
|
33
30
|
/** @type {string[]} */
|
|
34
31
|
const dependencies = [];
|
|
35
|
-
/** @type {import('svelte/
|
|
32
|
+
/** @type {import('svelte/compiler').Warning[]} */
|
|
36
33
|
const warnings = [];
|
|
37
34
|
|
|
38
35
|
if (options.stats) {
|
|
@@ -63,18 +60,17 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
63
60
|
const compileOptions = {
|
|
64
61
|
...options.compilerOptions,
|
|
65
62
|
filename,
|
|
66
|
-
|
|
67
|
-
generate: isSvelte5 ? (ssr ? 'server' : 'client') : ssr ? 'ssr' : 'dom'
|
|
63
|
+
generate: ssr ? 'server' : 'client'
|
|
68
64
|
};
|
|
69
65
|
|
|
70
|
-
if (
|
|
66
|
+
if (compileOptions.hmr && options.emitCss) {
|
|
71
67
|
const hash = `s-${safeBase64Hash(normalizedFilename)}`;
|
|
72
68
|
compileOptions.cssHash = () => hash;
|
|
73
69
|
}
|
|
74
70
|
|
|
75
71
|
let preprocessed;
|
|
76
72
|
let preprocessors = options.preprocess;
|
|
77
|
-
if (!options.isBuild && options.emitCss &&
|
|
73
|
+
if (!options.isBuild && options.emitCss && compileOptions.hmr) {
|
|
78
74
|
// inject preprocessor that ensures css hmr works better
|
|
79
75
|
if (!Array.isArray(preprocessors)) {
|
|
80
76
|
preprocessors = preprocessors
|
|
@@ -134,10 +130,21 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
134
130
|
: compileOptions;
|
|
135
131
|
|
|
136
132
|
const endStat = stats?.start(filename);
|
|
137
|
-
/** @type {import('svelte/
|
|
133
|
+
/** @type {import('svelte/compiler').CompileResult} */
|
|
138
134
|
let compiled;
|
|
139
135
|
try {
|
|
140
136
|
compiled = svelte.compile(finalCode, finalCompileOptions);
|
|
137
|
+
// patch output with partial accept until svelte does it
|
|
138
|
+
// TODO remove later
|
|
139
|
+
if (
|
|
140
|
+
options.server?.config.experimental.hmrPartialAccept &&
|
|
141
|
+
compiled.js.code.includes('import.meta.hot.accept(')
|
|
142
|
+
) {
|
|
143
|
+
compiled.js.code = compiled.js.code.replaceAll(
|
|
144
|
+
'import.meta.hot.accept(',
|
|
145
|
+
'import.meta.hot.acceptExports(["default"],'
|
|
146
|
+
);
|
|
147
|
+
}
|
|
141
148
|
} catch (e) {
|
|
142
149
|
enhanceCompileError(e, code, preprocessors);
|
|
143
150
|
throw e;
|
|
@@ -156,25 +163,12 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
156
163
|
}
|
|
157
164
|
if (!raw) {
|
|
158
165
|
// wire css import and code for hmr
|
|
159
|
-
const hasCss = compiled.css?.code?.trim()
|
|
166
|
+
const hasCss = compiled.css?.code?.trim()?.length ?? 0 > 0;
|
|
160
167
|
// compiler might not emit css with mode none or it may be empty
|
|
161
168
|
if (emitCss && hasCss) {
|
|
162
169
|
// TODO properly update sourcemap?
|
|
163
170
|
compiled.js.code += `\nimport ${JSON.stringify(cssId)};\n`;
|
|
164
171
|
}
|
|
165
|
-
|
|
166
|
-
// only apply hmr when not in ssr context and hot options are set
|
|
167
|
-
if (!ssr && makeHot) {
|
|
168
|
-
compiled.js.code = makeHot({
|
|
169
|
-
id: filename,
|
|
170
|
-
compiledCode: compiled.js.code,
|
|
171
|
-
//@ts-expect-error hot isn't a boolean at this point
|
|
172
|
-
hotOptions: { ...options.hot, injectCss: options.hot?.injectCss === true && hasCss },
|
|
173
|
-
compiled,
|
|
174
|
-
originalCode: code,
|
|
175
|
-
compileOptions: finalCompileOptions
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
172
|
}
|
|
179
173
|
|
|
180
174
|
let lang = 'js';
|
|
@@ -196,34 +190,4 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
196
190
|
preprocessed: preprocessed ?? { code }
|
|
197
191
|
};
|
|
198
192
|
};
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
203
|
-
* @returns {Function | undefined}
|
|
204
|
-
*/
|
|
205
|
-
function buildMakeHot(options) {
|
|
206
|
-
const needsMakeHot =
|
|
207
|
-
!isSvelte5 && options.hot !== false && options.isServe && !options.isProduction;
|
|
208
|
-
if (needsMakeHot) {
|
|
209
|
-
// @ts-ignore
|
|
210
|
-
const hotApi = options?.hot?.hotApi;
|
|
211
|
-
// @ts-ignore
|
|
212
|
-
const adapter = options?.hot?.adapter;
|
|
213
|
-
return createMakeHot({
|
|
214
|
-
walk: svelte.walk,
|
|
215
|
-
hotApi,
|
|
216
|
-
adapter,
|
|
217
|
-
hotOptions: { noOverlay: true, .../** @type {object} */ (options.hot) }
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
224
|
-
* @returns {import('../types/compile.d.ts').CompileSvelte}
|
|
225
|
-
*/
|
|
226
|
-
export function createCompileSvelte(options) {
|
|
227
|
-
const makeHot = buildMakeHot(options);
|
|
228
|
-
return _createCompileSvelte(makeHot);
|
|
229
193
|
}
|
package/src/utils/constants.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { isSvelte5 } from './svelte-version.js';
|
|
2
|
-
|
|
3
1
|
export const VITE_RESOLVE_MAIN_FIELDS = ['browser', 'module', 'jsnext:main', 'jsnext'];
|
|
4
2
|
|
|
5
3
|
export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte'];
|
|
@@ -10,23 +8,14 @@ export const SVELTE_IMPORTS = [
|
|
|
10
8
|
'svelte/internal',
|
|
11
9
|
'svelte/internal/disclose-version',
|
|
12
10
|
'svelte/motion',
|
|
13
|
-
'svelte/ssr',
|
|
14
11
|
'svelte/store',
|
|
15
12
|
'svelte/transition',
|
|
13
|
+
'svelte/server',
|
|
14
|
+
'svelte/internal/server',
|
|
15
|
+
'svelte/legacy',
|
|
16
16
|
'svelte'
|
|
17
17
|
];
|
|
18
18
|
|
|
19
|
-
export const SVELTE_HMR_IMPORTS = [
|
|
20
|
-
'svelte-hmr/runtime/hot-api-esm.js',
|
|
21
|
-
'svelte-hmr/runtime/proxy-adapter-dom.js',
|
|
22
|
-
'svelte-hmr'
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
if (isSvelte5) {
|
|
26
|
-
SVELTE_IMPORTS.push('svelte/server', 'svelte/internal/server', 'svelte/legacy');
|
|
27
|
-
SVELTE_HMR_IMPORTS.length = 0; // truncate, svelte-hmr isn't used with svelte5
|
|
28
|
-
}
|
|
29
|
-
|
|
30
19
|
export const SVELTE_EXPORT_CONDITIONS = ['svelte'];
|
|
31
20
|
|
|
32
21
|
export const FAQ_LINK_MISSING_EXPORTS_CONDITION =
|
package/src/utils/error.js
CHANGED
|
@@ -2,7 +2,7 @@ import { buildExtendedLogMessage } from './log.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* convert an error thrown by svelte.compile to a RollupError so that vite displays it in a user friendly way
|
|
5
|
-
* @param {import('svelte/
|
|
5
|
+
* @param {import('svelte/compiler').Warning & Error & {frame?: string}} error a svelte compiler error, which is a mix of Warning and an error
|
|
6
6
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
7
7
|
* @returns {import('vite').Rollup.RollupError} the converted error
|
|
8
8
|
*/
|
|
@@ -29,7 +29,7 @@ export function toRollupError(error, options) {
|
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* convert an error thrown by svelte.compile to an esbuild PartialMessage
|
|
32
|
-
* @param {import('svelte/
|
|
32
|
+
* @param {import('svelte/compiler').Warning & Error & {frame?: string}} error a svelte compiler error, which is a mix of Warning and an error
|
|
33
33
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
34
34
|
* @returns {import('esbuild').PartialMessage} the converted error
|
|
35
35
|
*/
|
|
@@ -102,7 +102,7 @@ function formatFrameForVite(frame) {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
* @param {import('svelte/
|
|
105
|
+
* @param {import('svelte/compiler').Warning & Error} err a svelte compiler error, which is a mix of Warning and an error
|
|
106
106
|
* @param {string} originalCode
|
|
107
107
|
* @param {import('../public.d.ts').Options['preprocess']} [preprocessors]
|
|
108
108
|
*/
|
|
@@ -112,35 +112,6 @@ export function enhanceCompileError(err, originalCode, preprocessors) {
|
|
|
112
112
|
/** @type {string[]} */
|
|
113
113
|
const additionalMessages = [];
|
|
114
114
|
|
|
115
|
-
// Handle incorrect TypeScript usage
|
|
116
|
-
if (err.code === 'parse-error') {
|
|
117
|
-
// Reference from Svelte: https://github.com/sveltejs/svelte/blob/9926347ad9dbdd0f3324d5538e25dcb7f5e442f8/packages/svelte/src/compiler/preprocess/index.js#L259
|
|
118
|
-
const scriptRe =
|
|
119
|
-
/<!--[^]*?-->|<script((?:\s+[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"/]+)*\s*)(?:\/>|>([\S\s]*?)<\/script>)/g;
|
|
120
|
-
const errIndex = err.pos ?? -1;
|
|
121
|
-
|
|
122
|
-
let m;
|
|
123
|
-
while ((m = scriptRe.exec(originalCode))) {
|
|
124
|
-
const matchStart = m.index;
|
|
125
|
-
const matchEnd = matchStart + m[0].length;
|
|
126
|
-
const isErrorInScript = matchStart <= errIndex && errIndex <= matchEnd;
|
|
127
|
-
if (isErrorInScript) {
|
|
128
|
-
// Warn missing lang="ts"
|
|
129
|
-
const hasLangTs = m[1]?.includes('lang="ts"');
|
|
130
|
-
if (!hasLangTs) {
|
|
131
|
-
additionalMessages.push('Did you forget to add lang="ts" to your script tag?');
|
|
132
|
-
}
|
|
133
|
-
// Warn missing script preprocessor
|
|
134
|
-
if (preprocessors.every((p) => p.script == null)) {
|
|
135
|
-
const preprocessorType = hasLangTs ? 'TypeScript' : 'script';
|
|
136
|
-
additionalMessages.push(
|
|
137
|
-
`Did you forget to add a ${preprocessorType} preprocessor? See https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md for more information.`
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
115
|
// Handle incorrect CSS preprocessor usage
|
|
145
116
|
if (err.code === 'css-syntax-error') {
|
|
146
117
|
// Reference from Svelte: https://github.com/sveltejs/svelte/blob/9926347ad9dbdd0f3324d5538e25dcb7f5e442f8/packages/svelte/src/compiler/preprocess/index.js#L257
|
package/src/utils/esbuild.js
CHANGED
|
@@ -2,7 +2,6 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import * as svelte from 'svelte/compiler';
|
|
3
3
|
import { log } from './log.js';
|
|
4
4
|
import { toESBuildError } from './error.js';
|
|
5
|
-
import { isSvelte5 } from './svelte-version.js';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* @typedef {NonNullable<import('vite').DepOptimizationOptions['esbuildOptions']>} EsbuildOptions
|
|
@@ -26,9 +25,8 @@ export function esbuildSveltePlugin(options) {
|
|
|
26
25
|
if (build.initialOptions.plugins?.some((v) => v.name === 'vite:dep-scan')) return;
|
|
27
26
|
|
|
28
27
|
const svelteExtensions = (options.extensions ?? ['.svelte']).map((ext) => ext.slice(1));
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
28
|
+
svelteExtensions.push(svelteModuleExtension.slice(1));
|
|
29
|
+
|
|
32
30
|
const svelteFilter = new RegExp('\\.(' + svelteExtensions.join('|') + ')(\\?.*)?$');
|
|
33
31
|
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined} */
|
|
34
32
|
let statsCollection;
|
|
@@ -60,9 +58,8 @@ export function esbuildSveltePlugin(options) {
|
|
|
60
58
|
* @returns {Promise<string>}
|
|
61
59
|
*/
|
|
62
60
|
async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
63
|
-
if (
|
|
61
|
+
if (filename.endsWith(svelteModuleExtension)) {
|
|
64
62
|
const endStat = statsCollection?.start(filename);
|
|
65
|
-
// @ts-expect-error compileModule does not exist in svelte4
|
|
66
63
|
const compiled = svelte.compileModule(code, {
|
|
67
64
|
filename,
|
|
68
65
|
generate: 'client'
|
|
@@ -75,7 +72,7 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
|
75
72
|
: compiled.js.code;
|
|
76
73
|
}
|
|
77
74
|
let css = options.compilerOptions.css;
|
|
78
|
-
if (css !== '
|
|
75
|
+
if (css !== 'injected') {
|
|
79
76
|
// TODO ideally we'd be able to externalize prebundled styles too, but for now always put them in the js
|
|
80
77
|
css = 'injected';
|
|
81
78
|
}
|
|
@@ -84,8 +81,7 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
|
84
81
|
...options.compilerOptions,
|
|
85
82
|
css,
|
|
86
83
|
filename,
|
|
87
|
-
|
|
88
|
-
generate: isSvelte5 ? 'client' : 'dom'
|
|
84
|
+
generate: 'client'
|
|
89
85
|
};
|
|
90
86
|
|
|
91
87
|
let preprocessed;
|
package/src/utils/load-raw.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import { toRollupError } from './error.js';
|
|
3
3
|
import { log } from './log.js';
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
/**
|
|
6
6
|
* utility function to compile ?raw and ?direct requests in load hook
|
|
7
7
|
*
|
|
@@ -18,26 +18,16 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
|
|
|
18
18
|
const source = fs.readFileSync(filename, 'utf-8');
|
|
19
19
|
try {
|
|
20
20
|
//avoid compileSvelte doing extra ssr stuff unless requested
|
|
21
|
-
|
|
22
|
-
svelteRequest.ssr = query.compilerOptions?.generate === (isSvelte4 ? 'ssr' : 'server');
|
|
23
|
-
const type = query.type;
|
|
21
|
+
svelteRequest.ssr = query.compilerOptions?.generate === 'server';
|
|
24
22
|
compileData = await compileSvelte(svelteRequest, source, {
|
|
25
23
|
...options,
|
|
26
24
|
// don't use dynamic vite-plugin-svelte defaults here to ensure stable result between ssr,dev and build
|
|
27
25
|
compilerOptions: {
|
|
28
26
|
dev: false,
|
|
29
27
|
css: 'external',
|
|
30
|
-
|
|
31
|
-
? undefined
|
|
32
|
-
: query.sourcemap
|
|
33
|
-
? {
|
|
34
|
-
js: type === 'script' || type === 'all',
|
|
35
|
-
css: type === 'style' || type === 'all'
|
|
36
|
-
}
|
|
37
|
-
: false,
|
|
28
|
+
hmr: false,
|
|
38
29
|
...svelteRequest.query.compilerOptions
|
|
39
30
|
},
|
|
40
|
-
hot: false,
|
|
41
31
|
emitCss: true
|
|
42
32
|
});
|
|
43
33
|
} catch (e) {
|
|
@@ -45,7 +35,7 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
|
|
|
45
35
|
}
|
|
46
36
|
let result;
|
|
47
37
|
if (query.type === 'style') {
|
|
48
|
-
result = compileData.compiled.css;
|
|
38
|
+
result = compileData.compiled.css ?? { code: '', map: null };
|
|
49
39
|
} else if (query.type === 'script') {
|
|
50
40
|
result = compileData.compiled.js;
|
|
51
41
|
} else if (query.type === 'preprocessed') {
|
|
@@ -68,7 +58,9 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
|
|
|
68
58
|
}
|
|
69
59
|
log.debug(`load returns direct result for ${id}`, undefined, 'load');
|
|
70
60
|
let directOutput = result.code;
|
|
61
|
+
// @ts-expect-error might not be SourceMap but toUrl check should suffice
|
|
71
62
|
if (query.sourcemap && result.map?.toUrl) {
|
|
63
|
+
// @ts-expect-error
|
|
72
64
|
const map = `sourceMappingURL=${result.map.toUrl()}`;
|
|
73
65
|
if (query.type === 'style') {
|
|
74
66
|
directOutput += `\n\n/*# ${map} */\n`;
|
package/src/utils/log.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import { cyan, red, yellow } from 'kleur/colors';
|
|
3
3
|
import debug from 'debug';
|
|
4
|
-
import { VERSION } from 'svelte/compiler';
|
|
5
4
|
|
|
6
5
|
/** @type {import('../types/log.d.ts').LogLevel[]} */
|
|
7
6
|
const levels = ['debug', 'info', 'warn', 'error', 'silent'];
|
|
@@ -128,21 +127,21 @@ export const log = {
|
|
|
128
127
|
|
|
129
128
|
/**
|
|
130
129
|
* @param {import('../types/id.d.ts').SvelteRequest | import('../types/id.d.ts').SvelteModuleRequest} svelteRequest
|
|
131
|
-
* @param {import('svelte/
|
|
130
|
+
* @param {import('svelte/compiler').Warning[]} warnings
|
|
132
131
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
133
132
|
*/
|
|
134
133
|
export function logCompilerWarnings(svelteRequest, warnings, options) {
|
|
135
134
|
const { emitCss, onwarn, isBuild } = options;
|
|
136
135
|
const sendViaWS = !isBuild && options.experimental?.sendWarningsToBrowser;
|
|
137
136
|
let warn = isBuild ? warnBuild : warnDev;
|
|
138
|
-
/** @type {import('svelte/
|
|
137
|
+
/** @type {import('svelte/compiler').Warning[]} */
|
|
139
138
|
const handledByDefaultWarn = [];
|
|
140
139
|
const notIgnored = warnings?.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss));
|
|
141
140
|
const extra = buildExtraWarnings(warnings, isBuild);
|
|
142
141
|
const allWarnings = [...notIgnored, ...extra];
|
|
143
142
|
if (sendViaWS) {
|
|
144
143
|
const _warn = warn;
|
|
145
|
-
/** @type {(w: import('svelte/
|
|
144
|
+
/** @type {(w: import('svelte/compiler').Warning) => void} */
|
|
146
145
|
warn = (w) => {
|
|
147
146
|
handledByDefaultWarn.push(w);
|
|
148
147
|
_warn(w);
|
|
@@ -172,7 +171,7 @@ export function logCompilerWarnings(svelteRequest, warnings, options) {
|
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
/**
|
|
175
|
-
* @param {import('svelte/
|
|
174
|
+
* @param {import('svelte/compiler').Warning} warning
|
|
176
175
|
* @param {boolean} isBuild
|
|
177
176
|
* @param {boolean} [emitCss]
|
|
178
177
|
* @returns {boolean}
|
|
@@ -186,7 +185,7 @@ function ignoreCompilerWarning(warning, isBuild, emitCss) {
|
|
|
186
185
|
|
|
187
186
|
/**
|
|
188
187
|
*
|
|
189
|
-
* @param {import('svelte/
|
|
188
|
+
* @param {import('svelte/compiler').Warning} warning
|
|
190
189
|
* @returns {boolean}
|
|
191
190
|
*/
|
|
192
191
|
function isNoScopableElementWarning(warning) {
|
|
@@ -196,9 +195,9 @@ function isNoScopableElementWarning(warning) {
|
|
|
196
195
|
|
|
197
196
|
/**
|
|
198
197
|
*
|
|
199
|
-
* @param {import('svelte/
|
|
198
|
+
* @param {import('svelte/compiler').Warning[]} warnings
|
|
200
199
|
* @param {boolean} isBuild
|
|
201
|
-
* @returns {import('svelte/
|
|
200
|
+
* @returns {import('svelte/compiler').Warning[]}
|
|
202
201
|
*/
|
|
203
202
|
function buildExtraWarnings(warnings, isBuild) {
|
|
204
203
|
const extraWarnings = [];
|
|
@@ -220,21 +219,21 @@ function buildExtraWarnings(warnings, isBuild) {
|
|
|
220
219
|
}
|
|
221
220
|
|
|
222
221
|
/**
|
|
223
|
-
* @param {import('svelte/
|
|
222
|
+
* @param {import('svelte/compiler').Warning} w
|
|
224
223
|
*/
|
|
225
224
|
function warnDev(w) {
|
|
226
225
|
log.info.enabled && log.info(buildExtendedLogMessage(w));
|
|
227
226
|
}
|
|
228
227
|
|
|
229
228
|
/**
|
|
230
|
-
* @param {import('svelte/
|
|
229
|
+
* @param {import('svelte/compiler').Warning & {frame?: string}} w
|
|
231
230
|
*/
|
|
232
231
|
function warnBuild(w) {
|
|
233
232
|
log.warn.enabled && log.warn(buildExtendedLogMessage(w), w.frame);
|
|
234
233
|
}
|
|
235
234
|
|
|
236
235
|
/**
|
|
237
|
-
* @param {import('svelte/
|
|
236
|
+
* @param {import('svelte/compiler').Warning} w
|
|
238
237
|
*/
|
|
239
238
|
export function buildExtendedLogMessage(w) {
|
|
240
239
|
const parts = [];
|
|
@@ -260,9 +259,3 @@ export function buildExtendedLogMessage(w) {
|
|
|
260
259
|
export function isDebugNamespaceEnabled(namespace) {
|
|
261
260
|
return debug.enabled(`${prefix}:${namespace}`);
|
|
262
261
|
}
|
|
263
|
-
|
|
264
|
-
export function logSvelte5Warning() {
|
|
265
|
-
const notice = `You are using Svelte ${VERSION}. Svelte 5 support is experimental, breaking changes can occur in any release until this notice is removed.`;
|
|
266
|
-
const wip = ['svelte-inspector is disabled until dev mode implements node to code mapping'];
|
|
267
|
-
log.warn(`${notice}\nwork in progress:\n - ${wip.join('\n - ')}\n`);
|
|
268
|
-
}
|
package/src/utils/options.js
CHANGED
|
@@ -5,7 +5,6 @@ import { loadSvelteConfig } from './load-svelte-config.js';
|
|
|
5
5
|
import {
|
|
6
6
|
FAQ_LINK_MISSING_EXPORTS_CONDITION,
|
|
7
7
|
SVELTE_EXPORT_CONDITIONS,
|
|
8
|
-
SVELTE_HMR_IMPORTS,
|
|
9
8
|
SVELTE_IMPORTS,
|
|
10
9
|
SVELTE_RESOLVE_MAIN_FIELDS,
|
|
11
10
|
VITE_RESOLVE_MAIN_FIELDS
|
|
@@ -26,7 +25,6 @@ import {
|
|
|
26
25
|
import { isCommonDepWithoutSvelteField } from './dependencies.js';
|
|
27
26
|
import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
|
|
28
27
|
import { VitePluginSvelteCache } from './vite-plugin-svelte-cache.js';
|
|
29
|
-
import { isSvelte5, isSvelte5WithHMRSupport } from './svelte-version.js';
|
|
30
28
|
|
|
31
29
|
const allowedPluginOptions = new Set([
|
|
32
30
|
'include',
|
|
@@ -196,22 +194,11 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
|
|
|
196
194
|
const defaultOptions = {
|
|
197
195
|
compilerOptions: {
|
|
198
196
|
css,
|
|
199
|
-
dev: !viteConfig.isProduction
|
|
197
|
+
dev: !viteConfig.isProduction,
|
|
198
|
+
hmr: !viteConfig.isProduction && !preResolveOptions.isBuild
|
|
200
199
|
}
|
|
201
200
|
};
|
|
202
|
-
|
|
203
|
-
if (isSvelte5WithHMRSupport) {
|
|
204
|
-
// @ts-expect-error svelte4 does not have hmr option
|
|
205
|
-
defaultOptions.compilerOptions.hmr = !viteConfig.isProduction;
|
|
206
|
-
}
|
|
207
|
-
} else {
|
|
208
|
-
defaultOptions.hot = viteConfig.isProduction
|
|
209
|
-
? false
|
|
210
|
-
: {
|
|
211
|
-
injectCss: css === 'injected',
|
|
212
|
-
partialAccept: !!viteConfig.experimental?.hmrPartialAccept
|
|
213
|
-
};
|
|
214
|
-
}
|
|
201
|
+
|
|
215
202
|
/** @type {Partial<import('../types/options.d.ts').ResolvedOptions>} */
|
|
216
203
|
const extraOptions = {
|
|
217
204
|
root: viteConfig.root,
|
|
@@ -237,55 +224,12 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
|
|
|
237
224
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
238
225
|
*/
|
|
239
226
|
function enforceOptionsForHmr(options) {
|
|
240
|
-
if (
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
// @ts-expect-error hmr option doesn't exist in svelte4
|
|
247
|
-
options.compilerOptions.hmr = true;
|
|
248
|
-
}
|
|
249
|
-
} else {
|
|
250
|
-
if (options.hot) {
|
|
251
|
-
if (!options.compilerOptions.dev) {
|
|
252
|
-
log.warn('hmr is enabled but compilerOptions.dev is false, forcing it to true');
|
|
253
|
-
options.compilerOptions.dev = true;
|
|
254
|
-
}
|
|
255
|
-
if (options.emitCss) {
|
|
256
|
-
if (options.hot !== true && options.hot.injectCss) {
|
|
257
|
-
log.warn('hmr and emitCss are enabled but hot.injectCss is true, forcing it to false');
|
|
258
|
-
options.hot.injectCss = false;
|
|
259
|
-
}
|
|
260
|
-
const css = options.compilerOptions.css;
|
|
261
|
-
if (css === true || css === 'injected') {
|
|
262
|
-
const forcedCss = 'external';
|
|
263
|
-
log.warn(
|
|
264
|
-
`hmr and emitCss are enabled but compilerOptions.css is ${css}, forcing it to ${forcedCss}`
|
|
265
|
-
);
|
|
266
|
-
options.compilerOptions.css = forcedCss;
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
if (options.hot === true || !options.hot.injectCss) {
|
|
270
|
-
log.warn(
|
|
271
|
-
'hmr with emitCss disabled requires option hot.injectCss to be enabled, forcing it to true'
|
|
272
|
-
);
|
|
273
|
-
if (options.hot === true) {
|
|
274
|
-
options.hot = { injectCss: true };
|
|
275
|
-
} else {
|
|
276
|
-
options.hot.injectCss = true;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
const css = options.compilerOptions.css;
|
|
280
|
-
if (!(css === true || css === 'injected')) {
|
|
281
|
-
const forcedCss = 'injected';
|
|
282
|
-
log.warn(
|
|
283
|
-
`hmr with emitCss disabled requires compilerOptions.css to be enabled, forcing it to ${forcedCss}`
|
|
284
|
-
);
|
|
285
|
-
options.compilerOptions.css = forcedCss;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
227
|
+
if (options.hot) {
|
|
228
|
+
log.warn(
|
|
229
|
+
'svelte 5 has hmr integrated in core. Please remove the vitePlugin.hot option and use compilerOptions.hmr instead'
|
|
230
|
+
);
|
|
231
|
+
delete options.hot;
|
|
232
|
+
options.compilerOptions.hmr = true;
|
|
289
233
|
}
|
|
290
234
|
}
|
|
291
235
|
|
|
@@ -294,9 +238,11 @@ function enforceOptionsForHmr(options) {
|
|
|
294
238
|
*/
|
|
295
239
|
function enforceOptionsForProduction(options) {
|
|
296
240
|
if (options.isProduction) {
|
|
297
|
-
if (options.
|
|
298
|
-
log.warn(
|
|
299
|
-
|
|
241
|
+
if (options.compilerOptions.hmr) {
|
|
242
|
+
log.warn(
|
|
243
|
+
'you are building for production but compilerOptions.hmr is true, forcing it to false'
|
|
244
|
+
);
|
|
245
|
+
options.compilerOptions.hmr = false;
|
|
300
246
|
}
|
|
301
247
|
if (options.compilerOptions.dev) {
|
|
302
248
|
log.warn(
|
|
@@ -384,7 +330,7 @@ export async function buildExtraViteConfig(options, config) {
|
|
|
384
330
|
/** @type {Partial<import('vite').UserConfig>} */
|
|
385
331
|
const extraViteConfig = {
|
|
386
332
|
resolve: {
|
|
387
|
-
dedupe: [...SVELTE_IMPORTS
|
|
333
|
+
dedupe: [...SVELTE_IMPORTS],
|
|
388
334
|
conditions: [...SVELTE_EXPORT_CONDITIONS]
|
|
389
335
|
}
|
|
390
336
|
// this option is still awaiting a PR in vite to be supported
|
|
@@ -443,12 +389,7 @@ export async function buildExtraViteConfig(options, config) {
|
|
|
443
389
|
}
|
|
444
390
|
|
|
445
391
|
// enable hmrPartialAccept if not explicitly disabled
|
|
446
|
-
if (
|
|
447
|
-
(options.hot == null ||
|
|
448
|
-
options.hot === true ||
|
|
449
|
-
(options.hot && options.hot.partialAccept !== false)) && // deviate from svelte-hmr, default to true
|
|
450
|
-
config.experimental?.hmrPartialAccept !== false
|
|
451
|
-
) {
|
|
392
|
+
if (config.experimental?.hmrPartialAccept !== false) {
|
|
452
393
|
log.debug('enabling "experimental.hmrPartialAccept" in vite config', undefined, 'config');
|
|
453
394
|
extraViteConfig.experimental = { hmrPartialAccept: true };
|
|
454
395
|
}
|
|
@@ -593,9 +534,10 @@ function buildExtraConfigForSvelte(config) {
|
|
|
593
534
|
// include svelte imports for optimization unless explicitly excluded
|
|
594
535
|
/** @type {string[]} */
|
|
595
536
|
const include = [];
|
|
596
|
-
|
|
537
|
+
/** @type {string[]} */
|
|
538
|
+
const exclude = [];
|
|
597
539
|
if (!isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
|
|
598
|
-
const svelteImportsToInclude = SVELTE_IMPORTS
|
|
540
|
+
const svelteImportsToInclude = SVELTE_IMPORTS;
|
|
599
541
|
log.debug(
|
|
600
542
|
`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `,
|
|
601
543
|
undefined,
|
|
@@ -614,7 +556,7 @@ function buildExtraConfigForSvelte(config) {
|
|
|
614
556
|
/** @type {string[]} */
|
|
615
557
|
const external = [];
|
|
616
558
|
// add svelte to ssr.noExternal unless it is present in ssr.external
|
|
617
|
-
// so
|
|
559
|
+
// so it is correctly resolving according to the conditions in sveltes exports map
|
|
618
560
|
if (!isDepExternaled('svelte', config.ssr?.external ?? [])) {
|
|
619
561
|
noExternal.push('svelte', /^svelte\//);
|
|
620
562
|
}
|
package/src/utils/preprocess.js
CHANGED
|
@@ -127,10 +127,10 @@ export function addExtraPreprocessors(options, config) {
|
|
|
127
127
|
*
|
|
128
128
|
* @param filename {string}
|
|
129
129
|
* @param dependencies {string[]}
|
|
130
|
-
* @returns {({dependencies: string[], warnings:import('svelte/
|
|
130
|
+
* @returns {({dependencies: string[], warnings:import('svelte/compiler').Warning[] })}
|
|
131
131
|
*/
|
|
132
132
|
export function checkPreprocessDependencies(filename, dependencies) {
|
|
133
|
-
/** @type {import('svelte/
|
|
133
|
+
/** @type {import('svelte/compiler').Warning[]} */
|
|
134
134
|
const warnings = [];
|
|
135
135
|
|
|
136
136
|
// to find self, we have to compare normalized filenames, but must keep the original values in `dependencies`
|
|
@@ -16,9 +16,9 @@ import { normalizePath } from 'vite';
|
|
|
16
16
|
* @class
|
|
17
17
|
*/
|
|
18
18
|
export class VitePluginSvelteCache {
|
|
19
|
-
/** @type {Map<string, import('../types/compile.d.ts').Code>} */
|
|
19
|
+
/** @type {Map<string, import('../types/compile.d.ts').Code | null>} */
|
|
20
20
|
#css = new Map();
|
|
21
|
-
/** @type {Map<string, import('../types/compile.d.ts').Code>} */
|
|
21
|
+
/** @type {Map<string, import('../types/compile.d.ts').Code | null>} */
|
|
22
22
|
#js = new Map();
|
|
23
23
|
/** @type {Map<string, string[]>} */
|
|
24
24
|
#dependencies = new Map();
|
|
@@ -133,7 +133,7 @@ export class VitePluginSvelteCache {
|
|
|
133
133
|
|
|
134
134
|
/**
|
|
135
135
|
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
|
|
136
|
-
* @returns {import('../types/compile.d.ts').Code | undefined}
|
|
136
|
+
* @returns {import('../types/compile.d.ts').Code | undefined | null}
|
|
137
137
|
*/
|
|
138
138
|
getCSS(svelteRequest) {
|
|
139
139
|
return this.#css.get(svelteRequest.normalizedFilename);
|
|
@@ -141,7 +141,7 @@ export class VitePluginSvelteCache {
|
|
|
141
141
|
|
|
142
142
|
/**
|
|
143
143
|
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
|
|
144
|
-
* @returns {import('../types/compile.d.ts').Code | undefined}
|
|
144
|
+
* @returns {import('../types/compile.d.ts').Code | undefined | null}
|
|
145
145
|
*/
|
|
146
146
|
getJS(svelteRequest) {
|
|
147
147
|
if (!svelteRequest.ssr) {
|
package/types/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
declare module '@sveltejs/vite-plugin-svelte' {
|
|
2
2
|
import type { InlineConfig, ResolvedConfig } from 'vite';
|
|
3
|
-
import type { CompileOptions, PreprocessorGroup } from 'svelte/compiler';
|
|
4
|
-
import type { Warning } from 'svelte/types/compiler/interfaces';
|
|
3
|
+
import type { CompileOptions, Warning, PreprocessorGroup } from 'svelte/compiler';
|
|
5
4
|
import type { Options as InspectorOptions } from '@sveltejs/vite-plugin-svelte-inspector';
|
|
6
5
|
export type Options = Omit<SvelteConfig, 'vitePlugin'> & PluginOptionsInline;
|
|
7
6
|
|
|
@@ -39,27 +38,13 @@ declare module '@sveltejs/vite-plugin-svelte' {
|
|
|
39
38
|
emitCss?: boolean;
|
|
40
39
|
/**
|
|
41
40
|
* Enable or disable Hot Module Replacement.
|
|
41
|
+
* Deprecated, use compilerOptions.hmr instead!
|
|
42
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
|
|
43
|
+
* @deprecated
|
|
54
44
|
* @default true for development, always false for production
|
|
55
45
|
*/
|
|
56
|
-
hot?:
|
|
57
|
-
|
|
58
|
-
| {
|
|
59
|
-
injectCss?: boolean;
|
|
60
|
-
partialAccept?: boolean;
|
|
61
|
-
[key: string]: any;
|
|
62
|
-
};
|
|
46
|
+
hot?: boolean;
|
|
47
|
+
|
|
63
48
|
/**
|
|
64
49
|
* Some Vite plugins can contribute additional preprocessors by defining `api.sveltePreprocess`.
|
|
65
50
|
* If you don't want to use them, set this to true to ignore them all or use an array of strings
|
|
@@ -150,8 +135,18 @@ declare module '@sveltejs/vite-plugin-svelte' {
|
|
|
150
135
|
|
|
151
136
|
/**
|
|
152
137
|
* Handles warning emitted from the Svelte compiler
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```
|
|
141
|
+
* (warning, defaultHandler) => {
|
|
142
|
+
* // ignore some warnings
|
|
143
|
+
* if (!['foo','bar'].includes(warning.code)) {
|
|
144
|
+
* defaultHandler(warning);
|
|
145
|
+
* }
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
153
148
|
*/
|
|
154
|
-
onwarn?: (warning: Warning, defaultHandler
|
|
149
|
+
onwarn?: (warning: Warning, defaultHandler: (warning: Warning) => void) => void;
|
|
155
150
|
/**
|
|
156
151
|
* Options for vite-plugin-svelte
|
|
157
152
|
*/
|
|
@@ -186,7 +181,16 @@ declare module '@sveltejs/vite-plugin-svelte' {
|
|
|
186
181
|
type Arrayable<T> = T | T[];
|
|
187
182
|
|
|
188
183
|
export interface VitePreprocessOptions {
|
|
184
|
+
/**
|
|
185
|
+
* preprocess script block with vite pipeline.
|
|
186
|
+
* Since svelte5 this is not needed for typescript anymore
|
|
187
|
+
*
|
|
188
|
+
* @default false
|
|
189
|
+
*/
|
|
189
190
|
script?: boolean;
|
|
191
|
+
/**
|
|
192
|
+
* preprocess style blocks with vite pipeline
|
|
193
|
+
*/
|
|
190
194
|
style?: boolean | InlineConfig | ResolvedConfig;
|
|
191
195
|
}
|
|
192
196
|
export function svelte(inlineOptions?: Partial<Options> | undefined): import('vite').Plugin[];
|
package/types/index.d.ts.map
CHANGED
|
@@ -21,5 +21,5 @@
|
|
|
21
21
|
null,
|
|
22
22
|
null
|
|
23
23
|
],
|
|
24
|
-
"mappings": "
|
|
24
|
+
"mappings": ";;;;aAMYA,OAAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA6GFC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAqEZC,qBAAqBA;;;;;;;;;;;;;iBChKtBC,MAAMA;iBCTNC,cAAcA;iBCgBRC,gBAAgBA"
|
|
25
25
|
}
|