@sveltejs/vite-plugin-svelte 3.0.1 → 3.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/vite-plugin-svelte",
3
- "version": "3.0.1",
3
+ "version": "3.1.0",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
@@ -36,12 +36,12 @@
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-next.0 || ^2.0.0",
39
+ "@sveltejs/vite-plugin-svelte-inspector": "^2.0.0",
40
40
  "debug": "^4.3.4",
41
41
  "deepmerge": "^4.3.1",
42
42
  "kleur": "^4.1.5",
43
- "magic-string": "^0.30.5",
44
- "svelte-hmr": "^0.15.3",
43
+ "magic-string": "^0.30.9",
44
+ "svelte-hmr": "^0.16.0",
45
45
  "vitefu": "^0.2.5"
46
46
  },
47
47
  "peerDependencies": {
@@ -50,10 +50,10 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/debug": "^4.1.12",
53
- "esbuild": "^0.19.7",
54
- "sass": "^1.69.5",
55
- "svelte": "^4.2.7",
56
- "vite": "^5.0.2"
53
+ "esbuild": "^0.20.2",
54
+ "sass": "^1.74.1",
55
+ "svelte": "^4.2.12",
56
+ "vite": "^5.2.8"
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);
@@ -16,7 +16,7 @@ import { isSvelte5 } from './svelte-version.js';
16
16
  // which is closer to the other regexes in at least not falling into commented script
17
17
  // but ideally would be shared exactly with svelte and other tools that use it
18
18
  const scriptLangRE =
19
- /<!--[^]*?-->|<script (?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/;
19
+ /<!--[^]*?-->|<script (?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=["']?([^"' >]+)["']?[^>]*>/g;
20
20
 
21
21
  /**
22
22
  * @param {Function} [makeHot]
@@ -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;
@@ -137,7 +130,7 @@ export const _createCompileSvelte = (makeHot) => {
137
130
  ? {
138
131
  ...compileOptions,
139
132
  ...dynamicCompileOptions
140
- }
133
+ }
141
134
  : compileOptions;
142
135
 
143
136
  const endStat = stats?.start(filename);
@@ -184,10 +177,18 @@ export const _createCompileSvelte = (makeHot) => {
184
177
  }
185
178
  }
186
179
 
180
+ let lang = 'js';
181
+ for (const match of code.matchAll(scriptLangRE)) {
182
+ if (match[1]) {
183
+ lang = match[1];
184
+ break;
185
+ }
186
+ }
187
+
187
188
  return {
188
189
  filename,
189
190
  normalizedFilename,
190
- lang: code.match(scriptLangRE)?.[1] || 'js',
191
+ lang,
191
192
  // @ts-ignore
192
193
  compiled,
193
194
  ssr,
@@ -202,7 +203,8 @@ export const _createCompileSvelte = (makeHot) => {
202
203
  * @returns {Function | undefined}
203
204
  */
204
205
  function buildMakeHot(options) {
205
- const needsMakeHot = options.hot !== false && options.isServe && !options.isProduction;
206
+ const needsMakeHot =
207
+ !isSvelte5 && options.hot !== false && options.isServe && !options.isProduction;
206
208
  if (needsMakeHot) {
207
209
  // @ts-ignore
208
210
  const hotApi = options?.hot?.hotApi;
@@ -120,7 +120,7 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
120
120
  ? {
121
121
  ...compileOptions,
122
122
  ...dynamicCompileOptions
123
- }
123
+ }
124
124
  : compileOptions;
125
125
  const endStat = statsCollection?.start(filename);
126
126
  const compiled = svelte.compile(finalCode, finalCompileOptions);
@@ -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,
@@ -63,7 +63,7 @@ export async function loadSvelteConfig(viteConfig, inlineOptions) {
63
63
  const _require = import.meta.url
64
64
  ? esmRequire ?? (esmRequire = createRequire(import.meta.url))
65
65
  : // eslint-disable-next-line no-undef
66
- require;
66
+ require;
67
67
 
68
68
  // avoid loading cached version on reload
69
69
  delete _require.cache[_require.resolve(configFile)];
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,24 @@ 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
+ if (isSvelte5) {
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
+ }
208
215
  /** @type {Partial<import('../types/options.d.ts').ResolvedOptions>} */
209
216
  const extraOptions = {
210
217
  root: viteConfig.root,
@@ -231,45 +238,52 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
231
238
  */
232
239
  function enforceOptionsForHmr(options) {
233
240
  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
- 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;
241
+ if (options.hot && isSvelte5WithHMRSupport) {
242
+ log.warn(
243
+ 'svelte 5 has hmr integrated in core. Please remove the hot option and use compilerOptions.hmr instead'
244
+ );
245
+ delete options.hot;
246
+ // @ts-expect-error hmr option doesn't exist in svelte4
247
+ options.compilerOptions.hmr = true;
241
248
  }
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;
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
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;
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;
264
286
  }
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
287
  }
274
288
  }
275
289
  }
@@ -551,7 +565,7 @@ async function buildExtraConfigForDependencies(options, config) {
551
565
  .slice(0, -1)
552
566
  .some((d) => isDepExcluded(d.trim(), userExclude))
553
567
  );
554
- });
568
+ });
555
569
  }
556
570
  if (options.disableDependencyReinclusion === true) {
557
571
  depsConfig.optimizeDeps.include = depsConfig.optimizeDeps.include.filter(
@@ -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;