@sveltejs/vite-plugin-svelte 1.0.0-next.24 → 1.0.0-next.28

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
@@ -22,9 +22,6 @@ var __spreadValues = (a, b) => {
22
22
  };
23
23
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
24
  var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
25
- var __require = typeof require !== "undefined" ? require : (x) => {
26
- throw new Error('Dynamic require of "' + x + '" is not supported');
27
- };
28
25
  var __export = (target, all) => {
29
26
  __markAsModule(target);
30
27
  for (var name in all)
@@ -46,6 +43,11 @@ var __toModule = (module2) => {
46
43
  __export(exports, {
47
44
  svelte: () => svelte
48
45
  });
46
+
47
+ // ../../node_modules/.pnpm/tsup@5.4.0_typescript@4.4.4/node_modules/tsup/assets/cjs_shims.js
48
+ var importMetaUrlShim = typeof document === "undefined" ? new (require("url")).URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
49
+
50
+ // src/index.ts
49
51
  var import_fs5 = __toModule(require("fs"));
50
52
 
51
53
  // src/utils/log.ts
@@ -451,7 +453,6 @@ var import_module = __toModule(require("module"));
451
453
  var import_path = __toModule(require("path"));
452
454
  var import_fs = __toModule(require("fs"));
453
455
  var import_url = __toModule(require("url"));
454
- var import_meta = {};
455
456
  var esmRequire;
456
457
  var knownSvelteConfigNames = [
457
458
  "svelte.config.js",
@@ -480,7 +481,7 @@ async function loadSvelteConfig(viteConfig, inlineOptions) {
480
481
  }
481
482
  if (!configFile.endsWith(".mjs")) {
482
483
  try {
483
- const _require = import_meta.url ? esmRequire != null ? esmRequire : esmRequire = (0, import_module.createRequire)(import_meta.url) : require;
484
+ const _require = importMetaUrlShim ? esmRequire != null ? esmRequire : esmRequire = (0, import_module.createRequire)(importMetaUrlShim) : require;
484
485
  delete _require.cache[_require.resolve(configFile)];
485
486
  const result = _require(configFile);
486
487
  if (result != null) {
@@ -707,8 +708,7 @@ var knownOptions = new Set([
707
708
  "disableDependencyReinclusion",
708
709
  "experimental"
709
710
  ]);
710
- function buildDefaultOptions(isProduction, options) {
711
- const emitCss = (options == null ? void 0 : options.emitCss) != null ? options.emitCss : true;
711
+ function buildDefaultOptions(isProduction, emitCss = true) {
712
712
  const hot = isProduction ? false : {
713
713
  injectCss: !emitCss
714
714
  };
@@ -789,11 +789,12 @@ function mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfig, v
789
789
  return merged;
790
790
  }
791
791
  async function resolveOptions(inlineOptions = {}, viteConfig, viteEnv) {
792
+ var _a;
792
793
  const viteConfigWithResolvedRoot = __spreadProps(__spreadValues({}, viteConfig), {
793
794
  root: resolveViteRoot(viteConfig)
794
795
  });
795
- const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", inlineOptions);
796
796
  const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions) || {};
797
+ const defaultOptions = buildDefaultOptions(viteEnv.mode === "production", (_a = inlineOptions.emitCss) != null ? _a : svelteConfig.emitCss);
797
798
  const resolvedOptions = mergeOptions(defaultOptions, svelteConfig, inlineOptions, viteConfigWithResolvedRoot, viteEnv);
798
799
  enforceOptionsForProduction(resolvedOptions);
799
800
  enforceOptionsForHmr(resolvedOptions);
@@ -803,7 +804,6 @@ function resolveViteRoot(viteConfig) {
803
804
  return (0, import_vite2.normalizePath)(viteConfig.root ? import_path3.default.resolve(viteConfig.root) : process.cwd());
804
805
  }
805
806
  function buildExtraViteConfig(options, config, configEnv) {
806
- var _a;
807
807
  const svelteDeps = findRootSvelteDependencies(options.root);
808
808
  const extraViteConfig = {
809
809
  resolve: {
@@ -815,15 +815,6 @@ function buildExtraViteConfig(options, config, configEnv) {
815
815
  extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps);
816
816
  }
817
817
  extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
818
- if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
819
- extraViteConfig.esbuild = {
820
- tsconfigRaw: {
821
- compilerOptions: {
822
- importsNotUsedAsValues: "preserve"
823
- }
824
- }
825
- };
826
- }
827
818
  return extraViteConfig;
828
819
  }
829
820
  function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) {
@@ -1059,7 +1050,9 @@ function isBareImport(importee) {
1059
1050
  }
1060
1051
 
1061
1052
  // src/utils/preprocess.ts
1053
+ var import_vite3 = __toModule(require("vite"));
1062
1054
  var import_magic_string2 = __toModule(require("magic-string"));
1055
+ var import_compiler2 = __toModule(require("svelte/compiler"));
1063
1056
 
1064
1057
  // src/utils/sourcemap.ts
1065
1058
  var import_magic_string = __toModule(require("magic-string"));
@@ -1110,7 +1103,27 @@ async function buildSourceMap(from, to, filename) {
1110
1103
  // src/utils/preprocess.ts
1111
1104
  var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss"];
1112
1105
  var supportedScriptLangs = ["ts"];
1113
- function createPreprocessorFromVitePlugin(config, options, pluginName, supportedLangs) {
1106
+ function createViteScriptPreprocessor() {
1107
+ return async ({ attributes, content, filename = "" }) => {
1108
+ const lang = attributes.lang;
1109
+ if (!supportedScriptLangs.includes(lang))
1110
+ return;
1111
+ const transformResult = await (0, import_vite3.transformWithEsbuild)(content, filename, {
1112
+ loader: lang,
1113
+ tsconfigRaw: {
1114
+ compilerOptions: {
1115
+ importsNotUsedAsValues: "preserve"
1116
+ }
1117
+ }
1118
+ });
1119
+ return {
1120
+ code: transformResult.code,
1121
+ map: transformResult.map
1122
+ };
1123
+ };
1124
+ }
1125
+ function createViteStylePreprocessor(config) {
1126
+ const pluginName = "vite:css";
1114
1127
  const plugin = config.plugins.find((p) => p.name === pluginName);
1115
1128
  if (!plugin) {
1116
1129
  throw new Error(`failed to find plugin ${pluginName}`);
@@ -1119,33 +1132,31 @@ function createPreprocessorFromVitePlugin(config, options, pluginName, supported
1119
1132
  throw new Error(`plugin ${pluginName} has no transform`);
1120
1133
  }
1121
1134
  const pluginTransform = plugin.transform.bind(null);
1122
- return async ({ attributes, content, filename }) => {
1123
- var _a, _b, _c, _d;
1135
+ return async ({ attributes, content, filename = "" }) => {
1136
+ var _a, _b;
1124
1137
  const lang = attributes.lang;
1125
- if (!supportedLangs.includes(lang)) {
1126
- return { code: content };
1127
- }
1138
+ if (!supportedStyleLangs.includes(lang))
1139
+ return;
1128
1140
  const moduleId = `${filename}.${lang}`;
1129
- const moduleGraph = (_a = options.server) == null ? void 0 : _a.moduleGraph;
1130
- if (moduleGraph && !moduleGraph.getModuleById(moduleId)) {
1131
- await moduleGraph.ensureEntryFromUrl(moduleId);
1132
- }
1133
1141
  const transformResult = await pluginTransform(content, moduleId);
1134
- const hasMap = transformResult.map && ((_b = transformResult.map) == null ? void 0 : _b.mappings) !== "";
1135
- if (((_d = (_c = transformResult.map) == null ? void 0 : _c.sources) == null ? void 0 : _d[0]) === moduleId) {
1142
+ const hasMap = transformResult.map && transformResult.map.mappings !== "";
1143
+ if (hasMap && ((_b = (_a = transformResult.map) == null ? void 0 : _a.sources) == null ? void 0 : _b[0]) === moduleId) {
1136
1144
  transformResult.map.sources[0] = filename;
1137
1145
  }
1138
1146
  return {
1139
1147
  code: transformResult.code,
1140
- map: hasMap ? transformResult.map : void 0,
1141
- dependencies: transformResult.deps
1148
+ map: hasMap ? transformResult.map : void 0
1142
1149
  };
1143
1150
  };
1144
1151
  }
1145
- function createVitePreprocessorGroup(config, options) {
1152
+ function createVitePreprocessorGroup(config) {
1146
1153
  return {
1147
- script: createPreprocessorFromVitePlugin(config, options, "vite:esbuild", supportedScriptLangs),
1148
- style: createPreprocessorFromVitePlugin(config, options, "vite:css", supportedStyleLangs)
1154
+ markup({ content, filename }) {
1155
+ return (0, import_compiler2.preprocess)(content, {
1156
+ script: createViteScriptPreprocessor(),
1157
+ style: createViteStylePreprocessor(config)
1158
+ }, { filename });
1159
+ }
1149
1160
  };
1150
1161
  }
1151
1162
  function createInjectScopeEverythingRulePreprocessorGroup() {
@@ -1162,10 +1173,11 @@ function createInjectScopeEverythingRulePreprocessorGroup() {
1162
1173
  }
1163
1174
  function buildExtraPreprocessors(options, config) {
1164
1175
  var _a, _b;
1165
- const extraPreprocessors = [];
1176
+ const prependPreprocessors = [];
1177
+ const appendPreprocessors = [];
1166
1178
  if ((_a = options.experimental) == null ? void 0 : _a.useVitePreprocess) {
1167
1179
  log.debug("adding vite preprocessor");
1168
- extraPreprocessors.push(createVitePreprocessorGroup(config, options));
1180
+ prependPreprocessors.push(createVitePreprocessorGroup(config));
1169
1181
  }
1170
1182
  const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p == null ? void 0 : p.sveltePreprocess);
1171
1183
  if (pluginsWithPreprocessorsDeprecated.length > 0) {
@@ -1198,23 +1210,24 @@ function buildExtraPreprocessors(options, config) {
1198
1210
  }
1199
1211
  if (included.length > 0) {
1200
1212
  log.debug(`Adding svelte preprocessors defined by these vite plugins: ${included.map((p) => p.name).join(", ")}`);
1201
- extraPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
1213
+ appendPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
1202
1214
  }
1203
1215
  if (options.hot && options.emitCss) {
1204
- extraPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
1216
+ appendPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
1205
1217
  }
1206
- return extraPreprocessors;
1218
+ return { prependPreprocessors, appendPreprocessors };
1207
1219
  }
1208
1220
  function addExtraPreprocessors(options, config) {
1209
1221
  var _a;
1210
- const extra = buildExtraPreprocessors(options, config);
1211
- if ((extra == null ? void 0 : extra.length) > 0) {
1222
+ const { prependPreprocessors, appendPreprocessors } = buildExtraPreprocessors(options, config);
1223
+ if (prependPreprocessors.length > 0 || appendPreprocessors.length > 0) {
1212
1224
  if (!options.preprocess) {
1213
- options.preprocess = extra;
1225
+ options.preprocess = [...prependPreprocessors, ...appendPreprocessors];
1214
1226
  } else if (Array.isArray(options.preprocess)) {
1215
- options.preprocess.push(...extra);
1227
+ options.preprocess.unshift(...prependPreprocessors);
1228
+ options.preprocess.push(...appendPreprocessors);
1216
1229
  } else {
1217
- options.preprocess = [options.preprocess, ...extra];
1230
+ options.preprocess = [...prependPreprocessors, options.preprocess, ...appendPreprocessors];
1218
1231
  }
1219
1232
  }
1220
1233
  const generateMissingSourceMaps = !!((_a = options.experimental) == null ? void 0 : _a.generateMissingPreprocessorSourcemaps);
@@ -1301,7 +1314,8 @@ function svelte(inlineOptions) {
1301
1314
  options.server = server;
1302
1315
  setupWatchers(options, cache, requestParser);
1303
1316
  },
1304
- load(id, ssr) {
1317
+ load(id, opts) {
1318
+ const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1305
1319
  const svelteRequest = requestParser(id, !!ssr);
1306
1320
  if (svelteRequest) {
1307
1321
  const { filename, query } = svelteRequest;
@@ -1318,7 +1332,8 @@ function svelte(inlineOptions) {
1318
1332
  }
1319
1333
  }
1320
1334
  },
1321
- async resolveId(importee, importer, customOptions, ssr) {
1335
+ async resolveId(importee, importer, opts, _ssr) {
1336
+ const ssr = _ssr === true || opts.ssr;
1322
1337
  const svelteRequest = requestParser(importee, !!ssr);
1323
1338
  if (svelteRequest == null ? void 0 : svelteRequest.query.svelte) {
1324
1339
  if (svelteRequest.query.type === "style") {
@@ -1358,8 +1373,9 @@ function svelte(inlineOptions) {
1358
1373
  }
1359
1374
  }
1360
1375
  },
1361
- async transform(code, id, ssr) {
1376
+ async transform(code, id, opts) {
1362
1377
  var _a;
1378
+ const ssr = opts === true || (opts == null ? void 0 : opts.ssr);
1363
1379
  const svelteRequest = requestParser(id, !!ssr);
1364
1380
  if (!svelteRequest) {
1365
1381
  return;