Skip to content

Commit ec16340

Browse files
authored
fix: inspector read file for vite 3.1 (#423)
* fix: load inspector files with a clean filename; update lock and types * fix: one clean is enough * docs: remove comment
1 parent bac82c1 commit ec16340

File tree

5 files changed

+69
-45
lines changed

5 files changed

+69
-45
lines changed

.changeset/short-clocks-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix svelte-inspector import for vite 3.1

packages/vite-plugin-svelte/src/ui/inspector/plugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { InspectorOptions } from '../../utils/options';
44
import path from 'path';
55
import { fileURLToPath } from 'url';
66
import fs from 'fs';
7+
import { idToFile } from './utils';
78

89
const defaultInspectorOptions: InspectorOptions = {
910
toggleKeyCombo: process.platform === 'win32' ? 'control-shift' : 'meta-shift',
@@ -71,7 +72,12 @@ export function svelteInspector(): Plugin {
7172
return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
7273
} else if (id.startsWith(inspectorPath)) {
7374
// read file ourselves to avoid getting shut out by vites fs.allow check
74-
return await fs.promises.readFile(id, 'utf-8');
75+
const file = idToFile(id);
76+
if (fs.existsSync(file)) {
77+
return await fs.promises.readFile(file, 'utf-8');
78+
} else {
79+
log.error(`failed to find file for svelte-inspector: ${file}, referenced by id ${id}.`);
80+
}
7581
}
7682
},
7783

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function preResolveOptions(
138138
isServe: viteEnv.command === 'serve',
139139
isDebug: process.env.DEBUG != null
140140
};
141-
const merged = mergeConfigs<Partial<PreResolvedOptions> | undefined>(
141+
const merged = mergeConfigs<PreResolvedOptions>(
142142
defaultOptions,
143143
svelteConfig,
144144
inlineOptions,
@@ -152,15 +152,15 @@ export async function preResolveOptions(
152152
return merged;
153153
}
154154

155-
function mergeConfigs<T>(...configs: T[]): ResolvedOptions {
156-
let result = {} as T;
157-
for (const config of configs.filter(Boolean)) {
158-
result = deepmerge<T>(result, config, {
155+
function mergeConfigs<T>(...configs: (Partial<T> | undefined)[]): T {
156+
let result: Partial<T> = {};
157+
for (const config of configs.filter((x) => x != null)) {
158+
result = deepmerge<T>(result, config!, {
159159
// replace arrays
160160
arrayMerge: (target: any[], source: any[]) => source ?? target
161161
});
162162
}
163-
return result as ResolvedOptions;
163+
return result as T;
164164
}
165165

166166
// used in configResolved phase, merges a contextual default config, pre-resolved options, and some preprocessors.
@@ -180,7 +180,7 @@ export function resolveOptions(
180180
root: viteConfig.root,
181181
isProduction: viteConfig.isProduction
182182
};
183-
const merged: ResolvedOptions = mergeConfigs(defaultOptions, preResolveOptions, extraOptions);
183+
const merged = mergeConfigs<ResolvedOptions>(defaultOptions, preResolveOptions, extraOptions);
184184

185185
removeIgnoredOptions(merged);
186186
addSvelteKitOptions(merged);

pnpm-lock.yaml

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)