UNPKG

3.35 kBJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4
5var envs = require('envs');
6var webpack = require('webpack');
7var write = require('fs').writeFileSync;
8var list = require('fs').readdirSync;
9var join = require('path').join;
10var byExtension = require('./lib/loaders-by-extension');
11var ExtractTextPlugin = require('extract-text-webpack-plugin');
12var ResolveSelf = require('./lib/resolve-self');
13
14var DISABLE_MIN = !!envs('DISABLE_MIN');
15var NODE_ENV = envs('ASSET_ENV', envs('NODE_ENV', 'production'));
16var DEVELOPMENT = NODE_ENV === 'development';
17var HASH = typeof envs('DISABLE_HASH') === 'undefined';
18var MANIFEST = envs('MANIFEST');
19
20module.exports = function(dirname) {
21 var config = {};
22
23 /**
24 * Configure the entries
25 */
26
27 // Autoload all of the modules
28 config.entry = DEVELOPMENT ? {
29 app: dirname + '/index.js'
30 } : {
31 main: dirname + '/index.js'
32 };
33
34 config.output = {
35 path: dirname,
36 filename: (DEVELOPMENT || !HASH) ?
37 '[name].js' :
38 '[name]' + (DISABLE_MIN ? '' : '.min') + '.js?[chunkhash]',
39 libraryTarget: 'this'
40 };
41
42 if (DEVELOPMENT) {
43 config.output.pathinfo = true;
44 config.output.publicPath = '/build/';
45
46 // use the eval builder for optimal speed in development
47 config.devtool = 'eval';
48
49 var entries = [config.entry.app];
50 // append the hot reload entries
51 entries.push('webpack-dev-server/client?http://localhost:' + envs('PORT', '3000'));
52 entries.push('webpack/hot/dev-server');
53 config.entry = entries;
54 }
55
56 /**
57 * Configure plugins
58 */
59
60 config.plugins = [
61 new webpack.IgnorePlugin(/vertx/),
62 new webpack.DefinePlugin({
63 'process.env': {
64 NODE_ENV: JSON.stringify(NODE_ENV)
65 }
66 }),
67 new webpack.ResolverPlugin([
68 new ResolveSelf()
69 ], ['normal'])
70 ];
71
72 if (!DEVELOPMENT) {
73 config.plugins.push(
74 new webpack.optimize.AggressiveMergingPlugin(),
75 new ExtractTextPlugin('[name].css?[chunkhash]')
76 );
77
78 if (!envs('DISABLE_MIN')) config.plugins.push(
79 new webpack.optimize.OccurrenceOrderPlugin(),
80 new webpack.optimize.UglifyJsPlugin({output: {comments: false}}),
81 new webpack.optimize.DedupePlugin()
82 );
83
84 if (MANIFEST) config.plugins.push(createManifest(MANIFEST));
85 } else {
86 // append the hot reload plugin
87 config.plugins.push(new webpack.HotModuleReplacementPlugin());
88 }
89
90 /**
91 * configure resolution
92 */
93
94 config.resolve = {
95 extensions: ['', '.js', '.html']
96 };
97
98 /**
99 * Configure loaders
100 */
101
102 config.module = {
103 loaders: []
104 };
105
106 config.addLoader = function(ext, loader) {
107 var obj = {};
108 obj[ext] = loader;
109 config.module.loaders.push.apply(config.module.loaders, byExtension(obj));
110 };
111
112 /**
113 * Setup style loading
114 */
115
116 extract('css', 'css-loader');
117 extract('styl', 'css-loader!stylus-loader?paths=node_modules');
118
119 function extract(ext, loader) {
120 config.addLoader(ext, DEVELOPMENT ? 'style-loader!' + loader : ExtractTextPlugin.extract('style-loader', loader));
121 }
122
123 /**
124 * Configure development stuff
125 */
126
127 config.load = function() {
128 return webpack(config);
129 };
130
131 return config;
132};
133
134function createManifest(manifest) {
135 return function() {
136 this.plugin('done', function(stats) {
137 var out = JSON.stringify(stats.toJson().assetsByChunkName, null, ' ');
138 write(manifest, out);
139 });
140 };
141}