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

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
@@ -536,7 +536,7 @@ var SVELTE_HMR_IMPORTS = [
536
536
  ];
537
537
 
538
538
  // src/utils/options.ts
539
- import path3 from "path";
539
+ import path4 from "path";
540
540
 
541
541
  // src/utils/dependencies.ts
542
542
  import path2 from "path";
@@ -566,7 +566,7 @@ function findRootSvelteDependencies(root, cwdFallback = true) {
566
566
  ].filter((dep) => !is_common_without_svelte_field(dep));
567
567
  return getSvelteDependencies(deps, root);
568
568
  }
569
- function getSvelteDependencies(deps, pkgDir, path7 = []) {
569
+ function getSvelteDependencies(deps, pkgDir, path8 = []) {
570
570
  const result = [];
571
571
  const localRequire = createRequire2(`${pkgDir}/package.json`);
572
572
  const resolvedDeps = deps.map((dep) => resolveDependencyData(dep, localRequire)).filter(Boolean);
@@ -574,18 +574,18 @@ function getSvelteDependencies(deps, pkgDir, path7 = []) {
574
574
  const type = getSvelteDependencyType(pkg);
575
575
  if (!type)
576
576
  continue;
577
- result.push({ name: pkg.name, type, pkg, dir, path: path7 });
577
+ result.push({ name: pkg.name, type, pkg, dir, path: path8 });
578
578
  if (type === "component-library" && pkg.dependencies) {
579
579
  let dependencyNames = Object.keys(pkg.dependencies);
580
- const circular = dependencyNames.filter((name) => path7.includes(name));
580
+ const circular = dependencyNames.filter((name) => path8.includes(name));
581
581
  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));
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));
584
584
  }
585
- if (path7.length === 3) {
586
- log.debug.once(`encountered deep svelte dependency tree: ${path7.join(">")}`);
585
+ if (path8.length === 3) {
586
+ log.debug.once(`encountered deep svelte dependency tree: ${path8.join(">")}`);
587
587
  }
588
- result.push(...getSvelteDependencies(dependencyNames, dir, path7.concat(pkg.name)));
588
+ result.push(...getSvelteDependencies(dependencyNames, dir, path8.concat(pkg.name)));
589
589
  }
590
590
  }
591
591
  return result;
@@ -766,7 +766,9 @@ function esbuildSveltePlugin(options) {
766
766
  return {
767
767
  name: "vite-plugin-svelte:optimize-svelte",
768
768
  setup(build) {
769
- disableVitePrebundleSvelte(build);
769
+ var _a;
770
+ if ((_a = build.initialOptions.plugins) == null ? void 0 : _a.some((v) => v.name === "vite:dep-scan"))
771
+ return;
770
772
  const svelteExtensions = (options.extensions ?? [".svelte"]).map((ext) => ext.slice(1));
771
773
  const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join("|") + `)(\\?.*)?$`);
772
774
  build.onLoad({ filter: svelteFilter }, async ({ path: filename }) => {
@@ -781,23 +783,6 @@ function esbuildSveltePlugin(options) {
781
783
  }
782
784
  };
783
785
  }
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
786
  async function compileSvelte(options, { filename, code }) {
802
787
  var _a, _b;
803
788
  const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
@@ -885,6 +870,7 @@ async function buildSourceMap(from, to, filename) {
885
870
  }
886
871
 
887
872
  // src/utils/preprocess.ts
873
+ import path3 from "path";
888
874
  var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
889
875
  var supportedScriptLangs = ["ts"];
890
876
  function createViteScriptPreprocessor() {
@@ -925,7 +911,7 @@ function createViteStylePreprocessor(config) {
925
911
  const moduleId = `${filename}.${lang}`;
926
912
  const transformResult = await pluginTransform(content, moduleId);
927
913
  if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
928
- transformResult.map.sources[0] = filename;
914
+ transformResult.map.sources[0] = path3.basename(filename);
929
915
  }
930
916
  return {
931
917
  code: transformResult.code,
@@ -950,7 +936,10 @@ function createInjectScopeEverythingRulePreprocessorGroup() {
950
936
  s.append(" *{}");
951
937
  return {
952
938
  code: s.toString(),
953
- map: s.generateDecodedMap({ source: filename, hires: true })
939
+ map: s.generateDecodedMap({
940
+ source: filename ? path3.basename(filename) : void 0,
941
+ hires: true
942
+ })
954
943
  };
955
944
  }
956
945
  };
@@ -1168,7 +1157,7 @@ function enforceOptionsForProduction(options) {
1168
1157
  }
1169
1158
  }
1170
1159
  function resolveViteRoot(viteConfig) {
1171
- return normalizePath2(viteConfig.root ? path3.resolve(viteConfig.root) : process.cwd());
1160
+ return normalizePath2(viteConfig.root ? path4.resolve(viteConfig.root) : process.cwd());
1172
1161
  }
1173
1162
  function buildExtraViteConfig(options, config) {
1174
1163
  const svelteDeps = findRootSvelteDependencies(options.root);
@@ -1181,19 +1170,28 @@ function buildExtraViteConfig(options, config) {
1181
1170
  if (options.isServe) {
1182
1171
  extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
1183
1172
  }
1173
+ if (options.experimental.prebundleSvelteLibraries) {
1174
+ extraViteConfig.optimizeDeps = __spreadProps(__spreadValues({}, extraViteConfig.optimizeDeps), {
1175
+ extensions: options.extensions ?? [".svelte"],
1176
+ esbuildOptions: {
1177
+ plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
1178
+ } }]
1179
+ }
1180
+ });
1181
+ }
1184
1182
  extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
1185
1183
  return extraViteConfig;
1186
1184
  }
1187
- function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
1185
+ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
1188
1186
  const include = [];
1189
1187
  const exclude = ["svelte-hmr"];
1190
1188
  const isIncluded = (dep) => {
1191
1189
  var _a;
1192
- return include.includes(dep) || ((_a = optimizeDeps2 == null ? void 0 : optimizeDeps2.include) == null ? void 0 : _a.includes(dep));
1190
+ return include.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a.includes(dep));
1193
1191
  };
1194
1192
  const isExcluded = (dep) => {
1195
1193
  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}/`)));
1194
+ return exclude.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`)));
1197
1195
  };
1198
1196
  if (!isExcluded("svelte")) {
1199
1197
  const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr");
@@ -1203,14 +1201,7 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
1203
1201
  log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
1204
1202
  }
1205
1203
  if (options.experimental.prebundleSvelteLibraries) {
1206
- return {
1207
- include,
1208
- exclude,
1209
- esbuildOptions: {
1210
- plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
1211
- } }]
1212
- }
1213
- };
1204
+ return { include, exclude };
1214
1205
  }
1215
1206
  svelteDeps = svelteDeps.filter((dep) => dep.type === "component-library");
1216
1207
  const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter((dep) => !isIncluded(dep));
@@ -1332,8 +1323,8 @@ var VitePluginSvelteCache = class {
1332
1323
  return this._js.get(svelteRequest.normalizedFilename);
1333
1324
  }
1334
1325
  }
1335
- getDependants(path7) {
1336
- const dependants = this._dependants.get(path7);
1326
+ getDependants(path8) {
1327
+ const dependants = this._dependants.get(path8);
1337
1328
  return dependants ? [...dependants] : [];
1338
1329
  }
1339
1330
  getResolvedSvelteField(name, importer) {
@@ -1349,7 +1340,7 @@ var VitePluginSvelteCache = class {
1349
1340
 
1350
1341
  // src/utils/watch.ts
1351
1342
  import fs5 from "fs";
1352
- import path4 from "path";
1343
+ import path5 from "path";
1353
1344
  function setupWatchers(options, cache, requestParser) {
1354
1345
  const { server, configFile: svelteConfigFile } = options;
1355
1346
  if (!server) {
@@ -1388,7 +1379,7 @@ function setupWatchers(options, cache, requestParser) {
1388
1379
  server.restart();
1389
1380
  }
1390
1381
  };
1391
- const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
1382
+ const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path5.join(root, cfg));
1392
1383
  const restartOnConfigAdd = (filename) => {
1393
1384
  if (possibleSvelteConfigs.includes(filename)) {
1394
1385
  triggerViteRestart(filename);
@@ -1418,12 +1409,12 @@ function setupWatchers(options, cache, requestParser) {
1418
1409
  }
1419
1410
  function ensureWatchedFile(watcher, file, root) {
1420
1411
  if (file && !file.startsWith(root + "/") && !file.includes("\0") && fs5.existsSync(file)) {
1421
- watcher.add(path4.resolve(file));
1412
+ watcher.add(path5.resolve(file));
1422
1413
  }
1423
1414
  }
1424
1415
 
1425
1416
  // src/utils/resolve.ts
1426
- import path5 from "path";
1417
+ import path6 from "path";
1427
1418
  import { builtinModules, createRequire as createRequire4 } from "module";
1428
1419
  function resolveViaPackageJsonSvelte(importee, importer, cache) {
1429
1420
  if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
@@ -1436,7 +1427,7 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1436
1427
  if (pkgData) {
1437
1428
  const { pkg, dir } = pkgData;
1438
1429
  if (pkg.svelte) {
1439
- const result = path5.resolve(dir, pkg.svelte);
1430
+ const result = path6.resolve(dir, pkg.svelte);
1440
1431
  cache.setResolvedSvelteField(importee, importer, result);
1441
1432
  return result;
1442
1433
  }
@@ -1447,7 +1438,7 @@ function isNodeInternal(importee) {
1447
1438
  return importee.startsWith("node:") || builtinModules.includes(importee);
1448
1439
  }
1449
1440
  function isBareImport(importee) {
1450
- if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
1441
+ if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path6.isAbsolute(importee)) {
1451
1442
  return false;
1452
1443
  }
1453
1444
  const parts = importee.split("/");
@@ -1462,9 +1453,8 @@ function isBareImport(importee) {
1462
1453
  }
1463
1454
 
1464
1455
  // src/utils/optimizer.ts
1465
- import fs6 from "fs";
1466
- import path6 from "path";
1467
- import { optimizeDeps } from "vite";
1456
+ import { promises as fs6 } from "fs";
1457
+ import path7 from "path";
1468
1458
  var PREBUNDLE_SENSITIVE_OPTIONS = [
1469
1459
  "compilerOptions",
1470
1460
  "configFile",
@@ -1473,23 +1463,20 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1473
1463
  "ignorePluginPreprocessors",
1474
1464
  "preprocess"
1475
1465
  ];
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) => {
1466
+ async function saveSvelteMetadata(cacheDir, options) {
1467
+ const svelteMetadata = generateSvelteMetadata(options);
1468
+ const svelteMetadataPath = path7.resolve(cacheDir, "_svelte_metadata.json");
1469
+ const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
1484
1470
  return typeof value === "function" ? value.toString() : value;
1485
1471
  });
1486
- if (fs6.existsSync(svelteMetadataPath)) {
1487
- const existingSvelteMetadata = fs6.readFileSync(svelteMetadataPath, "utf8");
1488
- if (existingSvelteMetadata === currentSvelteMetadata)
1489
- return;
1472
+ let existingSvelteMetadata;
1473
+ try {
1474
+ existingSvelteMetadata = await fs6.readFile(svelteMetadataPath, "utf8");
1475
+ } catch {
1490
1476
  }
1491
- await optimizeDeps(viteConfig, true);
1492
- fs6.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
1477
+ await fs6.mkdir(cacheDir, { recursive: true });
1478
+ await fs6.writeFile(svelteMetadataPath, currentSvelteMetadata);
1479
+ return currentSvelteMetadata !== existingSvelteMetadata;
1493
1480
  }
1494
1481
  function generateSvelteMetadata(options) {
1495
1482
  const metadata = {};
@@ -1498,14 +1485,6 @@ function generateSvelteMetadata(options) {
1498
1485
  }
1499
1486
  return metadata;
1500
1487
  }
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
- }
1508
- }
1509
1488
 
1510
1489
  // src/index.ts
1511
1490
  function svelte(inlineOptions) {
@@ -1542,7 +1521,12 @@ function svelte(inlineOptions) {
1542
1521
  log.debug("resolved options", options);
1543
1522
  },
1544
1523
  async buildStart() {
1545
- await handleOptimizeDeps(options, viteConfig);
1524
+ if (!options.experimental.prebundleSvelteLibraries)
1525
+ return;
1526
+ const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
1527
+ if (isSvelteMetadataChanged) {
1528
+ viteConfig.server.force = true;
1529
+ }
1546
1530
  },
1547
1531
  configureServer(server) {
1548
1532
  options.server = server;