Skip to content

Commit 2132b5f

Browse files
authored
fix: use different way to resolve inspector path (see #330) (#332)
* fix: use different way to resolve inspector path (see #330) * test: add tests for inspector with kit and vite templates * fix: use import.meta.url to determine inspector path * fix: prefix windows paths with /@fs/ * fix: windows path handling * fix: inline svg and read inspector file ourselves to prevent fs.allow interfering
1 parent ece05a9 commit 2132b5f

22 files changed

+345
-46
lines changed

.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ module.exports = {
6969
'no-console': 'error'
7070
}
7171
},
72+
{
73+
files: ['packages/vite-plugin-svelte/src/ui/inspector/load-inspector.js'],
74+
env: {
75+
browser: true
76+
}
77+
},
7278
{
7379
files: ['packages/e2e-tests/**', 'packages/playground/**'],
7480
rules: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getEl, getText, isBuild } from '../../testUtils';
2+
3+
describe('inspector-kit', () => {
4+
it('should render page', async () => {
5+
expect(await getText('h1')).toBe('Hello Inspector!');
6+
});
7+
if (!isBuild) {
8+
it('should show inspector toggle during dev', async () => {
9+
expect(await getEl('.svelte-inspector-toggle')).not.toBe(null);
10+
});
11+
} else {
12+
it('should not show inspector toggle during preview', async () => {
13+
expect(await getEl('.svelte-inspector-toggle')).toBe(null);
14+
});
15+
}
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "e2e-tests-inspector-kit",
3+
"private": true,
4+
"version": "0.0.0",
5+
"scripts": {
6+
"dev": "svelte-kit dev",
7+
"build": "svelte-kit build",
8+
"package": "svelte-kit package",
9+
"preview": "svelte-kit preview"
10+
},
11+
"devDependencies": {
12+
"@sveltejs/kit": "^1.0.0-next.326",
13+
"svelte": "^3.48.0"
14+
},
15+
"type": "module",
16+
"pnpm": {
17+
"overrides": {
18+
"@sveltejs/vite-plugin-svelte": "workspace:*"
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
%svelte.head%
7+
</head>
8+
<body>
9+
<div>%svelte.body%</div>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello Inspector!</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('@sveltejs/kit').Config} */
2+
const config = {
3+
kit: {},
4+
experimental: {
5+
inspector: {
6+
showToggleButton: 'always'
7+
}
8+
}
9+
};
10+
11+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Svelte + Vite
2+
3+
This template should help get you started developing with Svelte in Vite.
4+
5+
## Recommended IDE Setup
6+
7+
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
8+
9+
## Need an official Svelte framework?
10+
11+
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
12+
13+
## Technical considerations
14+
15+
**Why use this over SvelteKit?**
16+
17+
- It brings its own routing solution which might not be preferable for some users.
18+
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
19+
`vite dev` and `vite build` wouldn't work in a SvelteKit environment, for example.
20+
21+
This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
22+
23+
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
24+
25+
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
26+
27+
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
28+
29+
**Why include `.vscode/extensions.json`?**
30+
31+
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
32+
33+
**Why enable `checkJs` in the JS template?**
34+
35+
It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.
36+
37+
**Why is HMR not preserving my local component state?**
38+
39+
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
40+
41+
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
42+
43+
```js
44+
// store.js
45+
// An extremely simple external store
46+
import { writable } from 'svelte/store';
47+
export default writable(0);
48+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getEl, getText, isBuild } from '../../testUtils';
2+
3+
describe('inspector-vite', () => {
4+
it('should render page', async () => {
5+
expect(await getText('h1')).toBe('Hello Inspector!');
6+
});
7+
if (!isBuild) {
8+
it('should show inspector toggle during dev', async () => {
9+
expect(await getEl('.svelte-inspector-toggle')).not.toBe(null);
10+
});
11+
} else {
12+
it('should not show inspector toggle during preview', async () => {
13+
expect(await getEl('.svelte-inspector-toggle')).toBe(null);
14+
});
15+
}
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
</head>
7+
<body>
8+
<div id="app"></div>
9+
<script type="module" src="/src/main.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"target": "esnext",
5+
"module": "esnext",
6+
/**
7+
* svelte-preprocess cannot figure out whether you have
8+
* a value or a type, so tell TypeScript to enforce using
9+
* `import type` instead of `import` for Types.
10+
*/
11+
"importsNotUsedAsValues": "error",
12+
"isolatedModules": true,
13+
"resolveJsonModule": true,
14+
/**
15+
* To have warnings / errors of the Svelte compiler at the
16+
* correct position, enable source maps by default.
17+
*/
18+
"sourceMap": true,
19+
"esModuleInterop": true,
20+
"skipLibCheck": true,
21+
"forceConsistentCasingInFileNames": true,
22+
"baseUrl": ".",
23+
/**
24+
* Typecheck JS in `.svelte` and `.js` files by default.
25+
* Disable this if you'd like to use dynamic types.
26+
*/
27+
"checkJs": true
28+
},
29+
/**
30+
* Use global.d.ts instead of compilerOptions.types
31+
* to avoid limiting type declarations.
32+
*/
33+
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "e2e-tests-inspector-vite",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@sveltejs/vite-plugin-svelte": "workspace:*",
13+
"svelte": "^3.48.0",
14+
"vite": "^2.9.8"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello Inspector!</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<script>
2+
let count = 0;
3+
const increment = () => {
4+
count += 1;
5+
};
6+
</script>
7+
8+
<button on:click={increment}>
9+
Clicks: {count}
10+
</button>
11+
12+
<style>
13+
button {
14+
font-family: inherit;
15+
font-size: inherit;
16+
padding: 1em 2em;
17+
color: #ff3e00;
18+
background-color: rgba(255, 62, 0, 0.1);
19+
border-radius: 2em;
20+
border: 2px solid rgba(255, 62, 0, 0);
21+
outline: none;
22+
width: 200px;
23+
font-variant-numeric: tabular-nums;
24+
cursor: pointer;
25+
}
26+
27+
button:focus {
28+
border: 2px solid #ff3e00;
29+
}
30+
31+
button:active {
32+
background-color: rgba(255, 62, 0, 0.2);
33+
}
34+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import App from './App.svelte';
2+
3+
const app = new App({
4+
target: document.getElementById('app')
5+
});
6+
7+
export default app;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="svelte" />
2+
/// <reference types="vite/client" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite';
2+
import { svelte } from '@sveltejs/vite-plugin-svelte';
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
plugins: [svelte({ experimental: { inspector: { showToggleButton: 'always' } } })]
7+
});

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

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
<script>
22
// do not use TS here so that this component works in non-ts projects too
33
import { onMount } from 'svelte';
4-
// @ts-expect-error virtual import is resolved by vite-plugin-svelte-inspector
5-
// eslint-disable-next-line node/no-missing-import
6-
import icon from 'virtual:svelte-inspector:icon.svg';
7-
// @ts-expect-error virtual import is resolved by vite-plugin-svelte-inspector
84
// eslint-disable-next-line node/no-missing-import
95
import options from 'virtual:svelte-inspector-options';
106
const toggle_combo = options.toggleKeyCombo?.toLowerCase().split('-');
117
128
let enabled = false;
139
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+
1422
// location of code in file
1523
let file_loc;
1624
// cursor pos and width for file_loc overlay positioning
@@ -131,7 +139,6 @@
131139
const b = document.body;
132140
if (options.customStyles) {
133141
b.classList.add('svelte-inspector-enabled');
134-
b.style.setProperty('--svelte-inspector-cursor', `url(${JSON.stringify(icon)})`);
135142
}
136143
listeners(b, enabled);
137144
}
@@ -143,12 +150,17 @@
143150
listeners(b, enabled);
144151
if (options.customStyles) {
145152
b.classList.remove('svelte-inspector-enabled');
146-
b.style.removeProperty('--svelte-inspector-cursor');
147153
active_el?.classList.remove('svelte-inspector-active-target');
148154
}
155+
active_el = null;
149156
}
150157
151158
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);
152164
if (toggle_combo) {
153165
document.body.addEventListener('keydown', keydown);
154166
if (options.holdMode) {
@@ -158,6 +170,10 @@
158170
return () => {
159171
// make sure we get rid of everything
160172
disable();
173+
const s = document.head.querySelector('#svelte-inspector-style');
174+
if (s) {
175+
document.head.removeChild(s);
176+
}
161177
if (toggle_combo) {
162178
document.body.removeEventListener('keydown', keydown);
163179
if (options.holdMode) {
@@ -172,7 +188,7 @@
172188
<div
173189
class="svelte-inspector-toggle"
174190
class:enabled
175-
style={`background-image:url(${icon});${options.toggleButtonPos
191+
style={`background-image: var(--svelte-inspector-icon);${options.toggleButtonPos
176192
.split('-')
177193
.map((p) => `${p}: 8px;`)
178194
.join('')}`}
@@ -193,7 +209,7 @@
193209
194210
<style>
195211
:global(body.svelte-inspector-enabled *) {
196-
cursor: var(--svelte-inspector-cursor), crosshair !important;
212+
cursor: var(--svelte-inspector-icon), crosshair !important;
197213
}
198214
:global(.svelte-inspector-active-target) {
199215
outline: 2px dashed #ff3e00 !important;

packages/vite-plugin-svelte/src/ui/inspector/icon.svg

-5
This file was deleted.

packages/vite-plugin-svelte/src/ui/inspector/load-inspector.ts renamed to packages/vite-plugin-svelte/src/ui/inspector/load-inspector.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// @ts-expect-error virtual import is resolved by vite-plugin-svelte-inspector
21
// eslint-disable-next-line node/no-missing-import
3-
import Inspector from 'virtual:svelte-inspector:Inspector.svelte';
2+
import Inspector from 'virtual:svelte-inspector-path:Inspector.svelte';
43

54
function create_inspector_host() {
65
const id = 'svelte-inspector-host';

0 commit comments

Comments
 (0)