@sveltejs/vite-plugin-svelte 2.2.0 → 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,97 +0,0 @@
1
- import { Plugin, normalizePath } from 'vite';
2
- import { log } from '../../utils/log';
3
- import path from 'path';
4
- import { fileURLToPath } from 'url';
5
- import fs from 'fs';
6
- import { idToFile } from './utils';
7
- import { defaultInspectorOptions, type InspectorOptions, parseEnvironmentOptions } from './options';
8
-
9
- function getInspectorPath() {
10
- const pluginPath = normalizePath(path.dirname(fileURLToPath(import.meta.url)));
11
- return pluginPath.replace(/\/vite-plugin-svelte\/dist$/, '/vite-plugin-svelte/src/ui/inspector/');
12
- }
13
-
14
- export function svelteInspector(): Plugin {
15
- const inspectorPath = getInspectorPath();
16
- log.debug.enabled && log.debug(`svelte inspector path: ${inspectorPath}`);
17
- let inspectorOptions: InspectorOptions;
18
- let disabled = false;
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) {
28
- log.warn('vite-plugin-svelte is missing, inspector disabled', undefined, 'inspector');
29
- disabled = true;
30
- return;
31
- }
32
- const configFileOptions = vps?.api?.options?.inspector;
33
- const environmentOptions = parseEnvironmentOptions(config);
34
- if (!configFileOptions && !environmentOptions) {
35
- log.debug('no options found, inspector disabled', undefined, 'inspector');
36
- disabled = true;
37
- return;
38
- }
39
- if (environmentOptions === true) {
40
- inspectorOptions = defaultInspectorOptions;
41
- } else {
42
- inspectorOptions = {
43
- ...defaultInspectorOptions,
44
- ...configFileOptions,
45
- ...(environmentOptions || {})
46
- };
47
- }
48
- inspectorOptions.__internal = {
49
- base: config.base?.replace(/\/$/, '') || ''
50
- };
51
- },
52
-
53
- async resolveId(importee: string, importer, options) {
54
- if (options?.ssr || disabled) {
55
- return;
56
- }
57
- if (importee.startsWith('virtual:svelte-inspector-options')) {
58
- return importee;
59
- } else if (importee.startsWith('virtual:svelte-inspector-path:')) {
60
- const resolved = importee.replace('virtual:svelte-inspector-path:', inspectorPath);
61
- log.debug.enabled &&
62
- log.debug(`resolved ${importee} with ${resolved}`, undefined, 'inspector');
63
- return resolved;
64
- }
65
- },
66
-
67
- async load(id, options) {
68
- if (options?.ssr || disabled) {
69
- return;
70
- }
71
- if (id === 'virtual:svelte-inspector-options') {
72
- return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
73
- } else if (id.startsWith(inspectorPath)) {
74
- // read file ourselves to avoid getting shut out by vites fs.allow check
75
- const file = idToFile(id);
76
- if (fs.existsSync(file)) {
77
- return await fs.promises.readFile(file, 'utf-8');
78
- } else {
79
- log.error(
80
- `failed to find file for svelte-inspector: ${file}, referenced by id ${id}.`,
81
- undefined,
82
- 'inspector'
83
- );
84
- }
85
- }
86
- },
87
-
88
- transform(code, id, options) {
89
- if (options?.ssr || disabled) {
90
- return;
91
- }
92
- if (id.includes('vite/dist/client/client.mjs')) {
93
- return { code: `${code}\nimport('virtual:svelte-inspector-path:load-inspector.js')` };
94
- }
95
- }
96
- };
97
- }
@@ -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
- }