-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathbuild.js
179 lines (147 loc) · 5.37 KB
/
build.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const {transform} = require('@babel/core');
const outputFileSync = require('output-file-sync');
const babelConfig = require('../../babel.config');
const {getPackage} = require('./package');
const {execSync} = require('./exec');
function buildFile(filename, destination, babelOptions = {}) {
const options = Object.assign({}, babelOptions);
const content = fs.readFileSync(filename, {encoding: 'utf8'});
const ext = path.extname(filename);
const outputPath = path.join(destination, path.basename(filename));
// Ignore non-JS files and test scripts
if (!filename.includes('.test.')) {
if (ext === '.js') {
options.filename = filename;
const result = transform(content, options);
return outputFileSync(outputPath, result.code, {encoding: 'utf8'});
}
// process with postcss if it's a css file
if (ext === '.css') {
return execSync(`postcss ${filename} -o ${outputPath}`);
}
// Copy if it's any other type of file
return outputFileSync(outputPath, content);
}
return false;
}
function babelBuild(folderPath, destination, babelOptions = {}, firstFolder = true) {
const stats = fs.statSync(folderPath);
if (stats.isFile()) {
try {
buildFile(folderPath, destination, babelOptions);
}
catch (err) {
throw new Error(`Error transpiling ${folderPath} package, ${err}`, err);
}
}
else if (stats.isDirectory()) {
const outputPath = firstFolder ? destination : path.join(destination, path.basename(folderPath));
const files = fs.readdirSync(folderPath).map((file) => path.join(folderPath, file));
files.forEach((filename) => {
// Ignore fixtures, mocks, and snapshots
if (!filename.includes('__')) {
babelBuild(filename, outputPath, babelOptions, false);
}
});
}
}
/**
* Builds a specific package with Webpack
* @param {string} pkgName
* @param {string} pkgPath
* @returns {undefined}
*/
function webpackBuild(pkgName, pkgPath) {
const targetPkgPath = pkgPath || getPackage(pkgName);
console.log('pkgPath:',`${pkgName}`)
if (`${pkgName}` === 'widget-call-history'||`${pkgName}` === 'widget-number-pad' || `${pkgName}` === 'widget-speed-dial' || `${pkgName}` === 'webex-sign-in-page' || `${pkgName}` === 'widget-voice-mail') {
try {
const webpackConfigPath = path.resolve(__dirname, '..', 'webpack', 'webpack-calling.prod.babel.js');
// Delete dist folder
console.info(`Cleaning ${pkgName} dist folder...`.cyan);
rimraf.sync(path.resolve(targetPkgPath, 'dist'));
console.info(`Bundling ${pkgName}...`.cyan);
execSync(`cd ${targetPkgPath} && webpack --config ${webpackConfigPath} --env.package=${pkgName}`);
console.info(`${pkgName}... Done\n\n`.cyan);
}
catch (err) {
throw new Error(`Error building ${pkgName} package, ${err}`, err);
}
}
else if (targetPkgPath) {
try {
const webpackConfigPath = path.resolve(__dirname, '..', 'webpack', 'webpack.prod.babel.js');
// Delete dist folder
console.info(`Cleaning ${pkgName} dist folder...`.cyan);
rimraf.sync(path.resolve(targetPkgPath, 'dist'));
console.info(`Bundling ${pkgName}...`.cyan);
execSync(`cd ${targetPkgPath} && webpack --config ${webpackConfigPath} --env.package=${pkgName}`);
console.info(`${pkgName}... Done\n\n`.cyan);
}
catch (err) {
throw new Error(`Error building ${pkgName} package, ${err}`, err);
}
}
return false;
}
/**
* Build a package to CommonJS
* @param {String} pkgName
* @param {String} pkgPath
* @returns {undefined}
*/
function buildCommonJS(pkgName, pkgPath) {
console.info(`Cleaning ${pkgName} cjs folder...`.cyan);
rimraf.sync(path.resolve(pkgPath, 'cjs'));
console.info(`Transpiling ${pkgName} to CommonJS...`.cyan);
babelConfig.plugins.push('transform-postcss');
babelBuild(`${pkgPath}/src`, `${pkgPath}/cjs`, babelConfig);
}
/**
* Build a package to ES5 with import/export
* @param {String} pkg
* @returns {undefined}
*/
function buildES(pkg) {
const targetPkgPath = getPackage(pkg);
if (targetPkgPath) {
try {
const rollupConfigPath = path.resolve(__dirname, '..', '..', 'rollup.config.js');
const callingRollupConfigPath = path.resolve(__dirname, '..', '..', 'rollup.calling-config.js');
if(`${pkg}` === '@webex/widget-call-history' || `${pkg}` === '@webex/widget-number-pad' || `${pkg}` === '@webex/widget-speed-dial' || `${pkg}` === '@webex/webex-sign-in-page' || `${pkg}` ==='@webex/widget-voice-mail'){
// Rollup cleans the `es` folder automatically
console.info(`Packaging ${pkg}...`.cyan);
execSync(`cd ${targetPkgPath} && rollup -c ${callingRollupConfigPath}`);
}else{
// Rollup cleans the `es` folder automatically
console.info(`Packaging ${pkg}...`.cyan);
execSync(`cd ${targetPkgPath} && rollup -c ${rollupConfigPath}`);
}
}
catch (err) {
throw new Error(`Error building ${pkg} package, ${err}`, err);
}
}
return false;
}
/**
* Build a package to ES5 and CommonJS
* @param {String} pkgName
* @param {String} pkgPath
* @returns {Promise}
*/
function transpile(pkgName, pkgPath) {
return Promise.all([
buildES(pkgName, pkgPath),
buildCommonJS(pkgName, pkgPath)
]);
}
module.exports = {
webpackBuild,
buildCommonJS,
buildES,
transpile
};