-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
379 lines (342 loc) · 13.3 KB
/
utils.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
'use strict';
var path = require( 'path' ),
fs = require( 'fs' ),
logger = require( './logger' ),
webpack = require( 'webpack' ),
ExtractTextPlugin = require( 'extract-text-webpack-plugin' ),
config = require( './config' ),
babel = require( './babel' );
class Utils {
/**
* load webpack:
* @task dev: add dev-server and enable HMR, according to mock config, enable mock
* @task release: add uglifyjs
* @return config
*/
loadWebpackCfg( target, args ) {
var pack_config,
babelQueryStr,
entryPath,
isDebug,
node_env = 'development';
switch ( target ) {
case 'dev':
isDebug = true;
process.env.NODE_ENV = node_env;
pack_config = this.mergeConfig( args, isDebug );
pack_config.devtool = '#eval';
// config css and less loader
pack_config.module.rules.push( {
test: /\.css$/,
use: [ {
loader: "style-loader"
}, {
loader: "css-loader",
options: {
sourceMap: true
}
} ]
} );
pack_config.module.rules.push( {
test: /\.less$/,
exclude: /(node_modules|bower_components)/,
loaders: [ {
loader: "style-loader"
}, {
loader: "css-loader",
options: {
sourceMap: true
}
}, {
loader: "less-loader",
options: {
sourceMap: true
}
} ]
} );
// add hot module replace plugin
pack_config.plugins.push( new webpack.HotModuleReplacementPlugin() );
pack_config.output.publicPath = `http://localhost:${config.getPort()}/`;
break;
case 'release':
isDebug = false;
node_env = 'production';
process.env.NODE_ENV = node_env;
if ( args.mock ) {
args.sourcemap = "";
}
pack_config = this.mergeConfig( args );
var sourcemap = !!args.sourcemap ? "?source-map" : "";
if ( sourcemap ) {
pack_config.devtool = '#source-map';
} else {
pack_config.devtool = '#eval';
}
// hash control, add hash when release
let hash = '';
if ( !isDebug && args.hash ) {
hash = '.[chunkhash]';
}
pack_config.module.rules.push( {
test: /\.css$/,
use: ExtractTextPlugin.extract( {
fallback: "style-loader",
use: [ {
loader: "css-loader",
options: {
sourcemap: sourcemap
}
}, {
loader: "postcss-loader",
options: {
plugins: function() {
return [
require( 'autoprefixer' )
];
}
}
} ]
} )
} );
pack_config.module.rules.push( {
test: /\.less$/,
exclude: /(node_modules|bower_components)/,
use: ExtractTextPlugin.extract( {
fallback: "style-loader",
use: [ {
loader: "css-loader",
options: {
sourcemap: sourcemap
}
}, {
loader: "postcss-loader",
options: {
plugins: function() {
return [
require( 'autoprefixer' )
];
}
}
}, {
loader: "less-loader",
options: {
sourcemap: sourcemap
}
} ]
} )
} );
pack_config.plugins.push( new ExtractTextPlugin( {
filename: `[name]${hash}.css`,
allChunks: true
} ) );
if ( args.uglify ) {
pack_config.plugins.push( new webpack.optimize.UglifyJsPlugin( {
compress: {
warnings: false
},
sourceMap: !!args.sourcemap
} ) );
}
break;
default:
break;
}
if ( args.mock === true ) {
babelQueryStr = babel( isDebug );
entryPath = [];
for ( let key in pack_config.entry ) {
let entry = pack_config.entry[ key ];
let type = Object.prototype.toString.call( entry );
if ( type === '[object String]' ) {
entryPath.push( path.resolve( process.cwd(), entry + '.js' ) );
} else if ( type === '[object Array]' ) {
entryPath.push( path.resolve( process.cwd(), entry[ entry.length - 1 ] + '.js' ) );
}
}
let mockPath = path.resolve( process.cwd(), 'mock/mock.js' );
//load mock.js before all
pack_config.module.rules.push( {
test: entryPath,
exclude: /(node_modules|bower_components)/,
loaders: [ `imports-loader?Mock=${mockPath}`, `babel-loader?${babelQueryStr}` ]
} );
}
// add variables define
pack_config.plugins.push( new webpack.DefinePlugin( {
WEBPACK_DEBUG: isDebug
} ) );
logger.debug( `kil ${target} with webpack config: ` );
logger.debug( pack_config );
return pack_config;
}
/**
* parse entry
* @param {[string]} dev:
* dev: add dev server and HMR entries
* release: do nothing with enties expcet convert string to array
* @return {[type]}
*/
parseEntry( entry, dev, depth ) {
if ( entry ) {
var type = Object.prototype.toString.call( entry );
if ( type === '[object String]' ) {
entry = [ entry ];
if ( dev ) {
entry.unshift( `webpack-dev-server/client?http://localhost:${config.getPort()}`,
'webpack/hot/dev-server' );
}
} else if ( type === '[object Array]' ) {
if ( dev ) {
entry.unshift( `webpack-dev-server/client?http://localhost:${config.getPort()}`,
'webpack/hot/dev-server' );
}
} else {
for ( var key in entry ) {
entry[ key ] = this.parseEntry( entry[ key ], dev );
}
}
return entry;
} else {
logger.error( 'No entry is found!' );
}
}
/**
* merge webpack default config, user's config, and config from package.json
* @param {Boolean} isDebug : is debug mode, add dev-sever entry or not
* @param {Object} args
* @return
*/
mergeConfig( args, isDebug ) {
var pack_def = require( './pack' );
var packPath = path.join( process.cwd(), 'webpack.config.js' );
var sysCfg = config.init( args );
var pack;
try {
pack = fs.statSync( packPath );
pack = require( packPath );
try {
pack = pack( path.resolve( __dirname, 'node_modules' ) );
} catch ( e ) {
logger.error( ' Error happens in webpack.config.js ' );
logger.error( e );
process.exit( 1 );
}
} catch ( e ) {
try {
packPath = path.join( process.cwd(), 'pack.js' );
pack = fs.statSync( packPath );
pack = require( packPath );
logger.warn( "find pack.js, please change pack.js to webpack.config.js" );
try {
pack = pack( path.resolve( __dirname, 'node_modules' ) );
} catch ( e ) {
logger.error( ' Error happens in pack.js ' );
logger.error( e );
process.exit( 1 );
}
} catch ( err ) {
logger.info( "can't find webpack.config.js, use webpack config from package.json or default." );
}
}
if ( !pack ) {
var conf = sysCfg.webpack;
pack = {
entry: conf.entry || 'main',
plugins: conf.plugins,
devServer: conf.devServer,
resolve: conf.resolve,
output: {
publicPath: conf.publicPath
}
};
}
if ( !pack ) {
return pack_def;
} else {
var pack_config = pack;
if ( !pack.entry ) {
pack_config.entry = pack_def.entry;
}
pack_config.entry = this.parseEntry( pack_config.entry, isDebug );
// use kil default webpack config, for build use
var publicPath = pack_config.output && pack_config.output.publicPath;
pack_config.output = pack_def.output;
if ( publicPath ) {
pack_config.output.publicPath = publicPath;
}
// hash control, add hash when release
let hash = '';
if ( !isDebug && args.hash ) {
hash = '.[chunkhash]';
}
pack_config.output.filename = `[name]${hash}.js`;
pack_config.output.chunkFilename = `[id]${hash}.js`;
if ( pack_config.module && pack_config.module.rules ) {
Array.prototype.push.apply( pack_def.module.rules, pack_config.module.rules );
} else {
pack_config.module = {};
}
pack_config.module.rules = pack_def.module.rules;
pack_config.module.rules.push( {
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: `babel-loader?${babel(isDebug)}`
} );
if ( pack_config.resolve ) {
pack_config.resolve.modules = pack_def.resolve.modules;
} else {
pack_config.resolve = pack_def.resolve;
}
pack_config.resolveLoader = pack.resolveLoader || pack_def.resolveLoader;
if ( pack_config.plugins ) {
Array.prototype.push.apply( pack_def.plugins, pack_config.plugins );
}
pack_config.plugins = pack_def.plugins;
let loaderOptions = {
debug: false,
options: {
context: __dirname
},
minimize: true
};
if ( sysCfg.vue ) {
// config loader for vue
pack_config.module.rules.push( {
test: /\.vue$/,
exclude: /(node_modules|bower_components)/,
loader: 'vue-loader',
options: {
loaders: {
js: `babel-loader?${babel(isDebug)}`,
css: ExtractTextPlugin.extract({
use: 'css-loader',
fallback: 'vue-style-loader' // <- 这是vue-loader的依赖,所以如果使用npm3,则不需要显式安装
})
}
}
} );
// var VueLoaderPlugin = require('vue-loader/lib/plugin');
// pack_config.plugins.push(
// new VueLoaderPlugin()
// )
}
if ( sysCfg.jshint ) {
pack_config.module.rules.push( {
test: /\.jsx?$/,
enforce: "pre",
exclude: /(node_modules|bower_components)/,
loader: "jshint-loader"
} );
}
pack_config.plugins.push( new webpack.LoaderOptionsPlugin( loaderOptions ) );
if ( pack_config.externals ) {
pack_config.externals = Object.assign({}, pack_config.externals, pack_def.externals);
}
if ( pack.devServer ) {
pack_config.devServer = pack.devServer;
}
return pack_config;
}
}
}
module.exports = new Utils();