-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-script.js
162 lines (145 loc) · 4.39 KB
/
build-script.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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const tar = require('tar');
const rimraf = require('rimraf');
const execSync = require('child_process').execSync;
const webpack = require("webpack");
const packageJson = require('./package.json');
const SRC_DIR = path.join(__dirname, '/src');
const fileName = packageJson.name + '-' + packageJson.version + '-build';
const BUILD_DIR = path.join(__dirname, '/' + fileName);
const chalk = require('chalk');
console.log(chalk.bold.white.bgCyan("初始化Build目录 "));
// 创建空的build目录
if (fs.existsSync(BUILD_DIR)) {
rimraf.sync(BUILD_DIR);
}
fs.mkdirSync(BUILD_DIR);
// 运行npm shrinkwrap以获得所有依赖的版本
console.log(chalk.bold.white.bgCyan("建立npm shrinkwrap "));
execSync("npm shrinkwrap", { stdio: [0, 1, 2] });
// 读取shrinkwrap信息
let shrinkWrapJson = JSON.parse(fs.readFileSync('./npm-shrinkwrap.json', { encoding: "UTF8" }));
function buildApp() {
return new Promise((resolve, reject) => {
// 给app代码做build
const entry = {};
entry.app = SRC_DIR;
const externals = {};
console.log(chalk.bold.green("总共" + Object.keys(packageJson.dependencies).length + "个依赖项 "));
Object.keys(shrinkWrapJson.dependencies).forEach(_ => {
let ver = shrinkWrapJson.dependencies[_].version;
let depName = _ + "_" + ver;
externals[_] = {
window: ["$deps", depName],
};
});
let appBuildConf = {
context: __dirname,
entry,
externals,
output: {
path: BUILD_DIR,
filename: '[name].js',
libraryTarget: "window",
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
}, {
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
],
},
],
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
})],
};
webpack(appBuildConf, (err, stats) => {
if (err || stats.hasErrors()) {
reject(err || stats.hasErrors());
}
console.log(chalk.bold.yellow(stats.toString()));
console.log(chalk.bold.green("业务代码构建完毕 "));
resolve();
});
});
}
function buildDeps() {
return new Promise((resolve, reject) => {
// 给依赖项做build
const entry = {};
const externals = {};
const deps = {};
Object.keys(shrinkWrapJson.dependencies).forEach(_ => {
// 只有在packageJson中出现的dependency才打包
if (!packageJson.dependencies[_]) {
return;
}
let ver = shrinkWrapJson.dependencies[_].version;
let depName = _ + "_" + ver;
entry[depName] = "./node_modules/" + _;
deps[_] = ver;
});
const depsBuildConf = {
context: __dirname,
entry,
externals: externals,
output: {
path: BUILD_DIR,
filename: 'deps/[name].js',
library: ['$deps', '[name]'],
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
})],
};
const meta = {
name: packageJson.name,
version: packageJson.version,
description: packageJson.description,
deps,
};
fs.writeFileSync(path.join(BUILD_DIR, "meta.json"), JSON.stringify(meta, null, 4), { encoding: "UTF8" });
console.log(chalk.bold.white.bgCyan("构建依赖项:" + Object.keys(packageJson.dependencies).join(",")));
webpack(depsBuildConf, (err, stats) => {
console.log(chalk.bold.yellow(stats.toString()));
console.log(chalk.bold.green("依赖项构建完毕 "));
if (err || stats.hasErrors()) {
reject(err || stats.hasErrors());
}
console.log(chalk.bold.white.bgCyan("生成APP元数据:"));
console.log(chalk.bold.yellow(JSON.stringify(meta, null, 4)));
resolve();
});
});
}
buildApp().then(() => {
buildDeps().then(() => {
tar.c({
gzip: true,
portable: true,
file: fileName + '.tar.gz',
cwd: BUILD_DIR
}, fs.readdirSync(BUILD_DIR)).then(() => {
rimraf(BUILD_DIR, () => {
console.log(chalk.bold.white.bgCyan("构建完毕"));
});
});
});
});