/
index.ts
156 lines (146 loc) · 3.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { sync as resolveBin } from 'resolve-bin';
import { fork } from 'child_process';
import { join } from 'path';
import fs from 'fs';
import Copyfile from './copyfile';
import build from 'af-webpack/build';
import camelcase from 'camelcase';
class LibraryBuild {
private babel: string;
private babelRc: string;
private hasIndex: boolean;
private baseFolder: string;
private api: any;
private cwd: string;
constructor(api) {
this.api = api;
this.cwd = api.paths.cwd || process.cwd();
this.babel = resolveBin('@babel/cli', { executable: 'babel' });
this.babelRc = join(__dirname, 'babel.config');
this.baseFolder = this.getBaseFolder();
this.hasIndex = fs.existsSync(join(this.baseFolder, 'index.js'));
}
public async build() {
try {
await this.beforeBuild();
await Promise.all([this.buildEs5(), this.buildEs6(), this.buildUmd()]);
await new Copyfile(this.baseFolder, this.cwd).run();
this.aferBuild();
} catch (error) {
this.api.debug(error);
}
}
private getBaseFolder() {
let baseFolder = join(this.cwd, 'src/component');
if (!fs.existsSync(baseFolder)) {
baseFolder = join(this.cwd, 'src/components');
}
if (!fs.existsSync(baseFolder)) {
baseFolder = join(this.cwd, 'components');
}
if (!fs.existsSync(baseFolder)) {
baseFolder = join(this.cwd, 'component');
}
if (!fs.existsSync(baseFolder)) {
throw new Error('components folder not found!');
}
return baseFolder;
}
private async beforeBuild() {
return new Promise(resolve => {
const rimraf = resolveBin('rimraf');
fork(rimraf, ['./umd']);
fork(rimraf, ['./lib']);
fork(rimraf, ['./es']);
if (!this.hasIndex) {
const createIndex = resolveBin('create-index');
const child = fork(createIndex, [this.baseFolder]);
child.on('exit', () => {
resolve();
});
} else {
resolve();
}
});
}
private buildEs5() {
return new Promise(resolve => {
const child = fork(
this.babel,
['--config-file', this.babelRc, this.baseFolder, '--out-dir', './lib'],
{
env: {
BABEL_ENV: 'es5',
},
}
);
child.on('exit', () => {
resolve();
});
});
}
private buildEs6() {
return new Promise(resolve => {
const child = fork(
this.babel,
['--config-file', this.babelRc, this.baseFolder, '--out-dir', './es'],
{
env: {
BABEL_ENV: 'es6',
},
}
);
child.on('exit', () => {
resolve();
});
});
}
private buildUmd() {
return new Promise(resolve => {
const { debug, webpackConfig: config } = this.api;
const webpackConfig = {
...config,
externals: {
react: 'window.React',
'react-dom': 'window.ReactDOM',
...config.externals,
},
entry: join(this.baseFolder, 'index'),
output: {
path: join(this.cwd, './umd'),
filename: '[name].js',
library: camelcase(this.api.pkg.name),
libraryTarget: 'umd',
umdNamedDefine: true,
},
};
build({
cwd: this.cwd,
webpackConfig,
onSuccess() {
// debug(stats);
resolve();
},
onFail({ err }) {
debug(err);
resolve();
},
});
});
}
private aferBuild() {
// 如果原本没有 index, 需要删除.
if (!this.hasIndex) {
fs.unlinkSync(join(this.baseFolder, 'index.js'));
}
// tslint:disable-next-line
console.log('build umi library done!');
}
}
export default (api, _, args) => {
const subCommand = args._[0];
if (subCommand === 'build') {
const bundler = new LibraryBuild(api);
bundler.build();
}
};