@sveltejs/vite-plugin-svelte 6.0.0-next.3 → 6.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": "6.0.0-next.3",
3
+ "version": "6.1.0",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
36
36
  "dependencies": {
37
- "@sveltejs/vite-plugin-svelte-inspector": "^5.0.0-next.0",
37
+ "@sveltejs/vite-plugin-svelte-inspector": "^5.0.0-next.1",
38
38
  "debug": "^4.4.1",
39
39
  "deepmerge": "^4.3.1",
40
40
  "kleur": "^4.1.5",
@@ -48,8 +48,8 @@
48
48
  "devDependencies": {
49
49
  "@types/debug": "^4.1.12",
50
50
  "sass": "^1.89.2",
51
- "svelte": "^5.34.9",
52
- "vite": "^7.0.0"
51
+ "svelte": "^5.36.1",
52
+ "vite": "^7.0.4"
53
53
  },
54
54
  "scripts": {
55
55
  "check:publint": "publint --strict",
@@ -1,7 +1,8 @@
1
1
  import { buildModuleIdFilter, buildModuleIdParser } from '../utils/id.js';
2
2
  import * as svelteCompiler from 'svelte/compiler';
3
- import { logCompilerWarnings } from '../utils/log.js';
3
+ import { log, logCompilerWarnings } from '../utils/log.js';
4
4
  import { toRollupError } from '../utils/error.js';
5
+ import { isSvelteWithAsync } from '../utils/svelte-version.js';
5
6
 
6
7
  /**
7
8
  * @param {import('../types/plugin-api.d.ts').PluginAPI} api
@@ -16,6 +17,12 @@ export function compileModule(api) {
16
17
  * @type {import("../types/id.js").ModuleIdParser}
17
18
  */
18
19
  let idParser;
20
+
21
+ /**
22
+ * @type {import('svelte/compiler').ModuleCompileOptions}
23
+ */
24
+ let staticModuleCompileOptions;
25
+
19
26
  /** @type {import('vite').Plugin} */
20
27
  const plugin = {
21
28
  name: 'vite-plugin-svelte:compile-module',
@@ -25,6 +32,7 @@ export function compileModule(api) {
25
32
  //@ts-expect-error transform defined below but filter not in type
26
33
  plugin.transform.filter = buildModuleIdFilter(options);
27
34
  idParser = buildModuleIdParser(options);
35
+ staticModuleCompileOptions = filterNonModuleCompilerOptions(options.compilerOptions);
28
36
  },
29
37
  transform: {
30
38
  async handler(code, id) {
@@ -33,12 +41,48 @@ export function compileModule(api) {
33
41
  if (!moduleRequest) {
34
42
  return;
35
43
  }
44
+ const filename = moduleRequest.filename;
45
+ /** @type {import('svelte/compiler').CompileOptions} */
46
+ const compileOptions = {
47
+ ...staticModuleCompileOptions,
48
+ dev: !this.environment.config.isProduction,
49
+ generate: ssr ? 'server' : 'client',
50
+ filename
51
+ };
52
+ const dynamicCompileOptions = await options?.dynamicCompileOptions?.({
53
+ filename,
54
+ code,
55
+ compileOptions
56
+ });
57
+ if (dynamicCompileOptions && log.debug.enabled) {
58
+ log.debug(
59
+ `dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`,
60
+ undefined,
61
+ 'compileModule'
62
+ );
63
+ }
64
+ const finalCompileOptions = dynamicCompileOptions
65
+ ? {
66
+ ...compileOptions,
67
+ ...dynamicCompileOptions
68
+ }
69
+ : compileOptions;
70
+ if (dynamicCompileOptions?.experimental) {
71
+ finalCompileOptions.experimental = {
72
+ ...compileOptions.experimental,
73
+ ...dynamicCompileOptions.experimental
74
+ };
75
+ }
76
+ const finalModuleCompileOptions = filterNonModuleCompilerOptions(finalCompileOptions);
77
+ if (log.debug.enabled) {
78
+ log.debug(
79
+ `final ModuleCompileOptions for ${filename}: ${JSON.stringify(finalModuleCompileOptions)}`,
80
+ undefined,
81
+ 'compileModule'
82
+ );
83
+ }
36
84
  try {
37
- const compileResult = svelteCompiler.compileModule(code, {
38
- dev: !this.environment.config.isProduction,
39
- generate: ssr ? 'server' : 'client',
40
- filename: moduleRequest.filename
41
- });
85
+ const compileResult = svelteCompiler.compileModule(code, finalModuleCompileOptions);
42
86
  logCompilerWarnings(moduleRequest, compileResult.warnings, options);
43
87
  return compileResult.js;
44
88
  } catch (e) {
@@ -49,3 +93,38 @@ export function compileModule(api) {
49
93
  };
50
94
  return plugin;
51
95
  }
96
+
97
+ /**
98
+ *
99
+ * @param {import('svelte/compiler').CompileOptions} compilerOptions
100
+ * @return {import('svelte/compiler').ModuleCompileOptions}
101
+ */
102
+ function filterNonModuleCompilerOptions(compilerOptions) {
103
+ /** @type {Array<keyof import('svelte/compiler').ModuleCompileOptions>} */
104
+ const knownModuleCompileOptionNames = ['dev', 'generate', 'filename', 'rootDir', 'warningFilter'];
105
+ if (isSvelteWithAsync) {
106
+ knownModuleCompileOptionNames.push('experimental');
107
+ }
108
+ // not typed but this is temporary until svelte itself ignores CompileOptions passed to compileModule
109
+ const experimentalModuleCompilerOptionNames = ['async'];
110
+
111
+ /** @type {import('svelte/compiler').ModuleCompileOptions} */
112
+ const filtered = filterByPropNames(compilerOptions, knownModuleCompileOptionNames);
113
+ if (filtered.experimental) {
114
+ filtered.experimental = filterByPropNames(
115
+ filtered.experimental,
116
+ experimentalModuleCompilerOptionNames
117
+ );
118
+ }
119
+ return filtered;
120
+ }
121
+
122
+ /**
123
+ *
124
+ * @param {object} o
125
+ * @param {string[]} names
126
+ * @returns {object}
127
+ */
128
+ function filterByPropNames(o, names) {
129
+ return Object.fromEntries(Object.entries(o).filter(([name]) => names.includes(name)));
130
+ }
@@ -132,6 +132,9 @@ export function enhanceCompileError(err, originalCode, preprocessors) {
132
132
 
133
133
  let m;
134
134
  while ((m = styleRe.exec(originalCode))) {
135
+ if (m[0]?.startsWith('<!--')) {
136
+ continue;
137
+ }
135
138
  // Warn missing lang attribute
136
139
  if (!m[1]?.includes('lang=')) {
137
140
  additionalMessages.push('Did you forget to add a lang attribute to your style tag?');
@@ -67,7 +67,7 @@ function findConfigToLoad(viteConfig, inlineOptions) {
67
67
  .map((candidate) => path.resolve(root, candidate))
68
68
  .filter((file) => fs.existsSync(file));
69
69
  if (existingKnownConfigFiles.length === 0) {
70
- log.debug(`no svelte config found at ${root}`, undefined, 'config');
70
+ log.info(`no Svelte config found at ${root} - using default configuration.`);
71
71
  return;
72
72
  } else if (existingKnownConfigFiles.length > 1) {
73
73
  log.warn(
@@ -3,10 +3,25 @@ import { VERSION } from 'svelte/compiler';
3
3
  /**
4
4
  * @type {boolean}
5
5
  */
6
- export const isSvelte5 = VERSION.startsWith('5.');
6
+ export const isSvelteWithAsync = gte(VERSION, '5.36.0');
7
7
 
8
8
  /**
9
- * @type {boolean}
9
+ * compare semver versions, does not include comparing tags (-next.xy is ignored)
10
+ *
11
+ * @param {string} a semver version
12
+ * @param {string} b semver version
13
+ * @return {boolean} true if a is greater or equal to b
10
14
  */
11
- export const isSvelte5WithHMRSupport =
12
- VERSION.startsWith('5.0.0-next.') && Number(VERSION.slice(11)) > 96;
15
+ export function gte(a, b) {
16
+ const aNum = a.split(/[.-]/, 3).map(Number);
17
+ const bNum = b.split(/[.-]/, 3).map(Number);
18
+ for (let i = 0; i < aNum.length; i++) {
19
+ if (aNum[i] < bNum[i]) {
20
+ return false;
21
+ }
22
+ if (aNum[i] > bNum[i]) {
23
+ return true;
24
+ }
25
+ }
26
+ return true;
27
+ }