@sveltejs/vite-plugin-svelte 1.0.0-next.41 → 1.0.0-next.44

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
@@ -8,9 +8,11 @@ interface Options {
8
8
  /**
9
9
  * Path to a svelte config file, either absolute or relative to Vite root
10
10
  *
11
+ * set to `false` to ignore the svelte config file
12
+ *
11
13
  * @see https://vitejs.dev/config/#root
12
14
  */
13
- configFile?: string;
15
+ configFile?: string | false;
14
16
  /**
15
17
  * A `picomatch` pattern, or array of patterns, which specifies the files the plugin should
16
18
  * operate on. By default, all svelte files are included.
@@ -148,11 +150,54 @@ interface ExperimentalOptions {
148
150
  code: string;
149
151
  compileOptions: Partial<CompileOptions>;
150
152
  }) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
153
+ /**
154
+ * enable svelte inspector
155
+ */
156
+ inspector?: InspectorOptions | boolean;
157
+ }
158
+ interface InspectorOptions {
159
+ /**
160
+ * define a key combo to toggle inspector,
161
+ * @default 'control-shift' on windows, 'meta-shift' on other os
162
+ *
163
+ * any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
164
+ * examples: control-shift, control-o, control-alt-s meta-x control-meta
165
+ * Some keys have native behavior (e.g. alt-s opens history menu on firefox).
166
+ * To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
167
+ */
168
+ toggleKeyCombo?: string;
169
+ /**
170
+ * inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
171
+ * @default false
172
+ */
173
+ holdMode?: boolean;
174
+ /**
175
+ * when to show the toggle button
176
+ * @default 'active'
177
+ */
178
+ showToggleButton?: 'always' | 'active' | 'never';
179
+ /**
180
+ * where to display the toggle button
181
+ * @default top-right
182
+ */
183
+ toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
184
+ /**
185
+ * inject custom styles when inspector is active
186
+ */
187
+ customStyles?: boolean;
188
+ /**
189
+ * append an import to the module id ending with `appendTo` instead of adding a script into body
190
+ * useful for frameworks that do not support trannsformIndexHtml hook
191
+ *
192
+ * WARNING: only set this if you know exactly what it does.
193
+ * Regular users of vite-plugin-svelte or SvelteKit do not need it
194
+ */
195
+ appendTo?: string;
151
196
  }
152
197
  declare type ModuleFormat = NonNullable<CompileOptions['format']>;
153
198
  declare type CssHashGetter = NonNullable<CompileOptions['cssHash']>;
154
199
  declare type Arrayable<T> = T | T[];
155
200
 
156
- declare function svelte(inlineOptions?: Partial<Options>): Plugin;
201
+ declare function svelte(inlineOptions?: Partial<Options>): Plugin[];
157
202
 
158
203
  export { Arrayable, CssHashGetter, ModuleFormat, Options, svelte };
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
26
26
  });
27
27
 
28
28
  // src/index.ts
29
- import fs7 from "fs";
29
+ import fs8 from "fs";
30
30
 
31
31
  // src/utils/log.ts
32
32
  import { cyan, yellow, red } from "kleur/colors";
@@ -257,7 +257,7 @@ function safeBase64Hash(input) {
257
257
  }
258
258
  const md5 = crypto.createHash("md5");
259
259
  md5.update(input);
260
- const hash = toSafe(md5.digest("base64")).substr(0, hash_length);
260
+ const hash = toSafe(md5.digest("base64")).slice(0, hash_length);
261
261
  hashes[input] = hash;
262
262
  return hash;
263
263
  }
@@ -272,8 +272,9 @@ function toSafe(base64) {
272
272
  }
273
273
 
274
274
  // src/utils/compile.ts
275
+ var scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
275
276
  var _createCompileSvelte = (makeHot) => async function compileSvelte2(svelteRequest, code, options) {
276
- var _a, _b;
277
+ var _a, _b, _c;
277
278
  const { filename, normalizedFilename, cssId, ssr } = svelteRequest;
278
279
  const { emitCss = true } = options;
279
280
  const dependencies = [];
@@ -336,6 +337,7 @@ import ${JSON.stringify(cssId)};
336
337
  return {
337
338
  filename,
338
339
  normalizedFilename,
340
+ lang: ((_c = code.match(scriptLangRE)) == null ? void 0 : _c[1]) || "js",
339
341
  compiled,
340
342
  ssr,
341
343
  dependencies
@@ -456,6 +458,9 @@ var knownSvelteConfigNames = [
456
458
  ];
457
459
  var dynamicImportDefault = new Function("path", 'return import(path + "?t=" + Date.now()).then(m => m.default)');
458
460
  async function loadSvelteConfig(viteConfig, inlineOptions) {
461
+ if (inlineOptions.configFile === false) {
462
+ return;
463
+ }
459
464
  const configFile = findConfigToLoad(viteConfig, inlineOptions);
460
465
  if (configFile) {
461
466
  let err;
@@ -566,7 +571,7 @@ function findRootSvelteDependencies(root, cwdFallback = true) {
566
571
  ].filter((dep) => !is_common_without_svelte_field(dep));
567
572
  return getSvelteDependencies(deps, root);
568
573
  }
569
- function getSvelteDependencies(deps, pkgDir, path8 = []) {
574
+ function getSvelteDependencies(deps, pkgDir, path9 = []) {
570
575
  const result = [];
571
576
  const localRequire = createRequire2(`${pkgDir}/package.json`);
572
577
  const resolvedDeps = deps.map((dep) => resolveDependencyData(dep, localRequire)).filter(Boolean);
@@ -574,18 +579,18 @@ function getSvelteDependencies(deps, pkgDir, path8 = []) {
574
579
  const type = getSvelteDependencyType(pkg);
575
580
  if (!type)
576
581
  continue;
577
- result.push({ name: pkg.name, type, pkg, dir, path: path8 });
582
+ result.push({ name: pkg.name, type, pkg, dir, path: path9 });
578
583
  if (type === "component-library" && pkg.dependencies) {
579
584
  let dependencyNames = Object.keys(pkg.dependencies);
580
- const circular = dependencyNames.filter((name) => path8.includes(name));
585
+ const circular = dependencyNames.filter((name) => path9.includes(name));
581
586
  if (circular.length > 0) {
582
- log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path8.concat(x).join(">")));
583
- dependencyNames = dependencyNames.filter((name) => !path8.includes(name));
587
+ log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path9.concat(x).join(">")));
588
+ dependencyNames = dependencyNames.filter((name) => !path9.includes(name));
584
589
  }
585
- if (path8.length === 3) {
586
- log.debug.once(`encountered deep svelte dependency tree: ${path8.join(">")}`);
590
+ if (path9.length === 3) {
591
+ log.debug.once(`encountered deep svelte dependency tree: ${path9.join(">")}`);
587
592
  }
588
- result.push(...getSvelteDependencies(dependencyNames, dir, path8.concat(pkg.name)));
593
+ result.push(...getSvelteDependencies(dependencyNames, dir, path9.concat(pkg.name)));
589
594
  }
590
595
  }
591
596
  return result;
@@ -1050,6 +1055,7 @@ function validateSourceMapOutputWrapper(group, i) {
1050
1055
  }
1051
1056
 
1052
1057
  // src/utils/options.ts
1058
+ import deepmerge from "deepmerge";
1053
1059
  var knownOptions = /* @__PURE__ */ new Set([
1054
1060
  "configFile",
1055
1061
  "include",
@@ -1062,7 +1068,8 @@ var knownOptions = /* @__PURE__ */ new Set([
1062
1068
  "hot",
1063
1069
  "ignorePluginPreprocessors",
1064
1070
  "disableDependencyReinclusion",
1065
- "experimental"
1071
+ "experimental",
1072
+ "kit"
1066
1073
  ]);
1067
1074
  function validateInlineOptions(inlineOptions) {
1068
1075
  const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
@@ -1082,19 +1089,27 @@ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
1082
1089
  }
1083
1090
  };
1084
1091
  const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
1085
- const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
1086
- compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.compilerOptions), svelteConfig == null ? void 0 : svelteConfig.compilerOptions), inlineOptions == null ? void 0 : inlineOptions.compilerOptions),
1087
- experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
1092
+ const extraOptions = {
1088
1093
  root: viteConfigWithResolvedRoot.root,
1089
1094
  isBuild: viteEnv.command === "build",
1090
1095
  isServe: viteEnv.command === "serve",
1091
1096
  isDebug: process.env.DEBUG != null
1092
- });
1097
+ };
1098
+ const merged = mergeConfigs(defaultOptions, svelteConfig, inlineOptions, extraOptions);
1093
1099
  if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
1094
1100
  merged.configFile = svelteConfig.configFile;
1095
1101
  }
1096
1102
  return merged;
1097
1103
  }
1104
+ function mergeConfigs(...configs) {
1105
+ let result = {};
1106
+ for (const config of configs.filter(Boolean)) {
1107
+ result = deepmerge(result, config, {
1108
+ arrayMerge: (target, source) => source ?? target
1109
+ });
1110
+ }
1111
+ return result;
1112
+ }
1098
1113
  function resolveOptions(preResolveOptions2, viteConfig) {
1099
1114
  const defaultOptions = {
1100
1115
  hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions2.emitCss },
@@ -1103,11 +1118,11 @@ function resolveOptions(preResolveOptions2, viteConfig) {
1103
1118
  dev: !viteConfig.isProduction
1104
1119
  }
1105
1120
  };
1106
- const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
1107
- compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
1121
+ const extraOptions = {
1108
1122
  root: viteConfig.root,
1109
1123
  isProduction: viteConfig.isProduction
1110
- });
1124
+ };
1125
+ const merged = mergeConfigs(defaultOptions, preResolveOptions2, extraOptions);
1111
1126
  addExtraPreprocessors(merged, viteConfig);
1112
1127
  enforceOptionsForHmr(merged);
1113
1128
  enforceOptionsForProduction(merged);
@@ -1160,6 +1175,7 @@ function resolveViteRoot(viteConfig) {
1160
1175
  return normalizePath2(viteConfig.root ? path4.resolve(viteConfig.root) : process.cwd());
1161
1176
  }
1162
1177
  function buildExtraViteConfig(options, config) {
1178
+ var _a;
1163
1179
  const svelteDeps = findRootSvelteDependencies(options.root);
1164
1180
  const extraViteConfig = {
1165
1181
  resolve: {
@@ -1170,7 +1186,7 @@ function buildExtraViteConfig(options, config) {
1170
1186
  if (options.isServe) {
1171
1187
  extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
1172
1188
  }
1173
- if (options.experimental.prebundleSvelteLibraries) {
1189
+ if ((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries) {
1174
1190
  extraViteConfig.optimizeDeps = __spreadProps(__spreadValues({}, extraViteConfig.optimizeDeps), {
1175
1191
  extensions: options.extensions ?? [".svelte"],
1176
1192
  esbuildOptions: {
@@ -1183,15 +1199,16 @@ function buildExtraViteConfig(options, config) {
1183
1199
  return extraViteConfig;
1184
1200
  }
1185
1201
  function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
1202
+ var _a;
1186
1203
  const include = [];
1187
1204
  const exclude = ["svelte-hmr"];
1188
1205
  const isIncluded = (dep) => {
1189
- var _a;
1190
- return include.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a.includes(dep));
1206
+ var _a2;
1207
+ return include.includes(dep) || ((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a2.includes(dep));
1191
1208
  };
1192
1209
  const isExcluded = (dep) => {
1193
- var _a;
1194
- return exclude.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
1210
+ var _a2;
1211
+ return exclude.includes(dep) || ((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a2.some((id) => dep === id || id.startsWith(`${dep}/`)));
1195
1212
  };
1196
1213
  if (!isExcluded("svelte")) {
1197
1214
  const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
@@ -1200,7 +1217,7 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
1200
1217
  } else {
1201
1218
  log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
1202
1219
  }
1203
- if (options.experimental.prebundleSvelteLibraries) {
1220
+ if ((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries) {
1204
1221
  return { include, exclude };
1205
1222
  }
1206
1223
  svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");
@@ -1323,8 +1340,8 @@ var VitePluginSvelteCache = class {
1323
1340
  return this._js.get(svelteRequest.normalizedFilename);
1324
1341
  }
1325
1342
  }
1326
- getDependants(path8) {
1327
- const dependants = this._dependants.get(path8);
1343
+ getDependants(path9) {
1344
+ const dependants = this._dependants.get(path9);
1328
1345
  return dependants ? [...dependants] : [];
1329
1346
  }
1330
1347
  getResolvedSvelteField(name, importer) {
@@ -1379,27 +1396,29 @@ function setupWatchers(options, cache, requestParser) {
1379
1396
  server.restart();
1380
1397
  }
1381
1398
  };
1382
- const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path5.join(root, cfg));
1383
- const restartOnConfigAdd = (filename) => {
1384
- if (possibleSvelteConfigs.includes(filename)) {
1385
- triggerViteRestart(filename);
1386
- }
1387
- };
1388
- const restartOnConfigChange = (filename) => {
1389
- if (filename === svelteConfigFile) {
1390
- triggerViteRestart(filename);
1391
- }
1392
- };
1393
1399
  const listenerCollection = {
1394
1400
  add: [],
1395
1401
  change: [emitChangeEventOnDependants],
1396
1402
  unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
1397
1403
  };
1398
- if (svelteConfigFile) {
1399
- listenerCollection.change.push(restartOnConfigChange);
1400
- listenerCollection.unlink.push(restartOnConfigChange);
1401
- } else {
1402
- listenerCollection.add.push(restartOnConfigAdd);
1404
+ if (svelteConfigFile !== false) {
1405
+ const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path5.join(root, cfg));
1406
+ const restartOnConfigAdd = (filename) => {
1407
+ if (possibleSvelteConfigs.includes(filename)) {
1408
+ triggerViteRestart(filename);
1409
+ }
1410
+ };
1411
+ const restartOnConfigChange = (filename) => {
1412
+ if (filename === svelteConfigFile) {
1413
+ triggerViteRestart(filename);
1414
+ }
1415
+ };
1416
+ if (svelteConfigFile) {
1417
+ listenerCollection.change.push(restartOnConfigChange);
1418
+ listenerCollection.unlink.push(restartOnConfigChange);
1419
+ } else {
1420
+ listenerCollection.add.push(restartOnConfigAdd);
1421
+ }
1403
1422
  }
1404
1423
  Object.entries(listenerCollection).forEach(([evt, listeners]) => {
1405
1424
  if (listeners.length > 0) {
@@ -1486,6 +1505,101 @@ function generateSvelteMetadata(options) {
1486
1505
  return metadata;
1487
1506
  }
1488
1507
 
1508
+ // src/ui/inspector/plugin.ts
1509
+ import { normalizePath as normalizePath3 } from "vite";
1510
+ import path8 from "path";
1511
+ import { fileURLToPath } from "url";
1512
+ import fs7 from "fs";
1513
+ var defaultInspectorOptions = {
1514
+ toggleKeyCombo: process.platform === "win32" ? "control-shift" : "meta-shift",
1515
+ holdMode: false,
1516
+ showToggleButton: "active",
1517
+ toggleButtonPos: "top-right",
1518
+ customStyles: true
1519
+ };
1520
+ function getInspectorPath() {
1521
+ const pluginPath = normalizePath3(path8.dirname(fileURLToPath(import.meta.url)));
1522
+ return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, "/vite-plugin-svelte/src/ui/inspector/");
1523
+ }
1524
+ function svelteInspector() {
1525
+ const inspectorPath = getInspectorPath();
1526
+ log.debug.enabled && log.debug(`svelte inspector path: ${inspectorPath}`);
1527
+ let inspectorOptions;
1528
+ let appendTo;
1529
+ let disabled = false;
1530
+ return {
1531
+ name: "vite-plugin-svelte:inspector",
1532
+ apply: "serve",
1533
+ enforce: "pre",
1534
+ configResolved(config) {
1535
+ var _a, _b, _c;
1536
+ const vps = config.plugins.find((p) => p.name === "vite-plugin-svelte");
1537
+ if ((_c = (_b = (_a = vps == null ? void 0 : vps.api) == null ? void 0 : _a.options) == null ? void 0 : _b.experimental) == null ? void 0 : _c.inspector) {
1538
+ inspectorOptions = __spreadValues(__spreadValues({}, defaultInspectorOptions), vps.api.options.experimental.inspector);
1539
+ }
1540
+ if (!vps || !inspectorOptions) {
1541
+ log.debug("inspector disabled, could not find config");
1542
+ disabled = true;
1543
+ } else {
1544
+ if (vps.api.options.kit && !inspectorOptions.appendTo) {
1545
+ const out_dir = vps.api.options.kit.outDir || ".svelte-kit";
1546
+ inspectorOptions.appendTo = `${out_dir}/runtime/client/start.js`;
1547
+ }
1548
+ appendTo = inspectorOptions.appendTo;
1549
+ }
1550
+ },
1551
+ async resolveId(importee, importer, options) {
1552
+ if ((options == null ? void 0 : options.ssr) || disabled) {
1553
+ return;
1554
+ }
1555
+ if (importee.startsWith("virtual:svelte-inspector-options")) {
1556
+ return importee;
1557
+ } else if (importee.startsWith("virtual:svelte-inspector-path:")) {
1558
+ const resolved = importee.replace("virtual:svelte-inspector-path:", inspectorPath);
1559
+ log.debug.enabled && log.debug(`resolved ${importee} with ${resolved}`);
1560
+ return resolved;
1561
+ }
1562
+ },
1563
+ async load(id, options) {
1564
+ if ((options == null ? void 0 : options.ssr) || disabled) {
1565
+ return;
1566
+ }
1567
+ if (id === "virtual:svelte-inspector-options") {
1568
+ return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
1569
+ } else if (id.startsWith(inspectorPath)) {
1570
+ return await fs7.promises.readFile(id, "utf-8");
1571
+ }
1572
+ },
1573
+ transform(code, id, options) {
1574
+ if ((options == null ? void 0 : options.ssr) || disabled || !appendTo) {
1575
+ return;
1576
+ }
1577
+ if (id.endsWith(appendTo)) {
1578
+ return { code: `${code}
1579
+ import 'virtual:svelte-inspector-path:load-inspector.js'` };
1580
+ }
1581
+ },
1582
+ transformIndexHtml(html) {
1583
+ if (disabled || appendTo) {
1584
+ return;
1585
+ }
1586
+ return {
1587
+ html,
1588
+ tags: [
1589
+ {
1590
+ tag: "script",
1591
+ injectTo: "body",
1592
+ attrs: {
1593
+ type: "module",
1594
+ src: "/@id/virtual:svelte-inspector-path:load-inspector.js"
1595
+ }
1596
+ }
1597
+ ]
1598
+ };
1599
+ }
1600
+ };
1601
+ }
1602
+
1489
1603
  // src/index.ts
1490
1604
  function svelte(inlineOptions) {
1491
1605
  if (process.env.DEBUG != null) {
@@ -1498,124 +1612,138 @@ function svelte(inlineOptions) {
1498
1612
  let viteConfig;
1499
1613
  let compileSvelte2;
1500
1614
  let resolvedSvelteSSR;
1501
- return {
1502
- name: "vite-plugin-svelte",
1503
- enforce: "pre",
1504
- async config(config, configEnv) {
1505
- if (process.env.DEBUG) {
1506
- log.setLevel("debug");
1507
- } else if (config.logLevel) {
1508
- log.setLevel(config.logLevel);
1509
- }
1510
- options = await preResolveOptions(inlineOptions, config, configEnv);
1511
- const extraViteConfig = buildExtraViteConfig(options, config);
1512
- log.debug("additional vite config", extraViteConfig);
1513
- return extraViteConfig;
1514
- },
1515
- async configResolved(config) {
1516
- options = resolveOptions(options, config);
1517
- patchResolvedViteConfig(config, options);
1518
- requestParser = buildIdParser(options);
1519
- compileSvelte2 = createCompileSvelte(options);
1520
- viteConfig = config;
1521
- log.debug("resolved options", options);
1522
- },
1523
- async buildStart() {
1524
- if (!options.experimental.prebundleSvelteLibraries)
1525
- return;
1526
- const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
1527
- if (isSvelteMetadataChanged) {
1528
- viteConfig.server.force = true;
1529
- }
1530
- },
1531
- configureServer(server) {
1532
- options.server = server;
1533
- setupWatchers(options, cache, requestParser);
1534
- },
1535
- load(id, opts) {
1536
- const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1537
- const svelteRequest = requestParser(id, !!ssr);
1538
- if (svelteRequest) {
1539
- const { filename, query } = svelteRequest;
1540
- if (query.svelte && query.type === "style") {
1541
- const css = cache.getCSS(svelteRequest);
1542
- if (css) {
1543
- log.debug(`load returns css for ${filename}`);
1544
- return css;
1615
+ const api = {};
1616
+ const plugins = [
1617
+ {
1618
+ name: "vite-plugin-svelte",
1619
+ enforce: "pre",
1620
+ api,
1621
+ async config(config, configEnv) {
1622
+ if (process.env.DEBUG) {
1623
+ log.setLevel("debug");
1624
+ } else if (config.logLevel) {
1625
+ log.setLevel(config.logLevel);
1626
+ }
1627
+ options = await preResolveOptions(inlineOptions, config, configEnv);
1628
+ const extraViteConfig = buildExtraViteConfig(options, config);
1629
+ log.debug("additional vite config", extraViteConfig);
1630
+ return extraViteConfig;
1631
+ },
1632
+ async configResolved(config) {
1633
+ options = resolveOptions(options, config);
1634
+ patchResolvedViteConfig(config, options);
1635
+ requestParser = buildIdParser(options);
1636
+ compileSvelte2 = createCompileSvelte(options);
1637
+ viteConfig = config;
1638
+ api.options = options;
1639
+ log.debug("resolved options", options);
1640
+ },
1641
+ async buildStart() {
1642
+ var _a;
1643
+ if (!((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries))
1644
+ return;
1645
+ const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
1646
+ if (isSvelteMetadataChanged) {
1647
+ viteConfig.server.force = true;
1648
+ }
1649
+ },
1650
+ configureServer(server) {
1651
+ options.server = server;
1652
+ setupWatchers(options, cache, requestParser);
1653
+ },
1654
+ load(id, opts) {
1655
+ const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1656
+ const svelteRequest = requestParser(id, !!ssr);
1657
+ if (svelteRequest) {
1658
+ const { filename, query } = svelteRequest;
1659
+ if (query.svelte && query.type === "style") {
1660
+ const css = cache.getCSS(svelteRequest);
1661
+ if (css) {
1662
+ log.debug(`load returns css for ${filename}`);
1663
+ return css;
1664
+ }
1665
+ }
1666
+ if (viteConfig.assetsInclude(filename)) {
1667
+ log.debug(`load returns raw content for ${filename}`);
1668
+ return fs8.readFileSync(filename, "utf-8");
1545
1669
  }
1546
1670
  }
1547
- if (viteConfig.assetsInclude(filename)) {
1548
- log.debug(`load returns raw content for ${filename}`);
1549
- return fs7.readFileSync(filename, "utf-8");
1671
+ },
1672
+ async resolveId(importee, importer, opts) {
1673
+ const ssr = !!(opts == null ? void 0 : opts.ssr);
1674
+ const svelteRequest = requestParser(importee, ssr);
1675
+ if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
1676
+ if (svelteRequest.query.type === "style") {
1677
+ log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
1678
+ return svelteRequest.cssId;
1679
+ }
1680
+ log.debug(`resolveId resolved ${importee}`);
1681
+ return importee;
1550
1682
  }
1551
- }
1552
- },
1553
- async resolveId(importee, importer, opts) {
1554
- const ssr = !!(opts == null ? void 0 : opts.ssr);
1555
- const svelteRequest = requestParser(importee, ssr);
1556
- if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
1557
- if (svelteRequest.query.type === "style") {
1558
- log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
1559
- return svelteRequest.cssId;
1683
+ if (ssr && importee === "svelte") {
1684
+ if (!resolvedSvelteSSR) {
1685
+ resolvedSvelteSSR = this.resolve("svelte/ssr", void 0, { skipSelf: true }).then((svelteSSR) => {
1686
+ log.debug("resolved svelte to svelte/ssr");
1687
+ return svelteSSR;
1688
+ }, (err) => {
1689
+ log.debug("failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it", err);
1690
+ return null;
1691
+ });
1692
+ }
1693
+ return resolvedSvelteSSR;
1560
1694
  }
1561
- log.debug(`resolveId resolved ${importee}`);
1562
- return importee;
1563
- }
1564
- if (ssr && importee === "svelte") {
1565
- if (!resolvedSvelteSSR) {
1566
- resolvedSvelteSSR = this.resolve("svelte/ssr", void 0, { skipSelf: true }).then((svelteSSR) => {
1567
- log.debug("resolved svelte to svelte/ssr");
1568
- return svelteSSR;
1569
- }, (err) => {
1570
- log.debug("failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it", err);
1571
- return null;
1572
- });
1695
+ try {
1696
+ const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1697
+ if (resolved) {
1698
+ log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1699
+ return resolved;
1700
+ }
1701
+ } catch (e) {
1702
+ log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
1573
1703
  }
1574
- return resolvedSvelteSSR;
1575
- }
1576
- try {
1577
- const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1578
- if (resolved) {
1579
- log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1580
- return resolved;
1704
+ },
1705
+ async transform(code, id, opts) {
1706
+ var _a;
1707
+ const ssr = !!(opts == null ? void 0 : opts.ssr);
1708
+ const svelteRequest = requestParser(id, ssr);
1709
+ if (!svelteRequest || svelteRequest.query.svelte) {
1710
+ return;
1581
1711
  }
1582
- } catch (e) {
1583
- log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
1584
- }
1585
- },
1586
- async transform(code, id, opts) {
1587
- var _a;
1588
- const ssr = !!(opts == null ? void 0 : opts.ssr);
1589
- const svelteRequest = requestParser(id, ssr);
1590
- if (!svelteRequest || svelteRequest.query.svelte) {
1591
- return;
1592
- }
1593
- let compileData;
1594
- try {
1595
- compileData = await compileSvelte2(svelteRequest, code, options);
1596
- } catch (e) {
1597
- throw toRollupError(e, options);
1598
- }
1599
- logCompilerWarnings(compileData.compiled.warnings, options);
1600
- cache.update(compileData);
1601
- if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
1602
- compileData.dependencies.forEach((d) => {
1603
- ensureWatchedFile(options.server.watcher, d, options.root);
1712
+ let compileData;
1713
+ try {
1714
+ compileData = await compileSvelte2(svelteRequest, code, options);
1715
+ } catch (e) {
1716
+ throw toRollupError(e, options);
1717
+ }
1718
+ logCompilerWarnings(compileData.compiled.warnings, options);
1719
+ cache.update(compileData);
1720
+ if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
1721
+ compileData.dependencies.forEach((d) => {
1722
+ ensureWatchedFile(options.server.watcher, d, options.root);
1723
+ });
1724
+ }
1725
+ log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1726
+ return __spreadProps(__spreadValues({}, compileData.compiled.js), {
1727
+ meta: {
1728
+ vite: {
1729
+ lang: compileData.lang
1730
+ }
1731
+ }
1604
1732
  });
1605
- }
1606
- log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1607
- return compileData.compiled.js;
1608
- },
1609
- handleHotUpdate(ctx) {
1610
- if (!options.hot || !options.emitCss) {
1611
- return;
1612
- }
1613
- const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
1614
- if (svelteRequest) {
1615
- return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1733
+ },
1734
+ handleHotUpdate(ctx) {
1735
+ if (!options.hot || !options.emitCss) {
1736
+ return;
1737
+ }
1738
+ const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
1739
+ if (svelteRequest) {
1740
+ return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1741
+ }
1616
1742
  }
1617
1743
  }
1618
- };
1744
+ ];
1745
+ plugins.push(svelteInspector());
1746
+ return plugins.filter(Boolean);
1619
1747
  }
1620
1748
  export {
1621
1749
  svelte