UNPKG

2.09 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Created by chkui on 2017/6/22.
5 */
6
7require('babel-polyfill');
8require('babel-register')({
9 presets: ['es2015', 'react', 'stage-0'],
10 plugins: ['transform-runtime', 'add-module-exports']
11});
12
13var init = require('./lib/common/init'),
14 log = require('./lib/common/log'),
15 deleteDir = require('./lib/common/deleteDir'),
16 path = require('path');
17
18/**
19 * 构建服务器运行的包。
20 * 1)服务器的包会使用webpack打包生产适用于生产运行的包。
21 * 2)打包后会生成如下结构
22 * --[dist]
23 * ----[client]
24 * ------.js file
25 * ------.html file
26 * ----[server]
27 * ------.js file
28 * ------.html file
29 * [dist]由 path.resolve(workDir, outPath)确定
30 * [client]由 path.resolve(workDir, outPath, clientPath)确定
31 * [server]由 path.resolve(workDir, outPath, serverPath)确定
32 * 3)打包完成后会生成在[dist][server]文件夹生成一个 [server.js] 文件(由serverEntryName参数指定名称)
33 * 4)直接运行[server.js]文件即可启动服务器: $ node [dist][server][server.js]
34 * @param {object} options {
35 * //options的配置详见 {./config/context}
36 * }
37 * @param cb 打包完成后的回调函数,如果没有则不执行 cb:()=>{}
38 */
39var builder = function builder(options, cb) {
40 log("building file!");
41 options = options || {};
42 options.isProd = true;
43
44 //初始化环境参数
45 var opts = init(options),
46 env = opts.getEnv(),
47 distPath = path.resolve(env.workDir, env.outPath);
48 //清空打包文件夹
49 log('remove dist file:', distPath);
50 deleteDir(distPath, false);
51
52 var webpack = require('webpack'),
53 config = require('./lib/scripts/webpack.ser'),
54 compiler = webpack(config);
55 compiler.run(function (err, stats) {
56 err ? function () {
57 log('build error! info:', err);
58 process.exit(0);
59 }() : function () {
60 log("build success");
61 cb ? cb() : process.exit(0);
62 }();
63 });
64};
65
66module.exports = builder;
\No newline at end of file