-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (188 loc) · 6.12 KB
/
index.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
/**
* Module dependencies
*/
var envs = require('envs');
var write = require('fs').writeFileSync;
var list = require('fs').readdirSync;
var join = require('path').join;
var resolve = require('path').resolve;
var Path = require('path');
var glob = require('glob');
var byExtension = require('./lib/loaders-by-extension');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
// polyfill Promises
require('es6-promise').polyfill();
var OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin');
var ResolveSelf = require('./lib/resolve-self');
// remove this once webpack releases it
// WatchIgnorePlugin = webpack.WatchIgnorePlugin
var WatchIgnorePlugin = require('./lib/watch-ignore');
var EnvifyPlugin = require('./lib/envify-plugin');
var DISABLE_MIN = !!envs('DISABLE_MIN');
var NODE_ENV = envs('ASSET_ENV', envs('NODE_ENV', 'production'));
var DEVELOPMENT = NODE_ENV === 'development';
var HASH = typeof envs('DISABLE_HASH') === 'undefined';
var MANIFEST = envs('MANIFEST');
var BUILD_TARGET = envs('BUILD_TARGET', 'web');
var EXTRACT_STYLE = !DEVELOPMENT && envs('EXTRACT_STYLE', BUILD_TARGET === 'node') !== '0';
var OUTPUT_PATTERN = envs('OUTPUT_PATTERN', '[name]');
var MAIN_ENTRY = DEVELOPMENT ? 'app' : envs('MAIN_ENTRY', 'main');
var COMPILE_ENV = envs('COMPILE_ENV', '1') !== '0';
module.exports = function(dirname, webpack) {
var config = {
sourcedir: dirname,
cache: true
};
/**
* Configure the entries
*/
var target = config.target = BUILD_TARGET;
// Autoload all of the modules
config.entry = {};
config.entry[MAIN_ENTRY] = dirname + '/index.js';
if (target === 'node') {
config.entry = {
server: dirname + '/server'
};
}
config.output = {
path: Path.dirname(dirname) + '/build',
filename: (DEVELOPMENT || !HASH) ?
OUTPUT_PATTERN + '.js' :
OUTPUT_PATTERN + (DISABLE_MIN ? '' : '.min') + '.js?[chunkhash]'
};
if (DEVELOPMENT) {
config.output.pathinfo = true;
config.output.publicPath = '/build/';
if (config.target === 'web') {
// use the eval builder for optimal speed in development
config.devtool = 'eval';
var entries = [config.entry.app];
// append the hot reload entries
entries.push('webpack-dev-server/client?' + envs('WEBPACK_DEV_SERVER_URL', 'http://localhost:' + envs('PORT', '3000')));
entries.push('webpack/hot/dev-server');
config.entry = entries;
}
}
/**
* Configure plugins
*/
config.plugins = [
new webpack.IgnorePlugin(/vertx/),
new webpack.DefinePlugin({
'browser.env': '__env__',
'process.BUILD_TARGET': JSON.stringify(target)
}),
new webpack.ResolverPlugin([
new ResolveSelf()
], ['normal'])
];
if (COMPILE_ENV) config.plugins.push(new EnvifyPlugin(null, webpack));
var extractPlugin = new ExtractTextPlugin('style', OUTPUT_PATTERN + (DISABLE_MIN ? '' : '.min') + '.css?[chunkhash]');
if (EXTRACT_STYLE) {
config.plugins.push(extractPlugin);
}
if (!DEVELOPMENT) {
config.plugins.push(
new webpack.optimize.AggressiveMergingPlugin({
moveToParents: true
})
);
if (!envs('DISABLE_MIN')) config.plugins.push(
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
output: {comments: false},
sourceMap: false,
exclude: [/\.ess/]
}),
new OptimizeCssPlugin({
assetNameRegExp: /\.css/,
cssProcessorOptions: {
autoprefixer: {add: true},
discardComments: {removeAll: true}
}
})
);
if (MANIFEST) config.plugins.push(createManifest(MANIFEST));
} else {
// append the hot reload plugin
if (config.target === 'web') config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
/**
* configure resolution
*/
config.resolve = {
extensions: ['', '.js'],
modulesDirectories: ['web_modules', 'node_modules', 'src/modules', process.cwd()],
fallback: [process.cwd(), process.cwd() + '/node_modules'],
alias: {
fs: __dirname + '/lib/fs.shim.js',
'~': process.cwd()
}
};
config.resolveLoader = {
fallback: [process.cwd(), process.cwd() + '/node_modules']
};
/**
* Configure loaders
*/
config.module = {
loaders: []
};
config.addLoader = function(ext, loader) {
if (ext instanceof RegExp) return config.module.loaders.push({test: ext, loader: loader, loaders: loader});
var obj = {};
obj[ext] = loader;
return config.module.loaders.push.apply(config.module.loaders, byExtension(obj));
};
/**
* Setup style loading
*/
config.addStyle = function(ext, loader, styleLoader) {
styleLoader = styleLoader || 'style-loader';
config.addLoader(ext, !EXTRACT_STYLE ? styleLoader + '!' + loader : extractPlugin.extract(loader));
};
/**
* Configure development stuff
*/
var watchIgnores = [];
config.load = function() {
if (watchIgnores.length) config.plugins.push(new WatchIgnorePlugin(watchIgnores));
return webpack(config);
};
config.watchIgnore = function() {
watchIgnores.push.apply(watchIgnores, arguments);
};
config.prefetchWildcard = function(pattern, opts, ignore) {
glob.sync(pattern, opts).forEach(function(file) {
if (ignore && file.match(ignore)) return;
config.prefetchModule(resolve(process.cwd(), file));
});
};
config.prefetchModule = function(context, request) {
config.plugins.push(new webpack.PrefetchPlugin(context, request));
};
return config;
};
function createManifest(manifest) {
return function() {
this.plugin('done', function(stats) {
var json = stats.toJson();
var byChunkName = json.assetsByChunkName;
var out = json.assets.reduce(function(acc, asset) {
if (!asset.chunkNames.length) {
if (~asset.name.indexOf('.js')) acc.chunks.push(asset.name);
return acc;
}
if (~asset.name.indexOf('.css')) acc.styles.push(asset.name);
if (~asset.name.indexOf('.js')) acc.scripts.push(asset.name);
return acc;
}, {
styles: [],
scripts: [],
chunks: []
});
write(manifest, JSON.stringify(out, null, ' '));
});
};
}