Skip to content

Commit f7ed1e1

Browse files
authored
refactor: switch to generated .d.ts (#835)
* build: generate d.ts * fix: preserve PublicMethods * fix: avoid exposing types of attrs * refactor: use existing setoption type * fix: expose root and chart * feat: use symbol as injection key * chore: add comment for the type casting of the exposed
1 parent 8be19fe commit f7ed1e1

File tree

9 files changed

+229
-613
lines changed

9 files changed

+229
-613
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"dev": "vite",
1313
"build": "pnpm run docs && rimraf dist && rollup -c",
14-
"typecheck": "vue-tsc",
14+
"typecheck": "tsc",
1515
"lint": "eslint . --fix",
1616
"format": "prettier . --write",
1717
"publint": "publint",
@@ -41,7 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"@highlightjs/vue-plugin": "^2.1.0",
44-
"@rollup/plugin-replace": "^5.0.7",
44+
"@rollup/plugin-replace": "^6.0.2",
4545
"@types/node": "^22.15.21",
4646
"@typescript-eslint/utils": "^8.32.1",
4747
"@vercel/analytics": "^1.3.1",
@@ -64,10 +64,10 @@
6464
"prettier": "^3.5.3",
6565
"publint": "^0.3.12",
6666
"rimraf": "^6.0.1",
67-
"rollup": "^4.19.0",
68-
"rollup-plugin-dts": "^6.1.0",
69-
"rollup-plugin-esbuild": "^6.1.1",
70-
"rollup-plugin-import-css": "^3.5.1",
67+
"rollup": "^4.41.1",
68+
"rollup-plugin-dts": "^6.2.1",
69+
"rollup-plugin-esbuild": "^6.2.1",
70+
"rollup-plugin-import-css": "^3.5.8",
7171
"typescript": "^5.8.3",
7272
"vite": "^6.3.5",
7373
"vue": "^3.5.13",

pnpm-lock.yaml

Lines changed: 164 additions & 494 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import replace from "@rollup/plugin-replace";
22
import esbuild from "rollup-plugin-esbuild";
33
import { dts } from "rollup-plugin-dts";
44
import css from "rollup-plugin-import-css";
5+
import { defineConfig } from "rollup";
56

67
/**
78
* Modifies the Rollup options for a build to support strict CSP
@@ -73,12 +74,24 @@ const builds = [
7374
},
7475
];
7576

76-
export default [
77+
export default defineConfig([
7778
...builds.map((options) => configBuild(options, false)),
7879
...builds.map((options) => configBuild(options, true)),
7980
{
80-
input: "src/index.d.ts",
81-
plugins: [dts()],
81+
input: "src/index.ts",
82+
plugins: [
83+
dts({
84+
// https://github.com/unjs/unbuild/pull/57/files
85+
compilerOptions: {
86+
preserveSymlinks: false,
87+
},
88+
}),
89+
{
90+
load(id) {
91+
if (id.endsWith(".css")) return "";
92+
},
93+
},
94+
],
8295
output: [
8396
{
8497
file: "dist/index.d.ts",
@@ -90,4 +103,4 @@ export default [
90103
},
91104
],
92105
},
93-
];
106+
]);

src/ECharts.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import {
1919
autoresizeProps,
2020
useLoading,
2121
loadingProps,
22+
type PublicMethods,
2223
} from "./composables";
2324
import { isOn, omitOn, toValue } from "./utils";
2425
import { register, TAG_NAME } from "./wc";
2526

2627
import type { PropType, InjectionKey } from "vue";
2728
import type {
2829
EChartsType,
30+
SetOptionType,
2931
Option,
3032
Theme,
3133
ThemeInjection,
@@ -42,11 +44,10 @@ import "./style.css";
4244
const __CSP__ = false;
4345
const wcRegistered = __CSP__ ? false : register();
4446

45-
export const THEME_KEY = "ecTheme" as unknown as InjectionKey<ThemeInjection>;
46-
export const INIT_OPTIONS_KEY =
47-
"ecInitOptions" as unknown as InjectionKey<InitOptionsInjection>;
48-
export const UPDATE_OPTIONS_KEY =
49-
"ecUpdateOptions" as unknown as InjectionKey<UpdateOptionsInjection>;
47+
export const THEME_KEY: InjectionKey<ThemeInjection> = Symbol();
48+
export const INIT_OPTIONS_KEY: InjectionKey<InitOptionsInjection> = Symbol();
49+
export const UPDATE_OPTIONS_KEY: InjectionKey<UpdateOptionsInjection> =
50+
Symbol();
5051
export { LOADING_OPTIONS_KEY } from "./composables";
5152

5253
export default defineComponent({
@@ -65,7 +66,7 @@ export default defineComponent({
6566
},
6667
emits: {} as unknown as Emits,
6768
inheritAttrs: false,
68-
setup(props, { attrs }) {
69+
setup(props, { attrs, expose }) {
6970
const root = shallowRef<EChartsElement>();
7071
const chart = shallowRef<EChartsType>();
7172
const manualOption = shallowRef<Option>();
@@ -189,8 +190,14 @@ export default defineComponent({
189190
commit();
190191
}
191192
}
193+
const setOption: SetOptionType = (
194+
option,
195+
notMerge,
196+
lazyUpdate?: boolean,
197+
) => {
198+
const updateOptions =
199+
typeof notMerge === "boolean" ? { notMerge, lazyUpdate } : notMerge;
192200

193-
function setOption(option: Option, updateOptions?: UpdateOptions) {
194201
if (props.manualUpdate) {
195202
manualOption.value = option;
196203
}
@@ -200,7 +207,7 @@ export default defineComponent({
200207
} else {
201208
chart.value.setOption(option, updateOptions || {});
202209
}
203-
}
210+
};
204211

205212
function cleanup() {
206213
if (chart.value) {
@@ -284,21 +291,23 @@ export default defineComponent({
284291
}
285292
});
286293

287-
return {
288-
chart,
289-
root,
294+
const exposed = {
290295
setOption,
291-
nonEventAttrs,
292-
nativeListeners,
293-
...publicApi,
296+
root,
297+
chart,
294298
};
295-
},
296-
render() {
297-
return h(TAG_NAME, {
298-
...this.nonEventAttrs,
299-
...this.nativeListeners,
300-
ref: "root",
301-
class: ["echarts", ...(this.nonEventAttrs.class || [])],
302-
});
299+
expose({ ...exposed, ...publicApi });
300+
301+
// While `expose()` exposes methods and properties to the parent component
302+
// via template refs at runtime, it doesn't contribute to TypeScript types.
303+
// This type casting ensures TypeScript correctly types the exposed members
304+
// that will be available when using this component.
305+
return (() =>
306+
h(TAG_NAME, {
307+
...nonEventAttrs.value,
308+
...nativeListeners,
309+
ref: root,
310+
class: ["echarts", ...(nonEventAttrs.value.class || [])],
311+
})) as unknown as typeof exposed & PublicMethods;
303312
},
304313
});

src/composables/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const METHOD_NAMES = [
2121

2222
type MethodName = (typeof METHOD_NAMES)[number];
2323

24-
type PublicMethods = Pick<EChartsType, MethodName>;
24+
export type PublicMethods = Pick<EChartsType, MethodName>;
2525

2626
export function usePublicAPI(
2727
chart: Ref<EChartsType | undefined>,

src/composables/loading.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import { inject, computed, watchEffect } from "vue";
22
import { toValue } from "../utils";
33

44
import type { Ref, InjectionKey, PropType } from "vue";
5-
import type { EChartsType, LoadingOptions } from "../types";
5+
import type {
6+
EChartsType,
7+
LoadingOptions,
8+
LoadingOptionsInjection,
9+
} from "../types";
610

7-
export const LOADING_OPTIONS_KEY =
8-
"ecLoadingOptions" as unknown as InjectionKey<
9-
LoadingOptions | Ref<LoadingOptions>
10-
>;
11+
export const LOADING_OPTIONS_KEY: InjectionKey<LoadingOptionsInjection> =
12+
Symbol();
1113

1214
export function useLoading(
1315
chart: Ref<EChartsType | undefined>,
14-
loading: Ref<boolean>,
16+
loading: Ref<boolean | undefined>,
1517
loadingOptions: Ref<LoadingOptions | undefined>,
1618
): void {
1719
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});

src/index.d.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ export type InitParameters = Parameters<InitType>;
1919
export type Theme = NonNullable<InitParameters[1]>;
2020
export type ThemeInjection = Injection<Theme>;
2121
export type InitOptions = NonNullable<InitParameters[2]>;
22-
2322
export type InitOptionsInjection = Injection<InitOptions>;
24-
2523
export type UpdateOptions = SetOptionOpts;
2624
export type UpdateOptionsInjection = Injection<UpdateOptions>;
2725

2826
export type EChartsType = ReturnType<InitType>;
2927

30-
type SetOptionType = EChartsType["setOption"];
28+
export type SetOptionType = EChartsType["setOption"];
3129
export type Option = Parameters<SetOptionType>[0];
3230

3331
export type AutoResize =

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
2-
"allowJs": true,
32
"compilerOptions": {
43
"target": "ESNext",
54
"module": "ESNext",
65
"strict": true,
76
"jsx": "preserve",
87
"importHelpers": true,
9-
"moduleResolution": "node",
8+
"moduleResolution": "bundler",
109
"removeComments": true,
1110
"skipLibCheck": true,
1211
"esModuleInterop": true,

0 commit comments

Comments
 (0)