@sveltejs/vite-plugin-svelte 1.0.0-next.40 → 1.0.0-next.43

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.js CHANGED
@@ -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;
@@ -536,7 +541,7 @@ var SVELTE_HMR_IMPORTS = [
536
541
  ];
537
542
 
538
543
  // src/utils/options.ts
539
- import path3 from "path";
544
+ import path4 from "path";
540
545
 
541
546
  // src/utils/dependencies.ts
542
547
  import path2 from "path";
@@ -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, path7 = []) {
574
+ function getSvelteDependencies(deps, pkgDir, path8 = []) {
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, path7 = []) {
574
579
  const type = getSvelteDependencyType(pkg);
575
580
  if (!type)
576
581
  continue;
577
- result.push({ name: pkg.name, type, pkg, dir, path: path7 });
582
+ result.push({ name: pkg.name, type, pkg, dir, path: path8 });
578
583
  if (type === "component-library" && pkg.dependencies) {
579
584
  let dependencyNames = Object.keys(pkg.dependencies);
580
- const circular = dependencyNames.filter((name) => path7.includes(name));
585
+ const circular = dependencyNames.filter((name) => path8.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) => path7.concat(x).join(">")));
583
- dependencyNames = dependencyNames.filter((name) => !path7.includes(name));
587
+ log.warn.enabled && log.warn(`skipping circular svelte dependencies in automated vite optimizeDeps handling`, circular.map((x) => path8.concat(x).join(">")));
588
+ dependencyNames = dependencyNames.filter((name) => !path8.includes(name));
584
589
  }
585
- if (path7.length === 3) {
586
- log.debug.once(`encountered deep svelte dependency tree: ${path7.join(">")}`);
590
+ if (path8.length === 3) {
591
+ log.debug.once(`encountered deep svelte dependency tree: ${path8.join(">")}`);
587
592
  }
588
- result.push(...getSvelteDependencies(dependencyNames, dir, path7.concat(pkg.name)));
593
+ result.push(...getSvelteDependencies(dependencyNames, dir, path8.concat(pkg.name)));
589
594
  }
590
595
  }
591
596
  return result;
@@ -766,7 +771,9 @@ function esbuildSveltePlugin(options) {
766
771
  return {
767
772
  name: "vite-plugin-svelte:optimize-svelte",
768
773
  setup(build) {
769
- disableVitePrebundleSvelte(build);
774
+ var _a;
775
+ if ((_a = build.initialOptions.plugins) == null ? void 0 : _a.some((v) => v.name === "vite:dep-scan"))
776
+ return;
770
777
  const svelteExtensions = (options.extensions ?? [".svelte"]).map((ext) => ext.slice(1));
771
778
  const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join("|") + `)(\\?.*)?$`);
772
779
  build.onLoad({ filter: svelteFilter }, async ({ path: filename }) => {
@@ -781,23 +788,6 @@ function esbuildSveltePlugin(options) {
781
788
  }
782
789
  };
783
790
  }
784
- function disableVitePrebundleSvelte(build) {
785
- var _a;
786
- const viteDepPrebundlePlugin = (_a = build.initialOptions.plugins) == null ? void 0 : _a.find((v) => v.name === "vite:dep-pre-bundle");
787
- if (!viteDepPrebundlePlugin)
788
- return;
789
- const _setup = viteDepPrebundlePlugin.setup.bind(viteDepPrebundlePlugin);
790
- viteDepPrebundlePlugin.setup = function(build2) {
791
- const _onResolve = build2.onResolve.bind(build2);
792
- build2.onResolve = function(options, callback) {
793
- if (options.filter.source.includes("svelte")) {
794
- options.filter = new RegExp(options.filter.source.replace("|svelte", ""), options.filter.flags);
795
- }
796
- return _onResolve(options, callback);
797
- };
798
- return _setup(build2);
799
- };
800
- }
801
791
  async function compileSvelte(options, { filename, code }) {
802
792
  var _a, _b;
803
793
  const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
@@ -885,6 +875,7 @@ async function buildSourceMap(from, to, filename) {
885
875
  }
886
876
 
887
877
  // src/utils/preprocess.ts
878
+ import path3 from "path";
888
879
  var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
889
880
  var supportedScriptLangs = ["ts"];
890
881
  function createViteScriptPreprocessor() {
@@ -925,7 +916,7 @@ function createViteStylePreprocessor(config) {
925
916
  const moduleId = `${filename}.${lang}`;
926
917
  const transformResult = await pluginTransform(content, moduleId);
927
918
  if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
928
- transformResult.map.sources[0] = filename;
919
+ transformResult.map.sources[0] = path3.basename(filename);
929
920
  }
930
921
  return {
931
922
  code: transformResult.code,
@@ -950,7 +941,10 @@ function createInjectScopeEverythingRulePreprocessorGroup() {
950
941
  s.append(" *{}");
951
942
  return {
952
943
  code: s.toString(),
953
- map: s.generateDecodedMap({ source: filename, hires: true })
944
+ map: s.generateDecodedMap({
945
+ source: filename ? path3.basename(filename) : void 0,
946
+ hires: true
947
+ })
954
948
  };
955
949
  }
956
950
  };
@@ -1061,6 +1055,7 @@ function validateSourceMapOutputWrapper(group, i) {
1061
1055
  }
1062
1056
 
1063
1057
  // src/utils/options.ts
1058
+ import deepmerge from "deepmerge";
1064
1059
  var knownOptions = /* @__PURE__ */ new Set([
1065
1060
  "configFile",
1066
1061
  "include",
@@ -1073,7 +1068,8 @@ var knownOptions = /* @__PURE__ */ new Set([
1073
1068
  "hot",
1074
1069
  "ignorePluginPreprocessors",
1075
1070
  "disableDependencyReinclusion",
1076
- "experimental"
1071
+ "experimental",
1072
+ "kit"
1077
1073
  ]);
1078
1074
  function validateInlineOptions(inlineOptions) {
1079
1075
  const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
@@ -1093,19 +1089,27 @@ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
1093
1089
  }
1094
1090
  };
1095
1091
  const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
1096
- const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
1097
- compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.compilerOptions), svelteConfig == null ? void 0 : svelteConfig.compilerOptions), inlineOptions == null ? void 0 : inlineOptions.compilerOptions),
1098
- 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 = {
1099
1093
  root: viteConfigWithResolvedRoot.root,
1100
1094
  isBuild: viteEnv.command === "build",
1101
1095
  isServe: viteEnv.command === "serve",
1102
1096
  isDebug: process.env.DEBUG != null
1103
- });
1097
+ };
1098
+ const merged = mergeConfigs(defaultOptions, svelteConfig, inlineOptions, extraOptions);
1104
1099
  if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
1105
1100
  merged.configFile = svelteConfig.configFile;
1106
1101
  }
1107
1102
  return merged;
1108
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
+ }
1109
1113
  function resolveOptions(preResolveOptions2, viteConfig) {
1110
1114
  const defaultOptions = {
1111
1115
  hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions2.emitCss },
@@ -1114,11 +1118,11 @@ function resolveOptions(preResolveOptions2, viteConfig) {
1114
1118
  dev: !viteConfig.isProduction
1115
1119
  }
1116
1120
  };
1117
- const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
1118
- compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
1121
+ const extraOptions = {
1119
1122
  root: viteConfig.root,
1120
1123
  isProduction: viteConfig.isProduction
1121
- });
1124
+ };
1125
+ const merged = mergeConfigs(defaultOptions, preResolveOptions2, extraOptions);
1122
1126
  addExtraPreprocessors(merged, viteConfig);
1123
1127
  enforceOptionsForHmr(merged);
1124
1128
  enforceOptionsForProduction(merged);
@@ -1168,9 +1172,10 @@ function enforceOptionsForProduction(options) {
1168
1172
  }
1169
1173
  }
1170
1174
  function resolveViteRoot(viteConfig) {
1171
- return normalizePath2(viteConfig.root ? path3.resolve(viteConfig.root) : process.cwd());
1175
+ return normalizePath2(viteConfig.root ? path4.resolve(viteConfig.root) : process.cwd());
1172
1176
  }
1173
1177
  function buildExtraViteConfig(options, config) {
1178
+ var _a;
1174
1179
  const svelteDeps = findRootSvelteDependencies(options.root);
1175
1180
  const extraViteConfig = {
1176
1181
  resolve: {
@@ -1181,19 +1186,29 @@ function buildExtraViteConfig(options, config) {
1181
1186
  if (options.isServe) {
1182
1187
  extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
1183
1188
  }
1189
+ if ((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries) {
1190
+ extraViteConfig.optimizeDeps = __spreadProps(__spreadValues({}, extraViteConfig.optimizeDeps), {
1191
+ extensions: options.extensions ?? [".svelte"],
1192
+ esbuildOptions: {
1193
+ plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
1194
+ } }]
1195
+ }
1196
+ });
1197
+ }
1184
1198
  extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
1185
1199
  return extraViteConfig;
1186
1200
  }
1187
- function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
1201
+ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
1202
+ var _a;
1188
1203
  const include = [];
1189
1204
  const exclude = ["svelte-hmr"];
1190
1205
  const isIncluded = (dep) => {
1191
- var _a;
1192
- return include.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.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));
1193
1208
  };
1194
1209
  const isExcluded = (dep) => {
1195
- var _a;
1196
- return exclude.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.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}/`)));
1197
1212
  };
1198
1213
  if (!isExcluded("svelte")) {
1199
1214
  const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
@@ -1202,15 +1217,8 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
1202
1217
  } else {
1203
1218
  log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
1204
1219
  }
1205
- if (options.experimental.prebundleSvelteLibraries) {
1206
- return {
1207
- include,
1208
- exclude,
1209
- esbuildOptions: {
1210
- plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
1211
- } }]
1212
- }
1213
- };
1220
+ if ((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries) {
1221
+ return { include, exclude };
1214
1222
  }
1215
1223
  svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");
1216
1224
  const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter((dep) => !isIncluded(dep));
@@ -1332,8 +1340,8 @@ var VitePluginSvelteCache = class {
1332
1340
  return this._js.get(svelteRequest.normalizedFilename);
1333
1341
  }
1334
1342
  }
1335
- getDependants(path7) {
1336
- const dependants = this._dependants.get(path7);
1343
+ getDependants(path8) {
1344
+ const dependants = this._dependants.get(path8);
1337
1345
  return dependants ? [...dependants] : [];
1338
1346
  }
1339
1347
  getResolvedSvelteField(name, importer) {
@@ -1349,7 +1357,7 @@ var VitePluginSvelteCache = class {
1349
1357
 
1350
1358
  // src/utils/watch.ts
1351
1359
  import fs5 from "fs";
1352
- import path4 from "path";
1360
+ import path5 from "path";
1353
1361
  function setupWatchers(options, cache, requestParser) {
1354
1362
  const { server, configFile: svelteConfigFile } = options;
1355
1363
  if (!server) {
@@ -1388,27 +1396,29 @@ function setupWatchers(options, cache, requestParser) {
1388
1396
  server.restart();
1389
1397
  }
1390
1398
  };
1391
- const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
1392
- const restartOnConfigAdd = (filename) => {
1393
- if (possibleSvelteConfigs.includes(filename)) {
1394
- triggerViteRestart(filename);
1395
- }
1396
- };
1397
- const restartOnConfigChange = (filename) => {
1398
- if (filename === svelteConfigFile) {
1399
- triggerViteRestart(filename);
1400
- }
1401
- };
1402
1399
  const listenerCollection = {
1403
1400
  add: [],
1404
1401
  change: [emitChangeEventOnDependants],
1405
1402
  unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
1406
1403
  };
1407
- if (svelteConfigFile) {
1408
- listenerCollection.change.push(restartOnConfigChange);
1409
- listenerCollection.unlink.push(restartOnConfigChange);
1410
- } else {
1411
- 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
+ }
1412
1422
  }
1413
1423
  Object.entries(listenerCollection).forEach(([evt, listeners]) => {
1414
1424
  if (listeners.length > 0) {
@@ -1418,12 +1428,12 @@ function setupWatchers(options, cache, requestParser) {
1418
1428
  }
1419
1429
  function ensureWatchedFile(watcher, file, root) {
1420
1430
  if (file && !file.startsWith(root + "/") && !file.includes("\0") && fs5.existsSync(file)) {
1421
- watcher.add(path4.resolve(file));
1431
+ watcher.add(path5.resolve(file));
1422
1432
  }
1423
1433
  }
1424
1434
 
1425
1435
  // src/utils/resolve.ts
1426
- import path5 from "path";
1436
+ import path6 from "path";
1427
1437
  import { builtinModules, createRequire as createRequire4 } from "module";
1428
1438
  function resolveViaPackageJsonSvelte(importee, importer, cache) {
1429
1439
  if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
@@ -1436,7 +1446,7 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1436
1446
  if (pkgData) {
1437
1447
  const { pkg, dir } = pkgData;
1438
1448
  if (pkg.svelte) {
1439
- const result = path5.resolve(dir, pkg.svelte);
1449
+ const result = path6.resolve(dir, pkg.svelte);
1440
1450
  cache.setResolvedSvelteField(importee, importer, result);
1441
1451
  return result;
1442
1452
  }
@@ -1447,7 +1457,7 @@ function isNodeInternal(importee) {
1447
1457
  return importee.startsWith("node:") || builtinModules.includes(importee);
1448
1458
  }
1449
1459
  function isBareImport(importee) {
1450
- if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
1460
+ if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path6.isAbsolute(importee)) {
1451
1461
  return false;
1452
1462
  }
1453
1463
  const parts = importee.split("/");
@@ -1462,9 +1472,8 @@ function isBareImport(importee) {
1462
1472
  }
1463
1473
 
1464
1474
  // src/utils/optimizer.ts
1465
- import fs6 from "fs";
1466
- import path6 from "path";
1467
- import { optimizeDeps } from "vite";
1475
+ import { promises as fs6 } from "fs";
1476
+ import path7 from "path";
1468
1477
  var PREBUNDLE_SENSITIVE_OPTIONS = [
1469
1478
  "compilerOptions",
1470
1479
  "configFile",
@@ -1473,23 +1482,20 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1473
1482
  "ignorePluginPreprocessors",
1474
1483
  "preprocess"
1475
1484
  ];
1476
- async function handleOptimizeDeps(options, viteConfig) {
1477
- if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1478
- return;
1479
- const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
1480
- if (!viteMetadataPath)
1481
- return;
1482
- const svelteMetadataPath = path6.resolve(viteMetadataPath, "../_svelte_metadata.json");
1483
- const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
1485
+ async function saveSvelteMetadata(cacheDir, options) {
1486
+ const svelteMetadata = generateSvelteMetadata(options);
1487
+ const svelteMetadataPath = path7.resolve(cacheDir, "_svelte_metadata.json");
1488
+ const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
1484
1489
  return typeof value === "function" ? value.toString() : value;
1485
1490
  });
1486
- if (fs6.existsSync(svelteMetadataPath)) {
1487
- const existingSvelteMetadata = fs6.readFileSync(svelteMetadataPath, "utf8");
1488
- if (existingSvelteMetadata === currentSvelteMetadata)
1489
- return;
1491
+ let existingSvelteMetadata;
1492
+ try {
1493
+ existingSvelteMetadata = await fs6.readFile(svelteMetadataPath, "utf8");
1494
+ } catch {
1490
1495
  }
1491
- await optimizeDeps(viteConfig, true);
1492
- fs6.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
1496
+ await fs6.mkdir(cacheDir, { recursive: true });
1497
+ await fs6.writeFile(svelteMetadataPath, currentSvelteMetadata);
1498
+ return currentSvelteMetadata !== existingSvelteMetadata;
1493
1499
  }
1494
1500
  function generateSvelteMetadata(options) {
1495
1501
  const metadata = {};
@@ -1498,13 +1504,94 @@ function generateSvelteMetadata(options) {
1498
1504
  }
1499
1505
  return metadata;
1500
1506
  }
1501
- function findViteMetadataPath(cacheDir) {
1502
- const metadataPaths = ["_metadata.json", "deps/_metadata.json"];
1503
- for (const metadataPath of metadataPaths) {
1504
- const viteMetadataPath = path6.resolve(cacheDir, metadataPath);
1505
- if (fs6.existsSync(viteMetadataPath))
1506
- return viteMetadataPath;
1507
- }
1507
+
1508
+ // src/ui/inspector/plugin.ts
1509
+ import { createRequire as createRequire5 } from "module";
1510
+ var defaultInspectorOptions = {
1511
+ toggleKeyCombo: process.platform === "win32" ? "control-shift" : "meta-shift",
1512
+ holdMode: false,
1513
+ showToggleButton: "active",
1514
+ toggleButtonPos: "top-right",
1515
+ customStyles: true
1516
+ };
1517
+ function svelteInspector() {
1518
+ let root;
1519
+ let rootRequire;
1520
+ let inspectorOptions;
1521
+ let append_to;
1522
+ return {
1523
+ name: "vite-plugin-svelte:inspector",
1524
+ apply: "serve",
1525
+ enforce: "pre",
1526
+ configResolved(config) {
1527
+ var _a, _b, _c;
1528
+ const vps = config.plugins.find((p) => p.name === "vite-plugin-svelte");
1529
+ 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) {
1530
+ inspectorOptions = __spreadValues(__spreadValues({}, defaultInspectorOptions), vps.api.options.experimental.inspector);
1531
+ }
1532
+ if (!vps || !inspectorOptions) {
1533
+ this.resolveId = this.load = this.transformIndexHtml = this.transform = () => {
1534
+ };
1535
+ } else {
1536
+ root = config.root || process.cwd();
1537
+ rootRequire = createRequire5(root);
1538
+ if (vps.api.options.kit && !inspectorOptions.appendTo) {
1539
+ const out_dir = vps.api.options.kit.outDir || ".svelte-kit";
1540
+ inspectorOptions.appendTo = `${out_dir}/runtime/client/start.js`;
1541
+ }
1542
+ append_to = inspectorOptions.appendTo;
1543
+ }
1544
+ },
1545
+ async resolveId(importee, importer, options) {
1546
+ if (options == null ? void 0 : options.ssr) {
1547
+ return;
1548
+ }
1549
+ if (importee === "virtual:svelte-inspector-options") {
1550
+ return importee;
1551
+ }
1552
+ if (importee.startsWith("virtual:svelte-inspector:")) {
1553
+ const file = importee.replace("virtual:svelte-inspector:", "@sveltejs/vite-plugin-svelte/src/ui/inspector/");
1554
+ const path8 = rootRequire.resolve(file);
1555
+ if (path8) {
1556
+ return path8;
1557
+ } else {
1558
+ log.error.once(`failed to resolve ${file} for ${importee} from ${root}`);
1559
+ }
1560
+ }
1561
+ },
1562
+ load(id) {
1563
+ if (id === "virtual:svelte-inspector-options") {
1564
+ return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
1565
+ }
1566
+ },
1567
+ transform(code, id, options) {
1568
+ if ((options == null ? void 0 : options.ssr) || !append_to) {
1569
+ return;
1570
+ }
1571
+ if (id.endsWith(append_to)) {
1572
+ return { code: `${code}
1573
+ import 'virtual:svelte-inspector:load-inspector.ts'` };
1574
+ }
1575
+ },
1576
+ transformIndexHtml(html) {
1577
+ if (append_to) {
1578
+ return;
1579
+ }
1580
+ return {
1581
+ html,
1582
+ tags: [
1583
+ {
1584
+ tag: "script",
1585
+ injectTo: "body",
1586
+ attrs: {
1587
+ type: "module",
1588
+ src: "/@id/virtual:svelte-inspector:load-inspector.ts"
1589
+ }
1590
+ }
1591
+ ]
1592
+ };
1593
+ }
1594
+ };
1508
1595
  }
1509
1596
 
1510
1597
  // src/index.ts
@@ -1519,119 +1606,138 @@ function svelte(inlineOptions) {
1519
1606
  let viteConfig;
1520
1607
  let compileSvelte2;
1521
1608
  let resolvedSvelteSSR;
1522
- return {
1523
- name: "vite-plugin-svelte",
1524
- enforce: "pre",
1525
- async config(config, configEnv) {
1526
- if (process.env.DEBUG) {
1527
- log.setLevel("debug");
1528
- } else if (config.logLevel) {
1529
- log.setLevel(config.logLevel);
1530
- }
1531
- options = await preResolveOptions(inlineOptions, config, configEnv);
1532
- const extraViteConfig = buildExtraViteConfig(options, config);
1533
- log.debug("additional vite config", extraViteConfig);
1534
- return extraViteConfig;
1535
- },
1536
- async configResolved(config) {
1537
- options = resolveOptions(options, config);
1538
- patchResolvedViteConfig(config, options);
1539
- requestParser = buildIdParser(options);
1540
- compileSvelte2 = createCompileSvelte(options);
1541
- viteConfig = config;
1542
- log.debug("resolved options", options);
1543
- },
1544
- async buildStart() {
1545
- await handleOptimizeDeps(options, viteConfig);
1546
- },
1547
- configureServer(server) {
1548
- options.server = server;
1549
- setupWatchers(options, cache, requestParser);
1550
- },
1551
- load(id, opts) {
1552
- const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1553
- const svelteRequest = requestParser(id, !!ssr);
1554
- if (svelteRequest) {
1555
- const { filename, query } = svelteRequest;
1556
- if (query.svelte && query.type === "style") {
1557
- const css = cache.getCSS(svelteRequest);
1558
- if (css) {
1559
- log.debug(`load returns css for ${filename}`);
1560
- return css;
1609
+ const api = {};
1610
+ const plugins = [
1611
+ {
1612
+ name: "vite-plugin-svelte",
1613
+ enforce: "pre",
1614
+ api,
1615
+ async config(config, configEnv) {
1616
+ if (process.env.DEBUG) {
1617
+ log.setLevel("debug");
1618
+ } else if (config.logLevel) {
1619
+ log.setLevel(config.logLevel);
1620
+ }
1621
+ options = await preResolveOptions(inlineOptions, config, configEnv);
1622
+ const extraViteConfig = buildExtraViteConfig(options, config);
1623
+ log.debug("additional vite config", extraViteConfig);
1624
+ return extraViteConfig;
1625
+ },
1626
+ async configResolved(config) {
1627
+ options = resolveOptions(options, config);
1628
+ patchResolvedViteConfig(config, options);
1629
+ requestParser = buildIdParser(options);
1630
+ compileSvelte2 = createCompileSvelte(options);
1631
+ viteConfig = config;
1632
+ api.options = options;
1633
+ log.debug("resolved options", options);
1634
+ },
1635
+ async buildStart() {
1636
+ var _a;
1637
+ if (!((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries))
1638
+ return;
1639
+ const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
1640
+ if (isSvelteMetadataChanged) {
1641
+ viteConfig.server.force = true;
1642
+ }
1643
+ },
1644
+ configureServer(server) {
1645
+ options.server = server;
1646
+ setupWatchers(options, cache, requestParser);
1647
+ },
1648
+ load(id, opts) {
1649
+ const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1650
+ const svelteRequest = requestParser(id, !!ssr);
1651
+ if (svelteRequest) {
1652
+ const { filename, query } = svelteRequest;
1653
+ if (query.svelte && query.type === "style") {
1654
+ const css = cache.getCSS(svelteRequest);
1655
+ if (css) {
1656
+ log.debug(`load returns css for ${filename}`);
1657
+ return css;
1658
+ }
1659
+ }
1660
+ if (viteConfig.assetsInclude(filename)) {
1661
+ log.debug(`load returns raw content for ${filename}`);
1662
+ return fs7.readFileSync(filename, "utf-8");
1561
1663
  }
1562
1664
  }
1563
- if (viteConfig.assetsInclude(filename)) {
1564
- log.debug(`load returns raw content for ${filename}`);
1565
- return fs7.readFileSync(filename, "utf-8");
1665
+ },
1666
+ async resolveId(importee, importer, opts) {
1667
+ const ssr = !!(opts == null ? void 0 : opts.ssr);
1668
+ const svelteRequest = requestParser(importee, ssr);
1669
+ if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
1670
+ if (svelteRequest.query.type === "style") {
1671
+ log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
1672
+ return svelteRequest.cssId;
1673
+ }
1674
+ log.debug(`resolveId resolved ${importee}`);
1675
+ return importee;
1566
1676
  }
1567
- }
1568
- },
1569
- async resolveId(importee, importer, opts) {
1570
- const ssr = !!(opts == null ? void 0 : opts.ssr);
1571
- const svelteRequest = requestParser(importee, ssr);
1572
- if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
1573
- if (svelteRequest.query.type === "style") {
1574
- log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
1575
- return svelteRequest.cssId;
1677
+ if (ssr && importee === "svelte") {
1678
+ if (!resolvedSvelteSSR) {
1679
+ resolvedSvelteSSR = this.resolve("svelte/ssr", void 0, { skipSelf: true }).then((svelteSSR) => {
1680
+ log.debug("resolved svelte to svelte/ssr");
1681
+ return svelteSSR;
1682
+ }, (err) => {
1683
+ log.debug("failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it", err);
1684
+ return null;
1685
+ });
1686
+ }
1687
+ return resolvedSvelteSSR;
1576
1688
  }
1577
- log.debug(`resolveId resolved ${importee}`);
1578
- return importee;
1579
- }
1580
- if (ssr && importee === "svelte") {
1581
- if (!resolvedSvelteSSR) {
1582
- resolvedSvelteSSR = this.resolve("svelte/ssr", void 0, { skipSelf: true }).then((svelteSSR) => {
1583
- log.debug("resolved svelte to svelte/ssr");
1584
- return svelteSSR;
1585
- }, (err) => {
1586
- log.debug("failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it", err);
1587
- return null;
1588
- });
1689
+ try {
1690
+ const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1691
+ if (resolved) {
1692
+ log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1693
+ return resolved;
1694
+ }
1695
+ } catch (e) {
1696
+ log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
1589
1697
  }
1590
- return resolvedSvelteSSR;
1591
- }
1592
- try {
1593
- const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1594
- if (resolved) {
1595
- log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1596
- return resolved;
1698
+ },
1699
+ async transform(code, id, opts) {
1700
+ var _a;
1701
+ const ssr = !!(opts == null ? void 0 : opts.ssr);
1702
+ const svelteRequest = requestParser(id, ssr);
1703
+ if (!svelteRequest || svelteRequest.query.svelte) {
1704
+ return;
1597
1705
  }
1598
- } catch (e) {
1599
- log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
1600
- }
1601
- },
1602
- async transform(code, id, opts) {
1603
- var _a;
1604
- const ssr = !!(opts == null ? void 0 : opts.ssr);
1605
- const svelteRequest = requestParser(id, ssr);
1606
- if (!svelteRequest || svelteRequest.query.svelte) {
1607
- return;
1608
- }
1609
- let compileData;
1610
- try {
1611
- compileData = await compileSvelte2(svelteRequest, code, options);
1612
- } catch (e) {
1613
- throw toRollupError(e, options);
1614
- }
1615
- logCompilerWarnings(compileData.compiled.warnings, options);
1616
- cache.update(compileData);
1617
- if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
1618
- compileData.dependencies.forEach((d) => {
1619
- ensureWatchedFile(options.server.watcher, d, options.root);
1706
+ let compileData;
1707
+ try {
1708
+ compileData = await compileSvelte2(svelteRequest, code, options);
1709
+ } catch (e) {
1710
+ throw toRollupError(e, options);
1711
+ }
1712
+ logCompilerWarnings(compileData.compiled.warnings, options);
1713
+ cache.update(compileData);
1714
+ if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
1715
+ compileData.dependencies.forEach((d) => {
1716
+ ensureWatchedFile(options.server.watcher, d, options.root);
1717
+ });
1718
+ }
1719
+ log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1720
+ return __spreadProps(__spreadValues({}, compileData.compiled.js), {
1721
+ meta: {
1722
+ vite: {
1723
+ lang: compileData.lang
1724
+ }
1725
+ }
1620
1726
  });
1621
- }
1622
- log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1623
- return compileData.compiled.js;
1624
- },
1625
- handleHotUpdate(ctx) {
1626
- if (!options.hot || !options.emitCss) {
1627
- return;
1628
- }
1629
- const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
1630
- if (svelteRequest) {
1631
- return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1727
+ },
1728
+ handleHotUpdate(ctx) {
1729
+ if (!options.hot || !options.emitCss) {
1730
+ return;
1731
+ }
1732
+ const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
1733
+ if (svelteRequest) {
1734
+ return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1735
+ }
1632
1736
  }
1633
1737
  }
1634
- };
1738
+ ];
1739
+ plugins.push(svelteInspector());
1740
+ return plugins.filter(Boolean);
1635
1741
  }
1636
1742
  export {
1637
1743
  svelte