diff --git a/packages/consumer/package.json b/packages/consumer/package.json index 67b681a7..c3aa4b01 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" @@ -26,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/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/package.json b/packages/core/package.json index af08c44d..7bb0ed03 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" @@ -27,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/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/package.json b/packages/distributor/package.json index 54efead0..fa189240 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" @@ -33,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/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/scripts/build.js b/scripts/build.js index 69b4da94..676082c4 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -15,18 +15,53 @@ 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'; + 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 +73,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 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,