@sveltejs/vite-plugin-svelte 1.0.0-next.40 → 1.0.0-next.43

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.
@@ -0,0 +1,101 @@
1
+ import { createRequire } from 'module';
2
+ import { Plugin } from 'vite';
3
+ import { log } from '../../utils/log';
4
+ import { InspectorOptions } from '../../utils/options';
5
+
6
+ const defaultInspectorOptions: InspectorOptions = {
7
+ toggleKeyCombo: process.platform === 'win32' ? 'control-shift' : 'meta-shift',
8
+ holdMode: false,
9
+ showToggleButton: 'active',
10
+ toggleButtonPos: 'top-right',
11
+ customStyles: true
12
+ };
13
+
14
+ export function svelteInspector(): Plugin {
15
+ let root: string;
16
+ let rootRequire: NodeRequire;
17
+ let inspectorOptions: InspectorOptions;
18
+ let append_to: string | undefined;
19
+
20
+ return {
21
+ name: 'vite-plugin-svelte:inspector',
22
+ apply: 'serve',
23
+ enforce: 'pre',
24
+
25
+ configResolved(config) {
26
+ const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte');
27
+ if (vps?.api?.options?.experimental?.inspector) {
28
+ inspectorOptions = {
29
+ ...defaultInspectorOptions,
30
+ ...vps.api.options.experimental.inspector
31
+ };
32
+ }
33
+ if (!vps || !inspectorOptions) {
34
+ // disabled, turn all hooks into noops
35
+ this.resolveId = this.load = this.transformIndexHtml = this.transform = () => {};
36
+ } else {
37
+ root = config.root || process.cwd();
38
+ rootRequire = createRequire(root);
39
+ if (vps.api.options.kit && !inspectorOptions.appendTo) {
40
+ const out_dir = vps.api.options.kit.outDir || '.svelte-kit';
41
+ inspectorOptions.appendTo = `${out_dir}/runtime/client/start.js`;
42
+ }
43
+ append_to = inspectorOptions.appendTo;
44
+ }
45
+ },
46
+
47
+ async resolveId(importee: string, importer, options) {
48
+ if (options?.ssr) {
49
+ return;
50
+ }
51
+ if (importee === 'virtual:svelte-inspector-options') {
52
+ return importee;
53
+ }
54
+ if (importee.startsWith('virtual:svelte-inspector:')) {
55
+ // this is needed because the plugin itself is not a dependency of the app so regular resolve may not find it
56
+ const file = importee.replace(
57
+ 'virtual:svelte-inspector:',
58
+ '@sveltejs/vite-plugin-svelte/src/ui/inspector/'
59
+ );
60
+ const path = rootRequire.resolve(file);
61
+ if (path) {
62
+ return path;
63
+ } else {
64
+ log.error.once(`failed to resolve ${file} for ${importee} from ${root}`);
65
+ }
66
+ }
67
+ },
68
+ load(id) {
69
+ if (id === 'virtual:svelte-inspector-options') {
70
+ return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
71
+ }
72
+ },
73
+ transform(code: string, id: string, options?: { ssr?: boolean }) {
74
+ if (options?.ssr || !append_to) {
75
+ return;
76
+ }
77
+ if (id.endsWith(append_to)) {
78
+ return { code: `${code}\nimport 'virtual:svelte-inspector:load-inspector.ts'` };
79
+ }
80
+ },
81
+ transformIndexHtml(html) {
82
+ if (append_to) {
83
+ return;
84
+ }
85
+ return {
86
+ html,
87
+ tags: [
88
+ {
89
+ tag: 'script',
90
+ injectTo: 'body',
91
+ attrs: {
92
+ type: 'module',
93
+ // /@id/ is needed, otherwise the virtual: is seen as protocol by browser and cors error happens
94
+ src: '/@id/virtual:svelte-inspector:load-inspector.ts'
95
+ }
96
+ }
97
+ ]
98
+ };
99
+ }
100
+ };
101
+ }
@@ -6,6 +6,8 @@ import { SvelteRequest } from './id';
6
6
  import { safeBase64Hash } from './hash';
7
7
  import { log } from './log';
8
8
 
9
+ const scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
10
+
9
11
  const _createCompileSvelte = (makeHot: Function) =>
10
12
  async function compileSvelte(
11
13
  svelteRequest: SvelteRequest,
@@ -88,6 +90,7 @@ const _createCompileSvelte = (makeHot: Function) =>
88
90
  return {
89
91
  filename,
90
92
  normalizedFilename,
93
+ lang: code.match(scriptLangRE)?.[1] || 'js',
91
94
  // @ts-ignore
92
95
  compiled,
93
96
  ssr,
@@ -148,6 +151,7 @@ export interface Compiled {
148
151
  export interface CompileData {
149
152
  filename: string;
150
153
  normalizedFilename: string;
154
+ lang: string;
151
155
  compiled: Compiled;
152
156
  ssr: boolean | undefined;
153
157
  dependencies: string[];
@@ -8,7 +8,6 @@ import { toESBuildError } from './error';
8
8
 
9
9
  type EsbuildOptions = NonNullable<DepOptimizationOptions['esbuildOptions']>;
10
10
  type EsbuildPlugin = NonNullable<EsbuildOptions['plugins']>[number];
11
- type EsbuildPluginBuild = Parameters<EsbuildPlugin['setup']>[0];
12
11
 
13
12
  export const facadeEsbuildSveltePluginName = 'vite-plugin-svelte:facade';
14
13
 
@@ -16,7 +15,9 @@ export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
16
15
  return {
17
16
  name: 'vite-plugin-svelte:optimize-svelte',
18
17
  setup(build) {
19
- disableVitePrebundleSvelte(build);
18
+ // Skip in scanning phase as Vite already handles scanning Svelte files.
19
+ // Otherwise this would heavily slow down the scanning phase.
20
+ if (build.initialOptions.plugins?.some((v) => v.name === 'vite:dep-scan')) return;
20
21
 
21
22
  const svelteExtensions = (options.extensions ?? ['.svelte']).map((ext) => ext.slice(1));
22
23
  const svelteFilter = new RegExp(`\\.(` + svelteExtensions.join('|') + `)(\\?.*)?$`);
@@ -34,30 +35,6 @@ export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
34
35
  };
35
36
  }
36
37
 
37
- function disableVitePrebundleSvelte(build: EsbuildPluginBuild) {
38
- const viteDepPrebundlePlugin = build.initialOptions.plugins?.find(
39
- (v) => v.name === 'vite:dep-pre-bundle'
40
- );
41
-
42
- if (!viteDepPrebundlePlugin) return;
43
-
44
- // Prevent vite:dep-pre-bundle from externalizing svelte files
45
- const _setup = viteDepPrebundlePlugin.setup.bind(viteDepPrebundlePlugin);
46
- viteDepPrebundlePlugin.setup = function (build) {
47
- const _onResolve = build.onResolve.bind(build);
48
- build.onResolve = function (options, callback) {
49
- if (options.filter.source.includes('svelte')) {
50
- options.filter = new RegExp(
51
- options.filter.source.replace('|svelte', ''),
52
- options.filter.flags
53
- );
54
- }
55
- return _onResolve(options, callback);
56
- };
57
- return _setup(build);
58
- };
59
- }
60
-
61
38
  async function compileSvelte(
62
39
  options: ResolvedOptions,
63
40
  { filename, code }: { filename: string; code: string }
package/src/utils/hash.ts CHANGED
@@ -14,7 +14,7 @@ export function safeBase64Hash(input: string) {
14
14
  // OR DON'T USE A HASH AT ALL, what about a simple counter?
15
15
  const md5 = crypto.createHash('md5');
16
16
  md5.update(input);
17
- const hash = toSafe(md5.digest('base64')).substr(0, hash_length);
17
+ const hash = toSafe(md5.digest('base64')).slice(0, hash_length);
18
18
  hashes[input] = hash;
19
19
  return hash;
20
20
  }
@@ -29,6 +29,9 @@ export async function loadSvelteConfig(
29
29
  viteConfig: UserConfig,
30
30
  inlineOptions: Partial<Options>
31
31
  ): Promise<Partial<Options> | undefined> {
32
+ if (inlineOptions.configFile === false) {
33
+ return;
34
+ }
32
35
  const configFile = findConfigToLoad(viteConfig, inlineOptions);
33
36
  if (configFile) {
34
37
  let err;
@@ -1,6 +1,5 @@
1
- import fs from 'fs';
1
+ import { promises as fs } from 'fs';
2
2
  import path from 'path';
3
- import { optimizeDeps, ResolvedConfig } from 'vite';
4
3
  import { ResolvedOptions } from './options';
5
4
 
6
5
  // List of options that changes the prebundling result
@@ -13,24 +12,28 @@ const PREBUNDLE_SENSITIVE_OPTIONS: (keyof ResolvedOptions)[] = [
13
12
  'preprocess'
14
13
  ];
15
14
 
16
- export async function handleOptimizeDeps(options: ResolvedOptions, viteConfig: ResolvedConfig) {
17
- if (!options.experimental.prebundleSvelteLibraries || !viteConfig.cacheDir) return;
15
+ /**
16
+ * @returns Whether the Svelte metadata has changed
17
+ */
18
+ export async function saveSvelteMetadata(cacheDir: string, options: ResolvedOptions) {
19
+ const svelteMetadata = generateSvelteMetadata(options);
20
+ const svelteMetadataPath = path.resolve(cacheDir, '_svelte_metadata.json');
18
21
 
19
- const viteMetadataPath = findViteMetadataPath(viteConfig.cacheDir);
20
- if (!viteMetadataPath) return;
21
-
22
- const svelteMetadataPath = path.resolve(viteMetadataPath, '../_svelte_metadata.json');
23
- const currentSvelteMetadata = JSON.stringify(generateSvelteMetadata(options), (_, value) => {
22
+ const currentSvelteMetadata = JSON.stringify(svelteMetadata, (_, value) => {
23
+ // Handle preprocessors
24
24
  return typeof value === 'function' ? value.toString() : value;
25
25
  });
26
26
 
27
- if (fs.existsSync(svelteMetadataPath)) {
28
- const existingSvelteMetadata = fs.readFileSync(svelteMetadataPath, 'utf8');
29
- if (existingSvelteMetadata === currentSvelteMetadata) return;
27
+ let existingSvelteMetadata: string | undefined;
28
+ try {
29
+ existingSvelteMetadata = await fs.readFile(svelteMetadataPath, 'utf8');
30
+ } catch {
31
+ // ignore
30
32
  }
31
33
 
32
- await optimizeDeps(viteConfig, true);
33
- fs.writeFileSync(svelteMetadataPath, currentSvelteMetadata);
34
+ await fs.mkdir(cacheDir, { recursive: true });
35
+ await fs.writeFile(svelteMetadataPath, currentSvelteMetadata);
36
+ return currentSvelteMetadata !== existingSvelteMetadata;
34
37
  }
35
38
 
36
39
  function generateSvelteMetadata(options: ResolvedOptions) {
@@ -40,11 +43,3 @@ function generateSvelteMetadata(options: ResolvedOptions) {
40
43
  }
41
44
  return metadata;
42
45
  }
43
-
44
- function findViteMetadataPath(cacheDir: string) {
45
- const metadataPaths = ['_metadata.json', 'deps/_metadata.json'];
46
- for (const metadataPath of metadataPaths) {
47
- const viteMetadataPath = path.resolve(cacheDir, metadataPath);
48
- if (fs.existsSync(viteMetadataPath)) return viteMetadataPath;
49
- }
50
- }
@@ -24,6 +24,7 @@ import { findRootSvelteDependencies, needsOptimization, SvelteDependency } from
24
24
  import { createRequire } from 'module';
25
25
  import { esbuildSveltePlugin, facadeEsbuildSveltePluginName } from './esbuild';
26
26
  import { addExtraPreprocessors } from './preprocess';
27
+ import deepmerge from 'deepmerge';
27
28
 
28
29
  const knownOptions = new Set([
29
30
  'configFile',
@@ -37,7 +38,8 @@ const knownOptions = new Set([
37
38
  'hot',
38
39
  'ignorePluginPreprocessors',
39
40
  'disableDependencyReinclusion',
40
- 'experimental'
41
+ 'experimental',
42
+ 'kit'
41
43
  ]);
42
44
 
43
45
  export function validateInlineOptions(inlineOptions?: Partial<Options>) {
@@ -65,26 +67,19 @@ export async function preResolveOptions(
65
67
  }
66
68
  };
67
69
  const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
68
- const merged = {
69
- ...defaultOptions,
70
- ...svelteConfig,
71
- ...inlineOptions,
72
- compilerOptions: {
73
- ...defaultOptions?.compilerOptions,
74
- ...svelteConfig?.compilerOptions,
75
- ...inlineOptions?.compilerOptions
76
- },
77
- experimental: {
78
- ...defaultOptions?.experimental,
79
- ...svelteConfig?.experimental,
80
- ...inlineOptions?.experimental
81
- },
82
- // extras
70
+ const extraOptions: Partial<PreResolvedOptions> = {
83
71
  root: viteConfigWithResolvedRoot.root!,
84
72
  isBuild: viteEnv.command === 'build',
85
73
  isServe: viteEnv.command === 'serve',
86
74
  isDebug: process.env.DEBUG != null
87
75
  };
76
+ const merged = mergeConfigs<Partial<PreResolvedOptions> | undefined>(
77
+ defaultOptions,
78
+ svelteConfig,
79
+ inlineOptions,
80
+ extraOptions
81
+ );
82
+
88
83
  // configFile of svelteConfig contains the absolute path it was loaded from,
89
84
  // prefer it over the possibly relative inline path
90
85
  if (svelteConfig?.configFile) {
@@ -93,6 +88,17 @@ export async function preResolveOptions(
93
88
  return merged;
94
89
  }
95
90
 
91
+ function mergeConfigs<T>(...configs: T[]): ResolvedOptions {
92
+ let result = {};
93
+ for (const config of configs.filter(Boolean)) {
94
+ result = deepmerge<T>(result, config, {
95
+ // replace arrays
96
+ arrayMerge: (target: any[], source: any[]) => source ?? target
97
+ });
98
+ }
99
+ return result as ResolvedOptions;
100
+ }
101
+
96
102
  // used in configResolved phase, merges a contextual default config, pre-resolved options, and some preprocessors.
97
103
  // also validates the final config.
98
104
  export function resolveOptions(
@@ -106,16 +112,12 @@ export function resolveOptions(
106
112
  dev: !viteConfig.isProduction
107
113
  }
108
114
  };
109
- const merged: ResolvedOptions = {
110
- ...defaultOptions,
111
- ...preResolveOptions,
112
- compilerOptions: {
113
- ...defaultOptions.compilerOptions,
114
- ...preResolveOptions.compilerOptions
115
- },
115
+ const extraOptions: Partial<ResolvedOptions> = {
116
116
  root: viteConfig.root,
117
117
  isProduction: viteConfig.isProduction
118
118
  };
119
+ const merged: ResolvedOptions = mergeConfigs(defaultOptions, preResolveOptions, extraOptions);
120
+
119
121
  addExtraPreprocessors(merged, viteConfig);
120
122
  enforceOptionsForHmr(merged);
121
123
  enforceOptionsForProduction(merged);
@@ -207,6 +209,21 @@ export function buildExtraViteConfig(
207
209
  );
208
210
  }
209
211
 
212
+ if (options.experimental?.prebundleSvelteLibraries) {
213
+ extraViteConfig.optimizeDeps = {
214
+ ...extraViteConfig.optimizeDeps,
215
+ // Experimental Vite API to allow these extensions to be scanned and prebundled
216
+ // @ts-ignore
217
+ extensions: options.extensions ?? ['.svelte'],
218
+ // Add esbuild plugin to prebundle Svelte files.
219
+ // Currently a placeholder as more information is needed after Vite config is resolved,
220
+ // the real Svelte plugin is added in `patchResolvedViteConfig()`
221
+ esbuildOptions: {
222
+ plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {} }]
223
+ }
224
+ };
225
+ }
226
+
210
227
  // @ts-ignore
211
228
  extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
212
229
 
@@ -241,14 +258,8 @@ function buildOptimizeDepsForSvelte(
241
258
  }
242
259
 
243
260
  // If we prebundle svelte libraries, we can skip the whole prebundling dance below
244
- if (options.experimental.prebundleSvelteLibraries) {
245
- return {
246
- include,
247
- exclude,
248
- esbuildOptions: {
249
- plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {} }]
250
- }
251
- };
261
+ if (options.experimental?.prebundleSvelteLibraries) {
262
+ return { include, exclude };
252
263
  }
253
264
 
254
265
  // only svelte component libraries needs to be processed for optimizeDeps, js libraries work fine
@@ -346,9 +357,11 @@ export interface Options {
346
357
  /**
347
358
  * Path to a svelte config file, either absolute or relative to Vite root
348
359
  *
360
+ * set to `false` to ignore the svelte config file
361
+ *
349
362
  * @see https://vitejs.dev/config/#root
350
363
  */
351
- configFile?: string;
364
+ configFile?: string | false;
352
365
 
353
366
  /**
354
367
  * A `picomatch` pattern, or array of patterns, which specifies the files the plugin should
@@ -498,6 +511,55 @@ export interface ExperimentalOptions {
498
511
  code: string;
499
512
  compileOptions: Partial<CompileOptions>;
500
513
  }) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
514
+
515
+ /**
516
+ * enable svelte inspector
517
+ */
518
+ inspector?: InspectorOptions | boolean;
519
+ }
520
+
521
+ export interface InspectorOptions {
522
+ /**
523
+ * define a key combo to toggle inspector,
524
+ * @default 'control-shift' on windows, 'meta-shift' on other os
525
+ *
526
+ * any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
527
+ * examples: control-shift, control-o, control-alt-s meta-x control-meta
528
+ * Some keys have native behavior (e.g. alt-s opens history menu on firefox).
529
+ * To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
530
+ */
531
+ toggleKeyCombo?: string;
532
+
533
+ /**
534
+ * inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
535
+ * @default false
536
+ */
537
+ holdMode?: boolean;
538
+ /**
539
+ * when to show the toggle button
540
+ * @default 'active'
541
+ */
542
+ showToggleButton?: 'always' | 'active' | 'never';
543
+
544
+ /**
545
+ * where to display the toggle button
546
+ * @default top-right
547
+ */
548
+ toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
549
+
550
+ /**
551
+ * inject custom styles when inspector is active
552
+ */
553
+ customStyles?: boolean;
554
+
555
+ /**
556
+ * append an import to the module id ending with `appendTo` instead of adding a script into body
557
+ * useful for frameworks that do not support trannsformIndexHtml hook
558
+ *
559
+ * WARNING: only set this if you know exactly what it does.
560
+ * Regular users of vite-plugin-svelte or SvelteKit do not need it
561
+ */
562
+ appendTo?: string;
501
563
  }
502
564
 
503
565
  export interface PreResolvedOptions extends Options {
@@ -11,6 +11,7 @@ import { Preprocessor, PreprocessorGroup, Processed, ResolvedOptions } from './o
11
11
  import { TransformPluginContext } from 'rollup';
12
12
  import { log } from './log';
13
13
  import { buildSourceMap } from './sourcemap';
14
+ import path from 'path';
14
15
 
15
16
  const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss'];
16
17
 
@@ -57,7 +58,7 @@ function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
57
58
  )) as TransformResult;
58
59
  // patch sourcemap source to point back to original filename
59
60
  if (transformResult.map?.sources?.[0] === moduleId) {
60
- transformResult.map.sources[0] = filename;
61
+ transformResult.map.sources[0] = path.basename(filename);
61
62
  }
62
63
  return {
63
64
  code: transformResult.code,
@@ -94,7 +95,10 @@ function createInjectScopeEverythingRulePreprocessorGroup(): PreprocessorGroup {
94
95
  s.append(' *{}');
95
96
  return {
96
97
  code: s.toString(),
97
- map: s.generateDecodedMap({ source: filename, hires: true })
98
+ map: s.generateDecodedMap({
99
+ source: filename ? path.basename(filename) : undefined,
100
+ hires: true
101
+ })
98
102
  };
99
103
  }
100
104
  };
@@ -58,30 +58,34 @@ export function setupWatchers(
58
58
  }
59
59
  };
60
60
 
61
- const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path.join(root, cfg));
62
- const restartOnConfigAdd = (filename: string) => {
63
- if (possibleSvelteConfigs.includes(filename)) {
64
- triggerViteRestart(filename);
65
- }
66
- };
67
-
68
- const restartOnConfigChange = (filename: string) => {
69
- if (filename === svelteConfigFile) {
70
- triggerViteRestart(filename);
71
- }
72
- };
73
-
74
61
  // collection of watcher listeners by event
75
62
  const listenerCollection = {
76
63
  add: [] as Array<Function>,
77
64
  change: [emitChangeEventOnDependants],
78
65
  unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
79
66
  };
80
- if (svelteConfigFile) {
81
- listenerCollection.change.push(restartOnConfigChange);
82
- listenerCollection.unlink.push(restartOnConfigChange);
83
- } else {
84
- listenerCollection.add.push(restartOnConfigAdd);
67
+
68
+ if (svelteConfigFile !== false) {
69
+ // configFile false means we ignore the file and external process is responsible
70
+ const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path.join(root, cfg));
71
+ const restartOnConfigAdd = (filename: string) => {
72
+ if (possibleSvelteConfigs.includes(filename)) {
73
+ triggerViteRestart(filename);
74
+ }
75
+ };
76
+
77
+ const restartOnConfigChange = (filename: string) => {
78
+ if (filename === svelteConfigFile) {
79
+ triggerViteRestart(filename);
80
+ }
81
+ };
82
+
83
+ if (svelteConfigFile) {
84
+ listenerCollection.change.push(restartOnConfigChange);
85
+ listenerCollection.unlink.push(restartOnConfigChange);
86
+ } else {
87
+ listenerCollection.add.push(restartOnConfigAdd);
88
+ }
85
89
  }
86
90
 
87
91
  Object.entries(listenerCollection).forEach(([evt, listeners]) => {