[[!Remember!]]: update the "exports" field of package.json if adding new public entry files.
See more details about in the "exports" field of package.json and why it is written like that in #19513 .
Only these entries are officially exported to users:
'echarts''echarts/index.js''echarts/index.blank.js''echarts/index.common.js''echarts/index.simple.js''echarts/core.js''echarts/charts.js''echarts/components.js''echarts/features.js''echarts/renderers.js''echarts/i18n/*'echarts/theme/*'echarts/types/*'echarts/extension/*'echarts/dist/*'echarts/ssr/client/index.js'
The other entries listed in the "exports" field of package.json make the internal files visible, but they are legacy usages, not recommended but not forbidden (for the sake of keeping backward compatible). These entries are made from the search in github about which internal files are imported.
Since v5.5.0, "type": "module" and "exports: {...}" are added to package.json. When upgrading to v5.5.0+, if you meet some problems about "can not find/resolve xxx" when importing echarts/i18n/xxx or echarts/theme/xxx or some internal files, it probably because of the issue "file extension not fully specified". Please try to make the file extension fully specified (that is, import 'xxx/xxx/xxx.js' rather than import 'xxx/xxx/xxx'), or change the config of you bundler tools to support auto adding file extensions.
About "./types/dist/shared": "./types/dist/shared.d.ts", in "exports", see #19663 .
Although using "exports" of package.json we can make alias (or say, route) to physical files (for example: { "exports": { "./xxx": "./yyy/zzz.js" } } enables import 'echarts/xxx' to route to 'echarts/yyy/zzz.js'), at present we can not make sure all the versions of bundle tools and runtimes in use do it consistently. So we do not use the alias setting, but keep providing physical files for each public entry. For example, for an official public entry 'echarts/core', we provide a file echarts/core.js (and echarts/core.d.ts).
For compatibility with older TS versions.
See MIN_VERSION in echarts/build/testDts.js for the minimal supported TS version.
- When importing
from "echarts":- dts is configured in
package.json:{ "exports": { ".": { "types": /*...*/ } }, "types": /*...*/ // fallback }
- dts is configured in
- When importing
from "echarts/core"from "echarts/features"from "echarts/components"from "echarts/charts"from "echarts/renderers":- Use
echarts.core.d.tsecharts.features.d.tsecharts.components.d.tsecharts.charts.d.tsecharts.renderers.d.tsrespectively; nopackage.jsonconfiguration.
- Use
-
ESM:
- dts:
echarts.core.d.tsecharts.features.d.tsecharts.components.d.tsecharts.charts.d.tsecharts.renderers.d.ts.echarts/type/dist/all.d.ts.echarts/ssr/client/index.d.ts.
- dts:
-
CJS (and UMD):
- dts:
echarts/types/dist/echarts.d.cts.
- Usage:
// It enables usage in CJS+TS: import echarts = require('echarts'); var myChart = echarts.init(/*...*/); const option: echarts.EChartsOption = {/*...*/}; // Get type in CJS. myChart.setOption(option);
// It enables usage via UMD global variables: /// <reference types="echarts"> var myChart = echarts.init(/*...*/);
// However, "echarts/core", "echarts/features", "echarts/components", "echarts/charts", // "echarts/renderers" have no types supported -- and are not necessary.
- dts:
-
Self-contained entry:
- dts:
echarts/types/dist/echarts.d.ts - This is a single, self-contained ESM with no imports, provided for online type checkers (e.g., echarts-examples, https://echarts.apache.org/examples/en/editor.html?c=line-simple) usage.
- dts:
-
Legacy entry:
- dts:
index.d.ts: - Historically, echarts has been providing a UMD bundle (
echarts/dist/echarts.js) as one of the default entries, andindex.d.tshas been corresponding to it. However, since ESM and UMD TS entries were refactored,index.d.tswas deprecated and temporarily retained in case users reference it like this:/// <reference path="../node_modules/echarts/index.d.ts" /> var myChart = echarts.init(/*...*/);
- dts:
-
Separated ESM and CJS(UMD) TS entries:
- Statements
export = echartsandexport as namespace echartsprovide types for the UMD bundle. However, they effectively mismatches the ESM entry.tscno longer tolerates this mismatch since TSv5.6and reports errorTS1203. Therefore, ESM entries and UMD entries should be separately provided.
- Statements
-
Explicit file extensions:
- i.e.,
export {some} from "echarts/types/dist/core.js", where a pseudo.jsis used here to redirect to.d.ts. - Explicit file extensions are required when
package.jsonuses"type": "module"andtsconfig.jsonuses"moduleResolution": "NodeNext"; otherwise, if using"from "echarts/types/dist/core", TS errorTS2834occurs. - If using
"from "echarts/types/dist/core.d.ts", TS errorTS2846occurs.
- i.e.,
-
Avoid duplicated type declarations:
echarts/type/dist/all.d.tsecharts/type/dist/core.d.tsecharts/type/dist/option.d.tsecharts/type/dist/features.d.tsecharts/type/dist/components.d.tsecharts/type/dist/charts.d.tsecharts/type/dist/renderers.d.tsimport type declarations from a single sourceecharts/types/dist/shared.d.ts; otherwise TS errorTS2442may occur.
-
TSv4.7~TSv5.7 disallows
requirea ESM file from a CJS file whenmoduleResolution: "NodeNext"(introduced in TSv4.7); otherwise TS errorTS1471may arise.tscrecognizes.ctsas CJS rather than ESM even whenpackage.jsonuses"type": "module".