From e63da29c33c14952963298daa06a26276128fbda Mon Sep 17 00:00:00 2001 From: Arthur Ming Date: Fri, 11 Feb 2022 16:16:15 +0800 Subject: [PATCH 1/3] build: migrate the module system from commonjs to es6 --- packages/consumer/tsconfig.json | 1 - packages/core/tsconfig.json | 1 - packages/distributor/tsconfig.json | 1 - packages/examples/tsconfig.json | 1 - tsconfig.json | 4 ++-- 5 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/consumer/tsconfig.json b/packages/consumer/tsconfig.json index b6bcc972..8e149208 100644 --- a/packages/consumer/tsconfig.json +++ b/packages/consumer/tsconfig.json @@ -4,7 +4,6 @@ "baseUrl": ".", "target": "es6", "lib": ["es6", "esnext", "dom"], - "module": "commonjs", "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 9ef27a40..7703bc54 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -4,7 +4,6 @@ "baseUrl": ".", "target": "esnext", "lib": ["es6", "esnext", "dom"], - "module": "commonjs", "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, diff --git a/packages/distributor/tsconfig.json b/packages/distributor/tsconfig.json index b6bcc972..8e149208 100644 --- a/packages/distributor/tsconfig.json +++ b/packages/distributor/tsconfig.json @@ -4,7 +4,6 @@ "baseUrl": ".", "target": "es6", "lib": ["es6", "esnext", "dom"], - "module": "commonjs", "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, diff --git a/packages/examples/tsconfig.json b/packages/examples/tsconfig.json index c7a80419..26c8443e 100644 --- a/packages/examples/tsconfig.json +++ b/packages/examples/tsconfig.json @@ -4,7 +4,6 @@ "baseUrl": ".", "target": "esnext", "lib": ["es6", "esnext", "dom"], - "module": "commonjs", "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, diff --git a/tsconfig.json b/tsconfig.json index 54bfb5e8..8d7d8806 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { "baseUrl": ".", - "target": "es5", + "target": "es6", "lib": ["es6", "esnext", "dom"], - "module": "commonjs", + "module": "es6", "moduleResolution": "node", "sourceMap": false, "experimentalDecorators": true, From af4d061c25aa5c040c1d627b201bbddc17d99d30 Mon Sep 17 00:00:00 2001 From: Arthur Ming Date: Fri, 11 Feb 2022 17:07:46 +0800 Subject: [PATCH 2/3] build: both support commonjs and es6 module system --- packages/consumer/package.json | 2 - packages/core/package.json | 2 - packages/distributor/package.json | 2 - scripts/build.js | 61 +++++++++++++++++++++++++------ 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/packages/consumer/package.json b/packages/consumer/package.json index 67b681a7..0054b9ed 100644 --- a/packages/consumer/package.json +++ b/packages/consumer/package.json @@ -9,8 +9,6 @@ "author": "Power Bot Kit Team", "homepage": "https://github.com/PowerBotKit/powerbot/tree/develop/packages/consumer#readme", "license": "MIT", - "main": "./index.js", - "types": "./index.d.ts", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/core/package.json b/packages/core/package.json index af08c44d..a3907c7a 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -9,8 +9,6 @@ "author": "Power Bot Kit Team", "homepage": "https://github.com/PowerBotKit/powerbot/tree/develop/packages/core#readme", "license": "MIT", - "main": "./index.js", - "types": "./index.d.ts", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/distributor/package.json b/packages/distributor/package.json index 54efead0..59e47088 100644 --- a/packages/distributor/package.json +++ b/packages/distributor/package.json @@ -9,8 +9,6 @@ "author": "Power Bot Kit Team", "homepage": "https://github.com/PowerBotKit/powerbot/tree/develop/packages/distributor#readme", "license": "MIT", - "main": "./index.js", - "types": "./index.d.ts", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/scripts/build.js b/scripts/build.js index 69b4da94..3f9e43e1 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -15,18 +15,57 @@ const shelljs = require('shelljs'); const { fetchTargets, fetchTopologicalSorting } = require('./utils'); +function buildES6(target) { + const { code } = shelljs.exec( + `yarn tsc --project ${path.join( + target.location, + 'tsconfig.json' + )} --outDir ${path.resolve('./dist', target.folderName, 'lib')}` + ); + + if (code !== 0) { + throw new Error(`fail to compile the ${target.name}`); + } +} + +function buildCommonjs(target) { + const { code } = shelljs.exec( + `yarn tsc --project ${path.join( + target.location, + 'tsconfig.json' + )} --outDir ${path.resolve( + './dist', + target.folderName, + 'src' + )} --module commonjs` + ); + + if (code !== 0) { + throw new Error(`fail to compile the ${target.name}`); + } +} + +async function updatePackageJson(target) { + const packageJsonPath = `${path.resolve( + './dist', + target.folderName, + 'package.json' + )}`; + const json = await fsEx.readJson(packageJsonPath); + json.main = './src/index.js'; + json.types = './src/index.d.ts'; + json.module = './lib/index.js'; + json.exports = { + require: './src/index.js', + import: './lib/index.js' + }; + await fsEx.writeJson(packageJsonPath, json, { spaces: 2 }); +} + async function buildTarget(target) { if (target.private !== true) { - const { code } = shelljs.exec( - `yarn tsc --project ${path.join( - target.location, - 'tsconfig.json' - )} --outDir ${path.resolve('./dist', target.folderName)}` - ); - - if (code !== 0) { - throw new Error(`fail to compile the ${target.name}`); - } + buildES6(target); + buildCommonjs(target); const files = ['package.json', 'README.md', 'LICENSE']; const copyTasks = files .map(file => { @@ -38,7 +77,7 @@ async function buildTarget(target) { .map(f => fs.copyFile(f.src, f.dest)); await Promise.all(copyTasks); console.log(`${colors.blue(target.name)} ${colors.green('success')} 🚀`); - + updatePackageJson(target); await fs.rm(`${path.resolve('./node_modules/' + target.name)}`, { force: true, recursive: true From 6b2d00c9ab2866689f47b9813c2ab5806b2baf43 Mon Sep 17 00:00:00 2001 From: Arthur Ming Date: Tue, 1 Mar 2022 18:37:06 +0800 Subject: [PATCH 3/3] build: add 'exports' field --- packages/consumer/package.json | 6 ++++++ packages/core/package.json | 21 +++++++++++++++++++++ packages/distributor/package.json | 6 ++++++ scripts/build.js | 4 ---- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/consumer/package.json b/packages/consumer/package.json index 0054b9ed..c3aa4b01 100644 --- a/packages/consumer/package.json +++ b/packages/consumer/package.json @@ -24,5 +24,11 @@ "adaptivecards-templating": "^2.2.0", "js-yaml": "^4.1.0", "redis": "^3.0.2" + }, + "exports": { + ".": { + "require": "./src/index.js", + "import": "./lib/index.js" + } } } diff --git a/packages/core/package.json b/packages/core/package.json index a3907c7a..7bb0ed03 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -25,5 +25,26 @@ }, "devDependencies": { "@types/redis": "^2.8.32" + }, + "exports": { + ".": { + "require": "./src/index.js", + "import": "./lib/index.js" + }, + "./template": { + "types": "./src/template/index.d.ts", + "require": "./src/template/index.js", + "import": "./lib/template/index.js" + } + }, + "typesVersions": { + "*": { + "*": [ + "./src/index.d.ts" + ], + "template": [ + "./src/template/index.d.ts" + ] + } } } diff --git a/packages/distributor/package.json b/packages/distributor/package.json index 59e47088..fa189240 100644 --- a/packages/distributor/package.json +++ b/packages/distributor/package.json @@ -31,5 +31,11 @@ "devDependencies": { "@types/restify": "^8.5.4", "nodemon": "^2.0.13" + }, + "exports": { + ".": { + "require": "./src/index.js", + "import": "./lib/index.js" + } } } diff --git a/scripts/build.js b/scripts/build.js index 3f9e43e1..676082c4 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -55,10 +55,6 @@ async function updatePackageJson(target) { json.main = './src/index.js'; json.types = './src/index.d.ts'; json.module = './lib/index.js'; - json.exports = { - require: './src/index.js', - import: './lib/index.js' - }; await fsEx.writeJson(packageJsonPath, json, { spaces: 2 }); }