Skip to content

Commit 5202b0a

Browse files
bluwydominikg
andauthored
feat: remove SvelteKit specific handling (#638)
* feat: import inspector from vite client * fix: remove kit options handling * fix: remove kit prop validation * chore: add changeset * fix: fail * fix: wait network idle * docs: remove internal information from changeset --------- Co-authored-by: Dominik G <[email protected]>
1 parent 73a2c65 commit 5202b0a

File tree

5 files changed

+12
-66
lines changed

5 files changed

+12
-66
lines changed

.changeset/smooth-llamas-reflect.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': minor
3+
---
4+
5+
Remove internal SvelteKit specific handling

docs/config.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,6 @@ export default {
338338
* inject custom styles when inspector is active
339339
*/
340340
customStyles?: boolean;
341-
342-
/**
343-
* append an import to the module id ending with `appendTo` instead of adding a script into body
344-
* useful for frameworks that do not support trannsformIndexHtml hook
345-
*
346-
* WARNING: only set this if you know exactly what it does.
347-
* Regular users of vite-plugin-svelte or SvelteKit do not need it
348-
*/
349-
appendTo?: string;
350341
}
351342
```
352343

packages/e2e-tests/inspector-kit/__tests__/inspector.kit.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { getEl, getText, isBuild } from '~utils';
1+
import { getEl, getText, isBuild, page } from '~utils';
22

33
describe('inspector-kit', () => {
44
it('should render page', async () => {
55
expect(await getText('h1')).toBe('Hello Inspector!');
66
});
77
if (!isBuild) {
88
it('should show inspector toggle during dev', async () => {
9+
await page.waitForLoadState('networkidle');
910
expect(await getEl('#svelte-inspector-toggle')).not.toBe(null);
1011
});
1112
} else {

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

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export function svelteInspector(): Plugin {
2525
const inspectorPath = getInspectorPath();
2626
log.debug.enabled && log.debug(`svelte inspector path: ${inspectorPath}`);
2727
let inspectorOptions: InspectorOptions;
28-
let appendTo: string | undefined;
2928
let disabled = false;
3029

3130
return {
@@ -45,13 +44,6 @@ export function svelteInspector(): Plugin {
4544
...defaultInspectorOptions,
4645
...options
4746
};
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;
5547
},
5648

5749
async resolveId(importee: string, importer, options) {
@@ -84,32 +76,13 @@ export function svelteInspector(): Plugin {
8476
}
8577
},
8678

87-
transform(code: string, id: string, options?: { ssr?: boolean }) {
88-
if (options?.ssr || disabled || !appendTo) {
79+
transform(code, id, options) {
80+
if (options?.ssr || disabled) {
8981
return;
9082
}
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;
83+
if (id.includes('vite/dist/client/client.mjs')) {
84+
return { code: `${code}\nimport('virtual:svelte-inspector-path:load-inspector.js')` };
9885
}
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-
};
11386
}
11487
};
11588
}

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ const allowedPluginOptions = new Set([
4949

5050
const knownRootOptions = new Set(['extensions', 'compilerOptions', 'preprocess', 'onwarn']);
5151

52-
const allowedInlineOptions = new Set([
53-
'configFile',
54-
'kit', // only for internal use by sveltekit
55-
...allowedPluginOptions,
56-
...knownRootOptions
57-
]);
52+
const allowedInlineOptions = new Set(['configFile', ...allowedPluginOptions, ...knownRootOptions]);
5853

5954
export function validateInlineOptions(inlineOptions?: Partial<Options>) {
6055
const invalidKeys = Object.keys(inlineOptions || {}).filter(
@@ -203,7 +198,6 @@ export function resolveOptions(
203198

204199
removeIgnoredOptions(merged);
205200
handleDeprecatedOptions(merged);
206-
addSvelteKitOptions(merged);
207201
addExtraPreprocessors(merged, viteConfig);
208202
enforceOptionsForHmr(merged);
209203
enforceOptionsForProduction(merged);
@@ -291,15 +285,6 @@ function removeIgnoredOptions(options: ResolvedOptions) {
291285
}
292286
}
293287

294-
// some SvelteKit options need compilerOptions to work, so set them here.
295-
function addSvelteKitOptions(options: ResolvedOptions) {
296-
// @ts-expect-error kit is not typed to avoid dependency on sveltekit
297-
if (options?.kit != null && options.compilerOptions.hydratable == null) {
298-
log.debug(`Setting compilerOptions.hydratable = true for SvelteKit`);
299-
options.compilerOptions.hydratable = true;
300-
}
301-
}
302-
303288
function handleDeprecatedOptions(options: ResolvedOptions) {
304289
if ((options.experimental as any)?.prebundleSvelteLibraries) {
305290
options.prebundleSvelteLibraries = (options.experimental as any)?.prebundleSvelteLibraries;
@@ -791,15 +776,6 @@ export interface InspectorOptions {
791776
* inject custom styles when inspector is active
792777
*/
793778
customStyles?: boolean;
794-
795-
/**
796-
* append an import to the module id ending with `appendTo` instead of adding a script into body
797-
* useful for frameworks that do not support trannsformIndexHtml hook
798-
*
799-
* WARNING: only set this if you know exactly what it does.
800-
* Regular users of vite-plugin-svelte or SvelteKit do not need it
801-
*/
802-
appendTo?: string;
803779
}
804780

805781
export interface PreResolvedOptions extends Options {

0 commit comments

Comments
 (0)