UNPKG

6.53 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = build;
7
8var _path = require('path');
9
10var _fs = require('fs');
11
12var _webpack = require('webpack');
13
14var _webpack2 = _interopRequireDefault(_webpack);
15
16var _chalk = require('chalk');
17
18var _chalk2 = _interopRequireDefault(_chalk);
19
20var _nodeNotifier = require('node-notifier');
21
22var _nodeNotifier2 = _interopRequireDefault(_nodeNotifier);
23
24var _mergeCustomConfig = require('./mergeCustomConfig');
25
26var _mergeCustomConfig2 = _interopRequireDefault(_mergeCustomConfig);
27
28var _getWebpackCommonConfig = require('./getWebpackCommonConfig');
29
30var _getWebpackCommonConfig2 = _interopRequireDefault(_getWebpackCommonConfig);
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34function _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); } }
35
36function checkConfig(webpackConfig) {
37 var config = Array.isArray(webpackConfig) ? webpackConfig : [webpackConfig];
38 var hasEmptyEntry = config.some(function (c) {
39 return Object.keys(c.entry || {}).length === 0;
40 });
41 if (hasEmptyEntry) {
42 var err = new Error('no webpack entry found');
43 err.name = 'NoEntry';
44 throw err;
45 }
46}
47
48function getWebpackConfig(args, cache) {
49 var webpackConfig = (0, _getWebpackCommonConfig2.default)(args);
50
51 webpackConfig.plugins = webpackConfig.plugins || [];
52
53 // Config outputPath.
54 if (args.outputPath) {
55 webpackConfig.output.path = args.outputPath;
56 }
57
58 if (args.publicPath) {
59 webpackConfig.output.publicPath = args.publicPath;
60 }
61
62 // Config if no --no-compress.
63 // Watch mode should not use UglifyJsPlugin
64 if (args.compress && !args.watch) {
65 webpackConfig.UglifyJsPluginConfig = {
66 output: {
67 ascii_only: true
68 },
69 compress: {
70 warnings: false
71 }
72 };
73 webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.optimize.UglifyJsPlugin(webpackConfig.UglifyJsPluginConfig), new _webpack2.default.DefinePlugin({
74 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
75 })]);
76 } else {
77 webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.DefinePlugin({
78 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
79 })]);
80 }
81
82 // Watch mode should not use DedupePlugin
83 if (!args.watch) {
84 webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.optimize.DedupePlugin()]);
85 }
86
87 webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [new _webpack2.default.NoErrorsPlugin()]);
88
89 // Output map.json if hash.
90 if (args.hash) {
91 var pkg = require((0, _path.join)(args.cwd, 'package.json'));
92 webpackConfig.output.filename = '[name]-[chunkhash].js';
93 webpackConfig.output.chunkFilename = '[name]-[chunkhash].js';
94 webpackConfig.plugins = [].concat(_toConsumableArray(webpackConfig.plugins), [require('map-json-webpack-plugin')({
95 assetsPath: pkg.name,
96 cache: cache
97 })]);
98 }
99
100 if (typeof args.config === 'function') {
101 webpackConfig = args.config(webpackConfig) || webpackConfig;
102 } else {
103 webpackConfig = (0, _mergeCustomConfig2.default)(webpackConfig, (0, _path.resolve)(args.cwd, args.config || 'webpack.config.js'));
104 }
105 checkConfig(webpackConfig);
106 return webpackConfig;
107}
108
109function build(args, callback) {
110 // Get config.
111 var webpackConfig = getWebpackConfig(args, {});
112 webpackConfig = Array.isArray(webpackConfig) ? webpackConfig : [webpackConfig];
113
114 var fileOutputPath = void 0;
115 webpackConfig.forEach(function (config) {
116 fileOutputPath = config.output.path;
117 });
118
119 webpackConfig.forEach(function (config) {
120 config.plugins.push(new _webpack.ProgressPlugin(function (percentage, msg) {
121 var stream = process.stderr;
122 if (stream.isTTY && percentage < 0.71) {
123 stream.cursorTo(0);
124 stream.write('\uD83D\uDCE6 ' + _chalk2.default.magenta(msg));
125 stream.clearLine(1);
126 } else if (percentage === 1) {
127 console.log(_chalk2.default.green('\nwebpack: bundle build is now finished.'));
128 }
129 }));
130 });
131
132 function doneHandler(err, stats) {
133 if (args.json) {
134 var filename = typeof args.json === 'boolean' ? 'build-bundle.json' : args.json;
135 var jsonPath = (0, _path.join)(fileOutputPath, filename);
136 (0, _fs.writeFileSync)(jsonPath, JSON.stringify(stats.toJson()), 'utf-8');
137 console.log('Generate Json File: ' + jsonPath);
138 }
139
140 var _stats$toJson = stats.toJson(),
141 errors = _stats$toJson.errors;
142
143 if (errors && errors.length) {
144 process.on('exit', function () {
145 process.exit(1);
146 });
147 }
148 // if watch enabled only stats.hasErrors would log info
149 // otherwise would always log info
150 if (!args.watch || stats.hasErrors()) {
151 var buildInfo = stats.toString({
152 colors: true,
153 children: true,
154 chunks: !!args.verbose,
155 modules: !!args.verbose,
156 chunkModules: !!args.verbose,
157 hash: !!args.verbose,
158 version: !!args.verbose
159 });
160 if (stats.hasErrors()) {
161 console.error(buildInfo);
162 } else {
163 console.log(buildInfo);
164 if (args.silent !== true) {
165 _nodeNotifier2.default.notify({
166 title: 'ant tool',
167 message: 'done',
168 subtitle: 'build successfully',
169 contentImage: (0, _path.join)(__dirname, '../assets/success.png'),
170 sound: 'Glass'
171 });
172 }
173 }
174 }
175
176 if (err) {
177 process.on('exit', function () {
178 process.exit(1);
179 });
180 console.error(err);
181 }
182
183 if (callback) {
184 callback(err);
185 }
186 }
187
188 // Run compiler.
189 var compiler = (0, _webpack2.default)(webpackConfig);
190
191 // Hack: remove extract-text-webpack-plugin log
192 if (!args.verbose) {
193 compiler.plugin('done', function (stats) {
194 stats.stats.forEach(function (stat) {
195 stat.compilation.children = stat.compilation.children.filter(function (child) {
196 // eslint-disable-line
197 return child.name !== 'extract-text-webpack-plugin';
198 });
199 });
200 });
201 }
202
203 if (args.watch) {
204 compiler.watch(args.watch || 200, doneHandler);
205 } else {
206 compiler.run(doneHandler);
207 }
208}
209module.exports = exports['default'];
\No newline at end of file