@sveltejs/vite-plugin-svelte 1.0.0-next.34 → 1.0.0-next.35

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
@@ -26,7 +26,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
26
26
  });
27
27
 
28
28
  // src/index.ts
29
- import fs8 from "fs";
29
+ import fs7 from "fs";
30
30
 
31
31
  // src/utils/log.ts
32
32
  import { cyan, yellow, red } from "kleur/colors";
@@ -1237,6 +1237,7 @@ var VitePluginSvelteCache = class {
1237
1237
  this._js = /* @__PURE__ */ new Map();
1238
1238
  this._dependencies = /* @__PURE__ */ new Map();
1239
1239
  this._dependants = /* @__PURE__ */ new Map();
1240
+ this._resolvedSvelteFields = /* @__PURE__ */ new Map();
1240
1241
  }
1241
1242
  update(compileData) {
1242
1243
  this.updateCSS(compileData);
@@ -1302,6 +1303,15 @@ var VitePluginSvelteCache = class {
1302
1303
  const dependants = this._dependants.get(path7);
1303
1304
  return dependants ? [...dependants] : [];
1304
1305
  }
1306
+ getResolvedSvelteField(name, importer) {
1307
+ return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
1308
+ }
1309
+ setResolvedSvelteField(importee, importer = void 0, resolvedSvelte) {
1310
+ this._resolvedSvelteFields.set(this._getResolvedSvelteFieldKey(importee, importer), resolvedSvelte);
1311
+ }
1312
+ _getResolvedSvelteFieldKey(importee, importer) {
1313
+ return importer ? `${importer} > ${importee}` : importee;
1314
+ }
1305
1315
  };
1306
1316
 
1307
1317
  // src/utils/watch.ts
@@ -1381,14 +1391,24 @@ function ensureWatchedFile(watcher, file, root) {
1381
1391
 
1382
1392
  // src/utils/resolve.ts
1383
1393
  import path5 from "path";
1384
- import fs6 from "fs";
1385
- import relative from "require-relative";
1386
- function resolveViaPackageJsonSvelte(importee, importer) {
1387
- if (importer && isBareImport(importee)) {
1388
- const importeePkgFile = relative.resolve(`${importee}/package.json`, path5.dirname(importer));
1389
- const importeePkg = JSON.parse(fs6.readFileSync(importeePkgFile, { encoding: "utf-8" }));
1390
- if (importeePkg.svelte) {
1391
- return path5.resolve(path5.dirname(importeePkgFile), importeePkg.svelte);
1394
+ import { createRequire as createRequire4 } from "module";
1395
+ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1396
+ if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
1397
+ const cached = cache.getResolvedSvelteField(importee, importer);
1398
+ if (cached) {
1399
+ return cached;
1400
+ }
1401
+ const localRequire = createRequire4(importer);
1402
+ const pkgData = resolveDependencyData(importee, localRequire);
1403
+ if (pkgData) {
1404
+ const { pkg, dir } = pkgData;
1405
+ if (pkg.svelte) {
1406
+ const result = path5.resolve(dir, pkg.svelte);
1407
+ cache.setResolvedSvelteField(importee, importer, result);
1408
+ return result;
1409
+ }
1410
+ } else {
1411
+ throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
1392
1412
  }
1393
1413
  }
1394
1414
  }
@@ -1408,7 +1428,7 @@ function isBareImport(importee) {
1408
1428
  }
1409
1429
 
1410
1430
  // src/utils/optimizer.ts
1411
- import fs7 from "fs";
1431
+ import fs6 from "fs";
1412
1432
  import path6 from "path";
1413
1433
  import { optimizeDeps } from "vite";
1414
1434
  var PREBUNDLE_SENSITIVE_OPTIONS = [
@@ -1423,19 +1443,19 @@ async function handleOptimizeDeps(options, viteConfig) {
1423
1443
  if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1424
1444
  return;
1425
1445
  const viteMetadataPath = path6.resolve(viteConfig.cacheDir, "_metadata.json");
1426
- if (!fs7.existsSync(viteMetadataPath))
1446
+ if (!fs6.existsSync(viteMetadataPath))
1427
1447
  return;
1428
1448
  const svelteMetadataPath = path6.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
1429
1449
  const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
1430
1450
  return typeof value === "function" ? value.toString() : value;
1431
1451
  });
1432
- if (fs7.existsSync(svelteMetadataPath)) {
1433
- const existingSvelteMetadata = fs7.readFileSync(svelteMetadataPath, "utf8");
1452
+ if (fs6.existsSync(svelteMetadataPath)) {
1453
+ const existingSvelteMetadata = fs6.readFileSync(svelteMetadataPath, "utf8");
1434
1454
  if (existingSvelteMetadata === currentSvelteMetadata)
1435
1455
  return;
1436
1456
  }
1437
1457
  await optimizeDeps(viteConfig, true);
1438
- fs7.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
1458
+ fs6.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
1439
1459
  }
1440
1460
  function generateSvelteMetadata(options) {
1441
1461
  const metadata = {};
@@ -1452,7 +1472,7 @@ function svelte(inlineOptions) {
1452
1472
  }
1453
1473
  validateInlineOptions(inlineOptions);
1454
1474
  const cache = new VitePluginSvelteCache();
1455
- const pkg_export_errors = /* @__PURE__ */ new Set();
1475
+ const pkg_resolve_errors = /* @__PURE__ */ new Set();
1456
1476
  let requestParser;
1457
1477
  let options;
1458
1478
  let viteConfig;
@@ -1501,7 +1521,7 @@ function svelte(inlineOptions) {
1501
1521
  }
1502
1522
  if (viteConfig.assetsInclude(filename)) {
1503
1523
  log.debug(`load returns raw content for ${filename}`);
1504
- return fs8.readFileSync(filename, "utf-8");
1524
+ return fs7.readFileSync(filename, "utf-8");
1505
1525
  }
1506
1526
  }
1507
1527
  },
@@ -1529,21 +1549,13 @@ function svelte(inlineOptions) {
1529
1549
  return resolvedSvelteSSR;
1530
1550
  }
1531
1551
  try {
1532
- const resolved = resolveViaPackageJsonSvelte(importee, importer);
1552
+ const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1533
1553
  if (resolved) {
1534
1554
  log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1535
1555
  return resolved;
1536
1556
  }
1537
1557
  } catch (err) {
1538
- switch (err.code) {
1539
- case "ERR_PACKAGE_PATH_NOT_EXPORTED":
1540
- pkg_export_errors.add(importee);
1541
- return null;
1542
- case "MODULE_NOT_FOUND":
1543
- return null;
1544
- default:
1545
- throw err;
1546
- }
1558
+ pkg_resolve_errors.add(importee);
1547
1559
  }
1548
1560
  },
1549
1561
  async transform(code, id, opts) {
@@ -1591,8 +1603,10 @@ function svelte(inlineOptions) {
1591
1603
  }
1592
1604
  },
1593
1605
  buildEnd() {
1594
- if (pkg_export_errors.size > 0) {
1595
- log.warn(`The following packages did not export their \`package.json\` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.`, Array.from(pkg_export_errors, (s) => `- ${s}`).join("\n"));
1606
+ if (pkg_resolve_errors.size > 0) {
1607
+ log.warn(`vite-plugin-svelte was unable to find package.json of the following packages and wasn't able to resolve via their "svelte" field.
1608
+ If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
1609
+ ${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join("\n")}`.replace(/\t/g, ""));
1596
1610
  }
1597
1611
  }
1598
1612
  };