@svelte-devtools/vite-plugin 0.1.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.
@@ -0,0 +1,15 @@
1
+ import type { Plugin } from 'vite';
2
+ export interface SvelteDevtoolsOptions {
3
+ allowedHosts?: string[];
4
+ }
5
+ /**
6
+ * Vite plugin for Svelte DevTools.
7
+ *
8
+ * For SvelteKit projects, also add the handle hook to hooks.server.ts:
9
+ *
10
+ * import { sequence } from '@sveltejs/kit/hooks';
11
+ * import { svelteDevtoolsHandle } from '@svelte-devtools/kit';
12
+ * export const handle = sequence(svelteDevtoolsHandle);
13
+ */
14
+ export declare function svelteDevtools(options?: SvelteDevtoolsOptions): Plugin;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAUnC,MAAM,WAAW,qBAAqB;IACpC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAqBD;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,qBAA0B,GAAG,MAAM,CA2G1E"}
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
1
+ import { existsSync, readFileSync } from 'fs';
2
+ import { resolve } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ const __dirname = fileURLToPath(new URL('.', import.meta.url));
5
+ const overlayDir = resolve(__dirname, '..', '..', 'overlay', 'dist');
6
+ const VIRTUAL_ID = 'virtual:svelte-devtools';
7
+ const RESOLVED_ID = '\0virtual:svelte-devtools';
8
+ const OVERLAY_VIRTUAL_ID = 'virtual:svelte-devtools-overlay';
9
+ const OVERLAY_RESOLVED = '\0virtual:svelte-devtools-overlay';
10
+ function normalizeHost(host) {
11
+ return host?.split(':')[0]?.toLowerCase() ?? '';
12
+ }
13
+ function isAllowedHost(host, allowedHosts) {
14
+ if (!allowedHosts?.length)
15
+ return true;
16
+ const hostname = normalizeHost(host);
17
+ const hostWithPort = host?.toLowerCase() ?? '';
18
+ return allowedHosts.some((allowedHost) => {
19
+ const allowed = allowedHost.toLowerCase();
20
+ return allowed === '*' || allowed === hostname || allowed === hostWithPort;
21
+ });
22
+ }
23
+ function isDevtoolsVirtualRequest(url) {
24
+ return Boolean(url?.includes(`/@id/${VIRTUAL_ID}`));
25
+ }
26
+ /**
27
+ * Vite plugin for Svelte DevTools.
28
+ *
29
+ * For SvelteKit projects, also add the handle hook to hooks.server.ts:
30
+ *
31
+ * import { sequence } from '@sveltejs/kit/hooks';
32
+ * import { svelteDevtoolsHandle } from '@svelte-devtools/kit';
33
+ * export const handle = sequence(svelteDevtoolsHandle);
34
+ */
35
+ export function svelteDevtools(options = {}) {
36
+ let isSvelteKit = false;
37
+ let base = '/';
38
+ let shouldInjectForCurrentRequest = true;
39
+ return {
40
+ name: '@svelte-devtools/vite-plugin',
41
+ apply: 'serve',
42
+ configResolved(config) {
43
+ base = config.base ?? '/';
44
+ isSvelteKit = config.plugins.some((p) => typeof p.name === 'string' && p.name.includes('sveltekit'));
45
+ },
46
+ configureServer(server) {
47
+ server.middlewares.use((req, res, next) => {
48
+ const allowed = isAllowedHost(req.headers.host, options.allowedHosts);
49
+ shouldInjectForCurrentRequest = allowed;
50
+ if (!allowed && isDevtoolsVirtualRequest(req.url)) {
51
+ res.statusCode = 403;
52
+ res.end('[svelte-devtools] host is not allowed');
53
+ return;
54
+ }
55
+ next();
56
+ });
57
+ const overlayFile = resolve(overlayDir, 'overlay.js');
58
+ server.watcher.add(overlayFile);
59
+ server.watcher.on('change', (file) => {
60
+ if (file !== overlayFile)
61
+ return;
62
+ const mod = server.moduleGraph.getModuleById(OVERLAY_RESOLVED);
63
+ if (mod)
64
+ server.moduleGraph.invalidateModule(mod);
65
+ server.ws.send({ type: 'full-reload' });
66
+ });
67
+ },
68
+ resolveId(id) {
69
+ if (id === VIRTUAL_ID)
70
+ return RESOLVED_ID;
71
+ if (id === OVERLAY_VIRTUAL_ID)
72
+ return OVERLAY_RESOLVED;
73
+ },
74
+ load(id, opts) {
75
+ if (opts?.ssr)
76
+ return;
77
+ // ── Pre-built overlay Svelte 5 bundle ─────────────────────────────────
78
+ if (id === OVERLAY_RESOLVED) {
79
+ const p = resolve(overlayDir, 'overlay.js');
80
+ if (!existsSync(p)) {
81
+ return 'console.warn("[svelte-devtools] overlay.js missing — run: npm run build:overlay")';
82
+ }
83
+ return readFileSync(p, 'utf-8');
84
+ }
85
+ // ── Bootstrap module ─────────────────────────────────────────────────
86
+ // Imports the overlay + wires up SvelteKit store subscriptions so the
87
+ // devtools panel stays in sync with navigation and page data.
88
+ if (id === RESOLVED_ID) {
89
+ const kitCode = isSvelteKit
90
+ ? `
91
+ import('$app/stores').then(({ page, navigating }) => {
92
+ page.subscribe((v) => {
93
+ window.__sdt.page = v;
94
+ window.dispatchEvent(new CustomEvent('__sdt:update', { detail: { type: 'page' } }));
95
+ });
96
+ navigating.subscribe((v) => {
97
+ window.__sdt.navigating = v;
98
+ window.dispatchEvent(new CustomEvent('__sdt:update', { detail: { type: 'navigating' } }));
99
+ });
100
+ }).catch(() => {});
101
+ `
102
+ : '';
103
+ return `
104
+ import '${OVERLAY_VIRTUAL_ID}';
105
+
106
+ window.__sdt = window.__sdt || {
107
+ page: null,
108
+ navigating: null,
109
+ components: [],
110
+ };
111
+
112
+ ${kitCode}
113
+ `;
114
+ }
115
+ },
116
+ // For non-SvelteKit Vite apps, transformIndexHtml works fine.
117
+ // SvelteKit bypasses this — use @svelte-devtools/kit handle instead.
118
+ transformIndexHtml: {
119
+ order: 'post',
120
+ handler() {
121
+ if (isSvelteKit)
122
+ return []; // handled by the SvelteKit hook
123
+ if (!shouldInjectForCurrentRequest)
124
+ return [];
125
+ return [
126
+ {
127
+ tag: 'script',
128
+ attrs: { type: 'module', src: `${base}@id/${VIRTUAL_ID}` },
129
+ injectTo: 'head-prepend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AO;oBACK,kBAAkB;;;;;;;;YAQ1B,OAAO;SACV,CAAC;YACJ,CAAC;QACH,CAAC;QAED,8DAA8D;QAC9D,qEAAqE;QACrE,kBAAkB,EAAE;YAClB,KAAK,EAAE,MAAM;YACb,OAAO;gBACL,IAAI,WAAW;oBAAE,OAAO,EAAE,CAAC,CAAC,gCAAgC;gBAC5D,IAAI,CAAC,6BAA6B;oBAAE,OAAO,EAAE,CAAC;gBAC9C,OAAO;oBACL;wBACE,GAAG,EAAE,QAAQ;wBACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,IAAI,OAAO,UAAU,EAAE,EAAE;wBAC1D,QAAQ,EAAE,cAAuB;qBAClC;iBACF,CAAC;YACJ,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@svelte-devtools/vite-plugin",
3
+ "version": "0.1.0",
4
+ "description": "Vite plugin that adds a development overlay for inspecting Svelte 5 and SvelteKit apps",
5
+ "type": "module",
6
+ "keywords": ["svelte", "svelte5", "sveltekit", "devtools", "vite", "vite-plugin", "debug", "inspector"],
7
+ "author": "Chris Lentz",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/chrislentz/svelte-devtools.git",
12
+ "directory": "packages/vite-plugin"
13
+ },
14
+ "homepage": "https://github.com/chrislentz/svelte-devtools#readme",
15
+ "bugs": "https://github.com/chrislentz/svelte-devtools/issues",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": ["dist"],
23
+ "scripts": {
24
+ "build": "tsc --project tsconfig.build.json",
25
+ "prepublishOnly": "npm run build"
26
+ },
27
+ "peerDependencies": {
28
+ "vite": ">=5.0.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^20.0.0",
32
+ "typescript": "^5.6.0",
33
+ "vite": "^6.0.0"
34
+ }
35
+ }