Skip to content

Commit 62afd80

Browse files
dominikgRich-Harrisbluwy
authored
refactor(types): use dts-buddy to generate types with map (#751)
* refactor(types): use dts-buddy to generate types with map, cleanup type definitions and tsconfig * chore: update generated types * fix: use .d.ts for type imports * fix: ignore .changesets/pre.json to prevent format check fails on main after release merge * fix: move types inside import condition to avoid masquerading as cjs warning * fix: types * bump dts-buddy * fix: regenerate types * fix: use correct type Options for plugin function arg * fix(types): reexport index.js from public.d.ts to ensure types and functions are properly exported through module declaration generated by dts-buddy; move dynamicCompileOptions to PluginOptions, rename SvelteOptions to SvelteConfig * fix: update lockfile * Update .changeset/metal-olives-wait.md Co-authored-by: Bjorn Lu <[email protected]> * fix: update types --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Bjorn Lu <[email protected]>
1 parent efd571f commit 62afd80

33 files changed

+475
-151
lines changed

.changeset/gorgeous-hats-bathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(types): use correct type Options for svelte function arg

.changeset/many-cows-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': major
3+
---
4+
5+
breaking: remove package.json export

.changeset/metal-olives-wait.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte-inspector': major
3+
'@sveltejs/vite-plugin-svelte': major
4+
---
5+
6+
breaking(types): emit types with dts-buddy to include type map

.changeset/small-pears-accept.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': major
3+
---
4+
5+
breaking(types): rename SvelteOptions to SvelteConfig

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ jobs:
6666
- name: publint
6767
if: (${{ success() }} || ${{ failure() }})
6868
run: pnpm check:publint
69+
- name: generated types are up to date
70+
if: (${{ success() }} || ${{ failure() }})
71+
run: pnpm generate:types && [ "`git status --porcelain=v1`" == "" ]
6972

7073
test:
7174
timeout-minutes: 10

.github/workflows/release.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ jobs:
4343
cache-dependency-path: '**/pnpm-lock.yaml'
4444
- name: install
4545
run: pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
46-
46+
- name: generated types are up to date
47+
run: pnpm generate:types && [ "`git status --porcelain=v1`" == "" ]
48+
- name: publint
49+
run: pnpm check:publint
4750
- name: Creating .npmrc
4851
run: |
4952
cat << EOF > "$HOME/.npmrc"

.prettierrc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ export default {
1616
},
1717
{
1818
files: [
19-
'**/vite.config.js.timestamp-*.mjs',
2019
'**/CHANGELOG.md',
2120
'.github/renovate.json5',
21+
'**/types/index.d.ts',
22+
'**/types/index.d.ts.map',
23+
'**/pnpm-lock.yaml',
24+
'.changeset/pre.json',
25+
'**/vite.config.js.timestamp-*.mjs',
2226
'packages/e2e-tests/dynamic-compile-options/src/components/A.svelte'
2327
],
2428
options: {

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"fixup": "run-s lint format",
1919
"release": "pnpm changeset publish",
2020
"prepare": "husky install",
21-
"playwright": "playwright-core"
21+
"playwright": "playwright-core",
22+
"generate:types": "pnpm --filter \"./packages/*\" --parallel generate:types"
2223
},
2324
"devDependencies": {
2425
"@changesets/cli": "^2.26.2",
@@ -28,6 +29,7 @@
2829
"@typescript-eslint/eslint-plugin": "^6.8.0",
2930
"@typescript-eslint/parser": "^6.8.0",
3031
"cross-env": "^7.0.3",
32+
"dts-buddy": "^0.3.0",
3133
"eslint": "^8.51.0",
3234
"eslint-config-prettier": "^9.0.0",
3335
"eslint-plugin-html": "^7.1.0",

packages/vite-plugin-svelte-inspector/package.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
"license": "MIT",
55
"author": "dominikg",
66
"files": [
7-
"src"
7+
"src",
8+
"types"
89
],
910
"type": "module",
10-
"types": "src/index.d.ts",
11+
"types": "types/index.d.ts",
1112
"exports": {
1213
".": {
13-
"types": "./src/index.d.ts",
14-
"import": "./src/index.js"
14+
"import": {
15+
"types": "./types/index.d.ts",
16+
"default": "./src/index.js"
17+
}
1518
}
1619
},
1720
"scripts": {
1821
"check:publint": "publint --strict",
19-
"check:types": "tsc --noEmit"
22+
"check:types": "tsc --noEmit",
23+
"generate:types": "dts-buddy -m \"@sveltejs/vite-plugin-svelte-inspector:src/public.d.ts\""
2024
},
2125
"engines": {
2226
"node": "^18.0.0 || >=20"

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ function getInspectorPath() {
1414
);
1515
}
1616

17-
/** @type {import('.').svelteInspector} */
17+
/**
18+
* @param {Partial<import('./public.d.ts').Options>} [options]
19+
* @returns {import('vite').Plugin}
20+
*/
1821
export function svelteInspector(options) {
1922
const inspectorPath = getInspectorPath();
2023
debug(`svelte inspector path: ${inspectorPath}`);
2124

2225
/** @type {import('vite').ResolvedConfig} */
2326
let viteConfig;
24-
/** @type {import('.').Options} */
27+
/** @type {import('./public.d.ts').Options} */
2528
let inspectorOptions;
2629
let disabled = false;
2730

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { loadEnv } from 'vite';
22
import { debug } from './debug.js';
33

4-
/** @type {import('.').Options} */
4+
/** @type {import('./public.d.ts').Options} */
55
export const defaultInspectorOptions = {
66
toggleKeyCombo: process.platform === 'darwin' ? 'meta-shift' : 'control-shift',
77
navKeys: { parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' },
@@ -14,7 +14,7 @@ export const defaultInspectorOptions = {
1414

1515
/**
1616
* @param {import('vite').ResolvedConfig} config
17-
* @returns {Partial<import('.').Options> | boolean | void}
17+
* @returns {Partial<import('./public.d.ts').Options> | boolean | void}
1818
*/
1919
export function parseEnvironmentOptions(config) {
2020
const env = loadEnv(config.mode, config.envDir ?? process.cwd(), 'SVELTE_INSPECTOR');

packages/vite-plugin-svelte-inspector/src/index.d.ts renamed to packages/vite-plugin-svelte-inspector/src/public.d.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Plugin } from 'vite';
2-
31
export interface Options {
42
/**
53
* define a key combo to toggle inspector,
@@ -63,11 +61,12 @@ export interface Options {
6361

6462
/**
6563
* internal options that are automatically set, not to be set or used by users
64+
* @internal
6665
*/
6766
__internal?: {
6867
// vite base url
6968
base: string;
7069
};
7170
}
7271

73-
export declare function svelteInspector(options?: Options): Plugin;
72+
export * from './index.js';
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
{
22
"include": ["src"],
3-
"exclude": ["**/*.spec.ts"],
43
"compilerOptions": {
54
"noEmit": true,
65
"target": "ES2020",
7-
"module": "ES2020",
6+
"module": "ES2022",
87
"moduleResolution": "node",
98
"strict": true,
10-
"declaration": true,
11-
"sourceMap": true,
129
"noUnusedLocals": true,
13-
"esModuleInterop": true,
1410
"baseUrl": ".",
15-
"resolveJsonModule": true,
1611
// see https://devblogs.microsoft.com/typescript/announcing-typescript-4-4-beta/#use-unknown-catch-variables
1712
"useUnknownInCatchVariables": false,
1813
"allowJs": true,
19-
"checkJs": true
14+
"checkJs": true,
15+
"allowSyntheticDefaultImports": true
2016
}
2117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
declare module '@sveltejs/vite-plugin-svelte-inspector' {
2+
export interface Options {
3+
/**
4+
* define a key combo to toggle inspector,
5+
* @default 'meta-shift' on mac, 'control-shift' on other os
6+
*
7+
* any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
8+
* examples: control-shift, control-o, control-alt-s meta-x control-meta
9+
* Some keys have native behavior (e.g. alt-s opens history menu on firefox).
10+
* To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
11+
*/
12+
toggleKeyCombo?: string;
13+
14+
/**
15+
* define keys to select elements with via keyboard
16+
* @default {parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' }
17+
*
18+
* improves accessibility and also helps when you want to select elements that do not have a hoverable surface area
19+
* due to tight wrapping
20+
*
21+
* A note for users of screen-readers:
22+
* If you are using arrow keys to navigate the page itself, change the navKeys to avoid conflicts.
23+
* e.g. navKeys: {parent: 'w', prev: 'a', child: 's', next: 'd'}
24+
*
25+
*
26+
* parent: select closest parent
27+
* child: select first child (or grandchild)
28+
* next: next sibling (or parent if no next sibling exists)
29+
* prev: previous sibling (or parent if no prev sibling exists)
30+
*/
31+
navKeys?: { parent: string; child: string; next: string; prev: string };
32+
33+
/**
34+
* define key to open the editor for the currently selected dom node
35+
*
36+
* @default 'Enter'
37+
*/
38+
openKey?: string;
39+
40+
/**
41+
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
42+
* @default true
43+
*/
44+
holdMode?: boolean;
45+
46+
/**
47+
* when to show the toggle button
48+
* @default 'active'
49+
*/
50+
showToggleButton?: 'always' | 'active' | 'never';
51+
52+
/**
53+
* where to display the toggle button
54+
* @default top-right
55+
*/
56+
toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
57+
58+
/**
59+
* inject custom styles when inspector is active
60+
*/
61+
customStyles?: boolean;
62+
63+
/**
64+
* internal options that are automatically set, not to be set or used by users
65+
* */
66+
__internal?: {
67+
// vite base url
68+
base: string;
69+
};
70+
}
71+
export function svelteInspector(options?: Partial<Options> | undefined): import('vite').Plugin;
72+
}
73+
74+
//# sourceMappingURL=index.d.ts.map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": 3,
3+
"file": "index.d.ts",
4+
"names": [
5+
"Options",
6+
"svelteInspector"
7+
],
8+
"sources": [
9+
"../src/public.d.ts",
10+
"../src/index.js"
11+
],
12+
"sourcesContent": [
13+
null,
14+
null
15+
],
16+
"mappings": ";kBAAiBA,OAAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCoBRC,eAAeA"
17+
}

packages/vite-plugin-svelte/package.json

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
"license": "MIT",
55
"author": "dominikg",
66
"files": [
7-
"src"
7+
"src",
8+
"types"
89
],
910
"type": "module",
10-
"types": "src/index.d.ts",
11+
"types": "types/index.d.ts",
1112
"exports": {
1213
".": {
13-
"types": "./src/index.d.ts",
14-
"import": "./src/index.js"
15-
},
16-
"./package.json": "./package.json"
14+
"import": {
15+
"types": "./types/index.d.ts",
16+
"default": "./src/index.js"
17+
}
18+
}
1719
},
1820
"scripts": {
1921
"check:publint": "publint --strict",
20-
"check:types": "tsc --noEmit"
22+
"check:types": "tsc --noEmit",
23+
"generate:types": "dts-buddy -m \"@sveltejs/vite-plugin-svelte:src/public.d.ts\""
2124
},
2225
"engines": {
2326
"node": "^18.0.0 || >=20"

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import { saveSvelteMetadata } from './utils/optimizer.js';
2020
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js';
2121
import { loadRaw } from './utils/load-raw.js';
2222

23-
/** @type {import('./index.d.ts').svelte} */
23+
/**
24+
* @param {Partial<import('./public.d.ts').Options>} [inlineOptions]
25+
* @returns {import('vite').Plugin[]}
26+
*/
2427
export function svelte(inlineOptions) {
2528
if (process.env.DEBUG != null) {
2629
log.setLevel('debug');

packages/vite-plugin-svelte/src/preprocess.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ const supportedScriptLangs = ['ts'];
99

1010
export const lang_sep = '.vite-preprocess';
1111

12-
/** @type {import('./index.d.ts').vitePreprocess} */
12+
/**
13+
* @param {import('./public.d.ts').VitePreprocessOptions} opts
14+
* @returns {import('svelte/compiler').PreprocessorGroup}
15+
*/
1316
export function vitePreprocess(opts) {
14-
/** @type {import('svelte/types/compiler/preprocess').PreprocessorGroup} */
17+
/** @type {import('svelte/compiler').PreprocessorGroup} */
1518
const preprocessor = { name: 'vite-preprocess' };
1619
if (opts?.script !== false) {
1720
preprocessor.script = viteScript().script;
@@ -24,7 +27,7 @@ export function vitePreprocess(opts) {
2427
}
2528

2629
/**
27-
* @returns {{ script: import('svelte/types/compiler/preprocess').Preprocessor }}
30+
* @returns {{ script: import('svelte/compiler').Preprocessor }}
2831
*/
2932
function viteScript() {
3033
return {
@@ -55,12 +58,12 @@ function viteScript() {
5558

5659
/**
5760
* @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config
58-
* @returns {{ style: import('svelte/types/compiler/preprocess').Preprocessor }}
61+
* @returns {{ style: import('svelte/compiler').Preprocessor }}
5962
*/
6063
function viteStyle(config = {}) {
6164
/** @type {CssTransform} */
6265
let transform;
63-
/** @type {import('svelte/types/compiler/preprocess').Preprocessor} */
66+
/** @type {import('svelte/compiler').Preprocessor} */
6467
const style = async ({ attributes, content, filename = '' }) => {
6568
const ext = attributes.lang ? `.${attributes.lang}` : '.css';
6669
if (attributes.lang && !isCSSRequest(ext)) return;

0 commit comments

Comments
 (0)