1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = build;
|
7 |
|
8 | var _path = require('path');
|
9 |
|
10 | var _fs = require('fs');
|
11 |
|
12 | var _webpack = require('webpack');
|
13 |
|
14 | var _webpack2 = _interopRequireDefault(_webpack);
|
15 |
|
16 | var _chalk = require('chalk');
|
17 |
|
18 | var _chalk2 = _interopRequireDefault(_chalk);
|
19 |
|
20 | var _mergeCustomConfig = require('./mergeCustomConfig');
|
21 |
|
22 | var _mergeCustomConfig2 = _interopRequireDefault(_mergeCustomConfig);
|
23 |
|
24 | var _getWebpackCommonConfig = require('./getWebpackCommonConfig');
|
25 |
|
26 | var _getWebpackCommonConfig2 = _interopRequireDefault(_getWebpackCommonConfig);
|
27 |
|
28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
29 |
|
30 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
31 |
|
32 | function getWebpackConfig(args) {
|
33 | var webpackConfig = (0, _getWebpackCommonConfig2.default)(args);
|
34 |
|
35 | webpackConfig.plugins = webpackConfig.plugins || [];
|
36 |
|
37 |
|
38 | if (args.outputPath) {
|
39 | webpackConfig.output.path = args.outputPath;
|
40 | }
|
41 |
|
42 | if (args.publicPath) {
|
43 | webpackConfig.output.publicPath = args.publicPath;
|
44 | }
|
45 |
|
46 |
|
47 | if (args.compress) {
|
48 | webpackConfig.UglifyJsPluginConfig = {
|
49 | output: {
|
50 | ascii_only: true
|
51 | },
|
52 | compress: {
|
53 | warnings: false
|
54 | }
|
55 | };
|
56 | webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.optimize.UglifyJsPlugin(webpackConfig.UglifyJsPluginConfig), new _webpack2.default.DefinePlugin({
|
57 | 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
|
58 | })]);
|
59 | } else {
|
60 | if (process.env.NODE_ENV) {
|
61 | webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.DefinePlugin({
|
62 | 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
|
63 | })]);
|
64 | }
|
65 | }
|
66 |
|
67 | webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.optimize.DedupePlugin(), new _webpack2.default.NoErrorsPlugin()]);
|
68 |
|
69 |
|
70 | if (args.hash) {
|
71 | var pkg = require((0, _path.join)(args.cwd, 'package.json'));
|
72 | webpackConfig.output.filename = webpackConfig.output.chunkFilename = '[name]-[chunkhash].js';
|
73 | webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [require('map-json-webpack-plugin')({
|
74 | assetsPath: pkg.name
|
75 | })]);
|
76 | }
|
77 |
|
78 | if (typeof args.config === 'function') {
|
79 | webpackConfig = args.config(webpackConfig) || webpackConfig;
|
80 | } else {
|
81 | webpackConfig = (0, _mergeCustomConfig2.default)(webpackConfig, (0, _path.resolve)(args.cwd, args.config || 'webpack.config.js'));
|
82 | }
|
83 | return webpackConfig;
|
84 | }
|
85 |
|
86 | function build(args, callback) {
|
87 |
|
88 | var webpackConfig = getWebpackConfig(args);
|
89 | webpackConfig = Array.isArray(webpackConfig) ? webpackConfig : [webpackConfig];
|
90 |
|
91 | var fileOutputPath = void 0;
|
92 | webpackConfig.forEach(function (config) {
|
93 | fileOutputPath = config.output.path;
|
94 | });
|
95 |
|
96 | if (args.watch) {
|
97 | webpackConfig.forEach(function (config) {
|
98 | config.plugins.push(new _webpack.ProgressPlugin(function (percentage, msg) {
|
99 | var stream = process.stderr;
|
100 | if (stream.isTTY && percentage < 0.71) {
|
101 | stream.cursorTo(0);
|
102 | stream.write('📦 ' + _chalk2.default.magenta(msg));
|
103 | stream.clearLine(1);
|
104 | } else if (percentage === 1) {
|
105 | console.log(_chalk2.default.green('\nwebpack: bundle build is now finished.'));
|
106 | }
|
107 | }));
|
108 | });
|
109 | }
|
110 |
|
111 | function doneHandler(err, stats) {
|
112 | if (args.json) {
|
113 | var filename = typeof args.json === 'boolean' ? 'build-bundle.json' : args.json;
|
114 | var jsonPath = (0, _path.join)(fileOutputPath, filename);
|
115 | (0, _fs.writeFileSync)(jsonPath, JSON.stringify(stats.toJson()), 'utf-8');
|
116 | console.log('Generate Json File: ' + jsonPath);
|
117 | }
|
118 |
|
119 | var _stats$toJson = stats.toJson();
|
120 |
|
121 | var errors = _stats$toJson.errors;
|
122 |
|
123 | if (errors && errors.length) {
|
124 | process.on('exit', function () {
|
125 | process.exit(1);
|
126 | });
|
127 | }
|
128 |
|
129 |
|
130 | if (!args.watch || stats.hasErrors()) {
|
131 | var buildInfo = stats.toString({
|
132 | colors: true,
|
133 | children: true,
|
134 | chunks: !!args.verbose,
|
135 | modules: !!args.verbose,
|
136 | chunkModules: !!args.verbose,
|
137 | hash: !!args.verbose,
|
138 | version: !!args.verbose
|
139 | });
|
140 | if (stats.hasErrors()) {
|
141 | console.error(buildInfo);
|
142 | } else {
|
143 | console.log(buildInfo);
|
144 | }
|
145 | }
|
146 |
|
147 | if (err) {
|
148 | process.on('exit', function () {
|
149 | process.exit(1);
|
150 | });
|
151 | console.error(err);
|
152 | }
|
153 |
|
154 | if (callback) {
|
155 | callback(err);
|
156 | }
|
157 | }
|
158 |
|
159 |
|
160 | var compiler = (0, _webpack2.default)(webpackConfig);
|
161 |
|
162 |
|
163 | if (!args.verbose) {
|
164 | compiler.plugin('done', function (stats) {
|
165 | stats.stats.forEach(function (stat) {
|
166 | stat.compilation.children = stat.compilation.children.filter(function (child) {
|
167 |
|
168 | return child.name !== 'extract-text-webpack-plugin';
|
169 | });
|
170 | });
|
171 | });
|
172 | }
|
173 |
|
174 | if (args.watch) {
|
175 | compiler.watch(args.watch || 200, doneHandler);
|
176 | } else {
|
177 | compiler.run(doneHandler);
|
178 | }
|
179 | }
|
180 | module.exports = exports['default']; |
\ | No newline at end of file |