@sveltejs/vite-plugin-svelte 3.0.2 → 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 -21
- 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 -61
- 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 -12
- package/src/utils/log.js +10 -20
- package/src/utils/options.js +19 -63
- package/src/utils/preprocess.js +2 -2
- package/src/utils/svelte-version.js +3 -2
- 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",
|
|
@@ -39,21 +39,20 @@
|
|
|
39
39
|
"debug": "^4.3.4",
|
|
40
40
|
"deepmerge": "^4.3.1",
|
|
41
41
|
"kleur": "^4.1.5",
|
|
42
|
-
"magic-string": "^0.30.
|
|
43
|
-
"svelte-hmr": "^0.15.3",
|
|
42
|
+
"magic-string": "^0.30.10",
|
|
44
43
|
"vitefu": "^0.2.5",
|
|
45
|
-
"@sveltejs/vite-plugin-svelte-inspector": "^
|
|
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
|
-
"esbuild": "^0.
|
|
54
|
-
"sass": "^1.
|
|
55
|
-
"svelte": "^
|
|
56
|
-
"vite": "^5.
|
|
52
|
+
"esbuild": "^0.20.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,7 +174,7 @@ export function svelte(inlineOptions) {
|
|
|
178
174
|
},
|
|
179
175
|
|
|
180
176
|
handleHotUpdate(ctx) {
|
|
181
|
-
if (!options.
|
|
177
|
+
if (!options.compilerOptions.hmr || !options.emitCss) {
|
|
182
178
|
return;
|
|
183
179
|
}
|
|
184
180
|
const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
|
|
@@ -189,12 +185,8 @@ export function svelte(inlineOptions) {
|
|
|
189
185
|
async buildEnd() {
|
|
190
186
|
await options.stats?.finishAll();
|
|
191
187
|
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (isSvelte5) {
|
|
195
|
-
logSvelte5Warning();
|
|
196
|
-
// TODO move to separate file
|
|
197
|
-
plugins.push({
|
|
188
|
+
},
|
|
189
|
+
{
|
|
198
190
|
name: 'vite-plugin-svelte-module',
|
|
199
191
|
enforce: 'post',
|
|
200
192
|
async configResolved() {
|
|
@@ -207,8 +199,7 @@ export function svelte(inlineOptions) {
|
|
|
207
199
|
return;
|
|
208
200
|
}
|
|
209
201
|
try {
|
|
210
|
-
|
|
211
|
-
const compileResult = await svelteCompiler.compileModule(code, {
|
|
202
|
+
const compileResult = svelteCompiler.compileModule(code, {
|
|
212
203
|
generate: ssr ? 'server' : 'client',
|
|
213
204
|
filename: moduleRequest.filename
|
|
214
205
|
});
|
|
@@ -218,12 +209,9 @@ export function svelte(inlineOptions) {
|
|
|
218
209
|
throw toRollupError(e, options);
|
|
219
210
|
}
|
|
220
211
|
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
// TODO reenable once svelte5 has support and update utils/log.js#logSvelte5Warning
|
|
225
|
-
plugins.push(svelteInspector());
|
|
226
|
-
}
|
|
212
|
+
},
|
|
213
|
+
svelteInspector()
|
|
214
|
+
];
|
|
227
215
|
return plugins;
|
|
228
216
|
}
|
|
229
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,25 +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
|
-
if (ssr && compileOptions.enableSourcemap !== false) {
|
|
75
|
-
if (typeof compileOptions.enableSourcemap === 'object') {
|
|
76
|
-
compileOptions.enableSourcemap.css = false;
|
|
77
|
-
} else {
|
|
78
|
-
compileOptions.enableSourcemap = { js: true, css: false };
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
70
|
|
|
82
71
|
let preprocessed;
|
|
83
72
|
let preprocessors = options.preprocess;
|
|
84
|
-
if (!options.isBuild && options.emitCss &&
|
|
73
|
+
if (!options.isBuild && options.emitCss && compileOptions.hmr) {
|
|
85
74
|
// inject preprocessor that ensures css hmr works better
|
|
86
75
|
if (!Array.isArray(preprocessors)) {
|
|
87
76
|
preprocessors = preprocessors
|
|
@@ -141,10 +130,21 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
141
130
|
: compileOptions;
|
|
142
131
|
|
|
143
132
|
const endStat = stats?.start(filename);
|
|
144
|
-
/** @type {import('svelte/
|
|
133
|
+
/** @type {import('svelte/compiler').CompileResult} */
|
|
145
134
|
let compiled;
|
|
146
135
|
try {
|
|
147
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
|
+
}
|
|
148
148
|
} catch (e) {
|
|
149
149
|
enhanceCompileError(e, code, preprocessors);
|
|
150
150
|
throw e;
|
|
@@ -163,25 +163,12 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
163
163
|
}
|
|
164
164
|
if (!raw) {
|
|
165
165
|
// wire css import and code for hmr
|
|
166
|
-
const hasCss = compiled.css?.code?.trim()
|
|
166
|
+
const hasCss = compiled.css?.code?.trim()?.length ?? 0 > 0;
|
|
167
167
|
// compiler might not emit css with mode none or it may be empty
|
|
168
168
|
if (emitCss && hasCss) {
|
|
169
169
|
// TODO properly update sourcemap?
|
|
170
170
|
compiled.js.code += `\nimport ${JSON.stringify(cssId)};\n`;
|
|
171
171
|
}
|
|
172
|
-
|
|
173
|
-
// only apply hmr when not in ssr context and hot options are set
|
|
174
|
-
if (!ssr && makeHot) {
|
|
175
|
-
compiled.js.code = makeHot({
|
|
176
|
-
id: filename,
|
|
177
|
-
compiledCode: compiled.js.code,
|
|
178
|
-
//@ts-expect-error hot isn't a boolean at this point
|
|
179
|
-
hotOptions: { ...options.hot, injectCss: options.hot?.injectCss === true && hasCss },
|
|
180
|
-
compiled,
|
|
181
|
-
originalCode: code,
|
|
182
|
-
compileOptions: finalCompileOptions
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
172
|
}
|
|
186
173
|
|
|
187
174
|
let lang = 'js';
|
|
@@ -203,33 +190,4 @@ export const _createCompileSvelte = (makeHot) => {
|
|
|
203
190
|
preprocessed: preprocessed ?? { code }
|
|
204
191
|
};
|
|
205
192
|
};
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
210
|
-
* @returns {Function | undefined}
|
|
211
|
-
*/
|
|
212
|
-
function buildMakeHot(options) {
|
|
213
|
-
const needsMakeHot = options.hot !== false && options.isServe && !options.isProduction;
|
|
214
|
-
if (needsMakeHot) {
|
|
215
|
-
// @ts-ignore
|
|
216
|
-
const hotApi = options?.hot?.hotApi;
|
|
217
|
-
// @ts-ignore
|
|
218
|
-
const adapter = options?.hot?.adapter;
|
|
219
|
-
return createMakeHot({
|
|
220
|
-
walk: svelte.walk,
|
|
221
|
-
hotApi,
|
|
222
|
-
adapter,
|
|
223
|
-
hotOptions: { noOverlay: true, .../** @type {object} */ (options.hot) }
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
230
|
-
* @returns {import('../types/compile.d.ts').CompileSvelte}
|
|
231
|
-
*/
|
|
232
|
-
export function createCompileSvelte(options) {
|
|
233
|
-
const makeHot = buildMakeHot(options);
|
|
234
|
-
return _createCompileSvelte(makeHot);
|
|
235
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,24 +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
|
-
? {
|
|
32
|
-
js: type === 'script' || type === 'all',
|
|
33
|
-
css: type === 'style' || type === 'all'
|
|
34
|
-
}
|
|
35
|
-
: false,
|
|
28
|
+
hmr: false,
|
|
36
29
|
...svelteRequest.query.compilerOptions
|
|
37
30
|
},
|
|
38
|
-
hot: false,
|
|
39
31
|
emitCss: true
|
|
40
32
|
});
|
|
41
33
|
} catch (e) {
|
|
@@ -43,7 +35,7 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
|
|
|
43
35
|
}
|
|
44
36
|
let result;
|
|
45
37
|
if (query.type === 'style') {
|
|
46
|
-
result = compileData.compiled.css;
|
|
38
|
+
result = compileData.compiled.css ?? { code: '', map: null };
|
|
47
39
|
} else if (query.type === 'script') {
|
|
48
40
|
result = compileData.compiled.js;
|
|
49
41
|
} else if (query.type === 'preprocessed') {
|
|
@@ -66,7 +58,9 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
|
|
|
66
58
|
}
|
|
67
59
|
log.debug(`load returns direct result for ${id}`, undefined, 'load');
|
|
68
60
|
let directOutput = result.code;
|
|
61
|
+
// @ts-expect-error might not be SourceMap but toUrl check should suffice
|
|
69
62
|
if (query.sourcemap && result.map?.toUrl) {
|
|
63
|
+
// @ts-expect-error
|
|
70
64
|
const map = `sourceMappingURL=${result.map.toUrl()}`;
|
|
71
65
|
if (query.type === 'style') {
|
|
72
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,12 +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 = `Your are using Svelte ${VERSION}. Svelte 5 support is experimental, breaking changes can occur in any release until this notice is removed.`;
|
|
266
|
-
const wip = [
|
|
267
|
-
'svelte-inspector is disabled until dev mode implements node to code mapping',
|
|
268
|
-
'hmr for .svelte files is disabled until hmr api is implemented'
|
|
269
|
-
];
|
|
270
|
-
log.warn(`${notice}\nwork in progress:\n - ${wip.join('\n - ')}\n`);
|
|
271
|
-
}
|
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 } from './svelte-version.js';
|
|
30
28
|
|
|
31
29
|
const allowedPluginOptions = new Set([
|
|
32
30
|
'include',
|
|
@@ -194,17 +192,13 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
|
|
|
194
192
|
const css = preResolveOptions.emitCss ? 'external' : 'injected';
|
|
195
193
|
/** @type {Partial<import('../public.d.ts').Options>} */
|
|
196
194
|
const defaultOptions = {
|
|
197
|
-
hot: viteConfig.isProduction
|
|
198
|
-
? false
|
|
199
|
-
: {
|
|
200
|
-
injectCss: css === 'injected',
|
|
201
|
-
partialAccept: !!viteConfig.experimental?.hmrPartialAccept
|
|
202
|
-
},
|
|
203
195
|
compilerOptions: {
|
|
204
196
|
css,
|
|
205
|
-
dev: !viteConfig.isProduction
|
|
197
|
+
dev: !viteConfig.isProduction,
|
|
198
|
+
hmr: !viteConfig.isProduction && !preResolveOptions.isBuild
|
|
206
199
|
}
|
|
207
200
|
};
|
|
201
|
+
|
|
208
202
|
/** @type {Partial<import('../types/options.d.ts').ResolvedOptions>} */
|
|
209
203
|
const extraOptions = {
|
|
210
204
|
root: viteConfig.root,
|
|
@@ -230,48 +224,12 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
|
|
|
230
224
|
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
231
225
|
*/
|
|
232
226
|
function enforceOptionsForHmr(options) {
|
|
233
|
-
if (isSvelte5) {
|
|
234
|
-
// TODO add hmr options for svelte5 once it is supported and update utils/log.js#logSvelte5Warning
|
|
235
|
-
options.hot = false;
|
|
236
|
-
}
|
|
237
227
|
if (options.hot) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (options.hot !== true && options.hot.injectCss) {
|
|
244
|
-
log.warn('hmr and emitCss are enabled but hot.injectCss is true, forcing it to false');
|
|
245
|
-
options.hot.injectCss = false;
|
|
246
|
-
}
|
|
247
|
-
const css = options.compilerOptions.css;
|
|
248
|
-
if (css === true || css === 'injected') {
|
|
249
|
-
const forcedCss = 'external';
|
|
250
|
-
log.warn(
|
|
251
|
-
`hmr and emitCss are enabled but compilerOptions.css is ${css}, forcing it to ${forcedCss}`
|
|
252
|
-
);
|
|
253
|
-
options.compilerOptions.css = forcedCss;
|
|
254
|
-
}
|
|
255
|
-
} else {
|
|
256
|
-
if (options.hot === true || !options.hot.injectCss) {
|
|
257
|
-
log.warn(
|
|
258
|
-
'hmr with emitCss disabled requires option hot.injectCss to be enabled, forcing it to true'
|
|
259
|
-
);
|
|
260
|
-
if (options.hot === true) {
|
|
261
|
-
options.hot = { injectCss: true };
|
|
262
|
-
} else {
|
|
263
|
-
options.hot.injectCss = true;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
const css = options.compilerOptions.css;
|
|
267
|
-
if (!(css === true || css === 'injected')) {
|
|
268
|
-
const forcedCss = 'injected';
|
|
269
|
-
log.warn(
|
|
270
|
-
`hmr with emitCss disabled requires compilerOptions.css to be enabled, forcing it to ${forcedCss}`
|
|
271
|
-
);
|
|
272
|
-
options.compilerOptions.css = forcedCss;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
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;
|
|
275
233
|
}
|
|
276
234
|
}
|
|
277
235
|
|
|
@@ -280,9 +238,11 @@ function enforceOptionsForHmr(options) {
|
|
|
280
238
|
*/
|
|
281
239
|
function enforceOptionsForProduction(options) {
|
|
282
240
|
if (options.isProduction) {
|
|
283
|
-
if (options.
|
|
284
|
-
log.warn(
|
|
285
|
-
|
|
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;
|
|
286
246
|
}
|
|
287
247
|
if (options.compilerOptions.dev) {
|
|
288
248
|
log.warn(
|
|
@@ -370,7 +330,7 @@ export async function buildExtraViteConfig(options, config) {
|
|
|
370
330
|
/** @type {Partial<import('vite').UserConfig>} */
|
|
371
331
|
const extraViteConfig = {
|
|
372
332
|
resolve: {
|
|
373
|
-
dedupe: [...SVELTE_IMPORTS
|
|
333
|
+
dedupe: [...SVELTE_IMPORTS],
|
|
374
334
|
conditions: [...SVELTE_EXPORT_CONDITIONS]
|
|
375
335
|
}
|
|
376
336
|
// this option is still awaiting a PR in vite to be supported
|
|
@@ -429,12 +389,7 @@ export async function buildExtraViteConfig(options, config) {
|
|
|
429
389
|
}
|
|
430
390
|
|
|
431
391
|
// enable hmrPartialAccept if not explicitly disabled
|
|
432
|
-
if (
|
|
433
|
-
(options.hot == null ||
|
|
434
|
-
options.hot === true ||
|
|
435
|
-
(options.hot && options.hot.partialAccept !== false)) && // deviate from svelte-hmr, default to true
|
|
436
|
-
config.experimental?.hmrPartialAccept !== false
|
|
437
|
-
) {
|
|
392
|
+
if (config.experimental?.hmrPartialAccept !== false) {
|
|
438
393
|
log.debug('enabling "experimental.hmrPartialAccept" in vite config', undefined, 'config');
|
|
439
394
|
extraViteConfig.experimental = { hmrPartialAccept: true };
|
|
440
395
|
}
|
|
@@ -579,9 +534,10 @@ function buildExtraConfigForSvelte(config) {
|
|
|
579
534
|
// include svelte imports for optimization unless explicitly excluded
|
|
580
535
|
/** @type {string[]} */
|
|
581
536
|
const include = [];
|
|
582
|
-
|
|
537
|
+
/** @type {string[]} */
|
|
538
|
+
const exclude = [];
|
|
583
539
|
if (!isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
|
|
584
|
-
const svelteImportsToInclude = SVELTE_IMPORTS
|
|
540
|
+
const svelteImportsToInclude = SVELTE_IMPORTS;
|
|
585
541
|
log.debug(
|
|
586
542
|
`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `,
|
|
587
543
|
undefined,
|
|
@@ -600,7 +556,7 @@ function buildExtraConfigForSvelte(config) {
|
|
|
600
556
|
/** @type {string[]} */
|
|
601
557
|
const external = [];
|
|
602
558
|
// add svelte to ssr.noExternal unless it is present in ssr.external
|
|
603
|
-
// so
|
|
559
|
+
// so it is correctly resolving according to the conditions in sveltes exports map
|
|
604
560
|
if (!isDepExternaled('svelte', config.ssr?.external ?? [])) {
|
|
605
561
|
noExternal.push('svelte', /^svelte\//);
|
|
606
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`
|
|
@@ -3,9 +3,10 @@ import { VERSION } from 'svelte/compiler';
|
|
|
3
3
|
/**
|
|
4
4
|
* @type {boolean}
|
|
5
5
|
*/
|
|
6
|
-
export const
|
|
6
|
+
export const isSvelte5 = VERSION.startsWith('5.');
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @type {boolean}
|
|
10
10
|
*/
|
|
11
|
-
export const
|
|
11
|
+
export const isSvelte5WithHMRSupport =
|
|
12
|
+
VERSION.startsWith('5.0.0-next.') && Number(VERSION.slice(11)) > 96;
|
|
@@ -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
|
}
|