You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2020/11/05 10:02:55 UTC

[incubator-echarts] 02/02: chore(type): add dts bundle

This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch bundle-dts
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git

commit e7b7171afd8c5c547c4f89e2554785ee9c95467c
Author: pissang <bm...@gmail.com>
AuthorDate: Thu Nov 5 18:02:35 2020 +0800

    chore(type): add dts bundle
---
 build/pre-publish.js | 108 +++++++++++++++++++++++++++++----------------------
 package.json         |   3 +-
 2 files changed, 63 insertions(+), 48 deletions(-)

diff --git a/build/pre-publish.js b/build/pre-publish.js
index b3c4b7d..29982e7 100644
--- a/build/pre-publish.js
+++ b/build/pre-publish.js
@@ -37,6 +37,9 @@ const ts = require('typescript');
 const globby = require('globby');
 const transformDEVUtil = require('./transform-dev');
 const preamble = require('./preamble');
+// NOTE: v1.4.2 is the latest version that supports ts 3.8.3
+const dts = require('rollup-plugin-dts').default;
+const rollup = require('rollup');
 
 const ecDir = nodePath.resolve(__dirname, '..');
 const tmpDir = nodePath.resolve(ecDir, 'pre-publish-tmp');
@@ -72,47 +75,6 @@ const typesDir = nodePath.resolve(ecDir, 'types');
 
 const compileWorkList = [
     {
-        logLabel: 'main ts -> js-cjs',
-        compilerOptionsOverride: {
-            module: 'CommonJS',
-            // `rootDir` Only use to control the output
-            // directory structure with --outDir.
-            rootDir: ecDir,
-            outDir: tmpDir
-        },
-        srcGlobby: mainSrcGlobby,
-        transformOptions: {
-            filesGlobby: {patterns: ['**/*.js'], cwd: tmpDir},
-            preamble: preamble.js,
-            transformDEV: true
-        },
-        before: async function () {
-            fsExtra.removeSync(tmpDir);
-            fsExtra.removeSync(nodePath.resolve(ecDir, 'lib'));
-            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.js'));
-            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.blank.js'));
-            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.common.js'));
-            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.simple.js'));
-        },
-        after: async function () {
-            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.all.js'), nodePath.resolve(ecDir, 'index.js'));
-            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.blank.js'), nodePath.resolve(ecDir, 'index.blank.js'));
-            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.common.js'), nodePath.resolve(ecDir, 'index.common.js'));
-            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.simple.js'), nodePath.resolve(ecDir, 'index.simple.js'));
-            fs.renameSync(nodePath.resolve(tmpDir, 'src'), nodePath.resolve(ecDir, 'lib'));
-
-            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.js'), 'lib');
-            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.blank.js'), 'lib');
-            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.common.js'), 'lib');
-            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.simple.js'), 'lib');
-
-            await transformDistributionFiles(nodePath.resolve(ecDir, 'lib'), 'lib');
-            removeESmoduleMark();
-
-            fsExtra.removeSync(tmpDir);
-        }
-    },
-    {
         logLabel: 'main ts -> js-esm',
         compilerOptionsOverride: {
             module: 'ES2015',
@@ -150,6 +112,48 @@ const compileWorkList = [
 
             await transformDistributionFiles(nodePath.resolve(ecDir, 'esm'), 'esm');
             await transformDistributionFiles(nodePath.resolve(ecDir, 'types'), 'esm');
+            fsExtra.removeSync(tmpDir);
+
+            await bundleDTS();
+        }
+    },
+    {
+        logLabel: 'main ts -> js-cjs',
+        compilerOptionsOverride: {
+            module: 'CommonJS',
+            // `rootDir` Only use to control the output
+            // directory structure with --outDir.
+            rootDir: ecDir,
+            outDir: tmpDir
+        },
+        srcGlobby: mainSrcGlobby,
+        transformOptions: {
+            filesGlobby: {patterns: ['**/*.js'], cwd: tmpDir},
+            preamble: preamble.js,
+            transformDEV: true
+        },
+        before: async function () {
+            fsExtra.removeSync(tmpDir);
+            fsExtra.removeSync(nodePath.resolve(ecDir, 'lib'));
+            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.js'));
+            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.blank.js'));
+            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.common.js'));
+            fsExtra.removeSync(nodePath.resolve(ecDir, 'index.simple.js'));
+        },
+        after: async function () {
+            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.all.js'), nodePath.resolve(ecDir, 'index.js'));
+            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.blank.js'), nodePath.resolve(ecDir, 'index.blank.js'));
+            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.common.js'), nodePath.resolve(ecDir, 'index.common.js'));
+            fs.renameSync(nodePath.resolve(tmpDir, 'src/echarts.simple.js'), nodePath.resolve(ecDir, 'index.simple.js'));
+            fs.renameSync(nodePath.resolve(tmpDir, 'src'), nodePath.resolve(ecDir, 'lib'));
+
+            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.js'), 'lib');
+            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.blank.js'), 'lib');
+            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.common.js'), 'lib');
+            transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.simple.js'), 'lib');
+
+            await transformDistributionFiles(nodePath.resolve(ecDir, 'lib'), 'lib');
+            removeESmoduleMark();
 
             fsExtra.removeSync(tmpDir);
         }
@@ -311,9 +315,9 @@ async function transformDistributionFiles(rooltFolder, replacement) {
         code = singleTransformZRRootFolder(code, replacement);
         // For lower ts version, not use import type
         // TODO Use https://github.com/sandersn/downlevel-dts ?
-        if (fileName.endsWith('.d.ts')) {
-            code = singleTransformImportType(code);
-        }
+        // if (fileName.endsWith('.d.ts')) {
+        //     code = singleTransformImportType(code);
+        // }
         fs.writeFileSync(fileName, code, 'utf-8');
     }
 }
@@ -342,9 +346,9 @@ function singleTransformZRRootFolder(code, replacement) {
     return code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`);
 }
 
-function singleTransformImportType(code) {
-    return code.replace(/import\s+type\s+/g, 'import ');
-}
+// function singleTransformImportType(code) {
+//     return code.replace(/import\s+type\s+/g, 'import ');
+// }
 
 /**
  * @param {Object} transformOptions
@@ -383,6 +387,16 @@ async function readFilePaths({patterns, cwd}) {
     );
 }
 
+async function bundleDTS() {
+    const bundle = await rollup.rollup({
+        input: nodePath.resolve(__dirname, '../index.d.ts'),
+        plugins: [dts()]
+    });
+    await bundle.write({
+        file: nodePath.resolve(__dirname, '../types/dist/echarts.d.ts')
+    });
+}
+
 function readTSConfig() {
     // tsconfig.json may have comment string, which is invalid if
     // using `require('tsconfig.json'). So we use a loose parser.
diff --git a/package.json b/package.json
index 779145a..8c4b761 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
   "main": "index.js",
   "module": "echarts.all.js",
   "jsdelivr": "dist/echarts.min.js",
-  "types": "index.d.ts",
+  "types": "types/dist/echarts.d.ts",
   "homepage": "http://echarts.apache.org",
   "repository": {
     "type": "git",
@@ -64,6 +64,7 @@
     "pngjs": "3.4.0",
     "rollup": "1.28.0",
     "rollup-plugin-commonjs": "8.4.1",
+    "rollup-plugin-dts": "1.4.2",
     "rollup-plugin-node-resolve": "3.0.0",
     "rollup-plugin-typescript2": "0.25.3",
     "seedrandom": "3.0.3",


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org