New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New minimal import API, improve exported option types. #13890
Conversation
import {use, init} from 'echarts/core'; import {ChartBar} from 'echarts/charts'; import {ComponentGrid} from 'echarts/components'; use([ChartBar, ComponentGrid]);
optimize inner option determination
Thanks for your contribution! The pull request is marked to be Document changes are required in this PR. Please also make a PR to apache/incubator-echarts-doc for document changes. When the doc PR is merged, the maintainers will remove the |
Wow! Looks great! |
Needs to extract the specify component for each component type.
Congratulations! Your PR has been merged. Thanks for your contribution! |
Amazing work, thank you! |
Brief Information
This pull request is about refactoring of exports so developers can have better API to get minimal imports.
Also, in this PR ts is upgraded to 4.1 to make sure exported types won't have problems in the higher ts version.
Related ZRender PR: ecomfe/zrender#685
Fixed issues
#13764
#13755
#13763
#13850
#13701
#13804
Details
Before: What was the problem?
We kept providing CommonJS modules in the earlier version because we want to make sure echarts can be compatible with environments as more as possible, like old Webpack and NodeJS.
So after rewriting our code with TypeScript. We still export two kinds of modules, CommonJS and ESM. CommonJS modules are in the
lib
folder, which is the same as the previous version and mainly used in NodeJS. ESM modules are in theesm
folder, which will be used in the bundlers which target browser. This brings a serious issue, we were suggesting developers using the lib folder when import modules. Likeimport * as echarts from 'echarts/lib/echarts'
. But now in most scenarios ESM module is much better because bundler can tree-shake it and get a smaller bundle. This contradiction leads us to not know how to deal with the compatibility when still providing a best practice.Even worse, because codes of different extensions may both reference
lib
andesm
. The bundle will have two namespaces. This leads to a much larger bundle size and bugs like registered components can not be found.After: How is it fixed in this PR?
First of all, we remove CommonJS modules and export ESM format modules in the
lib
folder. Since very few bundlers don't support ESM module now. And for NodeJS we can provide the UMD formatdist/echarts.js
Sencondly, we provide a higher level of exports for each components and each to avoid exposing our underlying folder structure and provide a
use
method to register the componentsFor TypeScript, we also provide a better type when using the new minimal imports.
Usage
Are there any API changes?
Related test cases or examples to use the new APIs
Import the whole package.
Importing of whole package is not changed.
On top of that, we expose more types for cases like #13755
Minimal import.
Then let the bundler do the tree-shaking and remove all unused components.
PS: We are working on generating tests of this minimal import in the https://github.com/apache/incubator-echarts-examples/tree/next. We will provide a new feature in the example page about generating a minimal import code based on the example option.
The legacy minimal import is still supported but not suggested.
This is still supported. You should use it only when tree-shaking can't work in the bundlers.
Internal changes
All very core modules have been moved into
core
dir. The originalsrc/echarts.ts
has been changed to only export necessary modules for compatible reasons.So the old code
Can still work well to avoid issues like #13701
But for the new minimal import code. We can tree shake it as much as possible. For example, we can remove the CanvasRenderer if we only wan't to render with SVG now.
Other Changes
The dependencies in
theme
andi18n
has been changed fromecharts
toecharts/lib/echarts
to avoid bundling the whole package. ecomfe/vue-echarts#345 (comment)