@sveltejs/vite-plugin-svelte 2.1.1 → 2.2.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/dist/index.d.ts CHANGED
@@ -4,6 +4,73 @@ export { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
4
4
  import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
5
5
  export { MarkupPreprocessor, Preprocessor, PreprocessorGroup, Processed } from 'svelte/types/compiler/preprocess';
6
6
 
7
+ interface InspectorOptions {
8
+ /**
9
+ * define a key combo to toggle inspector,
10
+ * @default 'meta-shift' on mac, 'control-shift' on other os
11
+ *
12
+ * any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
13
+ * examples: control-shift, control-o, control-alt-s meta-x control-meta
14
+ * Some keys have native behavior (e.g. alt-s opens history menu on firefox).
15
+ * To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
16
+ */
17
+ toggleKeyCombo?: string;
18
+ /**
19
+ * define keys to select elements with via keyboard
20
+ * @default {parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }
21
+ *
22
+ * improves accessibility and also helps when you want to select elements that do not have a hoverable surface area
23
+ * due to tight wrapping
24
+ *
25
+ * A note for users of screen-readers:
26
+ * If you are using arrow keys to navigate the page itself, change the navKeys to avoid conflicts.
27
+ * e.g. navKeys: {parent: 'w', prev: 'a', child: 's', next: 'd'}
28
+ *
29
+ *
30
+ * parent: select closest parent
31
+ * child: select first child (or grandchild)
32
+ * next: next sibling (or parent if no next sibling exists)
33
+ * prev: previous sibling (or parent if no prev sibling exists)
34
+ */
35
+ navKeys?: {
36
+ parent: string;
37
+ child: string;
38
+ next: string;
39
+ prev: string;
40
+ };
41
+ /**
42
+ * define key to open the editor for the currently selected dom node
43
+ *
44
+ * @default 'Enter'
45
+ */
46
+ openKey?: string;
47
+ /**
48
+ * inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
49
+ * @default true
50
+ */
51
+ holdMode?: boolean;
52
+ /**
53
+ * when to show the toggle button
54
+ * @default 'active'
55
+ */
56
+ showToggleButton?: 'always' | 'active' | 'never';
57
+ /**
58
+ * where to display the toggle button
59
+ * @default top-right
60
+ */
61
+ toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
62
+ /**
63
+ * inject custom styles when inspector is active
64
+ */
65
+ customStyles?: boolean;
66
+ /**
67
+ * internal options that are automatically set, not to be set or used by users
68
+ */
69
+ __internal?: {
70
+ base: string;
71
+ };
72
+ }
73
+
7
74
  type Options = Omit<SvelteOptions, 'vitePlugin'> & PluginOptionsInline;
8
75
  interface PluginOptionsInline extends PluginOptions {
9
76
  /**
@@ -85,6 +152,12 @@ interface PluginOptions {
85
152
  * @default true for dev, false for build
86
153
  */
87
154
  prebundleSvelteLibraries?: boolean;
155
+ /**
156
+ * toggle/configure Svelte Inspector
157
+ *
158
+ * @default true
159
+ */
160
+ inspector?: InspectorOptions | boolean;
88
161
  /**
89
162
  * These options are considered experimental and breaking changes to them can occur in any release
90
163
  */
@@ -148,10 +221,6 @@ interface ExperimentalOptions {
148
221
  code: string;
149
222
  compileOptions: Partial<CompileOptions>;
150
223
  }) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
151
- /**
152
- * enable svelte inspector
153
- */
154
- inspector?: InspectorOptions | boolean;
155
224
  /**
156
225
  * send a websocket message with svelte compiler warnings during dev
157
226
  *
@@ -164,74 +233,6 @@ interface ExperimentalOptions {
164
233
  */
165
234
  disableSvelteResolveWarnings?: boolean;
166
235
  }
167
- interface InspectorOptions {
168
- /**
169
- * define a key combo to toggle inspector,
170
- * @default 'control-shift' on windows, 'meta-shift' on other os
171
- *
172
- * any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
173
- * examples: control-shift, control-o, control-alt-s meta-x control-meta
174
- * Some keys have native behavior (e.g. alt-s opens history menu on firefox).
175
- * To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
176
- */
177
- toggleKeyCombo?: string;
178
- /**
179
- * define keys to select elements with via keyboard
180
- * @default {parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }
181
- *
182
- * improves accessibility and also helps when you want to select elements that do not have a hoverable surface area
183
- * due to tight wrapping
184
- *
185
- * A note for users of screen-readers:
186
- * If you are using arrow keys to navigate the page itself, change the navKeys to avoid conflicts.
187
- * e.g. navKeys: {parent: 'w', prev: 'a', child: 's', next: 'd'}
188
- *
189
- *
190
- * parent: select closest parent
191
- * child: select first child (or grandchild)
192
- * next: next sibling (or parent if no next sibling exists)
193
- * prev: previous sibling (or parent if no prev sibling exists)
194
- */
195
- navKeys?: {
196
- parent: string;
197
- child: string;
198
- next: string;
199
- prev: string;
200
- };
201
- /**
202
- * define key to open the editor for the currently selected dom node
203
- *
204
- * @default 'Enter'
205
- */
206
- openKey?: string;
207
- /**
208
- * inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
209
- * @default false
210
- */
211
- holdMode?: boolean;
212
- /**
213
- * when to show the toggle button
214
- * @default 'active'
215
- */
216
- showToggleButton?: 'always' | 'active' | 'never';
217
- /**
218
- * where to display the toggle button
219
- * @default top-right
220
- */
221
- toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
222
- /**
223
- * inject custom styles when inspector is active
224
- */
225
- customStyles?: boolean;
226
- /**
227
- * append an import to the module id ending with `appendTo` instead of adding a script into body
228
- * useful for frameworks that do not support trannsformIndexHtml hook
229
- *
230
- * WARNING: only set this if you know exactly what it does.
231
- * Regular users of vite-plugin-svelte or SvelteKit do not need it
232
- */
233
- appendTo?: string;
234
- }
235
236
  type ModuleFormat = NonNullable<CompileOptions['format']>;
236
237
  type CssHashGetter = NonNullable<CompileOptions['cssHash']>;
237
238
  type Arrayable<T> = T | T[];
package/dist/index.js CHANGED
@@ -1169,16 +1169,11 @@ var allowedPluginOptions = /* @__PURE__ */ new Set([
1169
1169
  "ignorePluginPreprocessors",
1170
1170
  "disableDependencyReinclusion",
1171
1171
  "prebundleSvelteLibraries",
1172
+ "inspector",
1172
1173
  "experimental"
1173
1174
  ]);
1174
1175
  var knownRootOptions = /* @__PURE__ */ new Set(["extensions", "compilerOptions", "preprocess", "onwarn"]);
1175
- var allowedInlineOptions = /* @__PURE__ */ new Set([
1176
- "configFile",
1177
- "kit",
1178
- // only for internal use by sveltekit
1179
- ...allowedPluginOptions,
1180
- ...knownRootOptions
1181
- ]);
1176
+ var allowedInlineOptions = /* @__PURE__ */ new Set(["configFile", ...allowedPluginOptions, ...knownRootOptions]);
1182
1177
  function validateInlineOptions(inlineOptions) {
1183
1178
  const invalidKeys = Object.keys(inlineOptions || {}).filter(
1184
1179
  (key) => !allowedInlineOptions.has(key)
@@ -1300,7 +1295,6 @@ function resolveOptions(preResolveOptions2, viteConfig, cache) {
1300
1295
  const merged = mergeConfigs(defaultOptions, preResolveOptions2, extraOptions);
1301
1296
  removeIgnoredOptions(merged);
1302
1297
  handleDeprecatedOptions(merged);
1303
- addSvelteKitOptions(merged);
1304
1298
  addExtraPreprocessors(merged, viteConfig);
1305
1299
  enforceOptionsForHmr(merged);
1306
1300
  enforceOptionsForProduction(merged);
@@ -1382,21 +1376,21 @@ function removeIgnoredOptions(options) {
1382
1376
  });
1383
1377
  }
1384
1378
  }
1385
- function addSvelteKitOptions(options) {
1386
- if (options?.kit != null && options.compilerOptions.hydratable == null) {
1387
- log.debug(`Setting compilerOptions.hydratable = true for SvelteKit`);
1388
- options.compilerOptions.hydratable = true;
1389
- }
1390
- }
1391
1379
  function handleDeprecatedOptions(options) {
1392
- if (options.experimental?.prebundleSvelteLibraries) {
1393
- options.prebundleSvelteLibraries = options.experimental?.prebundleSvelteLibraries;
1394
- log.warn(
1395
- "experimental.prebundleSvelteLibraries is no longer experimental and has moved to prebundleSvelteLibraries"
1396
- );
1397
- }
1398
- if (options.experimental?.generateMissingPreprocessorSourcemaps) {
1399
- log.warn("experimental.generateMissingPreprocessorSourcemaps has been removed.");
1380
+ const experimental = options.experimental;
1381
+ if (experimental) {
1382
+ for (const promoted of ["prebundleSvelteLibraries", "inspector"]) {
1383
+ if (experimental[promoted]) {
1384
+ options[promoted] = experimental[promoted];
1385
+ delete experimental[promoted];
1386
+ log.warn(
1387
+ `Option "vitePlugin.experimental.${promoted}" is no longer experimental and has moved to "vitePlugin.${promoted}". Please update your svelte config.`
1388
+ );
1389
+ }
1390
+ }
1391
+ if (experimental.generateMissingPreprocessorSourcemaps) {
1392
+ log.warn("experimental.generateMissingPreprocessorSourcemaps has been removed.");
1393
+ }
1400
1394
  }
1401
1395
  }
1402
1396
  function resolveViteRoot(viteConfig) {
@@ -1763,16 +1757,67 @@ function idToFile(id) {
1763
1757
  return id.replace(hashRE, "").replace(queryRE, "");
1764
1758
  }
1765
1759
 
1766
- // src/ui/inspector/plugin.ts
1760
+ // src/ui/inspector/options.ts
1761
+ import * as process2 from "process";
1762
+ import { loadEnv } from "vite";
1767
1763
  var defaultInspectorOptions = {
1768
- toggleKeyCombo: process.platform === "win32" ? "control-shift" : "meta-shift",
1764
+ toggleKeyCombo: process2.platform === "darwin" ? "meta-shift" : "control-shift",
1769
1765
  navKeys: { parent: "ArrowUp", child: "ArrowDown", next: "ArrowRight", prev: "ArrowLeft" },
1770
1766
  openKey: "Enter",
1771
- holdMode: false,
1767
+ holdMode: true,
1772
1768
  showToggleButton: "active",
1773
1769
  toggleButtonPos: "top-right",
1774
1770
  customStyles: true
1775
1771
  };
1772
+ function parseEnvironmentOptions(config) {
1773
+ const env = loadEnv(config.mode, config.envDir ?? process2.cwd(), "SVELTE_INSPECTOR");
1774
+ const options = env?.SVELTE_INSPECTOR_OPTIONS;
1775
+ const toggle = env?.SVELTE_INSPECTOR_TOGGLE;
1776
+ if (options) {
1777
+ try {
1778
+ const parsed = JSON.parse(options);
1779
+ const parsedType = typeof parsed;
1780
+ if (parsedType === "boolean") {
1781
+ return parsed;
1782
+ } else if (parsedType === "object") {
1783
+ if (Array.isArray(parsed)) {
1784
+ throw new Error("invalid type, expected object map but got array");
1785
+ }
1786
+ const parsedKeys = Object.keys(parsed);
1787
+ const defaultKeys = Object.keys(defaultInspectorOptions);
1788
+ const unknownKeys = parsedKeys.filter((k) => !defaultKeys.includes(k));
1789
+ if (unknownKeys.length) {
1790
+ log.warn(
1791
+ `ignoring unknown options in environment SVELTE_INSPECTOR_OPTIONS: ${unknownKeys.join(
1792
+ ", "
1793
+ )}.`,
1794
+ void 0,
1795
+ "inspector"
1796
+ );
1797
+ for (const key of unknownKeys) {
1798
+ delete parsed[key];
1799
+ }
1800
+ }
1801
+ log.debug("loaded environment config", parsed, "inspector");
1802
+ return parsed;
1803
+ }
1804
+ } catch (e) {
1805
+ log.error(
1806
+ `failed to parse inspector options from environment SVELTE_INSPECTOR_OPTIONS="${options}"`,
1807
+ e,
1808
+ "inspector"
1809
+ );
1810
+ }
1811
+ } else if (toggle) {
1812
+ const keyConfig = {
1813
+ toggleKeyCombo: toggle
1814
+ };
1815
+ log.debug("loaded environment config", keyConfig, "inspector");
1816
+ return keyConfig;
1817
+ }
1818
+ }
1819
+
1820
+ // src/ui/inspector/plugin.ts
1776
1821
  function getInspectorPath() {
1777
1822
  const pluginPath = normalizePath5(path9.dirname(fileURLToPath(import.meta.url)));
1778
1823
  return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, "/vite-plugin-svelte/src/ui/inspector/");
@@ -1781,7 +1826,6 @@ function svelteInspector() {
1781
1826
  const inspectorPath = getInspectorPath();
1782
1827
  log.debug.enabled && log.debug(`svelte inspector path: ${inspectorPath}`);
1783
1828
  let inspectorOptions;
1784
- let appendTo;
1785
1829
  let disabled = false;
1786
1830
  return {
1787
1831
  name: "vite-plugin-svelte:inspector",
@@ -1789,21 +1833,30 @@ function svelteInspector() {
1789
1833
  enforce: "pre",
1790
1834
  configResolved(config) {
1791
1835
  const vps = config.plugins.find((p) => p.name === "vite-plugin-svelte");
1792
- const options = vps?.api?.options?.experimental?.inspector;
1793
- if (!vps || !options) {
1794
- log.debug("inspector disabled, could not find config");
1836
+ if (!vps) {
1837
+ log.warn("vite-plugin-svelte is missing, inspector disabled", void 0, "inspector");
1795
1838
  disabled = true;
1796
1839
  return;
1797
1840
  }
1798
- inspectorOptions = {
1799
- ...defaultInspectorOptions,
1800
- ...options
1801
- };
1802
- const isSvelteKit = config.plugins.some((p) => p.name.startsWith("vite-plugin-sveltekit"));
1803
- if (isSvelteKit && !inspectorOptions.appendTo) {
1804
- inspectorOptions.appendTo = `/generated/root.svelte`;
1841
+ const configFileOptions = vps?.api?.options?.inspector;
1842
+ const environmentOptions = parseEnvironmentOptions(config);
1843
+ if (!configFileOptions && !environmentOptions) {
1844
+ log.debug("no options found, inspector disabled", void 0, "inspector");
1845
+ disabled = true;
1846
+ return;
1847
+ }
1848
+ if (environmentOptions === true) {
1849
+ inspectorOptions = defaultInspectorOptions;
1850
+ } else {
1851
+ inspectorOptions = {
1852
+ ...defaultInspectorOptions,
1853
+ ...configFileOptions,
1854
+ ...environmentOptions || {}
1855
+ };
1805
1856
  }
1806
- appendTo = inspectorOptions.appendTo;
1857
+ inspectorOptions.__internal = {
1858
+ base: config.base?.replace(/\/$/, "") || ""
1859
+ };
1807
1860
  },
1808
1861
  async resolveId(importee, importer, options) {
1809
1862
  if (options?.ssr || disabled) {
@@ -1813,7 +1866,7 @@ function svelteInspector() {
1813
1866
  return importee;
1814
1867
  } else if (importee.startsWith("virtual:svelte-inspector-path:")) {
1815
1868
  const resolved = importee.replace("virtual:svelte-inspector-path:", inspectorPath);
1816
- log.debug.enabled && log.debug(`resolved ${importee} with ${resolved}`);
1869
+ log.debug.enabled && log.debug(`resolved ${importee} with ${resolved}`, void 0, "inspector");
1817
1870
  return resolved;
1818
1871
  }
1819
1872
  },
@@ -1828,37 +1881,22 @@ function svelteInspector() {
1828
1881
  if (fs6.existsSync(file)) {
1829
1882
  return await fs6.promises.readFile(file, "utf-8");
1830
1883
  } else {
1831
- log.error(`failed to find file for svelte-inspector: ${file}, referenced by id ${id}.`);
1884
+ log.error(
1885
+ `failed to find file for svelte-inspector: ${file}, referenced by id ${id}.`,
1886
+ void 0,
1887
+ "inspector"
1888
+ );
1832
1889
  }
1833
1890
  }
1834
1891
  },
1835
1892
  transform(code, id, options) {
1836
- if (options?.ssr || disabled || !appendTo) {
1893
+ if (options?.ssr || disabled) {
1837
1894
  return;
1838
1895
  }
1839
- if (id.endsWith(appendTo)) {
1896
+ if (id.includes("vite/dist/client/client.mjs")) {
1840
1897
  return { code: `${code}
1841
- import 'virtual:svelte-inspector-path:load-inspector.js'` };
1842
- }
1843
- },
1844
- transformIndexHtml(html) {
1845
- if (disabled || appendTo) {
1846
- return;
1898
+ import('virtual:svelte-inspector-path:load-inspector.js')` };
1847
1899
  }
1848
- return {
1849
- html,
1850
- tags: [
1851
- {
1852
- tag: "script",
1853
- injectTo: "body",
1854
- attrs: {
1855
- type: "module",
1856
- // /@id/ is needed, otherwise the virtual: is seen as protocol by browser and cors error happens
1857
- src: "/@id/virtual:svelte-inspector-path:load-inspector.js"
1858
- }
1859
- }
1860
- ]
1861
- };
1862
1900
  }
1863
1901
  };
1864
1902
  }
@@ -2377,10 +2415,10 @@ Please see ${FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE} for details.`
2377
2415
  );
2378
2416
  }
2379
2417
  }
2380
- }
2418
+ },
2419
+ svelteInspector()
2381
2420
  ];
2382
- plugins.push(svelteInspector());
2383
- return plugins.filter(Boolean);
2421
+ return plugins;
2384
2422
  }
2385
2423
  export {
2386
2424
  loadSvelteConfig,