@sveltejs/vite-plugin-svelte 1.0.9 → 1.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/vite-plugin-svelte",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "license": "MIT",
5
5
  "author": "dominikg",
6
6
  "files": [
@@ -16,6 +16,7 @@
16
16
  "types": "dist/index.d.ts",
17
17
  "exports": {
18
18
  ".": {
19
+ "types": "./dist/index.d.ts",
19
20
  "import": "./dist/index.js",
20
21
  "require": "./dist/index.cjs"
21
22
  },
@@ -41,11 +42,10 @@
41
42
  },
42
43
  "homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
43
44
  "dependencies": {
44
- "@rollup/pluginutils": "^4.2.1",
45
45
  "debug": "^4.3.4",
46
46
  "deepmerge": "^4.2.2",
47
47
  "kleur": "^4.1.5",
48
- "magic-string": "^0.26.5",
48
+ "magic-string": "^0.26.7",
49
49
  "svelte-hmr": "^0.15.0"
50
50
  },
51
51
  "peerDependencies": {
@@ -62,11 +62,11 @@
62
62
  "@types/debug": "^4.1.7",
63
63
  "@types/diff-match-patch": "^1.0.32",
64
64
  "diff-match-patch": "^1.0.5",
65
- "esbuild": "^0.15.10",
65
+ "esbuild": "^0.15.13",
66
66
  "rollup": "^2.79.1",
67
- "svelte": "^3.50.1",
68
- "tsup": "^6.2.3",
69
- "vite": "^3.1.4"
67
+ "svelte": "^3.52.0",
68
+ "tsup": "^6.4.0",
69
+ "vite": "^3.2.3"
70
70
  },
71
71
  "scripts": {
72
72
  "dev": "pnpm build:ci --sourcemap --watch src",
package/src/index.ts CHANGED
@@ -85,7 +85,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
85
85
  },
86
86
 
87
87
  async buildStart() {
88
- if (!options.experimental?.prebundleSvelteLibraries) return;
88
+ if (!options.prebundleSvelteLibraries) return;
89
89
  const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
90
90
  if (isSvelteMetadataChanged) {
91
91
  // Force Vite to optimize again. Although we mutate the config here, it works because
package/src/utils/id.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-unused-vars */
2
- import { createFilter } from '@rollup/pluginutils';
2
+ import { createFilter } from 'vite';
3
3
  import { Arrayable, ResolvedOptions } from './options';
4
4
  import { normalizePath } from 'vite';
5
5
  import * as fs from 'fs';
@@ -34,6 +34,7 @@ const allowedPluginOptions = new Set([
34
34
  'hot',
35
35
  'ignorePluginPreprocessors',
36
36
  'disableDependencyReinclusion',
37
+ 'prebundleSvelteLibraries',
37
38
  'experimental'
38
39
  ]);
39
40
 
@@ -188,6 +189,7 @@ export function resolveOptions(
188
189
  const merged = mergeConfigs<ResolvedOptions>(defaultOptions, preResolveOptions, extraOptions);
189
190
 
190
191
  removeIgnoredOptions(merged);
192
+ handleDeprecatedOptions(merged);
191
193
  addSvelteKitOptions(merged);
192
194
  addExtraPreprocessors(merged, viteConfig);
193
195
  enforceOptionsForHmr(merged);
@@ -288,6 +290,15 @@ function addSvelteKitOptions(options: ResolvedOptions) {
288
290
  }
289
291
  }
290
292
 
293
+ function handleDeprecatedOptions(options: ResolvedOptions) {
294
+ if ((options.experimental as any)?.prebundleSvelteLibraries) {
295
+ options.prebundleSvelteLibraries = (options.experimental as any)?.prebundleSvelteLibraries;
296
+ log.warn(
297
+ 'experimental.prebundleSvelteLibraries is no longer experimental and has moved to prebundleSvelteLibraries'
298
+ );
299
+ }
300
+ }
301
+
291
302
  // vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
292
303
  // https://github.com/sveltejs/vite-plugin-svelte/issues/113
293
304
  // https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
@@ -318,7 +329,7 @@ export function buildExtraViteConfig(
318
329
  config.optimizeDeps
319
330
  );
320
331
 
321
- if (options.experimental?.prebundleSvelteLibraries) {
332
+ if (options.prebundleSvelteLibraries) {
322
333
  extraViteConfig.optimizeDeps = {
323
334
  ...extraViteConfig.optimizeDeps,
324
335
  // Experimental Vite API to allow these extensions to be scanned and prebundled
@@ -378,7 +389,7 @@ function buildOptimizeDepsForSvelte(
378
389
  }
379
390
 
380
391
  // If we prebundle svelte libraries, we can skip the whole prebundling dance below
381
- if (options.experimental?.prebundleSvelteLibraries) {
392
+ if (options.prebundleSvelteLibraries) {
382
393
  return { include, exclude };
383
394
  }
384
395
 
@@ -540,6 +551,13 @@ export interface PluginOptions {
540
551
  */
541
552
  disableDependencyReinclusion?: boolean | string[];
542
553
 
554
+ /**
555
+ * Force Vite to pre-bundle Svelte libraries
556
+ *
557
+ * @default false
558
+ */
559
+ prebundleSvelteLibraries?: boolean;
560
+
543
561
  /**
544
562
  * These options are considered experimental and breaking changes to them can occur in any release
545
563
  */
@@ -594,13 +612,6 @@ export interface ExperimentalOptions {
594
612
  */
595
613
  useVitePreprocess?: boolean;
596
614
 
597
- /**
598
- * Force Vite to pre-bundle Svelte libraries
599
- *
600
- * @default false
601
- */
602
- prebundleSvelteLibraries?: boolean;
603
-
604
615
  /**
605
616
  * If a preprocessor does not provide a sourcemap, a best-effort fallback sourcemap will be provided.
606
617
  * This option requires `diff-match-patch` to be installed as a peer dependency.
@@ -1,14 +1,8 @@
1
- import {
2
- transformWithEsbuild,
3
- ESBuildOptions,
4
- ResolvedConfig,
5
- TransformResult,
6
- Plugin
7
- } from 'vite';
1
+ import * as vite from 'vite';
2
+ import type { ESBuildOptions, ResolvedConfig, Plugin } from 'vite';
8
3
  import MagicString from 'magic-string';
9
4
  import { preprocess } from 'svelte/compiler';
10
5
  import { Preprocessor, PreprocessorGroup, Processed, ResolvedOptions } from './options';
11
- import { TransformPluginContext } from 'rollup';
12
6
  import { log } from './log';
13
7
  import { buildSourceMap } from './sourcemap';
14
8
  import path from 'path';
@@ -21,7 +15,7 @@ function createViteScriptPreprocessor(): Preprocessor {
21
15
  return async ({ attributes, content, filename = '' }) => {
22
16
  const lang = attributes.lang as string;
23
17
  if (!supportedScriptLangs.includes(lang)) return;
24
- const transformResult = await transformWithEsbuild(content, filename, {
18
+ const transformResult = await vite.transformWithEsbuild(content, filename, {
25
19
  loader: lang as ESBuildOptions['loader'],
26
20
  target: 'esnext',
27
21
  tsconfigRaw: {
@@ -40,34 +34,47 @@ function createViteScriptPreprocessor(): Preprocessor {
40
34
  }
41
35
 
42
36
  function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
43
- const pluginName = 'vite:css';
44
- const plugin = config.plugins.find((p) => p.name === pluginName);
45
- if (!plugin) {
46
- throw new Error(`failed to find plugin ${pluginName}`);
47
- }
48
- if (!plugin.transform) {
49
- throw new Error(`plugin ${pluginName} has no transform`);
50
- }
51
- const pluginTransform = plugin.transform!.bind(null as unknown as TransformPluginContext);
37
+ const transform = getCssTransformFn(config);
52
38
  return async ({ attributes, content, filename = '' }) => {
53
39
  const lang = attributes.lang as string;
54
40
  if (!supportedStyleLangs.includes(lang)) return;
55
41
  const moduleId = `${filename}.${lang}`;
56
- const transformResult: TransformResult = (await pluginTransform(
57
- content,
58
- moduleId
59
- )) as TransformResult;
42
+ const result = await transform(content, moduleId);
60
43
  // patch sourcemap source to point back to original filename
61
- if (transformResult.map?.sources?.[0] === moduleId) {
62
- transformResult.map.sources[0] = path.basename(filename);
44
+ if (result.map?.sources?.[0] === moduleId) {
45
+ result.map.sources[0] = path.basename(filename);
63
46
  }
64
47
  return {
65
- code: transformResult.code,
66
- map: transformResult.map ?? undefined
48
+ code: result.code,
49
+ map: result.map ?? undefined
67
50
  };
68
51
  };
69
52
  }
70
53
 
54
+ // eslint-disable-next-line no-unused-vars
55
+ type CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>;
56
+
57
+ function getCssTransformFn(config: ResolvedConfig): CssTransform {
58
+ // API is only available in Vite 3.2 and above
59
+ // TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2
60
+ if (vite.preprocessCSS) {
61
+ return async (code, filename) => {
62
+ return vite.preprocessCSS(code, filename, config);
63
+ };
64
+ } else {
65
+ const pluginName = 'vite:css';
66
+ const plugin = config.plugins.find((p) => p.name === pluginName);
67
+ if (!plugin) {
68
+ throw new Error(`failed to find plugin ${pluginName}`);
69
+ }
70
+ if (!plugin.transform) {
71
+ throw new Error(`plugin ${pluginName} has no transform`);
72
+ }
73
+ // @ts-expect-error
74
+ return plugin.transform.bind(null);
75
+ }
76
+ }
77
+
71
78
  function createVitePreprocessorGroup(config: ResolvedConfig): PreprocessorGroup {
72
79
  return {
73
80
  markup({ content, filename }) {