@sveltejs/vite-plugin-svelte 2.1.0 → 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) {
@@ -1675,6 +1669,7 @@ function ensureWatchedFile(watcher, file, root) {
1675
1669
  // src/utils/resolve.ts
1676
1670
  import path7 from "path";
1677
1671
  import { builtinModules } from "module";
1672
+ import { normalizePath as normalizePath4 } from "vite";
1678
1673
  async function resolveViaPackageJsonSvelte(importee, importer, cache) {
1679
1674
  if (importer && isBareImport(importee) && !isNodeInternal(importee) && !isCommonDepWithoutSvelteField(importee)) {
1680
1675
  const cached = cache.getResolvedSvelteField(importee, importer);
@@ -1685,7 +1680,7 @@ async function resolveViaPackageJsonSvelte(importee, importer, cache) {
1685
1680
  if (pkgData) {
1686
1681
  const { pkg, dir } = pkgData;
1687
1682
  if (pkg.svelte) {
1688
- const result = path7.resolve(dir, pkg.svelte);
1683
+ const result = normalizePath4(path7.resolve(dir, pkg.svelte));
1689
1684
  cache.setResolvedSvelteField(importee, importer, result);
1690
1685
  return result;
1691
1686
  }
@@ -1745,7 +1740,7 @@ function generateSvelteMetadata(options) {
1745
1740
  }
1746
1741
 
1747
1742
  // src/ui/inspector/plugin.ts
1748
- import { normalizePath as normalizePath4 } from "vite";
1743
+ import { normalizePath as normalizePath5 } from "vite";
1749
1744
  import path9 from "path";
1750
1745
  import { fileURLToPath } from "url";
1751
1746
  import fs6 from "fs";
@@ -1762,25 +1757,75 @@ function idToFile(id) {
1762
1757
  return id.replace(hashRE, "").replace(queryRE, "");
1763
1758
  }
1764
1759
 
1765
- // src/ui/inspector/plugin.ts
1760
+ // src/ui/inspector/options.ts
1761
+ import * as process2 from "process";
1762
+ import { loadEnv } from "vite";
1766
1763
  var defaultInspectorOptions = {
1767
- toggleKeyCombo: process.platform === "win32" ? "control-shift" : "meta-shift",
1764
+ toggleKeyCombo: process2.platform === "darwin" ? "meta-shift" : "control-shift",
1768
1765
  navKeys: { parent: "ArrowUp", child: "ArrowDown", next: "ArrowRight", prev: "ArrowLeft" },
1769
1766
  openKey: "Enter",
1770
- holdMode: false,
1767
+ holdMode: true,
1771
1768
  showToggleButton: "active",
1772
1769
  toggleButtonPos: "top-right",
1773
1770
  customStyles: true
1774
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
1775
1821
  function getInspectorPath() {
1776
- const pluginPath = normalizePath4(path9.dirname(fileURLToPath(import.meta.url)));
1822
+ const pluginPath = normalizePath5(path9.dirname(fileURLToPath(import.meta.url)));
1777
1823
  return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, "/vite-plugin-svelte/src/ui/inspector/");
1778
1824
  }
1779
1825
  function svelteInspector() {
1780
1826
  const inspectorPath = getInspectorPath();
1781
1827
  log.debug.enabled && log.debug(`svelte inspector path: ${inspectorPath}`);
1782
1828
  let inspectorOptions;
1783
- let appendTo;
1784
1829
  let disabled = false;
1785
1830
  return {
1786
1831
  name: "vite-plugin-svelte:inspector",
@@ -1788,21 +1833,30 @@ function svelteInspector() {
1788
1833
  enforce: "pre",
1789
1834
  configResolved(config) {
1790
1835
  const vps = config.plugins.find((p) => p.name === "vite-plugin-svelte");
1791
- const options = vps?.api?.options?.experimental?.inspector;
1792
- if (!vps || !options) {
1793
- log.debug("inspector disabled, could not find config");
1836
+ if (!vps) {
1837
+ log.warn("vite-plugin-svelte is missing, inspector disabled", void 0, "inspector");
1794
1838
  disabled = true;
1795
1839
  return;
1796
1840
  }
1797
- inspectorOptions = {
1798
- ...defaultInspectorOptions,
1799
- ...options
1800
- };
1801
- const isSvelteKit = config.plugins.some((p) => p.name.startsWith("vite-plugin-sveltekit"));
1802
- if (isSvelteKit && !inspectorOptions.appendTo) {
1803
- 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
+ };
1804
1856
  }
1805
- appendTo = inspectorOptions.appendTo;
1857
+ inspectorOptions.__internal = {
1858
+ base: config.base?.replace(/\/$/, "") || ""
1859
+ };
1806
1860
  },
1807
1861
  async resolveId(importee, importer, options) {
1808
1862
  if (options?.ssr || disabled) {
@@ -1812,7 +1866,7 @@ function svelteInspector() {
1812
1866
  return importee;
1813
1867
  } else if (importee.startsWith("virtual:svelte-inspector-path:")) {
1814
1868
  const resolved = importee.replace("virtual:svelte-inspector-path:", inspectorPath);
1815
- log.debug.enabled && log.debug(`resolved ${importee} with ${resolved}`);
1869
+ log.debug.enabled && log.debug(`resolved ${importee} with ${resolved}`, void 0, "inspector");
1816
1870
  return resolved;
1817
1871
  }
1818
1872
  },
@@ -1827,37 +1881,22 @@ function svelteInspector() {
1827
1881
  if (fs6.existsSync(file)) {
1828
1882
  return await fs6.promises.readFile(file, "utf-8");
1829
1883
  } else {
1830
- 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
+ );
1831
1889
  }
1832
1890
  }
1833
1891
  },
1834
1892
  transform(code, id, options) {
1835
- if (options?.ssr || disabled || !appendTo) {
1893
+ if (options?.ssr || disabled) {
1836
1894
  return;
1837
1895
  }
1838
- if (id.endsWith(appendTo)) {
1896
+ if (id.includes("vite/dist/client/client.mjs")) {
1839
1897
  return { code: `${code}
1840
- import 'virtual:svelte-inspector-path:load-inspector.js'` };
1841
- }
1842
- },
1843
- transformIndexHtml(html) {
1844
- if (disabled || appendTo) {
1845
- return;
1898
+ import('virtual:svelte-inspector-path:load-inspector.js')` };
1846
1899
  }
1847
- return {
1848
- html,
1849
- tags: [
1850
- {
1851
- tag: "script",
1852
- injectTo: "body",
1853
- attrs: {
1854
- type: "module",
1855
- // /@id/ is needed, otherwise the virtual: is seen as protocol by browser and cors error happens
1856
- src: "/@id/virtual:svelte-inspector-path:load-inspector.js"
1857
- }
1858
- }
1859
- ]
1860
- };
1861
1900
  }
1862
1901
  };
1863
1902
  }
@@ -1866,7 +1905,7 @@ import 'virtual:svelte-inspector-path:load-inspector.js'` };
1866
1905
  import { readFileSync as readFileSync2 } from "fs";
1867
1906
  import { dirname } from "path";
1868
1907
  import { findClosestPkgJsonPath } from "vitefu";
1869
- import { normalizePath as normalizePath5 } from "vite";
1908
+ import { normalizePath as normalizePath6 } from "vite";
1870
1909
  var VitePluginSvelteCache = class {
1871
1910
  constructor() {
1872
1911
  this._css = /* @__PURE__ */ new Map();
@@ -2000,7 +2039,7 @@ async function findPackageInfo(file) {
2000
2039
  }
2001
2040
  return false;
2002
2041
  });
2003
- path10 = normalizePath5(dirname(path10 ?? file)) + "/";
2042
+ path10 = normalizePath6(dirname(path10 ?? file)) + "/";
2004
2043
  info.path = path10;
2005
2044
  return info;
2006
2045
  }
@@ -2376,10 +2415,10 @@ Please see ${FAQ_LINK_CONFLICTS_IN_SVELTE_RESOLVE} for details.`
2376
2415
  );
2377
2416
  }
2378
2417
  }
2379
- }
2418
+ },
2419
+ svelteInspector()
2380
2420
  ];
2381
- plugins.push(svelteInspector());
2382
- return plugins.filter(Boolean);
2421
+ return plugins;
2383
2422
  }
2384
2423
  export {
2385
2424
  loadSvelteConfig,