From 5e774ebaea2c0267b6b32dafa6679de668206cba Mon Sep 17 00:00:00 2001 From: pissang Date: Fri, 24 Dec 2021 12:56:51 +0800 Subject: [PATCH 1/4] chore: add .js extension in the import statement --- build/build.js | 1 - build/pre-publish.js | 51 ++++++++++--- test/browser-esm-partial.html | 111 +++++++++++++++++++++++++++++ test/browser-esm-partial2.html | 109 ++++++++++++++++++++++++++++ test/browser-esm.html | 2 +- test/browser-esm2.html | 126 +++++++++++++++++++++++++++++++++ 6 files changed, 389 insertions(+), 11 deletions(-) create mode 100644 test/browser-esm-partial.html create mode 100644 test/browser-esm-partial2.html create mode 100644 test/browser-esm2.html 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..57a7826474 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,40 @@ 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 + * + * FIXME. Comment in the named import statement is not supported. + * + * import { + * AAAA, + * // BBB + * } from 'foo'; + */ +function singleTransformImport(code, replacement) { + return transformImport( + code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`), + (moduleName) => { + // Ignore 'tslib' + if (moduleName === 'tslib') { + return moduleName; + } + else if (moduleName === 'zrender' || 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/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

+
+ + + + + + + + From 7ce052daa49f6e9d4c654114547c19b48d009667 Mon Sep 17 00:00:00 2001 From: pissang Date: Fri, 24 Dec 2021 13:27:19 +0800 Subject: [PATCH 2/4] chore: fix extension build error --- build/pre-publish.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build/pre-publish.js b/build/pre-publish.js index 57a7826474..a3b400e483 100644 --- a/build/pre-publish.js +++ b/build/pre-publish.js @@ -288,11 +288,11 @@ function singleTransformImport(code, replacement) { return transformImport( code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`), (moduleName) => { - // Ignore 'tslib' - if (moduleName === 'tslib') { + // Ignore 'tslib' and 'echarts' in the extensions. + if (moduleName === 'tslib' || moduleName === 'echarts') { return moduleName; } - else if (moduleName === 'zrender' || moduleName === 'zrender/lib/export') { + else if (moduleName === 'zrender/lib/export') { throw new Error('Should not import the whole zrender library.'); } else if (moduleName.endsWith('.ts')) { From a190e040ba82a7b95e9f829d0441c6fd9913e424 Mon Sep 17 00:00:00 2001 From: pissang Date: Fri, 24 Dec 2021 14:14:49 +0800 Subject: [PATCH 3/4] chore: remove outdated comment. --- build/pre-publish.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/build/pre-publish.js b/build/pre-publish.js index a3b400e483..d82ee0c449 100644 --- a/build/pre-publish.js +++ b/build/pre-publish.js @@ -276,13 +276,6 @@ async function transformLibFiles(rooltFolder, replacement) { /** * 1. Transform zrender/src to zrender/lib * 2. Add .js extensions - * - * FIXME. Comment in the named import statement is not supported. - * - * import { - * AAAA, - * // BBB - * } from 'foo'; */ function singleTransformImport(code, replacement) { return transformImport( From 166eb6bf3209af05a5d05d47ad739356771a74c6 Mon Sep 17 00:00:00 2001 From: pissang Date: Fri, 24 Dec 2021 14:22:53 +0800 Subject: [PATCH 4/4] chore: update zrender to latest --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) 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",