@sveltejs/vite-plugin-svelte 2.0.0-beta.1 → 2.0.0-beta.3

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.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import * as vite from 'vite';
2
- import { UserConfig, Plugin } from 'vite';
1
+ import { InlineConfig, ResolvedConfig, UserConfig, Plugin } from 'vite';
3
2
  import { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
4
3
  export { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
5
4
  import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
@@ -125,14 +124,6 @@ interface SvelteOptions {
125
124
  * These options are considered experimental and breaking changes to them can occur in any release
126
125
  */
127
126
  interface ExperimentalOptions {
128
- /**
129
- * Use extra preprocessors that delegate style and TypeScript preprocessing to native Vite plugins
130
- *
131
- * Do not use together with `svelte-preprocess`!
132
- *
133
- * @default false
134
- */
135
- useVitePreprocess?: boolean;
136
127
  /**
137
128
  * A function to update `compilerOptions` before compilation
138
129
  *
@@ -247,7 +238,7 @@ type Arrayable<T> = T | T[];
247
238
 
248
239
  declare function vitePreprocess(opts?: {
249
240
  script?: boolean;
250
- style?: boolean | vite.InlineConfig | vite.ResolvedConfig;
241
+ style?: boolean | InlineConfig | ResolvedConfig;
251
242
  }): PreprocessorGroup;
252
243
 
253
244
  declare function loadSvelteConfig(viteConfig?: UserConfig, inlineOptions?: Partial<Options>): Promise<Partial<SvelteOptions> | undefined>;
package/dist/index.js CHANGED
@@ -261,7 +261,7 @@ function normalizeJsCode(code) {
261
261
  }
262
262
 
263
263
  // src/utils/compile.ts
264
- import { compile, preprocess as preprocess2, walk } from "svelte/compiler";
264
+ import { compile, preprocess, walk } from "svelte/compiler";
265
265
  import { createMakeHot } from "svelte-hmr";
266
266
 
267
267
  // src/utils/hash.ts
@@ -290,110 +290,7 @@ function toSafe(base64) {
290
290
 
291
291
  // src/utils/preprocess.ts
292
292
  import MagicString from "magic-string";
293
- import { preprocess } from "svelte/compiler";
294
- import path2 from "path";
295
-
296
- // src/preprocess.ts
297
293
  import path from "path";
298
- import * as vite from "vite";
299
- var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss", "sss"];
300
- var supportedScriptLangs = ["ts"];
301
- function vitePreprocess(opts) {
302
- const preprocessor = {};
303
- if (opts?.script !== false) {
304
- preprocessor.script = viteScript().script;
305
- }
306
- if (opts?.style !== false) {
307
- const styleOpts = typeof opts?.style == "object" ? opts?.style : void 0;
308
- preprocessor.style = viteStyle(styleOpts).style;
309
- }
310
- return preprocessor;
311
- }
312
- function viteScript() {
313
- return {
314
- async script({ attributes, content, filename = "" }) {
315
- const lang = attributes.lang;
316
- if (!supportedScriptLangs.includes(lang))
317
- return;
318
- const transformResult = await vite.transformWithEsbuild(content, filename, {
319
- loader: lang,
320
- target: "esnext",
321
- tsconfigRaw: {
322
- compilerOptions: {
323
- importsNotUsedAsValues: "preserve",
324
- preserveValueImports: true
325
- }
326
- }
327
- });
328
- return {
329
- code: transformResult.code,
330
- map: transformResult.map
331
- };
332
- }
333
- };
334
- }
335
- function viteStyle(config = {}) {
336
- let transform;
337
- const style = async ({ attributes, content, filename = "" }) => {
338
- const lang = attributes.lang;
339
- if (!supportedStyleLangs.includes(lang))
340
- return;
341
- if (!transform) {
342
- let resolvedConfig;
343
- if (style.__resolvedConfig) {
344
- resolvedConfig = style.__resolvedConfig;
345
- } else if (isResolvedConfig(config)) {
346
- resolvedConfig = config;
347
- } else {
348
- resolvedConfig = await vite.resolveConfig(
349
- config,
350
- process.env.NODE_ENV === "production" ? "build" : "serve"
351
- );
352
- }
353
- transform = getCssTransformFn(resolvedConfig);
354
- }
355
- const moduleId = `${filename}.${lang}`;
356
- const result = await transform(content, moduleId);
357
- if (result.map?.sources?.[0] === moduleId) {
358
- result.map.sources[0] = path.basename(filename);
359
- }
360
- return {
361
- code: result.code,
362
- map: result.map ?? void 0
363
- };
364
- };
365
- style.__resolvedConfig = null;
366
- return { style };
367
- }
368
- function getCssTransformFn(config) {
369
- if (vite.preprocessCSS) {
370
- return async (code, filename) => {
371
- return vite.preprocessCSS(code, filename, config);
372
- };
373
- } else {
374
- const pluginName = "vite:css";
375
- const plugin = config.plugins.find((p) => p.name === pluginName);
376
- if (!plugin) {
377
- throw new Error(`failed to find plugin ${pluginName}`);
378
- }
379
- if (!plugin.transform) {
380
- throw new Error(`plugin ${pluginName} has no transform`);
381
- }
382
- return plugin.transform.bind(null);
383
- }
384
- }
385
- function isResolvedConfig(config) {
386
- return !!config.inlineConfig;
387
- }
388
-
389
- // src/utils/preprocess.ts
390
- function createVitePreprocessorGroup(config) {
391
- return {
392
- markup({ content, filename }) {
393
- return preprocess(content, vitePreprocess({ style: config }), { filename });
394
- }
395
- };
396
- }
397
294
  function createInjectScopeEverythingRulePreprocessorGroup() {
398
295
  return {
399
296
  style({ content, filename }) {
@@ -402,7 +299,7 @@ function createInjectScopeEverythingRulePreprocessorGroup() {
402
299
  return {
403
300
  code: s.toString(),
404
301
  map: s.generateDecodedMap({
405
- source: filename ? path2.basename(filename) : void 0,
302
+ source: filename ? path.basename(filename) : void 0,
406
303
  hires: true
407
304
  })
408
305
  };
@@ -412,12 +309,6 @@ function createInjectScopeEverythingRulePreprocessorGroup() {
412
309
  function buildExtraPreprocessors(options, config) {
413
310
  const prependPreprocessors = [];
414
311
  const appendPreprocessors = [];
415
- if (options.experimental?.useVitePreprocess) {
416
- log.warn(
417
- "`experimental.useVitePreprocess` is deprecated. Use the `vitePreprocess()` preprocessor instead. See https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md for more information."
418
- );
419
- prependPreprocessors.push(createVitePreprocessorGroup(config));
420
- }
421
312
  const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess);
422
313
  if (pluginsWithPreprocessorsDeprecated.length > 0) {
423
314
  log.warn(
@@ -473,14 +364,14 @@ function addExtraPreprocessors(options, config) {
473
364
  }
474
365
 
475
366
  // src/utils/compile.ts
476
- import path3 from "path";
367
+ import path2 from "path";
477
368
  var scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
478
369
  function mapSourcesToRelative(map, filename) {
479
370
  if (map?.sources) {
480
371
  map.sources = map.sources.map((s) => {
481
- if (path3.isAbsolute(s)) {
482
- const relative = path3.relative(filename, s);
483
- return relative === "" ? path3.basename(filename) : relative;
372
+ if (path2.isAbsolute(s)) {
373
+ const relative = path2.relative(filename, s);
374
+ return relative === "" ? path2.basename(filename) : relative;
484
375
  } else {
485
376
  return s;
486
377
  }
@@ -540,7 +431,7 @@ var _createCompileSvelte = (makeHot) => {
540
431
  }
541
432
  if (preprocessors) {
542
433
  try {
543
- preprocessed = await preprocess2(code, preprocessors, { filename });
434
+ preprocessed = await preprocess(code, preprocessors, { filename });
544
435
  } catch (e) {
545
436
  e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
546
437
  throw e;
@@ -749,7 +640,7 @@ import { normalizePath as normalizePath3 } from "vite";
749
640
 
750
641
  // src/utils/load-svelte-config.ts
751
642
  import { createRequire } from "module";
752
- import path4 from "path";
643
+ import path3 from "path";
753
644
  import fs2 from "fs";
754
645
  import { pathToFileURL } from "url";
755
646
  var esmRequire;
@@ -815,13 +706,13 @@ async function loadSvelteConfig(viteConfig, inlineOptions) {
815
706
  function findConfigToLoad(viteConfig, inlineOptions) {
816
707
  const root = viteConfig?.root || process.cwd();
817
708
  if (inlineOptions?.configFile) {
818
- const abolutePath = path4.isAbsolute(inlineOptions.configFile) ? inlineOptions.configFile : path4.resolve(root, inlineOptions.configFile);
709
+ const abolutePath = path3.isAbsolute(inlineOptions.configFile) ? inlineOptions.configFile : path3.resolve(root, inlineOptions.configFile);
819
710
  if (!fs2.existsSync(abolutePath)) {
820
711
  throw new Error(`failed to find svelte config file ${abolutePath}.`);
821
712
  }
822
713
  return abolutePath;
823
714
  } else {
824
- const existingKnownConfigFiles = knownSvelteConfigNames.map((candidate) => path4.resolve(root, candidate)).filter((file) => fs2.existsSync(file));
715
+ const existingKnownConfigFiles = knownSvelteConfigNames.map((candidate) => path3.resolve(root, candidate)).filter((file) => fs2.existsSync(file));
825
716
  if (existingKnownConfigFiles.length === 0) {
826
717
  log.debug(`no svelte config found at ${root}`);
827
718
  return;
@@ -856,11 +747,11 @@ var SVELTE_HMR_IMPORTS = [
856
747
  var SVELTE_EXPORT_CONDITIONS = ["svelte"];
857
748
 
858
749
  // src/utils/options.ts
859
- import path6 from "path";
750
+ import path5 from "path";
860
751
 
861
752
  // src/utils/esbuild.ts
862
753
  import { readFileSync } from "fs";
863
- import { compile as compile2, preprocess as preprocess3 } from "svelte/compiler";
754
+ import { compile as compile2, preprocess as preprocess2 } from "svelte/compiler";
864
755
 
865
756
  // src/utils/error.ts
866
757
  function toRollupError(error, options) {
@@ -961,7 +852,7 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
961
852
  let preprocessed;
962
853
  if (options.preprocess) {
963
854
  try {
964
- preprocessed = await preprocess3(code, options.preprocess, { filename });
855
+ preprocessed = await preprocess2(code, options.preprocess, { filename });
965
856
  } catch (e) {
966
857
  e.message = `Error while preprocessing ${filename}${e.message ? ` - ${e.message}` : ""}`;
967
858
  throw e;
@@ -1001,7 +892,7 @@ import {
1001
892
  } from "vitefu";
1002
893
 
1003
894
  // src/utils/dependencies.ts
1004
- import path5 from "path";
895
+ import path4 from "path";
1005
896
  import fs3 from "fs/promises";
1006
897
  import { findDepPkgJsonPath } from "vitefu";
1007
898
  async function resolveDependencyData(dep, parent) {
@@ -1010,7 +901,7 @@ async function resolveDependencyData(dep, parent) {
1010
901
  return void 0;
1011
902
  try {
1012
903
  return {
1013
- dir: path5.dirname(depDataPath),
904
+ dir: path4.dirname(depDataPath),
1014
905
  pkg: JSON.parse(await fs3.readFile(depDataPath, "utf-8"))
1015
906
  };
1016
907
  } catch {
@@ -1462,7 +1353,7 @@ function handleDeprecatedOptions(options) {
1462
1353
  }
1463
1354
  }
1464
1355
  function resolveViteRoot(viteConfig) {
1465
- return normalizePath3(viteConfig.root ? path6.resolve(viteConfig.root) : process.cwd());
1356
+ return normalizePath3(viteConfig.root ? path5.resolve(viteConfig.root) : process.cwd());
1466
1357
  }
1467
1358
  async function buildExtraViteConfig(options, config) {
1468
1359
  const extraViteConfig = {
@@ -1639,7 +1530,7 @@ function arraify(value) {
1639
1530
 
1640
1531
  // src/utils/watch.ts
1641
1532
  import fs4 from "fs";
1642
- import path7 from "path";
1533
+ import path6 from "path";
1643
1534
  function setupWatchers(options, cache, requestParser) {
1644
1535
  const { server, configFile: svelteConfigFile } = options;
1645
1536
  if (!server) {
@@ -1686,7 +1577,7 @@ function setupWatchers(options, cache, requestParser) {
1686
1577
  unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
1687
1578
  };
1688
1579
  if (svelteConfigFile !== false) {
1689
- const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path7.join(root, cfg));
1580
+ const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path6.join(root, cfg));
1690
1581
  const restartOnConfigAdd = (filename) => {
1691
1582
  if (possibleSvelteConfigs.includes(filename)) {
1692
1583
  triggerViteRestart(filename);
@@ -1712,12 +1603,12 @@ function setupWatchers(options, cache, requestParser) {
1712
1603
  }
1713
1604
  function ensureWatchedFile(watcher, file, root) {
1714
1605
  if (file && !file.startsWith(root + "/") && !file.includes("\0") && fs4.existsSync(file)) {
1715
- watcher.add(path7.resolve(file));
1606
+ watcher.add(path6.resolve(file));
1716
1607
  }
1717
1608
  }
1718
1609
 
1719
1610
  // src/utils/resolve.ts
1720
- import path8 from "path";
1611
+ import path7 from "path";
1721
1612
  import { builtinModules } from "module";
1722
1613
  async function resolveViaPackageJsonSvelte(importee, importer, cache) {
1723
1614
  if (importer && isBareImport(importee) && !isNodeInternal(importee) && !isCommonDepWithoutSvelteField(importee)) {
@@ -1729,7 +1620,7 @@ async function resolveViaPackageJsonSvelte(importee, importer, cache) {
1729
1620
  if (pkgData) {
1730
1621
  const { pkg, dir } = pkgData;
1731
1622
  if (pkg.svelte) {
1732
- const result = path8.resolve(dir, pkg.svelte);
1623
+ const result = path7.resolve(dir, pkg.svelte);
1733
1624
  cache.setResolvedSvelteField(importee, importer, result);
1734
1625
  return result;
1735
1626
  }
@@ -1740,7 +1631,7 @@ function isNodeInternal(importee) {
1740
1631
  return importee.startsWith("node:") || builtinModules.includes(importee);
1741
1632
  }
1742
1633
  function isBareImport(importee) {
1743
- if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path8.isAbsolute(importee)) {
1634
+ if (!importee || importee[0] === "." || importee[0] === "\0" || importee.includes(":") || path7.isAbsolute(importee)) {
1744
1635
  return false;
1745
1636
  }
1746
1637
  const parts = importee.split("/");
@@ -1756,7 +1647,7 @@ function isBareImport(importee) {
1756
1647
 
1757
1648
  // src/utils/optimizer.ts
1758
1649
  import { promises as fs5 } from "fs";
1759
- import path9 from "path";
1650
+ import path8 from "path";
1760
1651
  var PREBUNDLE_SENSITIVE_OPTIONS = [
1761
1652
  "compilerOptions",
1762
1653
  "configFile",
@@ -1767,7 +1658,7 @@ var PREBUNDLE_SENSITIVE_OPTIONS = [
1767
1658
  ];
1768
1659
  async function saveSvelteMetadata(cacheDir, options) {
1769
1660
  const svelteMetadata = generateSvelteMetadata(options);
1770
- const svelteMetadataPath = path9.resolve(cacheDir, "_svelte_metadata.json");
1661
+ const svelteMetadataPath = path8.resolve(cacheDir, "_svelte_metadata.json");
1771
1662
  const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
1772
1663
  return typeof value === "function" ? value.toString() : value;
1773
1664
  });
@@ -1790,7 +1681,7 @@ function generateSvelteMetadata(options) {
1790
1681
 
1791
1682
  // src/ui/inspector/plugin.ts
1792
1683
  import { normalizePath as normalizePath4 } from "vite";
1793
- import path10 from "path";
1684
+ import path9 from "path";
1794
1685
  import { fileURLToPath } from "url";
1795
1686
  import fs6 from "fs";
1796
1687
 
@@ -1817,7 +1708,7 @@ var defaultInspectorOptions = {
1817
1708
  customStyles: true
1818
1709
  };
1819
1710
  function getInspectorPath() {
1820
- const pluginPath = normalizePath4(path10.dirname(fileURLToPath(import.meta.url)));
1711
+ const pluginPath = normalizePath4(path9.dirname(fileURLToPath(import.meta.url)));
1821
1712
  return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, "/vite-plugin-svelte/src/ui/inspector/");
1822
1713
  }
1823
1714
  function svelteInspector() {
@@ -1843,7 +1734,7 @@ function svelteInspector() {
1843
1734
  disabled = true;
1844
1735
  } else {
1845
1736
  if (vps.api.options.kit && !inspectorOptions.appendTo) {
1846
- const out_dir = path10.basename(vps.api.options.kit.outDir || ".svelte-kit");
1737
+ const out_dir = path9.basename(vps.api.options.kit.outDir || ".svelte-kit");
1847
1738
  inspectorOptions.appendTo = `${out_dir}/generated/root.svelte`;
1848
1739
  }
1849
1740
  appendTo = inspectorOptions.appendTo;
@@ -2102,6 +1993,87 @@ function toRawExports(object) {
2102
1993
  return exports;
2103
1994
  }
2104
1995
 
1996
+ // src/preprocess.ts
1997
+ import path10 from "path";
1998
+ import { preprocessCSS, resolveConfig, transformWithEsbuild } from "vite";
1999
+ var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss", "sss"];
2000
+ var supportedScriptLangs = ["ts"];
2001
+ function vitePreprocess(opts) {
2002
+ const preprocessor = {};
2003
+ if (opts?.script !== false) {
2004
+ preprocessor.script = viteScript().script;
2005
+ }
2006
+ if (opts?.style !== false) {
2007
+ const styleOpts = typeof opts?.style == "object" ? opts?.style : void 0;
2008
+ preprocessor.style = viteStyle(styleOpts).style;
2009
+ }
2010
+ return preprocessor;
2011
+ }
2012
+ function viteScript() {
2013
+ return {
2014
+ async script({ attributes, content, filename = "" }) {
2015
+ const lang = attributes.lang;
2016
+ if (!supportedScriptLangs.includes(lang))
2017
+ return;
2018
+ const transformResult = await transformWithEsbuild(content, filename, {
2019
+ loader: lang,
2020
+ target: "esnext",
2021
+ tsconfigRaw: {
2022
+ compilerOptions: {
2023
+ importsNotUsedAsValues: "preserve",
2024
+ preserveValueImports: true
2025
+ }
2026
+ }
2027
+ });
2028
+ return {
2029
+ code: transformResult.code,
2030
+ map: transformResult.map
2031
+ };
2032
+ }
2033
+ };
2034
+ }
2035
+ function viteStyle(config = {}) {
2036
+ let transform;
2037
+ const style = async ({ attributes, content, filename = "" }) => {
2038
+ const lang = attributes.lang;
2039
+ if (!supportedStyleLangs.includes(lang))
2040
+ return;
2041
+ if (!transform) {
2042
+ let resolvedConfig;
2043
+ if (style.__resolvedConfig) {
2044
+ resolvedConfig = style.__resolvedConfig;
2045
+ } else if (isResolvedConfig(config)) {
2046
+ resolvedConfig = config;
2047
+ } else {
2048
+ resolvedConfig = await resolveConfig(
2049
+ config,
2050
+ process.env.NODE_ENV === "production" ? "build" : "serve"
2051
+ );
2052
+ }
2053
+ transform = getCssTransformFn(resolvedConfig);
2054
+ }
2055
+ const moduleId = `${filename}.${lang}`;
2056
+ const result = await transform(content, moduleId);
2057
+ if (result.map?.sources?.[0] === moduleId) {
2058
+ result.map.sources[0] = path10.basename(filename);
2059
+ }
2060
+ return {
2061
+ code: result.code,
2062
+ map: result.map ?? void 0
2063
+ };
2064
+ };
2065
+ style.__resolvedConfig = null;
2066
+ return { style };
2067
+ }
2068
+ function getCssTransformFn(config) {
2069
+ return async (code, filename) => {
2070
+ return preprocessCSS(code, filename, config);
2071
+ };
2072
+ }
2073
+ function isResolvedConfig(config) {
2074
+ return !!config.inlineConfig;
2075
+ }
2076
+
2105
2077
  // src/index.ts
2106
2078
  function svelte(inlineOptions) {
2107
2079
  if (process.env.DEBUG != null) {
@@ -2113,6 +2085,7 @@ function svelte(inlineOptions) {
2113
2085
  let options;
2114
2086
  let viteConfig;
2115
2087
  let compileSvelte2;
2088
+ let resolvedSvelteSSR;
2116
2089
  const api = {};
2117
2090
  const plugins = [
2118
2091
  {
@@ -2182,6 +2155,24 @@ function svelte(inlineOptions) {
2182
2155
  return svelteRequest.cssId;
2183
2156
  }
2184
2157
  }
2158
+ if (ssr && importee === "svelte") {
2159
+ if (!resolvedSvelteSSR) {
2160
+ resolvedSvelteSSR = this.resolve("svelte/ssr", void 0, { skipSelf: true }).then(
2161
+ (svelteSSR) => {
2162
+ log.debug("resolved svelte to svelte/ssr");
2163
+ return svelteSSR;
2164
+ },
2165
+ (err) => {
2166
+ log.debug(
2167
+ "failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it",
2168
+ err
2169
+ );
2170
+ return null;
2171
+ }
2172
+ );
2173
+ }
2174
+ return resolvedSvelteSSR;
2175
+ }
2185
2176
  const scan = !!opts?.scan;
2186
2177
  const isPrebundled = options.prebundleSvelteLibraries && viteConfig.optimizeDeps?.disabled !== true && viteConfig.optimizeDeps?.disabled !== (options.isBuild ? "build" : "dev") && !isDepExcluded2(importee, viteConfig.optimizeDeps?.exclude ?? []);
2187
2178
  if (ssr || scan || !isPrebundled) {