@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.js CHANGED
@@ -435,7 +435,9 @@ function buildIdParser(options) {
435
435
  }
436
436
 
437
437
  // src/utils/options.ts
438
- import { normalizePath as normalizePath2 } from "vite";
438
+ import {
439
+ normalizePath as normalizePath2
440
+ } from "vite";
439
441
 
440
442
  // src/utils/load-svelte-config.ts
441
443
  import { createRequire } from "module";
@@ -678,7 +680,11 @@ function needsOptimization(dep, localRequire) {
678
680
  if (!depData)
679
681
  return false;
680
682
  const pkg = depData.pkg;
681
- return pkg.main && !pkg.module && !pkg.exports;
683
+ const isCjs = pkg.main && !pkg.module && !pkg.exports;
684
+ if (!isCjs)
685
+ return false;
686
+ const entryExt = path2.extname(pkg.main);
687
+ return !entryExt || entryExt === ".js" || entryExt === ".cjs";
682
688
  }
683
689
 
684
690
  // src/utils/options.ts
@@ -739,6 +745,7 @@ function formatFrameForVite(frame) {
739
745
  }
740
746
 
741
747
  // src/utils/esbuild.ts
748
+ var facadeEsbuildSveltePluginName = "vite-plugin-svelte:facade";
742
749
  function esbuildSveltePlugin(options) {
743
750
  return {
744
751
  name: "vite-plugin-svelte:optimize-svelte",
@@ -780,6 +787,7 @@ async function compileSvelte(options, { filename, code }) {
780
787
  const compileOptions = __spreadProps(__spreadValues({}, options.compilerOptions), {
781
788
  css: true,
782
789
  filename,
790
+ format: "esm",
783
791
  generate: "dom"
784
792
  });
785
793
  let preprocessed;
@@ -802,6 +810,234 @@ async function compileSvelte(options, { filename, code }) {
802
810
  return compiled.js.code + "//# sourceMappingURL=" + compiled.js.map.toUrl();
803
811
  }
804
812
 
813
+ // src/utils/preprocess.ts
814
+ import {
815
+ transformWithEsbuild
816
+ } from "vite";
817
+ import MagicString2 from "magic-string";
818
+ import { preprocess as preprocess3 } from "svelte/compiler";
819
+
820
+ // src/utils/sourcemap.ts
821
+ import MagicString from "magic-string";
822
+ async function buildMagicString(from, to, options) {
823
+ let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
824
+ try {
825
+ const dmpPkg = await import("diff-match-patch");
826
+ diff_match_patch = dmpPkg.diff_match_patch;
827
+ DIFF_INSERT = dmpPkg.DIFF_INSERT;
828
+ DIFF_DELETE = dmpPkg.DIFF_DELETE;
829
+ } catch (e) {
830
+ log.error.once('Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.');
831
+ return null;
832
+ }
833
+ const dmp = new diff_match_patch();
834
+ const diffs = dmp.diff_main(from, to);
835
+ dmp.diff_cleanupSemantic(diffs);
836
+ const m = new MagicString(from, options);
837
+ let pos = 0;
838
+ for (let i = 0; i < diffs.length; i++) {
839
+ const diff = diffs[i];
840
+ const nextDiff = diffs[i + 1];
841
+ if (diff[0] === DIFF_DELETE) {
842
+ if ((nextDiff == null ? void 0 : nextDiff[0]) === DIFF_INSERT) {
843
+ m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
844
+ i++;
845
+ } else {
846
+ m.remove(pos, pos + diff[1].length);
847
+ }
848
+ pos += diff[1].length;
849
+ } else if (diff[0] === DIFF_INSERT) {
850
+ if (nextDiff) {
851
+ m.appendRight(pos, diff[1]);
852
+ } else {
853
+ m.append(diff[1]);
854
+ }
855
+ } else {
856
+ pos += diff[1].length;
857
+ }
858
+ }
859
+ return m;
860
+ }
861
+ async function buildSourceMap(from, to, filename) {
862
+ const m = await buildMagicString(from, to, { filename });
863
+ return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
864
+ }
865
+
866
+ // src/utils/preprocess.ts
867
+ var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
868
+ var supportedScriptLangs = ["ts"];
869
+ function createViteScriptPreprocessor() {
870
+ return async ({ attributes, content, filename = "" }) => {
871
+ const lang = attributes.lang;
872
+ if (!supportedScriptLangs.includes(lang))
873
+ return;
874
+ const transformResult = await transformWithEsbuild(content, filename, {
875
+ loader: lang,
876
+ tsconfigRaw: {
877
+ compilerOptions: {
878
+ importsNotUsedAsValues: "preserve"
879
+ }
880
+ }
881
+ });
882
+ return {
883
+ code: transformResult.code,
884
+ map: transformResult.map
885
+ };
886
+ };
887
+ }
888
+ function createViteStylePreprocessor(config) {
889
+ const pluginName = "vite:css";
890
+ const plugin = config.plugins.find((p) => p.name === pluginName);
891
+ if (!plugin) {
892
+ throw new Error(`failed to find plugin ${pluginName}`);
893
+ }
894
+ if (!plugin.transform) {
895
+ throw new Error(`plugin ${pluginName} has no transform`);
896
+ }
897
+ const pluginTransform = plugin.transform.bind(null);
898
+ return async ({ attributes, content, filename = "" }) => {
899
+ var _a, _b;
900
+ const lang = attributes.lang;
901
+ if (!supportedStyleLangs.includes(lang))
902
+ return;
903
+ const moduleId = `${filename}.${lang}`;
904
+ const transformResult = await pluginTransform(content, moduleId);
905
+ if (((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
906
+ transformResult.map.sources[0] = filename;
907
+ }
908
+ return {
909
+ code: transformResult.code,
910
+ map: transformResult.map ?? void 0
911
+ };
912
+ };
913
+ }
914
+ function createVitePreprocessorGroup(config) {
915
+ return {
916
+ markup({ content, filename }) {
917
+ return preprocess3(content, {
918
+ script: createViteScriptPreprocessor(),
919
+ style: createViteStylePreprocessor(config)
920
+ }, { filename });
921
+ }
922
+ };
923
+ }
924
+ function createInjectScopeEverythingRulePreprocessorGroup() {
925
+ return {
926
+ style({ content, filename }) {
927
+ const s = new MagicString2(content);
928
+ s.append(" *{}");
929
+ return {
930
+ code: s.toString(),
931
+ map: s.generateDecodedMap({ source: filename, hires: true })
932
+ };
933
+ }
934
+ };
935
+ }
936
+ function buildExtraPreprocessors(options, config) {
937
+ var _a, _b;
938
+ const prependPreprocessors = [];
939
+ const appendPreprocessors = [];
940
+ if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
941
+ log.debug("adding vite preprocessor");
942
+ prependPreprocessors.push(createVitePreprocessorGroup(config));
943
+ }
944
+ const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
945
+ if (pluginsWithPreprocessorsDeprecated.length > 0) {
946
+ 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(", ")}`);
947
+ pluginsWithPreprocessorsDeprecated.forEach((p) => {
948
+ if (!p.api) {
949
+ p.api = {};
950
+ }
951
+ if (p.api.sveltePreprocess === void 0) {
952
+ p.api.sveltePreprocess = p.sveltePreprocess;
953
+ } else {
954
+ log.error(`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`);
955
+ }
956
+ });
957
+ }
958
+ const pluginsWithPreprocessors = config.plugins.filter((p) => {
959
+ var _a2;
960
+ return (_a2 = p == null ? void 0 : p.api) == null ? void 0 : _a2.sveltePreprocess;
961
+ });
962
+ const ignored = [], included = [];
963
+ for (const p of pluginsWithPreprocessors) {
964
+ if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && ((_b = options.ignorePluginPreprocessors) == null ? void 0 : _b.includes(p.name))) {
965
+ ignored.push(p);
966
+ } else {
967
+ included.push(p);
968
+ }
969
+ }
970
+ if (ignored.length > 0) {
971
+ log.debug(`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`);
972
+ }
973
+ if (included.length > 0) {
974
+ log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
975
+ appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
976
+ }
977
+ if (options.hot && options.emitCss) {
978
+ appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
979
+ }
980
+ return { prependPreprocessors, appendPreprocessors };
981
+ }
982
+ function addExtraPreprocessors(options, config) {
983
+ var _a;
984
+ const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
985
+ if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
986
+ if (!options.preprocess) {
987
+ options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
988
+ } else if (Array.isArray(options.preprocess)) {
989
+ options.preprocess.unshift(...prependPreprocessors);
990
+ options.preprocess.push(...appendPreprocessors);
991
+ } else {
992
+ options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
993
+ }
994
+ }
995
+ const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
996
+ if (options.preprocess && generateMissingSourceMaps) {
997
+ options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
998
+ }
999
+ }
1000
+ function validateSourceMapOutputWrapper(group, i) {
1001
+ const wrapper = {};
1002
+ for (const [processorType, processorFn] of Object.entries(group)) {
1003
+ wrapper[processorType] = async (options) => {
1004
+ var _a;
1005
+ const result = await processorFn(options);
1006
+ if (result && result.code !== options.content) {
1007
+ let invalidMap = false;
1008
+ if (!result.map) {
1009
+ invalidMap = true;
1010
+ log.warn.enabled && log.warn.once(`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, {
1011
+ filename: options.filename,
1012
+ type: processorType,
1013
+ processor: processorFn.toString()
1014
+ });
1015
+ } else if (((_a = result.map) == null ? void 0 : _a.mappings) === "") {
1016
+ invalidMap = true;
1017
+ log.warn.enabled && log.warn.once(`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, {
1018
+ filename: options.filename,
1019
+ type: processorType,
1020
+ processor: processorFn.toString()
1021
+ });
1022
+ }
1023
+ if (invalidMap) {
1024
+ try {
1025
+ const map = await buildSourceMap(options.content, result.code, options.filename);
1026
+ if (map) {
1027
+ log.debug.enabled && log.debug(`adding generated sourcemap to preprocesor result for ${options.filename}`);
1028
+ result.map = map;
1029
+ }
1030
+ } catch (e) {
1031
+ log.error(`failed to build sourcemap`, e);
1032
+ }
1033
+ }
1034
+ }
1035
+ return result;
1036
+ };
1037
+ }
1038
+ return wrapper;
1039
+ }
1040
+
805
1041
  // src/utils/options.ts
806
1042
  var knownOptions = new Set([
807
1043
  "configFile",
@@ -817,28 +1053,52 @@ var knownOptions = new Set([
817
1053
  "disableDependencyReinclusion",
818
1054
  "experimental"
819
1055
  ]);
820
- function buildDefaultOptions(isProduction, emitCss = true) {
821
- const hot = isProduction ? false : {
822
- injectCss: !emitCss
823
- };
1056
+ function validateInlineOptions(inlineOptions) {
1057
+ const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
1058
+ if (invalidKeys.length) {
1059
+ log.warn(`invalid plugin options "${invalidKeys.join(", ")}" in config`, inlineOptions);
1060
+ }
1061
+ }
1062
+ async function preResolveOptions(inlineOptions = {}, viteUserConfig, viteEnv) {
1063
+ const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteUserConfig), {
1064
+ root: resolveViteRoot(viteUserConfig)
1065
+ });
824
1066
  const defaultOptions = {
825
1067
  extensions: [".svelte"],
826
- hot,
827
- emitCss,
1068
+ emitCss: true,
828
1069
  compilerOptions: {
829
- format: "esm",
830
- css: !emitCss,
831
- dev: !isProduction
1070
+ format: "esm"
832
1071
  }
833
1072
  };
834
- log.debug(`default options for ${isProduction ? "production" : "development"}`, defaultOptions);
835
- return defaultOptions;
836
- }
837
- function validateInlineOptions(inlineOptions) {
838
- const invalidKeys = Object.keys(inlineOptions || {}).filter((key) => !knownOptions.has(key));
839
- if (invalidKeys.length) {
840
- log.warn(`invalid plugin options "${invalidKeys.join(", ")}" in config`, inlineOptions);
1073
+ const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
1074
+ const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
1075
+ compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.compilerOptions), svelteConfig == null ? void 0 : svelteConfig.compilerOptions), inlineOptions == null ? void 0 : inlineOptions.compilerOptions),
1076
+ experimental: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions == null ? void 0 : defaultOptions.experimental), svelteConfig == null ? void 0 : svelteConfig.experimental), inlineOptions == null ? void 0 : inlineOptions.experimental),
1077
+ root: viteConfigWithResolvedRoot.root,
1078
+ isBuild: viteEnv.command === "build",
1079
+ isServe: viteEnv.command === "serve"
1080
+ });
1081
+ if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
1082
+ merged.configFile = svelteConfig.configFile;
841
1083
  }
1084
+ return merged;
1085
+ }
1086
+ function resolveOptions(preResolveOptions2, viteConfig) {
1087
+ const defaultOptions = {
1088
+ hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions2.emitCss },
1089
+ compilerOptions: {
1090
+ css: !preResolveOptions2.emitCss,
1091
+ dev: !viteConfig.isProduction
1092
+ }
1093
+ };
1094
+ const merged = __spreadProps(__spreadValues(__spreadValues({}, defaultOptions), preResolveOptions2), {
1095
+ compilerOptions: __spreadValues(__spreadValues({}, defaultOptions.compilerOptions), preResolveOptions2.compilerOptions),
1096
+ isProduction: viteConfig.isProduction
1097
+ });
1098
+ addExtraPreprocessors(merged, viteConfig);
1099
+ enforceOptionsForHmr(merged);
1100
+ enforceOptionsForProduction(merged);
1101
+ return merged;
842
1102
  }
843
1103
  function enforceOptionsForHmr(options) {
844
1104
  if (options.hot) {
@@ -883,32 +1143,6 @@ function enforceOptionsForProduction(options) {
883
1143
  }
884
1144
  }
885
1145
  }
886
- function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, viteEnv) {
887
- const merged = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultOptions), svelteConfig), inlineOptions), {
888
- compilerOptions: __spreadValues(__spreadValues(__spreadValues({}, defaultOptions.compilerOptions), (svelteConfig == null ? void 0 : svelteConfig.compilerOptions) || {}), (inlineOptions == null ? void 0 : inlineOptions.compilerOptions) || {}),
889
- experimental: __spreadValues(__spreadValues({}, (svelteConfig == null ? void 0 : svelteConfig.experimental) || {}), (inlineOptions == null ? void 0 : inlineOptions.experimental) || {}),
890
- root: viteConfig.root,
891
- isProduction: viteEnv.mode === "production",
892
- isBuild: viteEnv.command === "build",
893
- isServe: viteEnv.command === "serve",
894
- isSvelteKit: !!(svelteConfig == null ? void 0 : svelteConfig.kit)
895
- });
896
- if (svelteConfig == null ? void 0 : svelteConfig.configFile) {
897
- merged.configFile = svelteConfig.configFile;
898
- }
899
- return merged;
900
- }
901
- async function resolveOptions(inlineOptions = {}, viteConfig, viteEnv) {
902
- const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteConfig), {
903
- root: resolveViteRoot(viteConfig)
904
- });
905
- const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions) || {};
906
- const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", inlineOptions.emitCss ?? svelteConfig.emitCss);
907
- const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfigWithResolvedRoot, viteEnv);
908
- enforceOptionsForProduction(resolvedOptions);
909
- enforceOptionsForHmr(resolvedOptions);
910
- return resolvedOptions;
911
- }
912
1146
  function resolveViteRoot(viteConfig) {
913
1147
  return normalizePath2(viteConfig.root ? path3.resolve(viteConfig.root) : process.cwd());
914
1148
  }
@@ -949,7 +1183,8 @@ function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
949
1183
  include,
950
1184
  exclude,
951
1185
  esbuildOptions: {
952
- plugins: [esbuildSveltePlugin(options)]
1186
+ plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {
1187
+ } }]
953
1188
  }
954
1189
  };
955
1190
  }
@@ -989,6 +1224,13 @@ function buildSSROptionsForSvelte(svelteDeps, options, config) {
989
1224
  noExternal
990
1225
  };
991
1226
  }
1227
+ function patchResolvedViteConfig(viteConfig, options) {
1228
+ var _a, _b;
1229
+ const facadeEsbuildSveltePlugin = (_b = (_a = viteConfig.optimizeDeps.esbuildOptions) == null ? void 0 : _a.plugins) == null ? void 0 : _b.find((plugin) => plugin.name === facadeEsbuildSveltePluginName);
1230
+ if (facadeEsbuildSveltePlugin) {
1231
+ Object.assign(facadeEsbuildSveltePlugin, esbuildSveltePlugin(options));
1232
+ }
1233
+ }
992
1234
 
993
1235
  // src/utils/vite-plugin-svelte-cache.ts
994
1236
  var VitePluginSvelteCache = class {
@@ -1094,7 +1336,7 @@ function setupWatchers(options, cache, requestParser) {
1094
1336
  };
1095
1337
  const triggerViteRestart = (filename) => {
1096
1338
  var _a;
1097
- if (serverConfig.middlewareMode || options.isSvelteKit) {
1339
+ if (serverConfig.middlewareMode) {
1098
1340
  const message = "Svelte config change detected, restart your dev process to apply the changes.";
1099
1341
  log.info(message, filename);
1100
1342
  ws.send({
@@ -1168,235 +1410,6 @@ function isBareImport(importee) {
1168
1410
  }
1169
1411
  }
1170
1412
 
1171
- // src/utils/preprocess.ts
1172
- import {
1173
- transformWithEsbuild
1174
- } from "vite";
1175
- import MagicString2 from "magic-string";
1176
- import { preprocess as preprocess3 } from "svelte/compiler";
1177
-
1178
- // src/utils/sourcemap.ts
1179
- import MagicString from "magic-string";
1180
- async function buildMagicString(from, to, options) {
1181
- let diff_match_patch, DIFF_DELETE, DIFF_INSERT;
1182
- try {
1183
- const dmpPkg = await import("diff-match-patch");
1184
- diff_match_patch = dmpPkg.diff_match_patch;
1185
- DIFF_INSERT = dmpPkg.DIFF_INSERT;
1186
- DIFF_DELETE = dmpPkg.DIFF_DELETE;
1187
- } catch (e) {
1188
- log.error.once('Failed to import optional dependency "diff-match-patch". Please install it to enable generated sourcemaps.');
1189
- return null;
1190
- }
1191
- const dmp = new diff_match_patch();
1192
- const diffs = dmp.diff_main(from, to);
1193
- dmp.diff_cleanupSemantic(diffs);
1194
- const m = new MagicString(from, options);
1195
- let pos = 0;
1196
- for (let i = 0; i < diffs.length; i++) {
1197
- const diff = diffs[i];
1198
- const nextDiff = diffs[i + 1];
1199
- if (diff[0] === DIFF_DELETE) {
1200
- if ((nextDiff == null ? void 0 : nextDiff[0]) === DIFF_INSERT) {
1201
- m.overwrite(pos, pos + diff[1].length, nextDiff[1]);
1202
- i++;
1203
- } else {
1204
- m.remove(pos, pos + diff[1].length);
1205
- }
1206
- pos += diff[1].length;
1207
- } else if (diff[0] === DIFF_INSERT) {
1208
- if (nextDiff) {
1209
- m.appendRight(pos, diff[1]);
1210
- } else {
1211
- m.append(diff[1]);
1212
- }
1213
- } else {
1214
- pos += diff[1].length;
1215
- }
1216
- }
1217
- return m;
1218
- }
1219
- async function buildSourceMap(from, to, filename) {
1220
- const m = await buildMagicString(from, to, { filename });
1221
- return m ? m.generateDecodedMap({ source: filename, hires: true, includeContent: false }) : null;
1222
- }
1223
-
1224
- // src/utils/preprocess.ts
1225
- var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
1226
- var supportedScriptLangs = ["ts"];
1227
- function createViteScriptPreprocessor() {
1228
- return async ({ attributes, content, filename = "" }) => {
1229
- const lang = attributes.lang;
1230
- if (!supportedScriptLangs.includes(lang))
1231
- return;
1232
- const transformResult = await transformWithEsbuild(content, filename, {
1233
- loader: lang,
1234
- tsconfigRaw: {
1235
- compilerOptions: {
1236
- importsNotUsedAsValues: "preserve"
1237
- }
1238
- }
1239
- });
1240
- return {
1241
- code: transformResult.code,
1242
- map: transformResult.map
1243
- };
1244
- };
1245
- }
1246
- function createViteStylePreprocessor(config) {
1247
- const pluginName = "vite:css";
1248
- const plugin = config.plugins.find((p) => p.name === pluginName);
1249
- if (!plugin) {
1250
- throw new Error(`failed to find plugin ${pluginName}`);
1251
- }
1252
- if (!plugin.transform) {
1253
- throw new Error(`plugin ${pluginName} has no transform`);
1254
- }
1255
- const pluginTransform = plugin.transform.bind(null);
1256
- return async ({ attributes, content, filename = "" }) => {
1257
- var _a, _b;
1258
- const lang = attributes.lang;
1259
- if (!supportedStyleLangs.includes(lang))
1260
- return;
1261
- const moduleId = `${filename}.${lang}`;
1262
- const transformResult = await pluginTransform(content, moduleId);
1263
- const hasMap = transformResult.map && transformResult.map.mappings !== "";
1264
- if (hasMap && ((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
1265
- transformResult.map.sources[0] = filename;
1266
- }
1267
- return {
1268
- code: transformResult.code,
1269
- map: hasMap ? transformResult.map : void 0
1270
- };
1271
- };
1272
- }
1273
- function createVitePreprocessorGroup(config) {
1274
- return {
1275
- markup({ content, filename }) {
1276
- return preprocess3(content, {
1277
- script: createViteScriptPreprocessor(),
1278
- style: createViteStylePreprocessor(config)
1279
- }, { filename });
1280
- }
1281
- };
1282
- }
1283
- function createInjectScopeEverythingRulePreprocessorGroup() {
1284
- return {
1285
- style({ content, filename }) {
1286
- const s = new MagicString2(content);
1287
- s.append(" *{}");
1288
- return {
1289
- code: s.toString(),
1290
- map: s.generateDecodedMap({ source: filename, hires: true })
1291
- };
1292
- }
1293
- };
1294
- }
1295
- function buildExtraPreprocessors(options, config) {
1296
- var _a, _b;
1297
- const prependPreprocessors = [];
1298
- const appendPreprocessors = [];
1299
- if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
1300
- log.debug("adding vite preprocessor");
1301
- prependPreprocessors.push(createVitePreprocessorGroup(config));
1302
- }
1303
- const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
1304
- if (pluginsWithPreprocessorsDeprecated.length > 0) {
1305
- 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(", ")}`);
1306
- pluginsWithPreprocessorsDeprecated.forEach((p) => {
1307
- if (!p.api) {
1308
- p.api = {};
1309
- }
1310
- if (p.api.sveltePreprocess === void 0) {
1311
- p.api.sveltePreprocess = p.sveltePreprocess;
1312
- } else {
1313
- log.error(`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`);
1314
- }
1315
- });
1316
- }
1317
- const pluginsWithPreprocessors = config.plugins.filter((p) => {
1318
- var _a2;
1319
- return (_a2 = p == null ? void 0 : p.api) == null ? void 0 : _a2.sveltePreprocess;
1320
- });
1321
- const ignored = [], included = [];
1322
- for (const p of pluginsWithPreprocessors) {
1323
- if (options.ignorePluginPreprocessors === true || Array.isArray(options.ignorePluginPreprocessors) && ((_b = options.ignorePluginPreprocessors) == null ? void 0 : _b.includes(p.name))) {
1324
- ignored.push(p);
1325
- } else {
1326
- included.push(p);
1327
- }
1328
- }
1329
- if (ignored.length > 0) {
1330
- log.debug(`Ignoring svelte preprocessors defined by these vite plugins: ${ignored.map((p) => p.name).join(", ")}`);
1331
- }
1332
- if (included.length > 0) {
1333
- log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
1334
- appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
1335
- }
1336
- if (options.hot && options.emitCss) {
1337
- appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
1338
- }
1339
- return { prependPreprocessors, appendPreprocessors };
1340
- }
1341
- function addExtraPreprocessors(options, config) {
1342
- var _a;
1343
- const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
1344
- if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
1345
- if (!options.preprocess) {
1346
- options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
1347
- } else if (Array.isArray(options.preprocess)) {
1348
- options.preprocess.unshift(...prependPreprocessors);
1349
- options.preprocess.push(...appendPreprocessors);
1350
- } else {
1351
- options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
1352
- }
1353
- }
1354
- const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
1355
- if (options.preprocess && generateMissingSourceMaps) {
1356
- options.preprocess = Array.isArray(options.preprocess) ? options.preprocess.map((p, i) => validateSourceMapOutputWrapper(p, i)) : validateSourceMapOutputWrapper(options.preprocess, 0);
1357
- }
1358
- }
1359
- function validateSourceMapOutputWrapper(group, i) {
1360
- const wrapper = {};
1361
- for (const [processorType, processorFn] of Object.entries(group)) {
1362
- wrapper[processorType] = async (options) => {
1363
- var _a;
1364
- const result = await processorFn(options);
1365
- if (result && result.code !== options.content) {
1366
- let invalidMap = false;
1367
- if (!result.map) {
1368
- invalidMap = true;
1369
- log.warn.enabled && log.warn.once(`preprocessor at index ${i} did not return a sourcemap for ${processorType} transform`, {
1370
- filename: options.filename,
1371
- type: processorType,
1372
- processor: processorFn.toString()
1373
- });
1374
- } else if (((_a = result.map) == null ? void 0 : _a.mappings) === "") {
1375
- invalidMap = true;
1376
- log.warn.enabled && log.warn.once(`preprocessor at index ${i} returned an invalid empty sourcemap for ${processorType} transform`, {
1377
- filename: options.filename,
1378
- type: processorType,
1379
- processor: processorFn.toString()
1380
- });
1381
- }
1382
- if (invalidMap) {
1383
- try {
1384
- const map = await buildSourceMap(options.content, result.code, options.filename);
1385
- if (map) {
1386
- log.debug.enabled && log.debug(`adding generated sourcemap to preprocesor result for ${options.filename}`);
1387
- result.map = map;
1388
- }
1389
- } catch (e) {
1390
- log.error(`failed to build sourcemap`, e);
1391
- }
1392
- }
1393
- }
1394
- return result;
1395
- };
1396
- }
1397
- return wrapper;
1398
- }
1399
-
1400
1413
  // src/index.ts
1401
1414
  function svelte(inlineOptions) {
1402
1415
  if (process.env.DEBUG != null) {
@@ -1419,13 +1432,14 @@ function svelte(inlineOptions) {
1419
1432
  } else if (config.logLevel) {
1420
1433
  log.setLevel(config.logLevel);
1421
1434
  }
1422
- options = await resolveOptions(inlineOptions, config, configEnv);
1435
+ options = await preResolveOptions(inlineOptions, config, configEnv);
1423
1436
  const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
1424
1437
  log.debug("additional vite config", extraViteConfig);
1425
1438
  return extraViteConfig;
1426
1439
  },
1427
1440
  async configResolved(config) {
1428
- addExtraPreprocessors(options, config);
1441
+ options = resolveOptions(options, config);
1442
+ patchResolvedViteConfig(config, options);
1429
1443
  requestParser = buildIdParser(options);
1430
1444
  compileSvelte2 = createCompileSvelte(options);
1431
1445
  viteConfig = config;
@@ -1548,4 +1562,4 @@ function svelte(inlineOptions) {
1548
1562
  export {
1549
1563
  svelte
1550
1564
  };
1551
- //# sourceMappingURL=index.js.map
1565
+ //# sourceMappingURL=index.js.map