@sveltejs/vite-plugin-svelte 1.0.0-next.38 → 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,9 +1157,9 @@ 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
- function buildExtraViteConfig(options, config, configEnv) {
1162
+ function buildExtraViteConfig(options, config) {
1174
1163
  const svelteDeps = findRootSvelteDependencies(options.root);
1175
1164
  const extraViteConfig = {
1176
1165
  resolve: {
@@ -1178,22 +1167,31 @@ function buildExtraViteConfig(options, config, configEnv) {
1178
1167
  dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS]
1179
1168
  }
1180
1169
  };
1181
- if (configEnv.command === "serve") {
1170
+ if (options.isServe) {
1182
1171
  extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
1183
1172
  }
1184
- extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
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
+ }
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));
@@ -1244,9 +1235,16 @@ function buildSSROptionsForSvelte(svelteDeps, options, config) {
1244
1235
  var _a2, _b2, _c2, _d;
1245
1236
  return !((_b2 = (_a2 = config.ssr) == null ? void 0 : _a2.external) == null ? void 0 : _b2.includes(x)) && !((_d = (_c2 = config.optimizeDeps) == null ? void 0 : _c2.include) == null ? void 0 : _d.includes(x));
1246
1237
  }));
1247
- return {
1238
+ const ssr = {
1248
1239
  noExternal
1249
1240
  };
1241
+ if (options.isServe) {
1242
+ ssr.external = Array.from(new Set(svelteDeps.flatMap((dep) => Object.keys(dep.pkg.dependencies || {})))).filter((dep) => {
1243
+ var _a2, _b2, _c2, _d;
1244
+ return !ssr.noExternal.includes(dep) && !((_b2 = (_a2 = config.ssr) == null ? void 0 : _a2.noExternal) == null ? void 0 : _b2.includes(dep)) && !((_d = (_c2 = config.ssr) == null ? void 0 : _c2.external) == null ? void 0 : _d.includes(dep));
1245
+ });
1246
+ }
1247
+ return ssr;
1250
1248
  }
1251
1249
  function patchResolvedViteConfig(viteConfig, options) {
1252
1250
  var _a, _b;
@@ -1325,8 +1323,8 @@ var VitePluginSvelteCache = class {
1325
1323
  return this._js.get(svelteRequest.normalizedFilename);
1326
1324
  }
1327
1325
  }
1328
- getDependants(path7) {
1329
- const dependants = this._dependants.get(path7);
1326
+ getDependants(path8) {
1327
+ const dependants = this._dependants.get(path8);
1330
1328
  return dependants ? [...dependants] : [];
1331
1329
  }
1332
1330
  getResolvedSvelteField(name, importer) {
@@ -1342,7 +1340,7 @@ var VitePluginSvelteCache = class {
1342
1340
 
1343
1341
  // src/utils/watch.ts
1344
1342
  import fs5 from "fs";
1345
- import path4 from "path";
1343
+ import path5 from "path";
1346
1344
  function setupWatchers(options, cache, requestParser) {
1347
1345
  const { server, configFile: svelteConfigFile } = options;
1348
1346
  if (!server) {
@@ -1381,7 +1379,7 @@ function setupWatchers(options, cache, requestParser) {
1381
1379
  server.restart();
1382
1380
  }
1383
1381
  };
1384
- const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
1382
+ const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path5.join(root, cfg));
1385
1383
  const restartOnConfigAdd = (filename) => {
1386
1384
  if (possibleSvelteConfigs.includes(filename)) {
1387
1385
  triggerViteRestart(filename);
@@ -1411,12 +1409,12 @@ function setupWatchers(options, cache, requestParser) {
1411
1409
  }
1412
1410
  function ensureWatchedFile(watcher, file, root) {
1413
1411
  if (file && !file.startsWith(root + "/") && !file.includes("\0") && fs5.existsSync(file)) {
1414
- watcher.add(path4.resolve(file));
1412
+ watcher.add(path5.resolve(file));
1415
1413
  }
1416
1414
  }
1417
1415
 
1418
1416
  // src/utils/resolve.ts
1419
- import path5 from "path";
1417
+ import path6 from "path";
1420
1418
  import { builtinModules, createRequire as createRequire4 } from "module";
1421
1419
  function resolveViaPackageJsonSvelte(importee, importer, cache) {
1422
1420
  if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
@@ -1429,7 +1427,7 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1429
1427
  if (pkgData) {
1430
1428
  const { pkg, dir } = pkgData;
1431
1429
  if (pkg.svelte) {
1432
- const result = path5.resolve(dir, pkg.svelte);
1430
+ const result = path6.resolve(dir, pkg.svelte);
1433
1431
  cache.setResolvedSvelteField(importee, importer, result);
1434
1432
  return result;
1435
1433
  }
@@ -1440,7 +1438,7 @@ function isNodeInternal(importee) {
1440
1438
  return importee.startsWith("node:") || builtinModules.includes(importee);
1441
1439
  }
1442
1440
  function isBareImport(importee) {
1443
- 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)) {
1444
1442
  return false;
1445
1443
  }
1446
1444
  const parts = importee.split("/");
@@ -1455,9 +1453,8 @@ function isBareImport(importee) {
1455
1453
  }
1456
1454
 
1457
1455
  // src/utils/optimizer.ts
1458
- import fs6 from "fs";
1459
- import path6 from "path";
1460
- import { optimizeDeps } from "vite";
1456
+ import { promises as fs6 } from "fs";
1457
+ import path7 from "path";
1461
1458
  var PREBUNDLE_SENSITIVE_OPTIONS = [
1462
1459
  "compilerOptions",
1463
1460
  "configFile",
@@ -1466,23 +1463,20 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1466
1463
  "ignorePluginPreprocessors",
1467
1464
  "preprocess"
1468
1465
  ];
1469
- async function handleOptimizeDeps(options, viteConfig) {
1470
- if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1471
- return;
1472
- const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
1473
- if (!viteMetadataPath)
1474
- return;
1475
- const svelteMetadataPath = path6.resolve(viteMetadataPath, "../_svelte_metadata.json");
1476
- 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) => {
1477
1470
  return typeof value === "function" ? value.toString() : value;
1478
1471
  });
1479
- if (fs6.existsSync(svelteMetadataPath)) {
1480
- const existingSvelteMetadata = fs6.readFileSync(svelteMetadataPath, "utf8");
1481
- if (existingSvelteMetadata === currentSvelteMetadata)
1482
- return;
1472
+ let existingSvelteMetadata;
1473
+ try {
1474
+ existingSvelteMetadata = await fs6.readFile(svelteMetadataPath, "utf8");
1475
+ } catch {
1483
1476
  }
1484
- await optimizeDeps(viteConfig, true);
1485
- fs6.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
1477
+ await fs6.mkdir(cacheDir, { recursive: true });
1478
+ await fs6.writeFile(svelteMetadataPath, currentSvelteMetadata);
1479
+ return currentSvelteMetadata !== existingSvelteMetadata;
1486
1480
  }
1487
1481
  function generateSvelteMetadata(options) {
1488
1482
  const metadata = {};
@@ -1491,14 +1485,6 @@ function generateSvelteMetadata(options) {
1491
1485
  }
1492
1486
  return metadata;
1493
1487
  }
1494
- function findViteMetadataPath(cacheDir) {
1495
- const metadataPaths = ["_metadata.json", "deps/_metadata.json"];
1496
- for (const metadataPath of metadataPaths) {
1497
- const viteMetadataPath = path6.resolve(cacheDir, metadataPath);
1498
- if (fs6.existsSync(viteMetadataPath))
1499
- return viteMetadataPath;
1500
- }
1501
- }
1502
1488
 
1503
1489
  // src/index.ts
1504
1490
  function svelte(inlineOptions) {
@@ -1522,7 +1508,7 @@ function svelte(inlineOptions) {
1522
1508
  log.setLevel(config.logLevel);
1523
1509
  }
1524
1510
  options = await preResolveOptions(inlineOptions, config, configEnv);
1525
- const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
1511
+ const extraViteConfig = buildExtraViteConfig(options, config);
1526
1512
  log.debug("additional vite config", extraViteConfig);
1527
1513
  return extraViteConfig;
1528
1514
  },
@@ -1535,7 +1521,12 @@ function svelte(inlineOptions) {
1535
1521
  log.debug("resolved options", options);
1536
1522
  },
1537
1523
  async buildStart() {
1538
- 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
+ }
1539
1530
  },
1540
1531
  configureServer(server) {
1541
1532
  options.server = server;
@@ -1582,10 +1573,14 @@ function svelte(inlineOptions) {
1582
1573
  }
1583
1574
  return resolvedSvelteSSR;
1584
1575
  }
1585
- const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1586
- if (resolved) {
1587
- log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1588
- return resolved;
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;
1581
+ }
1582
+ } catch (e) {
1583
+ log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
1589
1584
  }
1590
1585
  },
1591
1586
  async transform(code, id, opts) {