Skip to content

Commit 6cefe27

Browse files
committed
fix: (1) Mistake in package.json "exports"."require" (2) Add entry for dist/** for file extension not specified usage (3) add dist/echarts.esm.mjs for case that not able to recognize as esm after dist/package.json with {"type": "commonjs"} added.
1 parent 3fbfd2b commit 6cefe27

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

build/build.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,32 @@ async function run() {
111111
}
112112
else {
113113
const types = buildType.split(',').map(a => a.trim());
114-
const cfgs = types.map(type =>
115-
config.createECharts({
116-
...opt,
117-
type
118-
})
119-
);
120-
await build(cfgs);
114+
115+
116+
// Since 5.5.0, echarts/package.json added `{"type": "module"}`, and added
117+
// echarts/dist/package.json with `{"type": "commonjs"}`, both of which makes
118+
// echarts/dist/echarts.esm.js can not be recognized as esm any more (at least
119+
// in webpack5 and nodejs) any more. So we provides echarts/dist/echarts.esm.mjs.
120+
// But for backward compat, we still provide provides echarts/dist/echarts.esm.js.
121+
const isBuildingDistESM = (opt.format || '').toLowerCase() === 'esm';
122+
if (isBuildingDistESM) {
123+
await makeConfigAndBuild(opt, '.js');
124+
await makeConfigAndBuild(opt, '.mjs');
125+
}
126+
else {
127+
await makeConfigAndBuild(opt);
128+
}
129+
130+
async function makeConfigAndBuild(opt, fileExtension) {
131+
const cfgs = types.map(type =>
132+
config.createECharts({
133+
...opt,
134+
type,
135+
fileExtension
136+
})
137+
);
138+
await build(cfgs);
139+
}
121140
}
122141
}
123142

build/config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function createAddLicensePlugin(sourcemap) {
3838
}
3939
}
4040

41-
function createOutputs(basename, { min }, commonOutputOpts) {
41+
function createOutputs(basename, { min, fileExtension }, commonOutputOpts) {
4242
commonOutputOpts = {
4343
format: 'umd',
4444
...commonOutputOpts
@@ -59,7 +59,7 @@ function createOutputs(basename, { min }, commonOutputOpts) {
5959
createReplacePlugin('development'),
6060
createAddLicensePlugin(true)
6161
],
62-
file: basename + '.js'
62+
file: basename + (fileExtension || '.js')
6363
}];
6464

6565
if (min) {
@@ -73,7 +73,7 @@ function createOutputs(basename, { min }, commonOutputOpts) {
7373
terser(),
7474
createAddLicensePlugin(false)
7575
],
76-
file: basename + '.min.js'
76+
file: basename + '.min' + (fileExtension || '.js')
7777
})
7878
}
7979
return output;
@@ -86,6 +86,7 @@ function createOutputs(basename, { min }, commonOutputOpts) {
8686
* @param {string} [opt.format='umd'] If set, `opt.input` is required too, and `opt.type` is ignored.
8787
* @param {string} [opt.min=false] If build minified output
8888
* @param {boolean} [opt.addBundleVersion=false] Only for debug in watch, prompt that the two build is different.
89+
* @param {string} [opt.fileExtension=undefined] output file extension, default is '.js'. Should start with '.'.
8990
*/
9091
exports.createECharts = function (opt = {}) {
9192
const srcType = opt.type !== 'all' ? '.' + opt.type : '';

package.README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# NOTICE about package.json
22

3+
See more details about in the "exports" field of `package.json` and why it is written like that in https://github.com/apache/echarts/pull/19513 .
4+
5+
6+
## Public and private
7+
38
Only these entries are officially exported to users:
49
+ `'echarts'`
510
+ `'echarts/index.js'`
@@ -20,6 +25,13 @@ Only these entries are officially exported to users:
2025

2126
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.
2227

28+
29+
## File extension fully specified
30+
2331
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.
2432

25-
See more details about in the "exports" field of `package.json` and why it is written like that in https://github.com/apache/echarts/pull/19513 .
33+
34+
## Use physical entry file or alias in `"exports"` of `package.json`
35+
36+
Although using `"exports"` of `package.json` we can make alias (or say, route) to physical file (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 file 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`).
37+

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
".": {
108108
"types": "./index.d.ts",
109109
"import": "./index.js",
110-
"require": "./index.js"
110+
"require": "./dist/echarts.js"
111111
},
112112
"./core": "./core.js",
113113
"./core.js": "./core.js",
@@ -200,6 +200,18 @@
200200
"./lib/component/visualMap": "./lib/component/visualMap.js",
201201
"./lib/component/visualMapContinuous": "./lib/component/visualMapContinuous.js",
202202
"./lib/component/visualMapPiecewise": "./lib/component/visualMapPiecewise.js",
203+
"./dist/echarts.common": "./dist/echarts.common.js",
204+
"./dist/echarts.common.min": "./dist/echarts.common.min.js",
205+
"./dist/echarts.esm": "./dist/echarts.esm.mjs",
206+
"./dist/echarts.esm.min": "./dist/echarts.esm.min.mjs",
207+
"./dist/echarts": "./dist/echarts.js",
208+
"./dist/echarts.min": "./dist/echarts.min.js",
209+
"./dist/echarts.simple": "./dist/echarts.simple.js",
210+
"./dist/echarts.simple.min": "./dist/echarts.simple.min.js",
211+
"./dist/extension/bmap": "./dist/extension/bmap.js",
212+
"./dist/extension/bmap.min": "./dist/extension/bmap.min.js",
213+
"./dist/extension/dataTool": "./dist/extension/dataTool.js",
214+
"./dist/extension/dataTool.min": "./dist/extension/dataTool.min.js",
203215
"./*": "./*"
204216
}
205217
}

0 commit comments

Comments
 (0)