@sveltejs/vite-plugin-svelte 1.0.0-next.41 → 1.0.0-next.44
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.
- package/README.md +5 -5
- package/dist/index.cjs +281 -153
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +47 -2
- package/dist/index.js +280 -152
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
- package/src/index.ts +162 -135
- package/src/ui/inspector/Inspector.svelte +245 -0
- package/src/ui/inspector/load-inspector.js +15 -0
- package/src/ui/inspector/plugin.ts +106 -0
- package/src/utils/compile.ts +4 -0
- package/src/utils/hash.ts +1 -1
- package/src/utils/load-svelte-config.ts +3 -0
- package/src/utils/options.ts +79 -26
- package/src/utils/watch.ts +22 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/vite-plugin-svelte",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.44",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"import": "./dist/index.js",
|
|
20
20
|
"require": "./dist/index.cjs"
|
|
21
21
|
},
|
|
22
|
-
"./package.json": "./package.json"
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
"./src/ui/*": "./src/ui/*"
|
|
23
24
|
},
|
|
24
25
|
"engines": {
|
|
25
26
|
"node": "^14.13.1 || >= 16"
|
|
@@ -40,8 +41,9 @@
|
|
|
40
41
|
},
|
|
41
42
|
"homepage": "https://github.com/sveltejs/vite-plugin-svelte#readme",
|
|
42
43
|
"dependencies": {
|
|
43
|
-
"@rollup/pluginutils": "^4.2.
|
|
44
|
+
"@rollup/pluginutils": "^4.2.1",
|
|
44
45
|
"debug": "^4.3.4",
|
|
46
|
+
"deepmerge": "^4.2.2",
|
|
45
47
|
"kleur": "^4.1.4",
|
|
46
48
|
"magic-string": "^0.26.1",
|
|
47
49
|
"svelte-hmr": "^0.14.11"
|
|
@@ -60,16 +62,15 @@
|
|
|
60
62
|
"@types/debug": "^4.1.7",
|
|
61
63
|
"@types/diff-match-patch": "^1.0.32",
|
|
62
64
|
"diff-match-patch": "^1.0.5",
|
|
63
|
-
"esbuild": "^0.14.
|
|
64
|
-
"rollup": "^2.
|
|
65
|
-
"svelte": "^3.
|
|
66
|
-
"tsup": "^5.12.
|
|
67
|
-
"vite": "^2.9.
|
|
65
|
+
"esbuild": "^0.14.38",
|
|
66
|
+
"rollup": "^2.72.1",
|
|
67
|
+
"svelte": "^3.48.0",
|
|
68
|
+
"tsup": "^5.12.7",
|
|
69
|
+
"vite": "^2.9.8"
|
|
68
70
|
},
|
|
69
71
|
"scripts": {
|
|
70
|
-
"dev": "pnpm
|
|
72
|
+
"dev": "pnpm build:ci --sourcemap --watch src",
|
|
71
73
|
"build:ci": "rimraf dist && tsup-node src/index.ts --format esm,cjs --no-splitting --target node14",
|
|
72
|
-
"build": "pnpm
|
|
73
|
-
}
|
|
74
|
-
"readme": "# @sveltejs/vite-plugin-svelte\n\nThe official [Svelte](https://svelte.dev) plugin for [Vite](https://vitejs.dev).\n\n## Usage\n\n```js\n// vite.config.js\nimport { defineConfig } from 'vite';\nimport { svelte } from '@sveltejs/vite-plugin-svelte';\n\nexport default defineConfig({\n\tplugins: [\n\t\tsvelte({\n\t\t\t/* plugin options */\n\t\t})\n\t]\n});\n```\n\n## Documentation\n\n- [Plugin options](../../docs/config.md)\n- [FAQ](../../docs/faq.md)\n\n## License\n\n[MIT](./LICENSE)\n"
|
|
74
|
+
"build": "pnpm build:ci --dts --sourcemap"
|
|
75
|
+
}
|
|
75
76
|
}
|
package/src/index.ts
CHANGED
|
@@ -20,8 +20,19 @@ import { resolveViaPackageJsonSvelte } from './utils/resolve';
|
|
|
20
20
|
import { PartialResolvedId } from 'rollup';
|
|
21
21
|
import { toRollupError } from './utils/error';
|
|
22
22
|
import { saveSvelteMetadata } from './utils/optimizer';
|
|
23
|
+
import { svelteInspector } from './ui/inspector/plugin';
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
interface PluginAPI {
|
|
26
|
+
/**
|
|
27
|
+
* must not be modified, should not be used outside of vite-plugin-svelte repo
|
|
28
|
+
* @internal
|
|
29
|
+
* @experimental
|
|
30
|
+
*/
|
|
31
|
+
options?: ResolvedOptions;
|
|
32
|
+
// TODO expose compile cache here so other utility plugins can use it
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
25
36
|
if (process.env.DEBUG != null) {
|
|
26
37
|
log.setLevel('debug');
|
|
27
38
|
}
|
|
@@ -40,155 +51,171 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
|
|
|
40
51
|
/* eslint-enable no-unused-vars */
|
|
41
52
|
|
|
42
53
|
let resolvedSvelteSSR: Promise<PartialResolvedId | null>;
|
|
54
|
+
const api: PluginAPI = {};
|
|
55
|
+
const plugins: Plugin[] = [
|
|
56
|
+
{
|
|
57
|
+
name: 'vite-plugin-svelte',
|
|
58
|
+
// make sure our resolver runs before vite internal resolver to resolve svelte field correctly
|
|
59
|
+
enforce: 'pre',
|
|
60
|
+
api,
|
|
61
|
+
async config(config, configEnv): Promise<Partial<UserConfig>> {
|
|
62
|
+
// setup logger
|
|
63
|
+
if (process.env.DEBUG) {
|
|
64
|
+
log.setLevel('debug');
|
|
65
|
+
} else if (config.logLevel) {
|
|
66
|
+
log.setLevel(config.logLevel);
|
|
67
|
+
}
|
|
68
|
+
// @ts-expect-error temporarily lend the options variable until fixed in configResolved
|
|
69
|
+
options = await preResolveOptions(inlineOptions, config, configEnv);
|
|
70
|
+
// extra vite config
|
|
71
|
+
const extraViteConfig = buildExtraViteConfig(options, config);
|
|
72
|
+
log.debug('additional vite config', extraViteConfig);
|
|
73
|
+
return extraViteConfig;
|
|
74
|
+
},
|
|
43
75
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
// @ts-expect-error temporarily lend the options variable until fixed in configResolved
|
|
56
|
-
options = await preResolveOptions(inlineOptions, config, configEnv);
|
|
57
|
-
// extra vite config
|
|
58
|
-
const extraViteConfig = buildExtraViteConfig(options, config);
|
|
59
|
-
log.debug('additional vite config', extraViteConfig);
|
|
60
|
-
return extraViteConfig;
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
async configResolved(config) {
|
|
64
|
-
options = resolveOptions(options, config);
|
|
65
|
-
patchResolvedViteConfig(config, options);
|
|
66
|
-
requestParser = buildIdParser(options);
|
|
67
|
-
compileSvelte = createCompileSvelte(options);
|
|
68
|
-
viteConfig = config;
|
|
69
|
-
log.debug('resolved options', options);
|
|
70
|
-
},
|
|
76
|
+
async configResolved(config) {
|
|
77
|
+
options = resolveOptions(options, config);
|
|
78
|
+
patchResolvedViteConfig(config, options);
|
|
79
|
+
requestParser = buildIdParser(options);
|
|
80
|
+
compileSvelte = createCompileSvelte(options);
|
|
81
|
+
viteConfig = config;
|
|
82
|
+
// TODO deep clone to avoid mutability from outside?
|
|
83
|
+
api.options = options;
|
|
84
|
+
log.debug('resolved options', options);
|
|
85
|
+
},
|
|
71
86
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
async buildStart() {
|
|
88
|
+
if (!options.experimental?.prebundleSvelteLibraries) return;
|
|
89
|
+
const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
|
|
90
|
+
if (isSvelteMetadataChanged) {
|
|
91
|
+
// Force Vite to optimize again. Although we mutate the config here, it works because
|
|
92
|
+
// Vite's optimizer runs after `buildStart()`.
|
|
93
|
+
viteConfig.server.force = true;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
81
96
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
configureServer(server) {
|
|
98
|
+
// eslint-disable-next-line no-unused-vars
|
|
99
|
+
options.server = server;
|
|
100
|
+
setupWatchers(options, cache, requestParser);
|
|
101
|
+
},
|
|
87
102
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
103
|
+
load(id, opts) {
|
|
104
|
+
// @ts-expect-error anticipate vite changing second parameter as options object
|
|
105
|
+
// see https://github.com/vitejs/vite/discussions/5109
|
|
106
|
+
const ssr: boolean = opts === true || opts?.ssr;
|
|
107
|
+
const svelteRequest = requestParser(id, !!ssr);
|
|
108
|
+
if (svelteRequest) {
|
|
109
|
+
const { filename, query } = svelteRequest;
|
|
110
|
+
// virtual css module
|
|
111
|
+
if (query.svelte && query.type === 'style') {
|
|
112
|
+
const css = cache.getCSS(svelteRequest);
|
|
113
|
+
if (css) {
|
|
114
|
+
log.debug(`load returns css for ${filename}`);
|
|
115
|
+
return css;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// prevent vite asset plugin from loading files as url that should be compiled in transform
|
|
119
|
+
if (viteConfig.assetsInclude(filename)) {
|
|
120
|
+
log.debug(`load returns raw content for ${filename}`);
|
|
121
|
+
return fs.readFileSync(filename, 'utf-8');
|
|
101
122
|
}
|
|
102
123
|
}
|
|
103
|
-
|
|
104
|
-
if (viteConfig.assetsInclude(filename)) {
|
|
105
|
-
log.debug(`load returns raw content for ${filename}`);
|
|
106
|
-
return fs.readFileSync(filename, 'utf-8');
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
},
|
|
124
|
+
},
|
|
110
125
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
126
|
+
async resolveId(importee, importer, opts) {
|
|
127
|
+
const ssr = !!opts?.ssr;
|
|
128
|
+
const svelteRequest = requestParser(importee, ssr);
|
|
129
|
+
if (svelteRequest?.query.svelte) {
|
|
130
|
+
if (svelteRequest.query.type === 'style') {
|
|
131
|
+
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
|
|
132
|
+
// see https://github.com/sveltejs/vite-plugin-svelte/issues/14
|
|
133
|
+
log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
|
|
134
|
+
return svelteRequest.cssId;
|
|
135
|
+
}
|
|
136
|
+
log.debug(`resolveId resolved ${importee}`);
|
|
137
|
+
return importee; // query with svelte tag, an id we generated, no need for further analysis
|
|
120
138
|
}
|
|
121
|
-
log.debug(`resolveId resolved ${importee}`);
|
|
122
|
-
return importee; // query with svelte tag, an id we generated, no need for further analysis
|
|
123
|
-
}
|
|
124
139
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
+
if (ssr && importee === 'svelte') {
|
|
141
|
+
if (!resolvedSvelteSSR) {
|
|
142
|
+
resolvedSvelteSSR = this.resolve('svelte/ssr', undefined, { skipSelf: true }).then(
|
|
143
|
+
(svelteSSR) => {
|
|
144
|
+
log.debug('resolved svelte to svelte/ssr');
|
|
145
|
+
return svelteSSR;
|
|
146
|
+
},
|
|
147
|
+
(err) => {
|
|
148
|
+
log.debug(
|
|
149
|
+
'failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it',
|
|
150
|
+
err
|
|
151
|
+
);
|
|
152
|
+
return null; // returning null here leads to svelte getting resolved regularly
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return resolvedSvelteSSR;
|
|
140
157
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
158
|
+
try {
|
|
159
|
+
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
|
|
160
|
+
if (resolved) {
|
|
161
|
+
log.debug(
|
|
162
|
+
`resolveId resolved ${resolved} via package.json svelte field of ${importee}`
|
|
163
|
+
);
|
|
164
|
+
return resolved;
|
|
165
|
+
}
|
|
166
|
+
} catch (e) {
|
|
167
|
+
log.debug.once(
|
|
168
|
+
`error trying to resolve ${importee} from ${importer} via package.json svelte field `,
|
|
169
|
+
e
|
|
170
|
+
);
|
|
171
|
+
// this error most likely happens due to non-svelte related importee/importers so swallow it here
|
|
172
|
+
// in case it really way a svelte library, users will notice anyway. (lib not working due to failed resolve)
|
|
148
173
|
}
|
|
149
|
-
}
|
|
150
|
-
log.debug.once(
|
|
151
|
-
`error trying to resolve ${importee} from ${importer} via package.json svelte field `,
|
|
152
|
-
e
|
|
153
|
-
);
|
|
154
|
-
// this error most likely happens due to non-svelte related importee/importers so swallow it here
|
|
155
|
-
// in case it really way a svelte library, users will notice anyway. (lib not working due to failed resolve)
|
|
156
|
-
}
|
|
157
|
-
},
|
|
174
|
+
},
|
|
158
175
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
176
|
+
async transform(code, id, opts) {
|
|
177
|
+
const ssr = !!opts?.ssr;
|
|
178
|
+
const svelteRequest = requestParser(id, ssr);
|
|
179
|
+
if (!svelteRequest || svelteRequest.query.svelte) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
let compileData;
|
|
183
|
+
try {
|
|
184
|
+
compileData = await compileSvelte(svelteRequest, code, options);
|
|
185
|
+
} catch (e) {
|
|
186
|
+
throw toRollupError(e, options);
|
|
187
|
+
}
|
|
188
|
+
logCompilerWarnings(compileData.compiled.warnings, options);
|
|
189
|
+
cache.update(compileData);
|
|
190
|
+
if (compileData.dependencies?.length && options.server) {
|
|
191
|
+
compileData.dependencies.forEach((d) => {
|
|
192
|
+
ensureWatchedFile(options.server!.watcher, d, options.root);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
log.debug(`transform returns compiled js for ${svelteRequest.filename}`);
|
|
196
|
+
return {
|
|
197
|
+
...compileData.compiled.js,
|
|
198
|
+
meta: {
|
|
199
|
+
vite: {
|
|
200
|
+
lang: compileData.lang
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
},
|
|
181
205
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
206
|
+
handleHotUpdate(ctx: HmrContext): void | Promise<Array<ModuleNode> | void> {
|
|
207
|
+
if (!options.hot || !options.emitCss) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
|
|
211
|
+
if (svelteRequest) {
|
|
212
|
+
return handleHotUpdate(compileSvelte, ctx, svelteRequest, cache, options);
|
|
213
|
+
}
|
|
189
214
|
}
|
|
190
215
|
}
|
|
191
|
-
|
|
216
|
+
];
|
|
217
|
+
plugins.push(svelteInspector());
|
|
218
|
+
return plugins.filter(Boolean);
|
|
192
219
|
}
|
|
193
220
|
|
|
194
221
|
export {
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
// do not use TS here so that this component works in non-ts projects too
|
|
3
|
+
import { onMount } from 'svelte';
|
|
4
|
+
// eslint-disable-next-line node/no-missing-import
|
|
5
|
+
import options from 'virtual:svelte-inspector-options';
|
|
6
|
+
const toggle_combo = options.toggleKeyCombo?.toLowerCase().split('-');
|
|
7
|
+
|
|
8
|
+
let enabled = false;
|
|
9
|
+
|
|
10
|
+
const icon = `data:image/svg+xml;base64,${btoa(
|
|
11
|
+
`
|
|
12
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="21" height="25" viewBox="0 0 107 128">
|
|
13
|
+
<title>svelte-inspector-logo</title>
|
|
14
|
+
<path d="M94.1566,22.8189c-10.4-14.8851-30.94-19.2971-45.7914-9.8348L22.2825,29.6078A29.9234,29.9234,0,0,0,8.7639,49.6506a31.5136,31.5136,0,0,0,3.1076,20.2318A30.0061,30.0061,0,0,0,7.3953,81.0653a31.8886,31.8886,0,0,0,5.4473,24.1157c10.4022,14.8865,30.9423,19.2966,45.7914,9.8348L84.7167,98.3921A29.9177,29.9177,0,0,0,98.2353,78.3493,31.5263,31.5263,0,0,0,95.13,58.117a30,30,0,0,0,4.4743-11.1824,31.88,31.88,0,0,0-5.4473-24.1157" style="fill:#ff3e00"/><path d="M45.8171,106.5815A20.7182,20.7182,0,0,1,23.58,98.3389a19.1739,19.1739,0,0,1-3.2766-14.5025,18.1886,18.1886,0,0,1,.6233-2.4357l.4912-1.4978,1.3363.9815a33.6443,33.6443,0,0,0,10.203,5.0978l.9694.2941-.0893.9675a5.8474,5.8474,0,0,0,1.052,3.8781,6.2389,6.2389,0,0,0,6.6952,2.485,5.7449,5.7449,0,0,0,1.6021-.7041L69.27,76.281a5.4306,5.4306,0,0,0,2.4506-3.631,5.7948,5.7948,0,0,0-.9875-4.3712,6.2436,6.2436,0,0,0-6.6978-2.4864,5.7427,5.7427,0,0,0-1.6.7036l-9.9532,6.3449a19.0329,19.0329,0,0,1-5.2965,2.3259,20.7181,20.7181,0,0,1-22.2368-8.2427,19.1725,19.1725,0,0,1-3.2766-14.5024,17.9885,17.9885,0,0,1,8.13-12.0513L55.8833,23.7472a19.0038,19.0038,0,0,1,5.3-2.3287A20.7182,20.7182,0,0,1,83.42,29.6611a19.1739,19.1739,0,0,1,3.2766,14.5025,18.4,18.4,0,0,1-.6233,2.4357l-.4912,1.4978-1.3356-.98a33.6175,33.6175,0,0,0-10.2037-5.1l-.9694-.2942.0893-.9675a5.8588,5.8588,0,0,0-1.052-3.878,6.2389,6.2389,0,0,0-6.6952-2.485,5.7449,5.7449,0,0,0-1.6021.7041L37.73,51.719a5.4218,5.4218,0,0,0-2.4487,3.63,5.7862,5.7862,0,0,0,.9856,4.3717,6.2437,6.2437,0,0,0,6.6978,2.4864,5.7652,5.7652,0,0,0,1.602-.7041l9.9519-6.3425a18.978,18.978,0,0,1,5.2959-2.3278,20.7181,20.7181,0,0,1,22.2368,8.2427,19.1725,19.1725,0,0,1,3.2766,14.5024,17.9977,17.9977,0,0,1-8.13,12.0532L51.1167,104.2528a19.0038,19.0038,0,0,1-5.3,2.3287" style="fill:#fff"/>
|
|
15
|
+
<polygon points="0,0 15,40 40,20" stroke="#ff3e00" fill="#ff3e00"></polygon>
|
|
16
|
+
</svg>
|
|
17
|
+
`
|
|
18
|
+
.replace(/[\n\r\t\s]+/g, ' ')
|
|
19
|
+
.trim()
|
|
20
|
+
)}`;
|
|
21
|
+
|
|
22
|
+
// location of code in file
|
|
23
|
+
let file_loc;
|
|
24
|
+
// cursor pos and width for file_loc overlay positioning
|
|
25
|
+
let x, y, w;
|
|
26
|
+
|
|
27
|
+
let active_el;
|
|
28
|
+
let toggle_el;
|
|
29
|
+
|
|
30
|
+
let enabled_ts;
|
|
31
|
+
|
|
32
|
+
$: show_toggle =
|
|
33
|
+
options.showToggleButton === 'always' || (options.showToggleButton === 'active' && enabled);
|
|
34
|
+
|
|
35
|
+
function mousemove(event) {
|
|
36
|
+
x = event.x;
|
|
37
|
+
y = event.y;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function findMetaEl(el) {
|
|
41
|
+
while (el) {
|
|
42
|
+
const file = el.__svelte_meta?.loc?.file;
|
|
43
|
+
if (el !== toggle_el && file && !file.includes('node_modules/')) {
|
|
44
|
+
return el;
|
|
45
|
+
}
|
|
46
|
+
el = el.parentNode;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function mouseover(event) {
|
|
51
|
+
const el = findMetaEl(event.target);
|
|
52
|
+
if (options.customStyles && el !== active_el) {
|
|
53
|
+
if (active_el) {
|
|
54
|
+
active_el.classList.remove('svelte-inspector-active-target');
|
|
55
|
+
}
|
|
56
|
+
if (el) {
|
|
57
|
+
el.classList.add('svelte-inspector-active-target');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (el) {
|
|
61
|
+
const { file, line, column } = el.__svelte_meta.loc;
|
|
62
|
+
file_loc = `${file}:${line + 1}:${column + 1}`;
|
|
63
|
+
} else {
|
|
64
|
+
file_loc = null;
|
|
65
|
+
}
|
|
66
|
+
active_el = el;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function click(event) {
|
|
70
|
+
if (file_loc) {
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
event.stopPropagation();
|
|
73
|
+
event.stopImmediatePropagation();
|
|
74
|
+
fetch(`/__open-in-editor?file=${encodeURIComponent(file_loc)}`);
|
|
75
|
+
if (options.holdMode && is_holding()) {
|
|
76
|
+
disable();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function is_key_active(key, event) {
|
|
82
|
+
switch (key) {
|
|
83
|
+
case 'shift':
|
|
84
|
+
case 'control':
|
|
85
|
+
case 'alt':
|
|
86
|
+
case 'meta':
|
|
87
|
+
return event.getModifierState(key.charAt(0).toUpperCase() + key.slice(1));
|
|
88
|
+
default:
|
|
89
|
+
return key === event.key.toLowerCase();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function is_combo(event) {
|
|
94
|
+
return toggle_combo?.every((key) => is_key_active(key, event));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function is_holding() {
|
|
98
|
+
return enabled_ts && Date.now() - enabled_ts > 250;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function keydown(event) {
|
|
102
|
+
if (event.repeat) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (is_combo(event)) {
|
|
107
|
+
toggle();
|
|
108
|
+
if (options.holdMode && enabled) {
|
|
109
|
+
enabled_ts = Date.now();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function keyup(event) {
|
|
115
|
+
if (event.repeat) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const k = event.key.toLowerCase();
|
|
119
|
+
if (enabled && is_holding() && toggle_combo.includes(k)) {
|
|
120
|
+
disable();
|
|
121
|
+
} else {
|
|
122
|
+
enabled_ts = null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function toggle() {
|
|
127
|
+
enabled ? disable() : enable();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function listeners(body, enabled) {
|
|
131
|
+
const l = enabled ? body.addEventListener : body.removeEventListener;
|
|
132
|
+
l('mousemove', mousemove);
|
|
133
|
+
l('mouseover', mouseover);
|
|
134
|
+
l('click', click, true);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function enable() {
|
|
138
|
+
enabled = true;
|
|
139
|
+
const b = document.body;
|
|
140
|
+
if (options.customStyles) {
|
|
141
|
+
b.classList.add('svelte-inspector-enabled');
|
|
142
|
+
}
|
|
143
|
+
listeners(b, enabled);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function disable() {
|
|
147
|
+
enabled = false;
|
|
148
|
+
enabled_ts = null;
|
|
149
|
+
const b = document.body;
|
|
150
|
+
listeners(b, enabled);
|
|
151
|
+
if (options.customStyles) {
|
|
152
|
+
b.classList.remove('svelte-inspector-enabled');
|
|
153
|
+
active_el?.classList.remove('svelte-inspector-active-target');
|
|
154
|
+
}
|
|
155
|
+
active_el = null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
onMount(() => {
|
|
159
|
+
const s = document.createElement('style');
|
|
160
|
+
s.setAttribute('type', 'text/css');
|
|
161
|
+
s.setAttribute('id', 'svelte-inspector-style');
|
|
162
|
+
s.textContent = `:root { --svelte-inspector-icon: url(${icon})};`;
|
|
163
|
+
document.head.append(s);
|
|
164
|
+
if (toggle_combo) {
|
|
165
|
+
document.body.addEventListener('keydown', keydown);
|
|
166
|
+
if (options.holdMode) {
|
|
167
|
+
document.body.addEventListener('keyup', keyup);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return () => {
|
|
171
|
+
// make sure we get rid of everything
|
|
172
|
+
disable();
|
|
173
|
+
const s = document.head.querySelector('#svelte-inspector-style');
|
|
174
|
+
if (s) {
|
|
175
|
+
document.head.removeChild(s);
|
|
176
|
+
}
|
|
177
|
+
if (toggle_combo) {
|
|
178
|
+
document.body.removeEventListener('keydown', keydown);
|
|
179
|
+
if (options.holdMode) {
|
|
180
|
+
document.body.addEventListener('keyup', keyup);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
</script>
|
|
186
|
+
|
|
187
|
+
{#if show_toggle}
|
|
188
|
+
<div
|
|
189
|
+
class="svelte-inspector-toggle"
|
|
190
|
+
class:enabled
|
|
191
|
+
style={`background-image: var(--svelte-inspector-icon);${options.toggleButtonPos
|
|
192
|
+
.split('-')
|
|
193
|
+
.map((p) => `${p}: 8px;`)
|
|
194
|
+
.join('')}`}
|
|
195
|
+
on:click={() => toggle()}
|
|
196
|
+
bind:this={toggle_el}
|
|
197
|
+
/>
|
|
198
|
+
{/if}
|
|
199
|
+
{#if enabled && file_loc}
|
|
200
|
+
<div
|
|
201
|
+
class="svelte-inspector-overlay"
|
|
202
|
+
style:left="{Math.min(x + 3, document.body.clientWidth - w - 10)}px"
|
|
203
|
+
style:top="{y + 30}px"
|
|
204
|
+
bind:offsetWidth={w}
|
|
205
|
+
>
|
|
206
|
+
{file_loc}
|
|
207
|
+
</div>
|
|
208
|
+
{/if}
|
|
209
|
+
|
|
210
|
+
<style>
|
|
211
|
+
:global(body.svelte-inspector-enabled *) {
|
|
212
|
+
cursor: var(--svelte-inspector-icon), crosshair !important;
|
|
213
|
+
}
|
|
214
|
+
:global(.svelte-inspector-active-target) {
|
|
215
|
+
outline: 2px dashed #ff3e00 !important;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.svelte-inspector-overlay {
|
|
219
|
+
position: fixed;
|
|
220
|
+
background-color: rgba(0, 0, 0, 0.8);
|
|
221
|
+
color: #fff;
|
|
222
|
+
padding: 2px 4px;
|
|
223
|
+
border-radius: 5px;
|
|
224
|
+
z-index: 999999;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.svelte-inspector-toggle {
|
|
228
|
+
border: 1px solid #ff3e00;
|
|
229
|
+
border-radius: 8px;
|
|
230
|
+
position: fixed;
|
|
231
|
+
height: 32px;
|
|
232
|
+
width: 32px;
|
|
233
|
+
background-color: white;
|
|
234
|
+
background-position: center;
|
|
235
|
+
background-repeat: no-repeat;
|
|
236
|
+
cursor: pointer;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.svelte-inspector-toggle:not(.enabled) {
|
|
240
|
+
filter: grayscale(1);
|
|
241
|
+
}
|
|
242
|
+
.svelte-inspector-toggle:hover {
|
|
243
|
+
background-color: #facece;
|
|
244
|
+
}
|
|
245
|
+
</style>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// eslint-disable-next-line node/no-missing-import
|
|
2
|
+
import Inspector from 'virtual:svelte-inspector-path:Inspector.svelte';
|
|
3
|
+
|
|
4
|
+
function create_inspector_host() {
|
|
5
|
+
const id = 'svelte-inspector-host';
|
|
6
|
+
if (document.getElementById(id) != null) {
|
|
7
|
+
throw new Error('svelte-inspector-host element already exists');
|
|
8
|
+
}
|
|
9
|
+
const el = document.createElement('div');
|
|
10
|
+
el.setAttribute('id', id);
|
|
11
|
+
document.getElementsByTagName('body')[0].appendChild(el);
|
|
12
|
+
return el;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
new Inspector({ target: create_inspector_host() });
|