@sveltejs/vite-plugin-svelte 1.0.0-next.29 → 1.0.0-next.32

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
@@ -470,7 +470,7 @@ async function loadSvelteConfig(viteConfig, inlineOptions) {
470
470
  }
471
471
  if (!configFile.endsWith(".mjs")) {
472
472
  try {
473
- const _require = import.meta.url ? esmRequire != null ? esmRequire : esmRequire = createRequire(import.meta.url) : __require;
473
+ const _require = import.meta.url ? esmRequire ?? (esmRequire = createRequire(import.meta.url)) : __require;
474
474
  delete _require.cache[_require.resolve(configFile)];
475
475
  const result = _require(configFile);
476
476
  if (result != null) {
@@ -687,18 +687,73 @@ import { createRequire as createRequire3 } from "module";
687
687
  // src/utils/esbuild.ts
688
688
  import { promises as fs4 } from "fs";
689
689
  import { compile as compile2, preprocess as preprocess2 } from "svelte/compiler";
690
+
691
+ // src/utils/error.ts
692
+ function toRollupError(error) {
693
+ const { filename, frame, start, code, name } = error;
694
+ const rollupError = {
695
+ name,
696
+ id: filename,
697
+ message: buildExtendedLogMessage(error),
698
+ frame: formatFrameForVite(frame),
699
+ code,
700
+ stack: ""
701
+ };
702
+ if (start) {
703
+ rollupError.loc = {
704
+ line: start.line,
705
+ column: start.column,
706
+ file: filename
707
+ };
708
+ }
709
+ return rollupError;
710
+ }
711
+ function toESBuildError(error) {
712
+ const { filename, frame, start } = error;
713
+ const partialMessage = {
714
+ text: buildExtendedLogMessage(error)
715
+ };
716
+ if (start) {
717
+ partialMessage.location = {
718
+ line: start.line,
719
+ column: start.column,
720
+ file: filename,
721
+ lineText: lineFromFrame(start.line, frame)
722
+ };
723
+ }
724
+ return partialMessage;
725
+ }
726
+ function lineFromFrame(lineNo, frame) {
727
+ if (!frame) {
728
+ return "";
729
+ }
730
+ const lines = frame.split("\n");
731
+ const errorLine = lines.find((line) => line.trimStart().startsWith(`${lineNo}: `));
732
+ return errorLine ? errorLine.substring(errorLine.indexOf(": ") + 3) : "";
733
+ }
734
+ function formatFrameForVite(frame) {
735
+ if (!frame) {
736
+ return "";
737
+ }
738
+ return frame.split("\n").map((line) => line.match(/^\s+\^/) ? " " + line : " " + line.replace(":", " | ")).join("\n");
739
+ }
740
+
741
+ // src/utils/esbuild.ts
690
742
  function esbuildSveltePlugin(options) {
691
743
  return {
692
744
  name: "vite-plugin-svelte:optimize-svelte",
693
745
  setup(build) {
694
- var _a;
695
746
  disableVitePrebundleSvelte(build);
696
- const svelteExtensions = ((_a = options.extensions) != null ? _a : [".svelte"]).map((ext) => ext.slice(1));
747
+ const svelteExtensions = (options.extensions ?? [".svelte"]).map((ext) => ext.slice(1));
697
748
  const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join("|") + `)(\\?.*)?$`);
698
749
  build.onLoad({ filter: svelteFilter }, async ({ path: filename }) => {
699
750
  const code = await fs4.readFile(filename, "utf8");
700
- const contents = await compileSvelte(options, { filename, code });
701
- return { contents };
751
+ try {
752
+ const contents = await compileSvelte(options, { filename, code });
753
+ return { contents };
754
+ } catch (e) {
755
+ return { errors: [toESBuildError(e)] };
756
+ }
702
757
  });
703
758
  }
704
759
  };
@@ -835,7 +890,8 @@ function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, v
835
890
  root: viteConfig.root,
836
891
  isProduction: viteEnv.mode === "production",
837
892
  isBuild: viteEnv.command === "build",
838
- isServe: viteEnv.command === "serve"
893
+ isServe: viteEnv.command === "serve",
894
+ isSvelteKit: !!(svelteConfig == null ? void 0 : svelteConfig.kit)
839
895
  });
840
896
  if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
841
897
  merged.configFile = svelteConfig.configFile;
@@ -843,12 +899,11 @@ function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, v
843
899
  return merged;
844
900
  }
845
901
  async function resolveOptions(inlineOptions = {}, viteConfig, viteEnv) {
846
- var _a;
847
902
  const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteConfig), {
848
903
  root: resolveViteRoot(viteConfig)
849
904
  });
850
905
  const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions) || {};
851
- const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", (_a = inlineOptions.emitCss) != null ? _a : svelteConfig.emitCss);
906
+ const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", inlineOptions.emitCss ?? svelteConfig.emitCss);
852
907
  const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfigWithResolvedRoot, viteEnv);
853
908
  enforceOptionsForProduction(resolvedOptions);
854
909
  enforceOptionsForHmr(resolvedOptions);
@@ -1018,7 +1073,7 @@ function setupWatchers(options, cache, requestParser) {
1018
1073
  return;
1019
1074
  }
1020
1075
  const { watcher, ws } = server;
1021
- const { configFile: viteConfigFile, root, server: serverConfig } = server.config;
1076
+ const { root, server: serverConfig } = server.config;
1022
1077
  const emitChangeEventOnDependants = (filename) => {
1023
1078
  const dependants = cache.getDependants(filename);
1024
1079
  dependants.forEach((dependant) => {
@@ -1038,16 +1093,17 @@ function setupWatchers(options, cache, requestParser) {
1038
1093
  }
1039
1094
  };
1040
1095
  const triggerViteRestart = (filename) => {
1041
- if (!!viteConfigFile && !serverConfig.middlewareMode) {
1042
- log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
1043
- watcher.emit("change", viteConfigFile);
1044
- } else {
1096
+ var _a;
1097
+ if (serverConfig.middlewareMode || options.isSvelteKit) {
1045
1098
  const message = "Svelte config change detected, restart your dev process to apply the changes.";
1046
1099
  log.info(message, filename);
1047
1100
  ws.send({
1048
1101
  type: "error",
1049
1102
  err: { message, stack: "", plugin: "vite-plugin-svelte", id: filename }
1050
1103
  });
1104
+ } else {
1105
+ log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
1106
+ server.restart(!!((_a = options.experimental) == null ? void 0 : _a.prebundleSvelteLibraries));
1051
1107
  }
1052
1108
  };
1053
1109
  const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path4.join(root, cfg));
@@ -1098,7 +1154,7 @@ function resolveViaPackageJsonSvelte(importee, importer) {
1098
1154
  }
1099
1155
  }
1100
1156
  function isBareImport(importee) {
1101
- if (!importee || importee[0] === "." || importee[0] === "\0" || path5.isAbsolute(importee)) {
1157
+ if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path5.isAbsolute(importee)) {
1102
1158
  return false;
1103
1159
  }
1104
1160
  const parts = importee.split("/");
@@ -1397,9 +1453,9 @@ function svelte(inlineOptions) {
1397
1453
  }
1398
1454
  }
1399
1455
  },
1400
- async resolveId(importee, importer, opts, _ssr) {
1401
- const ssr = _ssr === true || opts.ssr;
1402
- const svelteRequest = requestParser(importee, !!ssr);
1456
+ async resolveId(importee, importer, opts) {
1457
+ const ssr = !!(opts == null ? void 0 : opts.ssr);
1458
+ const svelteRequest = requestParser(importee, ssr);
1403
1459
  if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
1404
1460
  if (svelteRequest.query.type === "style") {
1405
1461
  log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
@@ -1440,8 +1496,8 @@ function svelte(inlineOptions) {
1440
1496
  },
1441
1497
  async transform(code, id, opts) {
1442
1498
  var _a;
1443
- const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1444
- const svelteRequest = requestParser(id, !!ssr);
1499
+ const ssr = !!(opts == null ? void 0 : opts.ssr);
1500
+ const svelteRequest = requestParser(id, ssr);
1445
1501
  if (!svelteRequest) {
1446
1502
  return;
1447
1503
  }
@@ -1457,7 +1513,12 @@ function svelte(inlineOptions) {
1457
1513
  log.error("failed to transform tagged svelte request", svelteRequest);
1458
1514
  throw new Error(`failed to transform tagged svelte request for id ${id}`);
1459
1515
  }
1460
- const compileData = await compileSvelte2(svelteRequest, code, options);
1516
+ let compileData;
1517
+ try {
1518
+ compileData = await compileSvelte2(svelteRequest, code, options);
1519
+ } catch (e) {
1520
+ throw toRollupError(e);
1521
+ }
1461
1522
  logCompilerWarnings(compileData.compiled.warnings, options);
1462
1523
  cache.update(compileData);
1463
1524
  if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {