Skip to content

Commit 94ac814

Browse files
committed
mostly working, but hacky raw/direct need work
1 parent bca41f2 commit 94ac814

21 files changed

+611
-562
lines changed

packages/vite-plugin-svelte-inspector/src/index.js

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export function svelteInspector(options) {
3333
apply: 'serve',
3434
enforce: 'pre',
3535

36+
applyToEnvironment(env) {
37+
return env.config.consumer === 'client';
38+
},
39+
3640
configResolved(config) {
3741
viteConfig = config;
3842
const environmentOptions = parseEnvironmentOptions(config);
@@ -43,7 +47,7 @@ export function svelteInspector(options) {
4347
}
4448

4549
// Handle config from svelte.config.js through vite-plugin-svelte
46-
const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte');
50+
const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte:config');
4751
const configFileOptions = vps?.api?.options?.inspector;
4852

4953
// vite-plugin-svelte can only pass options through it's `api` instead of `options`.
@@ -69,42 +73,53 @@ export function svelteInspector(options) {
6973
base: config.base?.replace(/\/$/, '') || ''
7074
};
7175
},
72-
73-
async resolveId(importee, _, options) {
74-
if (options?.ssr || disabled) {
75-
return;
76-
}
77-
if (importee.startsWith('virtual:svelte-inspector-options')) {
78-
return importee;
79-
} else if (importee.startsWith('virtual:svelte-inspector-path:')) {
80-
return importee.replace('virtual:svelte-inspector-path:', inspectorPath);
76+
resolveId: {
77+
filter: {
78+
id: /^virtual:svelte-inspector-/
79+
},
80+
async handler(importee, _, options) {
81+
if (options?.ssr || disabled) {
82+
return;
83+
}
84+
if (importee.startsWith('virtual:svelte-inspector-options')) {
85+
return importee;
86+
} else if (importee.startsWith('virtual:svelte-inspector-path:')) {
87+
return importee.replace('virtual:svelte-inspector-path:', inspectorPath);
88+
}
8189
}
8290
},
83-
84-
async load(id, options) {
85-
if (options?.ssr || disabled) {
86-
return;
87-
}
88-
if (id === 'virtual:svelte-inspector-options') {
89-
return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
90-
} else if (id.startsWith(inspectorPath)) {
91-
// read file ourselves to avoid getting shut out by vites fs.allow check
92-
const file = cleanUrl(id);
93-
if (fs.existsSync(id)) {
94-
return await fs.promises.readFile(file, 'utf-8');
95-
} else {
96-
viteConfig.logger.error(
97-
`[vite-plugin-svelte-inspector] failed to find svelte-inspector: ${id}`
98-
);
91+
load: {
92+
filter: {
93+
id: {
94+
include: [`${inspectorPath}/**`, /^virtual:svelte-inspector-options$/],
95+
exclude: [/style&lang\.css$/]
96+
}
97+
},
98+
async handler(id) {
99+
if (disabled) {
100+
return;
101+
}
102+
if (id === 'virtual:svelte-inspector-options') {
103+
return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
104+
} else if (id.startsWith(inspectorPath)) {
105+
// read file ourselves to avoid getting shut out by vites fs.allow check
106+
const file = cleanUrl(id);
107+
if (fs.existsSync(id)) {
108+
return await fs.promises.readFile(file, 'utf-8');
109+
} else {
110+
viteConfig.logger.error(
111+
`[vite-plugin-svelte-inspector] failed to find svelte-inspector: ${id}`
112+
);
113+
}
99114
}
100115
}
101116
},
102-
103-
transform(code, id, options) {
104-
if (options?.ssr || disabled) {
105-
return;
106-
}
107-
if (id.includes('vite/dist/client/client.mjs')) {
117+
transform: {
118+
filter: { id: /vite\/dist\/client\/client\.mjs(?:\?|$)/ },
119+
handler(code) {
120+
if (disabled) {
121+
return;
122+
}
108123
return { code: `${code}\nimport('virtual:svelte-inspector-path:load-inspector.js')` };
109124
}
110125
}

packages/vite-plugin-svelte/src/handle-hot-update.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function handleHotUpdate(compileSvelte, ctx, svelteRequest, cache,
2424
const { read, server, modules } = ctx;
2525

2626
const cachedJS = cache.getJS(svelteRequest);
27-
const cachedCss = cache.getCSS(svelteRequest.cssId);
27+
const cachedCss = cache.getCSS(svelteRequest);
2828

2929
const content = await read();
3030
/** @type {import('./types/compile.d.ts').CompileData} */

packages/vite-plugin-svelte/src/index-modular.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)