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

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
@@ -684,11 +684,20 @@ function needsOptimization(dep, localRequire) {
684
684
  if (!depData)
685
685
  return false;
686
686
  const pkg = depData.pkg;
687
- const isCjs = pkg.main && !pkg.module && !pkg.exports;
688
- if (!isCjs)
687
+ const hasEsmFields = pkg.module || pkg.exports;
688
+ if (hasEsmFields)
689
689
  return false;
690
- const entryExt = path2.extname(pkg.main);
691
- 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
+ }
692
701
  }
693
702
 
694
703
  // src/utils/options.ts
@@ -1161,7 +1170,7 @@ function enforceOptionsForProduction(options) {
1161
1170
  function resolveViteRoot(viteConfig) {
1162
1171
  return normalizePath2(viteConfig.root ? path3.resolve(viteConfig.root) : process.cwd());
1163
1172
  }
1164
- function buildExtraViteConfig(options, config, configEnv) {
1173
+ function buildExtraViteConfig(options, config) {
1165
1174
  const svelteDeps = findRootSvelteDependencies(options.root);
1166
1175
  const extraViteConfig = {
1167
1176
  resolve: {
@@ -1169,10 +1178,10 @@ function buildExtraViteConfig(options, config, configEnv) {
1169
1178
  dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS]
1170
1179
  }
1171
1180
  };
1172
- if (configEnv.command === "serve") {
1181
+ if (options.isServe) {
1173
1182
  extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
1174
1183
  }
1175
- extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
1184
+ extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
1176
1185
  return extraViteConfig;
1177
1186
  }
1178
1187
  function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps2) {
@@ -1235,9 +1244,16 @@ function buildSSROptionsForSvelte(svelteDeps, options, config) {
1235
1244
  var _a2, _b2, _c2, _d;
1236
1245
  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));
1237
1246
  }));
1238
- return {
1247
+ const ssr = {
1239
1248
  noExternal
1240
1249
  };
1250
+ if (options.isServe) {
1251
+ ssr.external = Array.from(new Set(svelteDeps.flatMap((dep) => Object.keys(dep.pkg.dependencies || {})))).filter((dep) => {
1252
+ var _a2, _b2, _c2, _d;
1253
+ 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));
1254
+ });
1255
+ }
1256
+ return ssr;
1241
1257
  }
1242
1258
  function patchResolvedViteConfig(viteConfig, options) {
1243
1259
  var _a, _b;
@@ -1424,8 +1440,6 @@ function resolveViaPackageJsonSvelte(importee, importer, cache) {
1424
1440
  cache.setResolvedSvelteField(importee, importer, result);
1425
1441
  return result;
1426
1442
  }
1427
- } else {
1428
- throw new Error(`failed to resolve package.json of ${importee} imported by ${importer}`);
1429
1443
  }
1430
1444
  }
1431
1445
  }
@@ -1462,10 +1476,10 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1462
1476
  async function handleOptimizeDeps(options, viteConfig) {
1463
1477
  if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir)
1464
1478
  return;
1465
- const viteMetadataPath = path6.resolve(viteConfig.cacheDir, "_metadata.json");
1466
- if (!fs6.existsSync(viteMetadataPath))
1479
+ const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
1480
+ if (!viteMetadataPath)
1467
1481
  return;
1468
- const svelteMetadataPath = path6.resolve(viteConfig.cacheDir, "_svelte_metadata.json");
1482
+ const svelteMetadataPath = path6.resolve(viteMetadataPath, "../_svelte_metadata.json");
1469
1483
  const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
1470
1484
  return typeof value === "function" ? value.toString() : value;
1471
1485
  });
@@ -1484,6 +1498,14 @@ function generateSvelteMetadata(options) {
1484
1498
  }
1485
1499
  return metadata;
1486
1500
  }
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
+ }
1487
1509
 
1488
1510
  // src/index.ts
1489
1511
  function svelte(inlineOptions) {
@@ -1492,7 +1514,6 @@ function svelte(inlineOptions) {
1492
1514
  }
1493
1515
  validateInlineOptions(inlineOptions);
1494
1516
  const cache = new VitePluginSvelteCache();
1495
- const pkg_resolve_errors = /* @__PURE__ */ new Set();
1496
1517
  let requestParser;
1497
1518
  let options;
1498
1519
  let viteConfig;
@@ -1508,7 +1529,7 @@ function svelte(inlineOptions) {
1508
1529
  log.setLevel(config.logLevel);
1509
1530
  }
1510
1531
  options = await preResolveOptions(inlineOptions, config, configEnv);
1511
- const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
1532
+ const extraViteConfig = buildExtraViteConfig(options, config);
1512
1533
  log.debug("additional vite config", extraViteConfig);
1513
1534
  return extraViteConfig;
1514
1535
  },
@@ -1574,29 +1595,17 @@ function svelte(inlineOptions) {
1574
1595
  log.debug(`resolveId resolved ${resolved} via package.json svelte field of ${importee}`);
1575
1596
  return resolved;
1576
1597
  }
1577
- } catch (err) {
1578
- pkg_resolve_errors.add(importee);
1598
+ } catch (e) {
1599
+ log.debug.once(`error trying to resolve ${importee} from ${importer} via package.json svelte field `, e);
1579
1600
  }
1580
1601
  },
1581
1602
  async transform(code, id, opts) {
1582
1603
  var _a;
1583
1604
  const ssr = !!(opts == null ? void 0 : opts.ssr);
1584
1605
  const svelteRequest = requestParser(id, ssr);
1585
- if (!svelteRequest) {
1606
+ if (!svelteRequest || svelteRequest.query.svelte) {
1586
1607
  return;
1587
1608
  }
1588
- const { filename, query } = svelteRequest;
1589
- if (query.svelte) {
1590
- if (query.type === "style") {
1591
- const css = cache.getCSS(svelteRequest);
1592
- if (css) {
1593
- log.debug(`transform returns css for ${filename}`);
1594
- return css;
1595
- }
1596
- }
1597
- log.error("failed to transform tagged svelte request", svelteRequest);
1598
- throw new Error(`failed to transform tagged svelte request for id ${id}`);
1599
- }
1600
1609
  let compileData;
1601
1610
  try {
1602
1611
  compileData = await compileSvelte2(svelteRequest, code, options);
@@ -1610,7 +1619,7 @@ function svelte(inlineOptions) {
1610
1619
  ensureWatchedFile(options.server.watcher, d, options.root);
1611
1620
  });
1612
1621
  }
1613
- log.debug(`transform returns compiled js for ${filename}`);
1622
+ log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
1614
1623
  return compileData.compiled.js;
1615
1624
  },
1616
1625
  handleHotUpdate(ctx) {
@@ -1621,13 +1630,6 @@ function svelte(inlineOptions) {
1621
1630
  if (svelteRequest) {
1622
1631
  return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1623
1632
  }
1624
- },
1625
- buildEnd() {
1626
- if (pkg_resolve_errors.size > 0) {
1627
- 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.
1628
- If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
1629
- ${Array.from(pkg_resolve_errors, (s) => `- ${s}`).join("\n")}`.replace(/\t/g, ""));
1630
- }
1631
1633
  }
1632
1634
  };
1633
1635
  }