@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.js CHANGED
@@ -164,7 +164,10 @@ function buildExtendedLogMessage(w) {
164
164
  parts.push(":", w.start.line, ":", w.start.column);
165
165
  }
166
166
  if (w.message) {
167
- parts.push(" ", w.message);
167
+ if (parts.length > 0) {
168
+ parts.push(" ");
169
+ }
170
+ parts.push(w.message);
168
171
  }
169
172
  return parts.join("");
170
173
  }
@@ -292,7 +295,12 @@ var _createCompileSvelte = (makeHot) => async function compileSvelte2(svelteRequ
292
295
  }
293
296
  let preprocessed;
294
297
  if (options.preprocess) {
295
- preprocessed = await preprocess(code, options.preprocess, { filename });
298
+ try {
299
+ preprocessed = await preprocess(code, options.preprocess, { filename });
300
+ } catch (e) {
301
+ e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
302
+ throw e;
303
+ }
296
304
  if (preprocessed.dependencies)
297
305
  dependencies.push(...preprocessed.dependencies);
298
306
  if (preprocessed.map)
@@ -676,11 +684,20 @@ function needsOptimization(dep, localRequire) {
676
684
  if (!depData)
677
685
  return false;
678
686
  const pkg = depData.pkg;
679
- const isCjs = pkg.main && !pkg.module && !pkg.exports;
680
- if (!isCjs)
687
+ const hasEsmFields = pkg.module || pkg.exports;
688
+ if (hasEsmFields)
681
689
  return false;
682
- const entryExt = path2.extname(pkg.main);
683
- return !entryExt || entryExt === ".js" || entryExt === ".cjs";
690
+ if (pkg.main) {
691
+ const entryExt = path2.extname(pkg.main);
692
+ return !entryExt || entryExt === ".js" || entryExt === ".cjs";
693
+ } else {
694
+ try {
695
+ localRequire.resolve(`${dep}/index.js`);
696
+ return true;
697
+ } catch {
698
+ return false;
699
+ }
700
+ }
684
701
  }
685
702
 
686
703
  // src/utils/options.ts
@@ -691,15 +708,15 @@ import { promises as fs4 } from "fs";
691
708
  import { compile as compile2, preprocess as preprocess2 } from "svelte/compiler";
692
709
 
693
710
  // src/utils/error.ts
694
- function toRollupError(error) {
695
- const { filename, frame, start, code, name } = error;
711
+ function toRollupError(error, options) {
712
+ const { filename, frame, start, code, name, stack } = error;
696
713
  const rollupError = {
697
714
  name,
698
715
  id: filename,
699
716
  message: buildExtendedLogMessage(error),
700
717
  frame: formatFrameForVite(frame),
701
718
  code,
702
- stack: ""
719
+ stack: options.isBuild || options.isDebug || !frame ? stack : ""
703
720
  };
704
721
  if (start) {
705
722
  rollupError.loc = {
@@ -710,8 +727,8 @@ function toRollupError(error) {
710
727
  }
711
728
  return rollupError;
712
729
  }
713
- function toESBuildError(error) {
714
- const { filename, frame, start } = error;
730
+ function toESBuildError(error, options) {
731
+ const { filename, frame, start, stack } = error;
715
732
  const partialMessage = {
716
733
  text: buildExtendedLogMessage(error)
717
734
  };
@@ -723,6 +740,9 @@ function toESBuildError(error) {
723
740
  lineText: lineFromFrame(start.line, frame)
724
741
  };
725
742
  }
743
+ if (options.isBuild || options.isDebug || !frame) {
744
+ partialMessage.detail = stack;
745
+ }
726
746
  return partialMessage;
727
747
  }
728
748
  function lineFromFrame(lineNo, frame) {
@@ -755,7 +775,7 @@ function esbuildSveltePlugin(options) {
755
775
  const contents = await compileSvelte(options, { filename, code });
756
776
  return { contents };
757
777
  } catch (e) {
758
- return { errors: [toESBuildError(e)] };
778
+ return { errors: [toESBuildError(e, options)] };
759
779
  }
760
780
  });
761
781
  }
@@ -788,7 +808,12 @@ async function compileSvelte(options, { filename, code }) {
788
808
  });
789
809
  let preprocessed;
790
810
  if (options.preprocess) {
791
- preprocessed = await preprocess2(code, options.preprocess, { filename });
811
+ try {
812
+ preprocessed = await preprocess2(code, options.preprocess, { filename });
813
+ } catch (e) {
814
+ e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
815
+ throw e;
816
+ }
792
817
  if (preprocessed.map)
793
818
  compileOptions.sourcemap = preprocessed.map;
794
819
  }
@@ -1073,7 +1098,8 @@ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
1073
1098
  experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
1074
1099
  root: viteConfigWithResolvedRoot.root,
1075
1100
  isBuild: viteEnv.command === "build",
1076
- isServe: viteEnv.command === "serve"
1101
+ isServe: viteEnv.command === "serve",
1102
+ isDebug: process.env.DEBUG != null
1077
1103
  });
1078
1104
  if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
1079
1105
  merged.configFile = svelteConfig.configFile;
@@ -1391,9 +1417,9 @@ function ensureWatchedFile(watcher, file, root) {
1391
1417
 
1392
1418
  // src/utils/resolve.ts
1393
1419
  import path5 from "path";
1394
- import { createRequire as createRequire4 } from "module";
1420
+ import { builtinModules, createRequire as createRequire4 } from "module";
1395
1421
  function resolveViaPackageJsonSvelte(importee, importer, cache) {
1396
- if (importer && isBareImport(importee) && !is_common_without_svelte_field(importee)) {
1422
+ if (importer && isBareImport(importee) && !isNodeInternal(importee) && !is_common_without_svelte_field(importee)) {
1397
1423
  const cached = cache.getResolvedSvelteField(importee, importer);
1398
1424
  if (cached) {
1399
1425
  return cached;
@@ -1407,11 +1433,12 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1407
1433
  cache.setResolvedSvelteField(importee, importer, result);
1408
1434
  return result;
1409
1435
  }
1410
- } else {
1411
- throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
1412
1436
  }
1413
1437
  }
1414
1438
  }
1439
+ function isNodeInternal(importee) {
1440
+ return importee.startsWith("node:") || builtinModules.includes(importee);
1441
+ }
1415
1442
  function isBareImport(importee) {
1416
1443
  if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
1417
1444
  return false;
@@ -1442,10 +1469,10 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1442
1469
  async function handleOptimizeDeps(options, viteConfig) {
1443
1470
  if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1444
1471
  return;
1445
- const viteMetadataPath = path6.resolve(viteConfig.cacheDir, "_metadata.json");
1446
- if (!fs6.existsSync(viteMetadataPath))
1472
+ const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
1473
+ if (!viteMetadataPath)
1447
1474
  return;
1448
- const svelteMetadataPath = path6.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
1475
+ const svelteMetadataPath = path6.resolve(viteMetadataPath, "../_svelte_metadata.json");
1449
1476
  const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
1450
1477
  return typeof value === "function" ? value.toString() : value;
1451
1478
  });
@@ -1464,6 +1491,14 @@ function generateSvelteMetadata(options) {
1464
1491
  }
1465
1492
  return metadata;
1466
1493
  }
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
+ }
1467
1502
 
1468
1503
  // src/index.ts
1469
1504
  function svelte(inlineOptions) {
@@ -1472,7 +1507,6 @@ function svelte(inlineOptions) {
1472
1507
  }
1473
1508
  validateInlineOptions(inlineOptions);
1474
1509
  const cache = new VitePluginSvelteCache();
1475
- const pkg_resolve_errors = /* @__PURE__ */ new Set();
1476
1510
  let requestParser;
1477
1511
  let options;
1478
1512
  let viteConfig;
@@ -1548,40 +1582,24 @@ function svelte(inlineOptions) {
1548
1582
  }
1549
1583
  return resolvedSvelteSSR;
1550
1584
  }
1551
- try {
1552
- const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
1553
- if (resolved) {
1554
- log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1555
- return resolved;
1556
- }
1557
- } catch (err) {
1558
- pkg_resolve_errors.add(importee);
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;
1559
1589
  }
1560
1590
  },
1561
1591
  async transform(code, id, opts) {
1562
1592
  var _a;
1563
1593
  const ssr = !!(opts == null ? void 0 : opts.ssr);
1564
1594
  const svelteRequest = requestParser(id, ssr);
1565
- if (!svelteRequest) {
1595
+ if (!svelteRequest || svelteRequest.query.svelte) {
1566
1596
  return;
1567
1597
  }
1568
- const { filename, query } = svelteRequest;
1569
- if (query.svelte) {
1570
- if (query.type === "style") {
1571
- const css = cache.getCSS(svelteRequest);
1572
- if (css) {
1573
- log.debug(`transform returns css for ${filename}`);
1574
- return css;
1575
- }
1576
- }
1577
- log.error("failed to transform tagged svelte request", svelteRequest);
1578
- throw new Error(`failed to transform tagged svelte request for id ${id}`);
1579
- }
1580
1598
  let compileData;
1581
1599
  try {
1582
1600
  compileData = await compileSvelte2(svelteRequest, code, options);
1583
1601
  } catch (e) {
1584
- throw toRollupError(e);
1602
+ throw toRollupError(e, options);
1585
1603
  }
1586
1604
  logCompilerWarnings(compileData.compiled.warnings, options);
1587
1605
  cache.update(compileData);
@@ -1590,7 +1608,7 @@ function svelte(inlineOptions) {
1590
1608
  ensureWatchedFile(options.server.watcher, d, options.root);
1591
1609
  });
1592
1610
  }
1593
- log.debug(`transform returns compiled js for ${filename}`);
1611
+ log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1594
1612
  return compileData.compiled.js;
1595
1613
  },
1596
1614
  handleHotUpdate(ctx) {
@@ -1601,13 +1619,6 @@ function svelte(inlineOptions) {
1601
1619
  if (svelteRequest) {
1602
1620
  return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1603
1621
  }
1604
- },
1605
- buildEnd() {
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, ""));
1610
- }
1611
1622
  }
1612
1623
  };
1613
1624
  }