Skip to content

Commit ca992c3

Browse files
committed
build: both support commonjs and es6 module system
1 parent 0ade963 commit ca992c3

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

packages/consumer/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"author": "Power Bot Kit Team",
1010
"homepage": "https://github.com/PowerBotKit/powerbot/tree/develop/packages/consumer#readme",
1111
"license": "MIT",
12-
"main": "./index.js",
13-
"types": "./index.d.ts",
1412
"publishConfig": {
1513
"access": "public",
1614
"registry": "https://registry.npmjs.org"

packages/core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"author": "Power Bot Kit Team",
1010
"homepage": "https://github.com/PowerBotKit/powerbot/tree/develop/packages/core#readme",
1111
"license": "MIT",
12-
"main": "./index.js",
13-
"types": "./index.d.ts",
1412
"publishConfig": {
1513
"access": "public",
1614
"registry": "https://registry.npmjs.org"

packages/distributor/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"author": "Power Bot Kit Team",
1010
"homepage": "https://github.com/PowerBotKit/powerbot/tree/develop/packages/distributor#readme",
1111
"license": "MIT",
12-
"main": "./index.js",
13-
"types": "./index.d.ts",
1412
"publishConfig": {
1513
"access": "public",
1614
"registry": "https://registry.npmjs.org"

scripts/build.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,57 @@ const shelljs = require('shelljs');
1515

1616
const { fetchTargets, fetchTopologicalSorting } = require('./utils');
1717

18+
function buildES6(target) {
19+
const { code } = shelljs.exec(
20+
`yarn tsc --project ${path.join(
21+
target.location,
22+
'tsconfig.json'
23+
)} --outDir ${path.resolve('./dist', target.folderName, 'lib')}`
24+
);
25+
26+
if (code !== 0) {
27+
throw new Error(`fail to compile the ${target.name}`);
28+
}
29+
}
30+
31+
function buildCommonjs(target) {
32+
const { code } = shelljs.exec(
33+
`yarn tsc --project ${path.join(
34+
target.location,
35+
'tsconfig.json'
36+
)} --outDir ${path.resolve(
37+
'./dist',
38+
target.folderName,
39+
'src'
40+
)} --module commonjs`
41+
);
42+
43+
if (code !== 0) {
44+
throw new Error(`fail to compile the ${target.name}`);
45+
}
46+
}
47+
48+
async function updatePackageJson(target) {
49+
const packageJsonPath = `${path.resolve(
50+
'./dist',
51+
target.folderName,
52+
'package.json'
53+
)}`;
54+
const json = await fsEx.readJson(packageJsonPath);
55+
json.main = './src/index.js';
56+
json.types = './src/index.d.ts';
57+
json.module = './lib/index.js';
58+
json.exports = {
59+
require: './src/index.js',
60+
import: './lib/index.js'
61+
};
62+
await fsEx.writeJson(packageJsonPath, json, { spaces: 2 });
63+
}
64+
1865
async function buildTarget(target) {
1966
if (target.private !== true) {
20-
const { code } = shelljs.exec(
21-
`yarn tsc --project ${path.join(
22-
target.location,
23-
'tsconfig.json'
24-
)} --outDir ${path.resolve('./dist', target.folderName)}`
25-
);
26-
27-
if (code !== 0) {
28-
throw new Error(`fail to compile the ${target.name}`);
29-
}
67+
buildES6(target);
68+
buildCommonjs(target);
3069
const files = ['package.json', 'README.md', 'LICENSE'];
3170
const copyTasks = files
3271
.map(file => {
@@ -38,7 +77,7 @@ async function buildTarget(target) {
3877
.map(f => fs.copyFile(f.src, f.dest));
3978
await Promise.all(copyTasks);
4079
console.log(`${chalk.blue(target.name)} ${chalk.green('success')} 🚀`);
41-
80+
updatePackageJson(target);
4281
await fs.rm(`${path.resolve('./node_modules/' + target.name)}`, {
4382
force: true,
4483
recursive: true

0 commit comments

Comments
 (0)