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

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.cjs CHANGED
@@ -49,7 +49,7 @@ __export(src_exports, {
49
49
  svelte: () => svelte
50
50
  });
51
51
 
52
- // ../../node_modules/.pnpm/tsup@5.11.11_typescript@4.5.4/node_modules/tsup/assets/cjs_shims.js
52
+ // ../../node_modules/.pnpm/tsup@5.11.13_typescript@4.5.5/node_modules/tsup/assets/cjs_shims.js
53
53
  var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
54
54
  var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
55
55
 
@@ -192,7 +192,10 @@ function buildExtendedLogMessage(w) {
192
192
  parts.push(":", w.start.line, ":", w.start.column);
193
193
  }
194
194
  if (w.message) {
195
- parts.push(" ", w.message);
195
+ if (parts.length > 0) {
196
+ parts.push(" ");
197
+ }
198
+ parts.push(w.message);
196
199
  }
197
200
  return parts.join("");
198
201
  }
@@ -320,7 +323,12 @@ var _createCompileSvelte = (makeHot) => async function compileSvelte2(svelteRequ
320
323
  }
321
324
  let preprocessed;
322
325
  if (options.preprocess) {
323
- preprocessed = await (0, import_compiler.preprocess)(code, options.preprocess, { filename });
326
+ try {
327
+ preprocessed = await (0, import_compiler.preprocess)(code, options.preprocess, { filename });
328
+ } catch (e) {
329
+ e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
330
+ throw e;
331
+ }
324
332
  if (preprocessed.dependencies)
325
333
  dependencies.push(...preprocessed.dependencies);
326
334
  if (preprocessed.map)
@@ -702,11 +710,20 @@ function needsOptimization(dep, localRequire) {
702
710
  if (!depData)
703
711
  return false;
704
712
  const pkg = depData.pkg;
705
- const isCjs = pkg.main && !pkg.module && !pkg.exports;
706
- if (!isCjs)
713
+ const hasEsmFields = pkg.module || pkg.exports;
714
+ if (hasEsmFields)
707
715
  return false;
708
- const entryExt = import_path2.default.extname(pkg.main);
709
- return !entryExt || entryExt === ".js" || entryExt === ".cjs";
716
+ if (pkg.main) {
717
+ const entryExt = import_path2.default.extname(pkg.main);
718
+ return !entryExt || entryExt === ".js" || entryExt === ".cjs";
719
+ } else {
720
+ try {
721
+ localRequire.resolve(`${dep}/index.js`);
722
+ return true;
723
+ } catch {
724
+ return false;
725
+ }
726
+ }
710
727
  }
711
728
 
712
729
  // src/utils/options.ts
@@ -717,15 +734,15 @@ var import_fs3 = require("fs");
717
734
  var import_compiler2 = require("svelte/compiler");
718
735
 
719
736
  // src/utils/error.ts
720
- function toRollupError(error) {
721
- const { filename, frame, start, code, name } = error;
737
+ function toRollupError(error, options) {
738
+ const { filename, frame, start, code, name, stack } = error;
722
739
  const rollupError = {
723
740
  name,
724
741
  id: filename,
725
742
  message: buildExtendedLogMessage(error),
726
743
  frame: formatFrameForVite(frame),
727
744
  code,
728
- stack: ""
745
+ stack: options.isBuild || options.isDebug || !frame ? stack : ""
729
746
  };
730
747
  if (start) {
731
748
  rollupError.loc = {
@@ -736,8 +753,8 @@ function toRollupError(error) {
736
753
  }
737
754
  return rollupError;
738
755
  }
739
- function toESBuildError(error) {
740
- const { filename, frame, start } = error;
756
+ function toESBuildError(error, options) {
757
+ const { filename, frame, start, stack } = error;
741
758
  const partialMessage = {
742
759
  text: buildExtendedLogMessage(error)
743
760
  };
@@ -749,6 +766,9 @@ function toESBuildError(error) {
749
766
  lineText: lineFromFrame(start.line, frame)
750
767
  };
751
768
  }
769
+ if (options.isBuild || options.isDebug || !frame) {
770
+ partialMessage.detail = stack;
771
+ }
752
772
  return partialMessage;
753
773
  }
754
774
  function lineFromFrame(lineNo, frame) {
@@ -781,7 +801,7 @@ function esbuildSveltePlugin(options) {
781
801
  const contents = await compileSvelte(options, { filename, code });
782
802
  return { contents };
783
803
  } catch (e) {
784
- return { errors: [toESBuildError(e)] };
804
+ return { errors: [toESBuildError(e, options)] };
785
805
  }
786
806
  });
787
807
  }
@@ -814,7 +834,12 @@ async function compileSvelte(options, { filename, code }) {
814
834
  });
815
835
  let preprocessed;
816
836
  if (options.preprocess) {
817
- preprocessed = await (0, import_compiler2.preprocess)(code, options.preprocess, { filename });
837
+ try {
838
+ preprocessed = await (0, import_compiler2.preprocess)(code, options.preprocess, { filename });
839
+ } catch (e) {
840
+ e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
841
+ throw e;
842
+ }
818
843
  if (preprocessed.map)
819
844
  compileOptions.sourcemap = preprocessed.map;
820
845
  }
@@ -1097,7 +1122,8 @@ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
1097
1122
  experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
1098
1123
  root: viteConfigWithResolvedRoot.root,
1099
1124
  isBuild: viteEnv.command === "build",
1100
- isServe: viteEnv.command === "serve"
1125
+ isServe: viteEnv.command === "serve",
1126
+ isDebug: process.env.DEBUG != null
1101
1127
  });
1102
1128
  if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
1103
1129
  merged.configFile = svelteConfig.configFile;
@@ -1417,7 +1443,7 @@ function ensureWatchedFile(watcher, file, root) {
1417
1443
  var import_path5 = __toESM(require("path"), 1);
1418
1444
  var import_module4 = require("module");
1419
1445
  function resolveViaPackageJsonSvelte(importee, importer, cache) {
1420
- if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
1446
+ if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
1421
1447
  const cached = cache.getResolvedSvelteField(importee, importer);
1422
1448
  if (cached) {
1423
1449
  return cached;
@@ -1431,11 +1457,12 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1431
1457
  cache.setResolvedSvelteField(importee, importer, result);
1432
1458
  return result;
1433
1459
  }
1434
- } else {
1435
- throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
1436
1460
  }
1437
1461
  }
1438
1462
  }
1463
+ function isNodeInternal(importee) {
1464
+ return importee.startsWith("node:") || import_module4.builtinModules.includes(importee);
1465
+ }
1439
1466
  function isBareImport(importee) {
1440
1467
  if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || import_path5.default.isAbsolute(importee)) {
1441
1468
  return false;
@@ -1466,10 +1493,10 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1466
1493
  async function handleOptimizeDeps(options, viteConfig) {
1467
1494
  if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1468
1495
  return;
1469
- const viteMetadataPath = import_path6.default.resolve(viteConfig.cacheDir, "_metadata.json");
1470
- if (!import_fs5.default.existsSync(viteMetadataPath))
1496
+ const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
1497
+ if (!viteMetadataPath)
1471
1498
  return;
1472
- const svelteMetadataPath = import_path6.default.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
1499
+ const svelteMetadataPath = import_path6.default.resolve(viteMetadataPath, "../_svelte_metadata.json");
1473
1500
  const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
1474
1501
  return typeof value === "function" ? value.toString() : value;
1475
1502
  });
@@ -1488,6 +1515,14 @@ function generateSvelteMetadata(options) {
1488
1515
  }
1489
1516
  return metadata;
1490
1517
  }
1518
+ function findViteMetadataPath(cacheDir) {
1519
+ const metadataPaths = ["_metadata.json", "deps/_metadata.json"];
1520
+ for (const metadataPath of metadataPaths) {
1521
+ const viteMetadataPath = import_path6.default.resolve(cacheDir, metadataPath);
1522
+ if (import_fs5.default.existsSync(viteMetadataPath))
1523
+ return viteMetadataPath;
1524
+ }
1525
+ }
1491
1526
 
1492
1527
  // src/index.ts
1493
1528
  function svelte(inlineOptions) {
@@ -1496,7 +1531,6 @@ function svelte(inlineOptions) {
1496
1531
  }
1497
1532
  validateInlineOptions(inlineOptions);
1498
1533
  const cache = new VitePluginSvelteCache();
1499
- const pkg_resolve_errors = /* @__PURE__ */ new Set();
1500
1534
  let requestParser;
1501
1535
  let options;
1502
1536
  let viteConfig;
@@ -1572,40 +1606,24 @@ function svelte(inlineOptions) {
1572
1606
  }
1573
1607
  return resolvedSvelteSSR;
1574
1608
  }
1575
- try {
1576
- const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1577
- if (resolved) {
1578
- log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1579
- return resolved;
1580
- }
1581
- } catch (err) {
1582
- pkg_resolve_errors.add(importee);
1609
+ const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1610
+ if (resolved) {
1611
+ log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1612
+ return resolved;
1583
1613
  }
1584
1614
  },
1585
1615
  async transform(code, id, opts) {
1586
1616
  var _a;
1587
1617
  const ssr = !!(opts == null ? void 0 : opts.ssr);
1588
1618
  const svelteRequest = requestParser(id, ssr);
1589
- if (!svelteRequest) {
1619
+ if (!svelteRequest || svelteRequest.query.svelte) {
1590
1620
  return;
1591
1621
  }
1592
- const { filename, query } = svelteRequest;
1593
- if (query.svelte) {
1594
- if (query.type === "style") {
1595
- const css = cache.getCSS(svelteRequest);
1596
- if (css) {
1597
- log.debug(`transform returns css for ${filename}`);
1598
- return css;
1599
- }
1600
- }
1601
- log.error("failed to transform tagged svelte request", svelteRequest);
1602
- throw new Error(`failed to transform tagged svelte request for id ${id}`);
1603
- }
1604
1622
  let compileData;
1605
1623
  try {
1606
1624
  compileData = await compileSvelte2(svelteRequest, code, options);
1607
1625
  } catch (e) {
1608
- throw toRollupError(e);
1626
+ throw toRollupError(e, options);
1609
1627
  }
1610
1628
  logCompilerWarnings(compileData.compiled.warnings, options);
1611
1629
  cache.update(compileData);
@@ -1614,7 +1632,7 @@ function svelte(inlineOptions) {
1614
1632
  ensureWatchedFile(options.server.watcher, d, options.root);
1615
1633
  });
1616
1634
  }
1617
- log.debug(`transform returns compiled js for ${filename}`);
1635
+ log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1618
1636
  return compileData.compiled.js;
1619
1637
  },
1620
1638
  handleHotUpdate(ctx) {
@@ -1625,13 +1643,6 @@ function svelte(inlineOptions) {
1625
1643
  if (svelteRequest) {
1626
1644
  return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1627
1645
  }
1628
- },
1629
- buildEnd() {
1630
- if (pkg_resolve_errors.size > 0) {
1631
- 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.
1632
- If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
1633
- ${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join("\n")}`.replace(/\t/g, ""));
1634
- }
1635
1646
  }
1636
1647
  };
1637
1648
  }