-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.js
161 lines (138 loc) · 4.83 KB
/
config.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
'use strict';
var path = require( 'path' ),
glob = require( 'glob' ),
logger = require( './logger' ),
webpack = require( 'webpack' ),
HtmlWebpackPlugin = require( 'html-webpack-plugin' ),
conf;
const DEFAULT = require( './default/package.default.js' );
class Config {
getPort() {
if ( !conf ) {
throw new Error( 'configuration is not initialized!' );
}
return conf.port;
}
getHtml5Mode() {
if ( !conf ) {
throw new Error( 'configuration is not initialized!' );
}
return conf.html5Mode;
}
getConfig() {
if ( !conf ) {
throw new Error( 'configuration is not initialized!' );
}
return conf;
}
/**
* load config from package.json if exists, otherwise use default config
* @param {Object} args : runtime arguments
* @return {[Object]}
* System Config
*/
init( args ) {
if ( conf ) {
return conf;
}
var json = {};
var source = true;
try {
var confPath = path.join( process.cwd(), 'package.json' );
json = require( confPath );
} catch ( ex ) {
logger.warn( "Can't find package.json, init system config with default. " );
source = false;
}
if ( json.kil ) {
conf = json.kil;
} else {
logger.warn( "Can't find a key named kil in package.json, init system config with default. " );
conf = DEFAULT;
}
conf.port = conf.port || DEFAULT.port;
if ( args && args.port ) {
try {
conf.port = parseInt( args.port );
} catch ( err ) {
logger.warn( 'Ignore passed error port! ' );
}
}
conf.mock = !!args.mock;
conf.jshint = !!args.jshint;
conf.react = !!conf.react;
conf.vue = !!conf.vue;
conf.html5Mode = !!conf.html5Mode;
var pack = conf.webpack || {};
pack.entry = {};
pack.plugins = [];
let comObj = {};
// check relationship between chunk and common
if ( pack.commonTrunk ) {
for ( let key in pack.commonTrunk ) {
comObj[ key ] = [];
}
}
// add output
if ( pack.output ) {
for ( let key in pack.output ) {
let files = glob.sync( key );
let resObj = pack.output[ key ];
files.forEach( ( file ) => {
let entry = file.replace( /\.(html|ejs)/g, '' );
let lastfix = file.replace( entry, '' );
if ( resObj && resObj.jsname ) {
entry = resObj.jsname;
}
pack.entry[ entry ] = './' + entry;
let name = './' + file;
let depends = [];
if ( resObj && resObj.commons ) {
resObj.commons.map( ( common ) => {
depends.push( common );
comObj[ common ].push( entry );
} );
Array.prototype.push.apply( depends, resObj.commons );
} else {
if ( pack.commonTrunk ) {
for ( let key in pack.commonTrunk ) {
depends.push( key );
comObj[ key ].push( entry );
}
}
}
depends.push( entry );
var plugin_obj = {
template: name,
filename: './' + entry + '.html',
chunks: depends
};
pack.plugins.push( new HtmlWebpackPlugin( plugin_obj ) );
} );
}
}
// add common trunk
if ( pack.commonTrunk ) {
for ( let key in pack.commonTrunk ) {
pack.entry[ key ] = pack.commonTrunk[ key ];
pack.plugins.push( new webpack.optimize.CommonsChunkPlugin( {
name: key,
chunks: comObj[ key ]
} ) );
}
}
if ( pack.global ) {
for ( var key in pack.global ) {
if ( pack.global[ key ].startsWith( './' ) || pack.global[ key ].startsWith( '../' ) ) {
pack.global[ key ] = path.resolve( pack.global[ key ] );
}
}
logger.debug( 'ProvidePlugin config from package.json:' );
logger.debug( pack.global );
pack.plugins.push( new webpack.ProvidePlugin( pack.global ) );
}
conf.webpack = pack;
return conf;
}
}
module.exports = new Config();