diff --git a/build/build.js b/build/build.js index 1d20e6e610..176449fb8b 100755 --- a/build/build.js +++ b/build/build.js @@ -26,7 +26,6 @@ const chalk = require('chalk'); const rollup = require('rollup'); const prePublish = require('./pre-publish'); const transformDEV = require('./transform-dev'); -const preamble = require('./preamble'); async function run() { diff --git a/build/pre-publish.js b/build/pre-publish.js index cca4b54424..d82ee0c449 100644 --- a/build/pre-publish.js +++ b/build/pre-publish.js @@ -39,6 +39,7 @@ const transformDEVUtil = require('./transform-dev'); const preamble = require('./preamble'); const dts = require('@lang/rollup-plugin-dts').default; const rollup = require('rollup'); +const { transformImport } = require('zrender/build/transformImport'); const ecDir = nodePath.resolve(__dirname, '..'); const tmpDir = nodePath.resolve(ecDir, 'pre-publish-tmp'); @@ -109,8 +110,8 @@ const compileWorkList = [ transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.common.js'), esmDir); transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.simple.js'), esmDir); - await transformDistributionFiles(nodePath.resolve(ecDir, esmDir), esmDir); - await transformDistributionFiles(nodePath.resolve(ecDir, 'types'), esmDir); + await transformLibFiles(nodePath.resolve(ecDir, esmDir), esmDir); + await transformLibFiles(nodePath.resolve(ecDir, 'types'), esmDir); fsExtra.removeSync(tmpDir); } }, @@ -131,7 +132,7 @@ const compileWorkList = [ fsExtra.removeSync(extensionESMDir); }, after: async function () { - await transformDistributionFiles(extensionESMDir, 'lib'); + await transformLibFiles(extensionESMDir, 'lib'); } } ]; @@ -245,15 +246,15 @@ function transformRootFolderInEntry(entryFile, replacement) { fs.writeFileSync( entryFile, // Also transform zrender. - singleTransformZRRootFolder(code, replacement), + singleTransformImport(code, replacement), 'utf-8' ); } /** - * Transform `zrender/src` to `zrender/esm` in all files + * Transform `zrender/src` to `zrender/lib` in all files */ -async function transformDistributionFiles(rooltFolder, replacement) { +async function transformLibFiles(rooltFolder, replacement) { const files = await readFilePaths({ patterns: ['**/*.js', '**/*.d.ts'], cwd: rooltFolder @@ -262,7 +263,7 @@ async function transformDistributionFiles(rooltFolder, replacement) { // TODO More robust way? for (let fileName of files) { let code = fs.readFileSync(fileName, 'utf-8'); - code = singleTransformZRRootFolder(code, replacement); + code = singleTransformImport(code, replacement); // For lower ts version, not use import type // TODO Use https://github.com/sandersn/downlevel-dts ? // if (fileName.endsWith('.d.ts')) { @@ -272,8 +273,33 @@ async function transformDistributionFiles(rooltFolder, replacement) { } } -function singleTransformZRRootFolder(code, replacement) { - return code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`); +/** + * 1. Transform zrender/src to zrender/lib + * 2. Add .js extensions + */ +function singleTransformImport(code, replacement) { + return transformImport( + code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`), + (moduleName) => { + // Ignore 'tslib' and 'echarts' in the extensions. + if (moduleName === 'tslib' || moduleName === 'echarts') { + return moduleName; + } + else if (moduleName === 'zrender/lib/export') { + throw new Error('Should not import the whole zrender library.'); + } + else if (moduleName.endsWith('.ts')) { + // Replace ts with js + return moduleName.replace(/\.ts$/, '.js'); + } + else if (moduleName.endsWith('.js')) { + return moduleName; + } + else { + return moduleName + '.js' + } + } + ); } // function singleTransformImportType(code) { diff --git a/package-lock.json b/package-lock.json index ab83eb9d03..4222fd8289 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11122,9 +11122,9 @@ } }, "zrender": { - "version": "npm:zrender-nightly@5.3.0-dev.20211222", - "resolved": "https://registry.npmjs.org/zrender-nightly/-/zrender-nightly-5.3.0-dev.20211222.tgz", - "integrity": "sha512-pwfuY4PVOfGV/RJ0BE3oJv2PXNvRR8E7HfjbPn6/HQ20DqT+We23fI39EhEFNUWKP5dLHKl+EGb+qpnxQQDVJg==", + "version": "npm:zrender-nightly@5.3.0-dev.20211224", + "resolved": "https://registry.npmjs.org/zrender-nightly/-/zrender-nightly-5.3.0-dev.20211224.tgz", + "integrity": "sha512-xpGRPACeiHz8F+wKgLs+WZrIsRietAuLCvNXgvMPkax1Hv1u+Gu+yVRSYkmCP3vjLHt93slax66FJteazeOeDg==", "requires": { "tslib": "2.3.0" } diff --git a/package.json b/package.json index 4b6d2fb866..73ec8033eb 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ }, "dependencies": { "tslib": "2.3.0", - "zrender": "npm:zrender-nightly@^5.3.0-dev.20211222" + "zrender": "npm:zrender-nightly@^5.3.0-dev.20211224" }, "devDependencies": { "@babel/code-frame": "7.10.4", diff --git a/test/browser-esm-partial.html b/test/browser-esm-partial.html new file mode 100644 index 0000000000..442e95b773 --- /dev/null +++ b/test/browser-esm-partial.html @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + +
+

Tests ESM lib files importing in browser partially

+
+ + + + + + + + diff --git a/test/browser-esm-partial2.html b/test/browser-esm-partial2.html new file mode 100644 index 0000000000..012a2315b1 --- /dev/null +++ b/test/browser-esm-partial2.html @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + +
+

Tests ESM lib files importing in browser partially

+
+ + + + + + + + diff --git a/test/browser-esm.html b/test/browser-esm.html index 7cd4a7fb05..dd2c9a6cd3 100644 --- a/test/browser-esm.html +++ b/test/browser-esm.html @@ -53,7 +53,7 @@
-

Tests for focus and blurScope

+

Tests ESM bundle importing in browser

+ + + + + + + + +
+

Tests ESM lib files importing in browser

+
+ + + + + + + +