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 _nodeNotifier = require('node-notifier');
|
21 |
|
22 | var _nodeNotifier2 = _interopRequireDefault(_nodeNotifier);
|
23 |
|
24 | var _mergeCustomConfig = require('./mergeCustomConfig');
|
25 |
|
26 | var _mergeCustomConfig2 = _interopRequireDefault(_mergeCustomConfig);
|
27 |
|
28 | var _getWebpackCommonConfig = require('./getWebpackCommonConfig');
|
29 |
|
30 | var _getWebpackCommonConfig2 = _interopRequireDefault(_getWebpackCommonConfig);
|
31 |
|
32 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33 |
|
34 | 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); } }
|
35 |
|
36 | function 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 |
|
48 | function getWebpackConfig(args, cache) {
|
49 | var webpackConfig = (0, _getWebpackCommonConfig2.default)(args);
|
50 |
|
51 | webpackConfig.plugins = webpackConfig.plugins || [];
|
52 |
|
53 |
|
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 |
|
63 |
|
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 |
|
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 |
|
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 |
|
109 | function build(args, callback) {
|
110 |
|
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 |
|
149 |
|
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 | _nodeNotifier2.default.notify({
|
165 | title: 'ant tool',
|
166 | message: 'done',
|
167 | subtitle: 'build successfully',
|
168 | contentImage: (0, _path.join)(__dirname, '../assets/success.png'),
|
169 | sound: 'Glass'
|
170 | });
|
171 | }
|
172 | }
|
173 |
|
174 | if (err) {
|
175 | process.on('exit', function () {
|
176 | process.exit(1);
|
177 | });
|
178 | console.error(err);
|
179 | }
|
180 |
|
181 | if (callback) {
|
182 | callback(err);
|
183 | }
|
184 | }
|
185 |
|
186 |
|
187 | var compiler = (0, _webpack2.default)(webpackConfig);
|
188 |
|
189 |
|
190 | if (!args.verbose) {
|
191 | compiler.plugin('done', function (stats) {
|
192 | stats.stats.forEach(function (stat) {
|
193 | stat.compilation.children = stat.compilation.children.filter(function (child) {
|
194 |
|
195 | return child.name !== 'extract-text-webpack-plugin';
|
196 | });
|
197 | });
|
198 | });
|
199 | }
|
200 |
|
201 | if (args.watch) {
|
202 | compiler.watch(args.watch || 200, doneHandler);
|
203 | } else {
|
204 | compiler.run(doneHandler);
|
205 | }
|
206 | }
|
207 | module.exports = exports['default']; |
\ | No newline at end of file |