@sveltejs/vite-plugin-svelte 2.1.1 → 2.3.0

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.
@@ -1,115 +0,0 @@
1
- import { Plugin, normalizePath } from 'vite';
2
- import { log } from '../../utils/log';
3
- import { InspectorOptions } from '../../utils/options';
4
- import path from 'path';
5
- import { fileURLToPath } from 'url';
6
- import fs from 'fs';
7
- import { idToFile } from './utils';
8
-
9
- const defaultInspectorOptions: InspectorOptions = {
10
- toggleKeyCombo: process.platform === 'win32' ? 'control-shift' : 'meta-shift',
11
- navKeys: { parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' },
12
- openKey: 'Enter',
13
- holdMode: false,
14
- showToggleButton: 'active',
15
- toggleButtonPos: 'top-right',
16
- customStyles: true
17
- };
18
-
19
- function getInspectorPath() {
20
- const pluginPath = normalizePath(path.dirname(fileURLToPath(import.meta.url)));
21
- return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, '/vite-plugin-svelte/src/ui/inspector/');
22
- }
23
-
24
- export function svelteInspector(): Plugin {
25
- const inspectorPath = getInspectorPath();
26
- log.debug.enabled && log.debug(`svelte inspector path: ${inspectorPath}`);
27
- let inspectorOptions: InspectorOptions;
28
- let appendTo: string | undefined;
29
- let disabled = false;
30
-
31
- return {
32
- name: 'vite-plugin-svelte:inspector',
33
- apply: 'serve',
34
- enforce: 'pre',
35
-
36
- configResolved(config) {
37
- const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte');
38
- const options = vps?.api?.options?.experimental?.inspector;
39
- if (!vps || !options) {
40
- log.debug('inspector disabled, could not find config');
41
- disabled = true;
42
- return;
43
- }
44
- inspectorOptions = {
45
- ...defaultInspectorOptions,
46
- ...options
47
- };
48
- const isSvelteKit = config.plugins.some((p) => p.name.startsWith('vite-plugin-sveltekit'));
49
- if (isSvelteKit && !inspectorOptions.appendTo) {
50
- // this could append twice if a user had a file that ends with /generated/root.svelte
51
- // but that should be rare and inspector doesn't execute twice
52
- inspectorOptions.appendTo = `/generated/root.svelte`;
53
- }
54
- appendTo = inspectorOptions.appendTo;
55
- },
56
-
57
- async resolveId(importee: string, importer, options) {
58
- if (options?.ssr || disabled) {
59
- return;
60
- }
61
- if (importee.startsWith('virtual:svelte-inspector-options')) {
62
- return importee;
63
- } else if (importee.startsWith('virtual:svelte-inspector-path:')) {
64
- const resolved = importee.replace('virtual:svelte-inspector-path:', inspectorPath);
65
- log.debug.enabled && log.debug(`resolved ${importee} with ${resolved}`);
66
- return resolved;
67
- }
68
- },
69
-
70
- async load(id, options) {
71
- if (options?.ssr || disabled) {
72
- return;
73
- }
74
- if (id === 'virtual:svelte-inspector-options') {
75
- return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
76
- } else if (id.startsWith(inspectorPath)) {
77
- // read file ourselves to avoid getting shut out by vites fs.allow check
78
- const file = idToFile(id);
79
- if (fs.existsSync(file)) {
80
- return await fs.promises.readFile(file, 'utf-8');
81
- } else {
82
- log.error(`failed to find file for svelte-inspector: ${file}, referenced by id ${id}.`);
83
- }
84
- }
85
- },
86
-
87
- transform(code: string, id: string, options?: { ssr?: boolean }) {
88
- if (options?.ssr || disabled || !appendTo) {
89
- return;
90
- }
91
- if (id.endsWith(appendTo)) {
92
- return { code: `${code}\nimport 'virtual:svelte-inspector-path:load-inspector.js'` };
93
- }
94
- },
95
- transformIndexHtml(html) {
96
- if (disabled || appendTo) {
97
- return;
98
- }
99
- return {
100
- html,
101
- tags: [
102
- {
103
- tag: 'script',
104
- injectTo: 'body',
105
- attrs: {
106
- type: 'module',
107
- // /@id/ is needed, otherwise the virtual: is seen as protocol by browser and cors error happens
108
- src: '/@id/virtual:svelte-inspector-path:load-inspector.js'
109
- }
110
- }
111
- ]
112
- };
113
- }
114
- };
115
- }
@@ -1,13 +0,0 @@
1
- const FS_PREFIX = `/@fs/`;
2
- const IS_WINDOWS = process.platform === 'win32';
3
- const queryRE = /\?.*$/s;
4
- const hashRE = /#.*$/s;
5
-
6
- export function idToFile(id: string): string {
7
- // strip /@fs/ but keep leading / on non-windows
8
- if (id.startsWith(FS_PREFIX)) {
9
- id = id = id.slice(IS_WINDOWS ? FS_PREFIX.length : FS_PREFIX.length - 1);
10
- }
11
- // strip query and hash
12
- return id.replace(hashRE, '').replace(queryRE, '');
13
- }