@sveltejs/vite-plugin-svelte 3.0.2 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/vite-plugin-svelte",
3
- "version": "3.0.2",
3
+ "version": "3.1.1",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
@@ -36,13 +36,13 @@
36
36
  },
37
37
  "homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
38
38
  "dependencies": {
39
+ "@sveltejs/vite-plugin-svelte-inspector": "^2.1.0",
39
40
  "debug": "^4.3.4",
40
41
  "deepmerge": "^4.3.1",
41
42
  "kleur": "^4.1.5",
42
- "magic-string": "^0.30.5",
43
- "svelte-hmr": "^0.15.3",
44
- "vitefu": "^0.2.5",
45
- "@sveltejs/vite-plugin-svelte-inspector": "^2.0.0"
43
+ "magic-string": "^0.30.10",
44
+ "svelte-hmr": "^0.16.0",
45
+ "vitefu": "^0.2.5"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "svelte": "^4.0.0 || ^5.0.0-next.0",
@@ -50,10 +50,10 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/debug": "^4.1.12",
53
- "esbuild": "^0.19.12",
54
- "sass": "^1.70.0",
55
- "svelte": "^4.2.9",
56
- "vite": "^5.0.11"
53
+ "esbuild": "^0.20.2",
54
+ "sass": "^1.75.0",
55
+ "svelte": "^4.2.15",
56
+ "vite": "^5.2.9"
57
57
  },
58
58
  "scripts": {
59
59
  "check:publint": "publint --strict",
package/src/index.js CHANGED
@@ -178,7 +178,8 @@ export function svelte(inlineOptions) {
178
178
  },
179
179
 
180
180
  handleHotUpdate(ctx) {
181
- if (!options.hot || !options.emitCss) {
181
+ // @ts-expect-error svelte4 does not have hmr option
182
+ if ((!options.hot && !options.compilerOptions.hmr) || !options.emitCss) {
182
183
  return;
183
184
  }
184
185
  const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
package/src/preprocess.js CHANGED
@@ -61,29 +61,16 @@ function viteScript() {
61
61
  * @returns {{ style: import('svelte/compiler').Preprocessor }}
62
62
  */
63
63
  function viteStyle(config = {}) {
64
- /** @type {CssTransform} */
65
- let transform;
64
+ /** @type {Promise<CssTransform> | CssTransform} */
65
+ let cssTransform;
66
66
  /** @type {import('svelte/compiler').Preprocessor} */
67
67
  const style = async ({ attributes, content, filename = '' }) => {
68
68
  const ext = attributes.lang ? `.${attributes.lang}` : '.css';
69
69
  if (attributes.lang && !isCSSRequest(ext)) return;
70
- if (!transform) {
71
- /** @type {import('vite').ResolvedConfig} */
72
- let resolvedConfig;
73
- // @ts-expect-error special prop added if running in v-p-s
74
- if (style.__resolvedConfig) {
75
- // @ts-expect-error
76
- resolvedConfig = style.__resolvedConfig;
77
- } else if (isResolvedConfig(config)) {
78
- resolvedConfig = config;
79
- } else {
80
- resolvedConfig = await resolveConfig(
81
- config,
82
- process.env.NODE_ENV === 'production' ? 'build' : 'serve'
83
- );
84
- }
85
- transform = getCssTransformFn(resolvedConfig);
70
+ if (!cssTransform) {
71
+ cssTransform = createCssTransform(style, config).then((t) => (cssTransform = t));
86
72
  }
73
+ const transform = await cssTransform;
87
74
  const suffix = `${lang_sep}${ext}`;
88
75
  const moduleId = `${filename}${suffix}`;
89
76
  const { code, map, deps } = await transform(content, moduleId);
@@ -102,12 +89,27 @@ function viteStyle(config = {}) {
102
89
  }
103
90
 
104
91
  /**
105
- * @param {import('vite').ResolvedConfig} config
106
- * @returns {CssTransform}
92
+ * @param {import('svelte/compiler').Preprocessor} style
93
+ * @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config
94
+ * @returns {Promise<CssTransform>}
107
95
  */
108
- function getCssTransformFn(config) {
96
+ async function createCssTransform(style, config) {
97
+ /** @type {import('vite').ResolvedConfig} */
98
+ let resolvedConfig;
99
+ // @ts-expect-error special prop added if running in v-p-s
100
+ if (style.__resolvedConfig) {
101
+ // @ts-expect-error
102
+ resolvedConfig = style.__resolvedConfig;
103
+ } else if (isResolvedConfig(config)) {
104
+ resolvedConfig = config;
105
+ } else {
106
+ resolvedConfig = await resolveConfig(
107
+ config,
108
+ process.env.NODE_ENV === 'production' ? 'build' : 'serve'
109
+ );
110
+ }
109
111
  return async (code, filename) => {
110
- return preprocessCSS(code, filename, config);
112
+ return preprocessCSS(code, filename, resolvedConfig);
111
113
  };
112
114
  }
113
115
 
@@ -71,13 +71,6 @@ export const _createCompileSvelte = (makeHot) => {
71
71
  const hash = `s-${safeBase64Hash(normalizedFilename)}`;
72
72
  compileOptions.cssHash = () => hash;
73
73
  }
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
74
 
82
75
  let preprocessed;
83
76
  let preprocessors = options.preprocess;
@@ -210,7 +203,8 @@ export const _createCompileSvelte = (makeHot) => {
210
203
  * @returns {Function | undefined}
211
204
  */
212
205
  function buildMakeHot(options) {
213
- const needsMakeHot = options.hot !== false && options.isServe && !options.isProduction;
206
+ const needsMakeHot =
207
+ !isSvelte5 && options.hot !== false && options.isServe && !options.isProduction;
214
208
  if (needsMakeHot) {
215
209
  // @ts-ignore
216
210
  const hotApi = options?.hot?.hotApi;
@@ -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
- import { isSvelte4 } from './svelte-version.js';
4
+ import { isSvelte4, isSvelte5 } from './svelte-version.js';
5
5
  /**
6
6
  * utility function to compile ?raw and ?direct requests in load hook
7
7
  *
@@ -27,12 +27,14 @@ export async function loadRaw(svelteRequest, compileSvelte, options) {
27
27
  compilerOptions: {
28
28
  dev: false,
29
29
  css: 'external',
30
- enableSourcemap: query.sourcemap
31
- ? {
32
- js: type === 'script' || type === 'all',
33
- css: type === 'style' || type === 'all'
34
- }
35
- : false,
30
+ enableSourcemap: isSvelte5
31
+ ? undefined
32
+ : query.sourcemap
33
+ ? {
34
+ js: type === 'script' || type === 'all',
35
+ css: type === 'style' || type === 'all'
36
+ }
37
+ : false,
36
38
  ...svelteRequest.query.compilerOptions
37
39
  },
38
40
  hot: false,
package/src/utils/log.js CHANGED
@@ -262,10 +262,7 @@ export function isDebugNamespaceEnabled(namespace) {
262
262
  }
263
263
 
264
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
- ];
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'];
270
267
  log.warn(`${notice}\nwork in progress:\n - ${wip.join('\n - ')}\n`);
271
268
  }
@@ -26,7 +26,7 @@ import {
26
26
  import { isCommonDepWithoutSvelteField } from './dependencies.js';
27
27
  import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
28
28
  import { VitePluginSvelteCache } from './vite-plugin-svelte-cache.js';
29
- import { isSvelte5 } from './svelte-version.js';
29
+ import { isSvelte5, isSvelte5WithHMRSupport } from './svelte-version.js';
30
30
 
31
31
  const allowedPluginOptions = new Set([
32
32
  'include',
@@ -194,17 +194,26 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
194
194
  const css = preResolveOptions.emitCss ? 'external' : 'injected';
195
195
  /** @type {Partial<import('../public.d.ts').Options>} */
196
196
  const defaultOptions = {
197
- hot: viteConfig.isProduction
198
- ? false
199
- : {
200
- injectCss: css === 'injected',
201
- partialAccept: !!viteConfig.experimental?.hmrPartialAccept
202
- },
203
197
  compilerOptions: {
204
198
  css,
205
199
  dev: !viteConfig.isProduction
206
200
  }
207
201
  };
202
+ const hot =
203
+ !viteConfig.isProduction && !preResolveOptions.isBuild && viteConfig.server.hmr !== false;
204
+ if (isSvelte5) {
205
+ if (isSvelte5WithHMRSupport) {
206
+ // @ts-expect-error svelte4 does not have hmr option
207
+ defaultOptions.compilerOptions.hmr = hot;
208
+ }
209
+ } else {
210
+ defaultOptions.hot = !hot
211
+ ? false
212
+ : {
213
+ injectCss: css === 'injected',
214
+ partialAccept: !!viteConfig.experimental?.hmrPartialAccept
215
+ };
216
+ }
208
217
  /** @type {Partial<import('../types/options.d.ts').ResolvedOptions>} */
209
218
  const extraOptions = {
210
219
  root: viteConfig.root,
@@ -217,7 +226,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
217
226
  removeIgnoredOptions(merged);
218
227
  handleDeprecatedOptions(merged);
219
228
  addExtraPreprocessors(merged, viteConfig);
220
- enforceOptionsForHmr(merged);
229
+ enforceOptionsForHmr(merged, viteConfig);
221
230
  enforceOptionsForProduction(merged);
222
231
  // mergeConfigs would mangle functions on the stats class, so do this afterwards
223
232
  if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
@@ -228,48 +237,62 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
228
237
 
229
238
  /**
230
239
  * @param {import('../types/options.d.ts').ResolvedOptions} options
240
+ * @param {import('vite').ResolvedConfig} viteConfig
231
241
  */
232
- function enforceOptionsForHmr(options) {
233
- if (isSvelte5) {
234
- // TODO add hmr options for svelte5 once it is supported and update utils/log.js#logSvelte5Warning
242
+ function enforceOptionsForHmr(options, viteConfig) {
243
+ if (options.hot && viteConfig.server.hmr === false) {
244
+ log.warn(
245
+ 'vite config server.hmr is false but hot is true. Forcing hot to false as it would not work.'
246
+ );
235
247
  options.hot = false;
236
248
  }
237
- if (options.hot) {
238
- if (!options.compilerOptions.dev) {
239
- log.warn('hmr is enabled but compilerOptions.dev is false, forcing it to true');
240
- options.compilerOptions.dev = true;
249
+ if (isSvelte5) {
250
+ if (options.hot && isSvelte5WithHMRSupport) {
251
+ log.warn(
252
+ 'svelte 5 has hmr integrated in core. Please remove the hot option and use compilerOptions.hmr instead'
253
+ );
254
+ delete options.hot;
255
+ // @ts-expect-error hmr option doesn't exist in svelte4
256
+ options.compilerOptions.hmr = true;
241
257
  }
242
- if (options.emitCss) {
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;
258
+ } else {
259
+ if (options.hot) {
260
+ if (!options.compilerOptions.dev) {
261
+ log.warn('hmr is enabled but compilerOptions.dev is false, forcing it to true');
262
+ options.compilerOptions.dev = true;
254
263
  }
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
+ if (options.emitCss) {
265
+ if (options.hot !== true && options.hot.injectCss) {
266
+ log.warn('hmr and emitCss are enabled but hot.injectCss is true, forcing it to false');
267
+ options.hot.injectCss = false;
268
+ }
269
+ const css = options.compilerOptions.css;
270
+ if (css === true || css === 'injected') {
271
+ const forcedCss = 'external';
272
+ log.warn(
273
+ `hmr and emitCss are enabled but compilerOptions.css is ${css}, forcing it to ${forcedCss}`
274
+ );
275
+ options.compilerOptions.css = forcedCss;
276
+ }
277
+ } else {
278
+ if (options.hot === true || !options.hot.injectCss) {
279
+ log.warn(
280
+ 'hmr with emitCss disabled requires option hot.injectCss to be enabled, forcing it to true'
281
+ );
282
+ if (options.hot === true) {
283
+ options.hot = { injectCss: true };
284
+ } else {
285
+ options.hot.injectCss = true;
286
+ }
287
+ }
288
+ const css = options.compilerOptions.css;
289
+ if (!(css === true || css === 'injected')) {
290
+ const forcedCss = 'injected';
291
+ log.warn(
292
+ `hmr with emitCss disabled requires compilerOptions.css to be enabled, forcing it to ${forcedCss}`
293
+ );
294
+ options.compilerOptions.css = forcedCss;
264
295
  }
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
296
  }
274
297
  }
275
298
  }
@@ -9,3 +9,9 @@ export const isSvelte4 = VERSION.startsWith('4.');
9
9
  * @type {boolean}
10
10
  */
11
11
  export const isSvelte5 = VERSION.startsWith('5.');
12
+
13
+ /**
14
+ * @type {boolean}
15
+ */
16
+ export const isSvelte5WithHMRSupport =
17
+ VERSION.startsWith('5.0.0-next.') && Number(VERSION.slice(11)) > 96;