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

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
@@ -44,8 +44,9 @@ __export(exports, {
44
44
  svelte: () => svelte
45
45
  });
46
46
 
47
- // ../../node_modules/.pnpm/tsup@5.10.1_typescript@4.5.2/node_modules/tsup/assets/cjs_shims.js
48
- var importMetaUrlShim = typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
47
+ // ../../node_modules/.pnpm/tsup@5.11.9_typescript@4.5.4/node_modules/tsup/assets/cjs_shims.js
48
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
49
+ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
49
50
 
50
51
  // src/index.ts
51
52
  var import_fs6 = __toModule(require("fs"));
@@ -453,7 +454,7 @@ function buildIdParser(options) {
453
454
  }
454
455
 
455
456
  // src/utils/options.ts
456
- var import_vite2 = __toModule(require("vite"));
457
+ var import_vite3 = __toModule(require("vite"));
457
458
 
458
459
  // src/utils/load-svelte-config.ts
459
460
  var import_module = __toModule(require("module"));
@@ -488,7 +489,7 @@ async function loadSvelteConfig(viteConfig, inlineOptions) {
488
489
  }
489
490
  if (!configFile.endsWith(".mjs")) {
490
491
  try {
491
- const _require = importMetaUrlShim ? esmRequire ?? (esmRequire = (0, import_module.createRequire)(importMetaUrlShim)) : require;
492
+ const _require = importMetaUrl ? esmRequire ?? (esmRequire = (0, import_module.createRequire)(importMetaUrl)) : require;
492
493
  delete _require.cache[_require.resolve(configFile)];
493
494
  const result = _require(configFile);
494
495
  if (result != null) {
@@ -696,7 +697,11 @@ function needsOptimization(dep, localRequire) {
696
697
  if (!depData)
697
698
  return false;
698
699
  const pkg = depData.pkg;
699
- return pkg.main && !pkg.module && !pkg.exports;
700
+ const isCjs = pkg.main && !pkg.module && !pkg.exports;
701
+ if (!isCjs)
702
+ return false;
703
+ const entryExt = import_path2.default.extname(pkg.main);
704
+ return !entryExt || entryExt === ".js" || entryExt === ".cjs";
700
705
  }
701
706
 
702
707
  // src/utils/options.ts
@@ -757,6 +762,7 @@ function formatFrameForVite(frame) {
757
762
  }
758
763
 
759
764
  // src/utils/esbuild.ts
765
+ var facadeEsbuildSveltePluginName = "vite-plugin-svelte:facade";
760
766
  function esbuildSveltePlugin(options) {
761
767
  return {
762
768
  name: "vite-plugin-svelte:optimize-svelte",
@@ -798,6 +804,7 @@ async function compileSvelte(options, { filename, code }) {
798
804
  const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
799
805
  css: true,
800
806
  filename,
807
+ format: "esm",
801
808
  generate: "dom"
802
809
  });
803
810
  let preprocessed;
@@ -820,6 +827,232 @@ async function compileSvelte(options, { filename, code }) {
820
827
  return compiled.js.code + "//# sourceMappingURL=" + compiled.js.map.toUrl();
821
828
  }
822
829
 
830
+ // src/utils/preprocess.ts
831
+ var import_vite2 = __toModule(require("vite"));
832
+ var import_magic_string2 = __toModule(require("magic-string"));
833
+ var import_compiler3 = __toModule(require("svelte/compiler"));
834
+
835
+ // src/utils/sourcemap.ts
836
+ var import_magic_string = __toModule(require("magic-string"));
837
+ async function buildMagicString(from, to, options) {
838
+ let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
839
+ try {
840
+ const dmpPkg = await import("diff-match-patch");
841
+ diff_match_patch = dmpPkg.diff_match_patch;
842
+ DIFF_INSERT = dmpPkg.DIFF_INSERT;
843
+ DIFF_DELETE = dmpPkg.DIFF_DELETE;
844
+ } catch (e) {
845
+ log.error.once('Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.');
846
+ return null;
847
+ }
848
+ const dmp = new diff_match_patch();
849
+ const diffs = dmp.diff_main(from, to);
850
+ dmp.diff_cleanupSemantic(diffs);
851
+ const m = new import_magic_string.default(from, options);
852
+ let pos = 0;
853
+ for (let i = 0; i < diffs.length; i++) {
854
+ const diff = diffs[i];
855
+ const nextDiff = diffs[i + 1];
856
+ if (diff[0] === DIFF_DELETE) {
857
+ if ((nextDiff == null ? void 0 : nextDiff[0]) === DIFF_INSERT) {
858
+ m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
859
+ i++;
860
+ } else {
861
+ m.remove(pos, pos + diff[1].length);
862
+ }
863
+ pos += diff[1].length;
864
+ } else if (diff[0] === DIFF_INSERT) {
865
+ if (nextDiff) {
866
+ m.appendRight(pos, diff[1]);
867
+ } else {
868
+ m.append(diff[1]);
869
+ }
870
+ } else {
871
+ pos += diff[1].length;
872
+ }
873
+ }
874
+ return m;
875
+ }
876
+ async function buildSourceMap(from, to, filename) {
877
+ const m = await buildMagicString(from, to, { filename });
878
+ return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
879
+ }
880
+
881
+ // src/utils/preprocess.ts
882
+ var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
883
+ var supportedScriptLangs = ["ts"];
884
+ function createViteScriptPreprocessor() {
885
+ return async ({ attributes, content, filename = "" }) => {
886
+ const lang = attributes.lang;
887
+ if (!supportedScriptLangs.includes(lang))
888
+ return;
889
+ const transformResult = await (0, import_vite2.transformWithEsbuild)(content, filename, {
890
+ loader: lang,
891
+ tsconfigRaw: {
892
+ compilerOptions: {
893
+ importsNotUsedAsValues: "preserve"
894
+ }
895
+ }
896
+ });
897
+ return {
898
+ code: transformResult.code,
899
+ map: transformResult.map
900
+ };
901
+ };
902
+ }
903
+ function createViteStylePreprocessor(config) {
904
+ const pluginName = "vite:css";
905
+ const plugin = config.plugins.find((p) => p.name === pluginName);
906
+ if (!plugin) {
907
+ throw new Error(`failed to find plugin ${pluginName}`);
908
+ }
909
+ if (!plugin.transform) {
910
+ throw new Error(`plugin ${pluginName} has no transform`);
911
+ }
912
+ const pluginTransform = plugin.transform.bind(null);
913
+ return async ({ attributes, content, filename = "" }) => {
914
+ var _a, _b;
915
+ const lang = attributes.lang;
916
+ if (!supportedStyleLangs.includes(lang))
917
+ return;
918
+ const moduleId = `${filename}.${lang}`;
919
+ const transformResult = await pluginTransform(content, moduleId);
920
+ if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
921
+ transformResult.map.sources[0] = filename;
922
+ }
923
+ return {
924
+ code: transformResult.code,
925
+ map: transformResult.map ?? void 0
926
+ };
927
+ };
928
+ }
929
+ function createVitePreprocessorGroup(config) {
930
+ return {
931
+ markup({ content, filename }) {
932
+ return (0, import_compiler3.preprocess)(content, {
933
+ script: createViteScriptPreprocessor(),
934
+ style: createViteStylePreprocessor(config)
935
+ }, { filename });
936
+ }
937
+ };
938
+ }
939
+ function createInjectScopeEverythingRulePreprocessorGroup() {
940
+ return {
941
+ style({ content, filename }) {
942
+ const s = new import_magic_string2.default(content);
943
+ s.append(" *{}");
944
+ return {
945
+ code: s.toString(),
946
+ map: s.generateDecodedMap({ source: filename, hires: true })
947
+ };
948
+ }
949
+ };
950
+ }
951
+ function buildExtraPreprocessors(options, config) {
952
+ var _a, _b;
953
+ const prependPreprocessors = [];
954
+ const appendPreprocessors = [];
955
+ if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
956
+ log.debug("adding vite preprocessor");
957
+ prependPreprocessors.push(createVitePreprocessorGroup(config));
958
+ }
959
+ const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
960
+ if (pluginsWithPreprocessorsDeprecated.length > 0) {
961
+ log.warn(`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to move it to 'plugin.api.sveltePreprocess': ${pluginsWithPreprocessorsDeprecated.map((p) => p.name).join(", ")}`);
962
+ pluginsWithPreprocessorsDeprecated.forEach((p) => {
963
+ if (!p.api) {
964
+ p.api = {};
965
+ }
966
+ if (p.api.sveltePreprocess === void 0) {
967
+ p.api.sveltePreprocess = p.sveltePreprocess;
968
+ } else {
969
+ log.error(`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`);
970
+ }
971
+ });
972
+ }
973
+ const pluginsWithPreprocessors = config.plugins.filter((p) => {
974
+ var _a2;
975
+ return (_a2 = p == null ? void 0 : p.api) == null ? void 0 : _a2.sveltePreprocess;
976
+ });
977
+ const ignored = [], included = [];
978
+ for (const p of pluginsWithPreprocessors) {
979
+ if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && ((_b = options.ignorePluginPreprocessors) == null ? void 0 : _b.includes(p.name))) {
980
+ ignored.push(p);
981
+ } else {
982
+ included.push(p);
983
+ }
984
+ }
985
+ if (ignored.length > 0) {
986
+ log.debug(`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`);
987
+ }
988
+ if (included.length > 0) {
989
+ log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
990
+ appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
991
+ }
992
+ if (options.hot && options.emitCss) {
993
+ appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
994
+ }
995
+ return { prependPreprocessors, appendPreprocessors };
996
+ }
997
+ function addExtraPreprocessors(options, config) {
998
+ var _a;
999
+ const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
1000
+ if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
1001
+ if (!options.preprocess) {
1002
+ options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
1003
+ } else if (Array.isArray(options.preprocess)) {
1004
+ options.preprocess.unshift(...prependPreprocessors);
1005
+ options.preprocess.push(...appendPreprocessors);
1006
+ } else {
1007
+ options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
1008
+ }
1009
+ }
1010
+ const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
1011
+ if (options.preprocess && generateMissingSourceMaps) {
1012
+ options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
1013
+ }
1014
+ }
1015
+ function validateSourceMapOutputWrapper(group, i) {
1016
+ const wrapper = {};
1017
+ for (const [processorType, processorFn] of Object.entries(group)) {
1018
+ wrapper[processorType] = async (options) => {
1019
+ var _a;
1020
+ const result = await processorFn(options);
1021
+ if (result && result.code !== options.content) {
1022
+ let invalidMap = false;
1023
+ if (!result.map) {
1024
+ invalidMap = true;
1025
+ log.warn.enabled && log.warn.once(`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, {
1026
+ filename: options.filename,
1027
+ type: processorType,
1028
+ processor: processorFn.toString()
1029
+ });
1030
+ } else if (((_a = result.map) == null ? void 0 : _a.mappings) === "") {
1031
+ invalidMap = true;
1032
+ log.warn.enabled && log.warn.once(`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, {
1033
+ filename: options.filename,
1034
+ type: processorType,
1035
+ processor: processorFn.toString()
1036
+ });
1037
+ }
1038
+ if (invalidMap) {
1039
+ try {
1040
+ const map = await buildSourceMap(options.content, result.code, options.filename);
1041
+ if (map) {
1042
+ log.debug.enabled && log.debug(`adding generated sourcemap to preprocesor result for ${options.filename}`);
1043
+ result.map = map;
1044
+ }
1045
+ } catch (e) {
1046
+ log.error(`failed to build sourcemap`, e);
1047
+ }
1048
+ }
1049
+ }
1050
+ return result;
1051
+ };
1052
+ }
1053
+ return wrapper;
1054
+ }
1055
+
823
1056
  // src/utils/options.ts
824
1057
  var knownOptions = new Set([
825
1058
  "configFile",
@@ -835,28 +1068,52 @@ var knownOptions = new Set([
835
1068
  "disableDependencyReinclusion",
836
1069
  "experimental"
837
1070
  ]);
838
- function buildDefaultOptions(isProduction, emitCss = true) {
839
- const hot = isProduction ? false : {
840
- injectCss: !emitCss
841
- };
1071
+ function validateInlineOptions(inlineOptions) {
1072
+ const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
1073
+ if (invalidKeys.length) {
1074
+ log.warn(`invalid plugin options "${invalidKeys.join(", ")}" in config`, inlineOptions);
1075
+ }
1076
+ }
1077
+ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
1078
+ const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteUserConfig), {
1079
+ root: resolveViteRoot(viteUserConfig)
1080
+ });
842
1081
  const defaultOptions = {
843
1082
  extensions: [".svelte"],
844
- hot,
845
- emitCss,
1083
+ emitCss: true,
846
1084
  compilerOptions: {
847
- format: "esm",
848
- css: !emitCss,
849
- dev: !isProduction
1085
+ format: "esm"
850
1086
  }
851
1087
  };
852
- log.debug(`default options for ${isProduction ? "production" : "development"}`, defaultOptions);
853
- return defaultOptions;
854
- }
855
- function validateInlineOptions(inlineOptions) {
856
- const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
857
- if (invalidKeys.length) {
858
- log.warn(`invalid plugin options "${invalidKeys.join(", ")}" in config`, inlineOptions);
1088
+ const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
1089
+ const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
1090
+ compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.compilerOptions), svelteConfig == null ? void 0 : svelteConfig.compilerOptions), inlineOptions == null ? void 0 : inlineOptions.compilerOptions),
1091
+ experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
1092
+ root: viteConfigWithResolvedRoot.root,
1093
+ isBuild: viteEnv.command === "build",
1094
+ isServe: viteEnv.command === "serve"
1095
+ });
1096
+ if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
1097
+ merged.configFile = svelteConfig.configFile;
859
1098
  }
1099
+ return merged;
1100
+ }
1101
+ function resolveOptions(preResolveOptions2, viteConfig) {
1102
+ const defaultOptions = {
1103
+ hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions2.emitCss },
1104
+ compilerOptions: {
1105
+ css: !preResolveOptions2.emitCss,
1106
+ dev: !viteConfig.isProduction
1107
+ }
1108
+ };
1109
+ const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
1110
+ compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
1111
+ isProduction: viteConfig.isProduction
1112
+ });
1113
+ addExtraPreprocessors(merged, viteConfig);
1114
+ enforceOptionsForHmr(merged);
1115
+ enforceOptionsForProduction(merged);
1116
+ return merged;
860
1117
  }
861
1118
  function enforceOptionsForHmr(options) {
862
1119
  if (options.hot) {
@@ -901,34 +1158,8 @@ function enforceOptionsForProduction(options) {
901
1158
  }
902
1159
  }
903
1160
  }
904
- function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, viteEnv) {
905
- const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
906
- compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions.compilerOptions), (svelteConfig == null ? void 0 : svelteConfig.compilerOptions) || {}), (inlineOptions == null ? void 0 : inlineOptions.compilerOptions) || {}),
907
- experimental: __spreadValues(__spreadValues({}, (svelteConfig == null ? void 0 : svelteConfig.experimental) || {}), (inlineOptions == null ? void 0 : inlineOptions.experimental) || {}),
908
- root: viteConfig.root,
909
- isProduction: viteEnv.mode === "production",
910
- isBuild: viteEnv.command === "build",
911
- isServe: viteEnv.command === "serve",
912
- isSvelteKit: !!(svelteConfig == null ? void 0 : svelteConfig.kit)
913
- });
914
- if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
915
- merged.configFile = svelteConfig.configFile;
916
- }
917
- return merged;
918
- }
919
- async function resolveOptions(inlineOptions = {}, viteConfig, viteEnv) {
920
- const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteConfig), {
921
- root: resolveViteRoot(viteConfig)
922
- });
923
- const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions) || {};
924
- const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", inlineOptions.emitCss ?? svelteConfig.emitCss);
925
- const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfigWithResolvedRoot, viteEnv);
926
- enforceOptionsForProduction(resolvedOptions);
927
- enforceOptionsForHmr(resolvedOptions);
928
- return resolvedOptions;
929
- }
930
1161
  function resolveViteRoot(viteConfig) {
931
- return (0, import_vite2.normalizePath)(viteConfig.root ? import_path3.default.resolve(viteConfig.root) : process.cwd());
1162
+ return (0, import_vite3.normalizePath)(viteConfig.root ? import_path3.default.resolve(viteConfig.root) : process.cwd());
932
1163
  }
933
1164
  function buildExtraViteConfig(options, config, configEnv) {
934
1165
  const svelteDeps = findRootSvelteDependencies(options.root);
@@ -967,7 +1198,8 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
967
1198
  include,
968
1199
  exclude,
969
1200
  esbuildOptions: {
970
- plugins: [esbuildSveltePlugin(options)]
1201
+ plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
1202
+ } }]
971
1203
  }
972
1204
  };
973
1205
  }
@@ -1007,6 +1239,13 @@ function buildSSROptionsForSvelte(svelteDeps, options, config) {
1007
1239
  noExternal
1008
1240
  };
1009
1241
  }
1242
+ function patchResolvedViteConfig(viteConfig, options) {
1243
+ var _a, _b;
1244
+ const facadeEsbuildSveltePlugin = (_b = (_a = viteConfig.optimizeDeps.esbuildOptions) == null ? void 0 : _a.plugins) == null ? void 0 : _b.find((plugin) => plugin.name === facadeEsbuildSveltePluginName);
1245
+ if (facadeEsbuildSveltePlugin) {
1246
+ Object.assign(facadeEsbuildSveltePlugin, esbuildSveltePlugin(options));
1247
+ }
1248
+ }
1010
1249
 
1011
1250
  // src/utils/vite-plugin-svelte-cache.ts
1012
1251
  var VitePluginSvelteCache = class {
@@ -1112,7 +1351,7 @@ function setupWatchers(options, cache, requestParser) {
1112
1351
  };
1113
1352
  const triggerViteRestart = (filename) => {
1114
1353
  var _a;
1115
- if (serverConfig.middlewareMode || options.isSvelteKit) {
1354
+ if (serverConfig.middlewareMode) {
1116
1355
  const message = "Svelte config change detected, restart your dev process to apply the changes.";
1117
1356
  log.info(message, filename);
1118
1357
  ws.send({
@@ -1186,233 +1425,6 @@ function isBareImport(importee) {
1186
1425
  }
1187
1426
  }
1188
1427
 
1189
- // src/utils/preprocess.ts
1190
- var import_vite3 = __toModule(require("vite"));
1191
- var import_magic_string2 = __toModule(require("magic-string"));
1192
- var import_compiler3 = __toModule(require("svelte/compiler"));
1193
-
1194
- // src/utils/sourcemap.ts
1195
- var import_magic_string = __toModule(require("magic-string"));
1196
- async function buildMagicString(from, to, options) {
1197
- let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
1198
- try {
1199
- const dmpPkg = await import("diff-match-patch");
1200
- diff_match_patch = dmpPkg.diff_match_patch;
1201
- DIFF_INSERT = dmpPkg.DIFF_INSERT;
1202
- DIFF_DELETE = dmpPkg.DIFF_DELETE;
1203
- } catch (e) {
1204
- log.error.once('Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.');
1205
- return null;
1206
- }
1207
- const dmp = new diff_match_patch();
1208
- const diffs = dmp.diff_main(from, to);
1209
- dmp.diff_cleanupSemantic(diffs);
1210
- const m = new import_magic_string.default(from, options);
1211
- let pos = 0;
1212
- for (let i = 0; i < diffs.length; i++) {
1213
- const diff = diffs[i];
1214
- const nextDiff = diffs[i + 1];
1215
- if (diff[0] === DIFF_DELETE) {
1216
- if ((nextDiff == null ? void 0 : nextDiff[0]) === DIFF_INSERT) {
1217
- m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
1218
- i++;
1219
- } else {
1220
- m.remove(pos, pos + diff[1].length);
1221
- }
1222
- pos += diff[1].length;
1223
- } else if (diff[0] === DIFF_INSERT) {
1224
- if (nextDiff) {
1225
- m.appendRight(pos, diff[1]);
1226
- } else {
1227
- m.append(diff[1]);
1228
- }
1229
- } else {
1230
- pos += diff[1].length;
1231
- }
1232
- }
1233
- return m;
1234
- }
1235
- async function buildSourceMap(from, to, filename) {
1236
- const m = await buildMagicString(from, to, { filename });
1237
- return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
1238
- }
1239
-
1240
- // src/utils/preprocess.ts
1241
- var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
1242
- var supportedScriptLangs = ["ts"];
1243
- function createViteScriptPreprocessor() {
1244
- return async ({ attributes, content, filename = "" }) => {
1245
- const lang = attributes.lang;
1246
- if (!supportedScriptLangs.includes(lang))
1247
- return;
1248
- const transformResult = await (0, import_vite3.transformWithEsbuild)(content, filename, {
1249
- loader: lang,
1250
- tsconfigRaw: {
1251
- compilerOptions: {
1252
- importsNotUsedAsValues: "preserve"
1253
- }
1254
- }
1255
- });
1256
- return {
1257
- code: transformResult.code,
1258
- map: transformResult.map
1259
- };
1260
- };
1261
- }
1262
- function createViteStylePreprocessor(config) {
1263
- const pluginName = "vite:css";
1264
- const plugin = config.plugins.find((p) => p.name === pluginName);
1265
- if (!plugin) {
1266
- throw new Error(`failed to find plugin ${pluginName}`);
1267
- }
1268
- if (!plugin.transform) {
1269
- throw new Error(`plugin ${pluginName} has no transform`);
1270
- }
1271
- const pluginTransform = plugin.transform.bind(null);
1272
- return async ({ attributes, content, filename = "" }) => {
1273
- var _a, _b;
1274
- const lang = attributes.lang;
1275
- if (!supportedStyleLangs.includes(lang))
1276
- return;
1277
- const moduleId = `${filename}.${lang}`;
1278
- const transformResult = await pluginTransform(content, moduleId);
1279
- const hasMap = transformResult.map && transformResult.map.mappings !== "";
1280
- if (hasMap && ((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
1281
- transformResult.map.sources[0] = filename;
1282
- }
1283
- return {
1284
- code: transformResult.code,
1285
- map: hasMap ? transformResult.map : void 0
1286
- };
1287
- };
1288
- }
1289
- function createVitePreprocessorGroup(config) {
1290
- return {
1291
- markup({ content, filename }) {
1292
- return (0, import_compiler3.preprocess)(content, {
1293
- script: createViteScriptPreprocessor(),
1294
- style: createViteStylePreprocessor(config)
1295
- }, { filename });
1296
- }
1297
- };
1298
- }
1299
- function createInjectScopeEverythingRulePreprocessorGroup() {
1300
- return {
1301
- style({ content, filename }) {
1302
- const s = new import_magic_string2.default(content);
1303
- s.append(" *{}");
1304
- return {
1305
- code: s.toString(),
1306
- map: s.generateDecodedMap({ source: filename, hires: true })
1307
- };
1308
- }
1309
- };
1310
- }
1311
- function buildExtraPreprocessors(options, config) {
1312
- var _a, _b;
1313
- const prependPreprocessors = [];
1314
- const appendPreprocessors = [];
1315
- if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
1316
- log.debug("adding vite preprocessor");
1317
- prependPreprocessors.push(createVitePreprocessorGroup(config));
1318
- }
1319
- const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
1320
- if (pluginsWithPreprocessorsDeprecated.length > 0) {
1321
- log.warn(`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to move it to 'plugin.api.sveltePreprocess': ${pluginsWithPreprocessorsDeprecated.map((p) => p.name).join(", ")}`);
1322
- pluginsWithPreprocessorsDeprecated.forEach((p) => {
1323
- if (!p.api) {
1324
- p.api = {};
1325
- }
1326
- if (p.api.sveltePreprocess === void 0) {
1327
- p.api.sveltePreprocess = p.sveltePreprocess;
1328
- } else {
1329
- log.error(`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`);
1330
- }
1331
- });
1332
- }
1333
- const pluginsWithPreprocessors = config.plugins.filter((p) => {
1334
- var _a2;
1335
- return (_a2 = p == null ? void 0 : p.api) == null ? void 0 : _a2.sveltePreprocess;
1336
- });
1337
- const ignored = [], included = [];
1338
- for (const p of pluginsWithPreprocessors) {
1339
- if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && ((_b = options.ignorePluginPreprocessors) == null ? void 0 : _b.includes(p.name))) {
1340
- ignored.push(p);
1341
- } else {
1342
- included.push(p);
1343
- }
1344
- }
1345
- if (ignored.length > 0) {
1346
- log.debug(`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`);
1347
- }
1348
- if (included.length > 0) {
1349
- log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
1350
- appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
1351
- }
1352
- if (options.hot && options.emitCss) {
1353
- appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
1354
- }
1355
- return { prependPreprocessors, appendPreprocessors };
1356
- }
1357
- function addExtraPreprocessors(options, config) {
1358
- var _a;
1359
- const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
1360
- if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
1361
- if (!options.preprocess) {
1362
- options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
1363
- } else if (Array.isArray(options.preprocess)) {
1364
- options.preprocess.unshift(...prependPreprocessors);
1365
- options.preprocess.push(...appendPreprocessors);
1366
- } else {
1367
- options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
1368
- }
1369
- }
1370
- const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
1371
- if (options.preprocess && generateMissingSourceMaps) {
1372
- options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
1373
- }
1374
- }
1375
- function validateSourceMapOutputWrapper(group, i) {
1376
- const wrapper = {};
1377
- for (const [processorType, processorFn] of Object.entries(group)) {
1378
- wrapper[processorType] = async (options) => {
1379
- var _a;
1380
- const result = await processorFn(options);
1381
- if (result && result.code !== options.content) {
1382
- let invalidMap = false;
1383
- if (!result.map) {
1384
- invalidMap = true;
1385
- log.warn.enabled && log.warn.once(`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, {
1386
- filename: options.filename,
1387
- type: processorType,
1388
- processor: processorFn.toString()
1389
- });
1390
- } else if (((_a = result.map) == null ? void 0 : _a.mappings) === "") {
1391
- invalidMap = true;
1392
- log.warn.enabled && log.warn.once(`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, {
1393
- filename: options.filename,
1394
- type: processorType,
1395
- processor: processorFn.toString()
1396
- });
1397
- }
1398
- if (invalidMap) {
1399
- try {
1400
- const map = await buildSourceMap(options.content, result.code, options.filename);
1401
- if (map) {
1402
- log.debug.enabled && log.debug(`adding generated sourcemap to preprocesor result for ${options.filename}`);
1403
- result.map = map;
1404
- }
1405
- } catch (e) {
1406
- log.error(`failed to build sourcemap`, e);
1407
- }
1408
- }
1409
- }
1410
- return result;
1411
- };
1412
- }
1413
- return wrapper;
1414
- }
1415
-
1416
1428
  // src/index.ts
1417
1429
  function svelte(inlineOptions) {
1418
1430
  if (process.env.DEBUG != null) {
@@ -1435,13 +1447,14 @@ function svelte(inlineOptions) {
1435
1447
  } else if (config.logLevel) {
1436
1448
  log.setLevel(config.logLevel);
1437
1449
  }
1438
- options = await resolveOptions(inlineOptions, config, configEnv);
1450
+ options = await preResolveOptions(inlineOptions, config, configEnv);
1439
1451
  const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
1440
1452
  log.debug("additional vite config", extraViteConfig);
1441
1453
  return extraViteConfig;
1442
1454
  },
1443
1455
  async configResolved(config) {
1444
- addExtraPreprocessors(options, config);
1456
+ options = resolveOptions(options, config);
1457
+ patchResolvedViteConfig(config, options);
1445
1458
  requestParser = buildIdParser(options);
1446
1459
  compileSvelte2 = createCompileSvelte(options);
1447
1460
  viteConfig = config;
@@ -1565,4 +1578,4 @@ function svelte(inlineOptions) {
1565
1578
  0 && (module.exports = {
1566
1579
  svelte
1567
1580
  });
1568
- //# sourceMappingURL=index.cjs.map
1581
+ //# sourceMappingURL=index.cjs.map