UNPKG

6.86 kBJavaScriptView Raw
1// config:
2// entry: entrypoint file
3// target: 'node' or 'client'
4// server: true/false for webpack-dev-server
5// devtool: specify webpack devtool
6// commonsChunk: split common files into commons.js chunk
7// longTermCaching: use hash name with files
8// minify: uglify and dedupe
9// debug (bool): output debug info
10// dir: (string): absolute path to app dir
11// hot: (bool): use hot reloading
12// port (number): webpack port
13
14var colors = require('colors');
15var path = require('path');
16var webpack = require('webpack');
17// var ReactStylePlugin = require('react-style-webpack-plugin');
18var ExtractTextPlugin = require('extract-text-webpack-plugin');
19var util = require('util');
20var joinEntry = require('./lib/joinEntry');
21var statsPlugin = require('./lib/statsPlugin');
22var linkModules = require('./lib/linkModules');
23var getExcludedModules = require('./lib/getExcludedModules');
24
25function makeAll(configs) {
26 if (Array.isArray(configs))
27 return configs.map(make);
28 else
29 return make(configs);
30}
31
32// makes from a single config object
33function make(config) {
34 // defaults
35 config.env = config.env || 'development';
36 config.debug = process.env.DEBUG || config.debug;
37 config.dir = process.env.DIR || config.dir;
38 config.target = process.env.TARGET || config.target;
39 config.hostname = config.hostname || 'localhost';
40 config.platform = config.platform;
41
42 // target
43 var node = config.target === 'node';
44 var web = config.target === 'web';
45
46 if (config.debug)
47 console.log("Making webpack config with:\n".bold.blue, config, "\n");
48
49 if (config.linkModules)
50 linkModules(config, config.dir + '/server_modules');
51
52 // LOADERS
53 var loaders = [
54 { test: /\.json$/, loader: 'json-loader' },
55 { test: /\.(png|jpg|jpeg|gif)$/, loader: 'url-loader?limit=10000&name=[name].[ext]' },
56 { test: /\.svg$/, loader: 'raw-loader' },
57 { test: /\.html$/, loader: 'file-loader?name=[name].[ext]' },
58 { test: /\.worker\.js$/, loader: 'worker-loader?inline=true' }
59 ]
60 .concat(config.loaders || []);
61
62 if (config.hot)
63 loaders.push({ test: /\.jsx$/, loader: 'react-hot' });
64
65 // if (node)
66 // loaders.push({ test: /\.jsx?$/, loader: ReactStylePlugin.loader() });
67
68 loaders.push({
69 test: /\.jsx?$/,
70 loader: 'babel-loader?experimental=true&optional=bluebirdCoroutines',
71 exclude: getExcludedModules(config)
72 });
73
74 // style loaders
75 var cssLoader = 'css-loader!autoprefixer-loader?browsers=last 2 version';
76 var stylesheetLoaders = [
77 { test: /\.css$/, loader: cssLoader },
78 { test: /\.styl$/, loader: cssLoader + '!stylus-loader' }
79 ];
80
81 // various ways of handling stylesheet requires
82 stylesheetLoaders.forEach(function(stylesheetLoader) {
83 var loader = stylesheetLoader.loader;
84
85 if (node)
86 stylesheetLoader.loader = 'null-loader';
87 else if (config.separateStylesheet)
88 stylesheetLoader.loader = ExtractTextPlugin.extract('style-loader', loader);
89 else
90 stylesheetLoader.loader = 'style-loader!' + loader;
91 });
92
93
94 // WEBPACK CONFIG
95
96 var entry = config.entry;
97
98 // allow shorthand for single entry
99 if (typeof entry === 'string') {
100 entry = { main: entry };
101 }
102
103 var alias = {};
104 var aliasLoader = {};
105 var externals = [];
106 var modulesDirectories = config.modulesDirectories || [
107 'node_modules',
108 'web_modules',
109 'server_modules',
110 // this adds a shorthand so you can require anything in ./app
111 // without using relative paths
112 'app'
113 ];
114
115 var extensions = config.extensions || ['', '.web.js', '.js', '.jsx'];
116
117 var root = config.root || [path.join(config.dir)];
118
119 var fallback = (config.fallback || ['node_modules', 'server_modules']).map(function(moduleDir) {
120 return config.dir + '/' + moduleDir
121 })
122
123 var output = {
124 path: path.join(config.dir, 'build',
125 node ? 'prerender' : config.platform || 'public'),
126
127 filename: '[name].js' +
128 (config.longTermCaching ? '?[chunkhash]' : ''),
129
130 chunkFilename: (config.commonsChunk ? '[name].js' : '[id].js') +
131 (config.longTermCaching ? '?[chunkhash]' : ''),
132
133 publicPath: '/',
134 sourceMapFilename: 'debugging/[file].map',
135 libraryTarget: node ? 'commonjs2' : undefined,
136 pathinfo: config.debug
137 };
138
139
140 // PLUGINS
141
142 var plugins = [
143 // set process.env for modules
144 new webpack.DefinePlugin({
145 'process.env': {
146 NODE_ENV: JSON.stringify(config.env),
147 TARGET: JSON.stringify(config.target),
148 PLATFORM: JSON.stringify(config.platform || 'web')
149 }
150 })
151 ];
152
153 // prefetch
154 var prefetches = config.prefetch ||
155 ['react', 'react/lib/ReactComponentBrowserEnvironment'];
156
157 prefetches.forEach(function(prefetch) {
158 plugins.push(new webpack.PrefetchPlugin(prefetch));
159 });
160
161 // outputs build stats to ./build/stats.json
162 if (config.debug)
163 plugins.push(statsPlugin(config));
164
165 // todo: awaiting new version of react-style
166 // if (config.separateStylesheet)
167 // plugins.push(new ReactStylePlugin('bundle.css'));
168
169 if (node) {
170 aliasLoader['react-proxy$'] = 'react-proxy/unavailable';
171 externals.push(/^react(\/.*)?$/);
172 plugins.push(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }));
173 }
174
175 if (config.hot) {
176 plugins.push(new webpack.HotModuleReplacementPlugin());
177 plugins.push(new webpack.NoErrorsPlugin());
178 entry = joinEntry('webpack/hot/only-dev-server', entry);
179 }
180
181 if (config.commonsChunk)
182 plugins.push(
183 new webpack.optimize.CommonsChunkPlugin('commons', 'commons.js' +
184 (config.longTermCaching && !node ? '?[chunkhash]' : '')));
185
186 if (config.server && web)
187 entry = joinEntry('webpack-dev-server/client?http://' + config.hostname + ':' + (config.port || 3011), entry);
188
189 if (config.separateStylesheet)
190 plugins.push(new ExtractTextPlugin('[name].css'));
191
192 if (config.minify)
193 plugins.push(
194 new webpack.optimize.UglifyJsPlugin({
195 compress: {
196 warnings: false
197 }
198 }),
199 new webpack.optimize.DedupePlugin()
200 );
201
202
203 // RETURN
204
205 var webpackConfig = {
206 hostname: config.hostname,
207 entry: entry,
208 output: output,
209 target: config.target,
210 module: {
211 loaders: loaders.concat(stylesheetLoaders)
212 },
213 devtool: config.devtool || 'eval',
214 debug: config.debug,
215 resolveLoader: {
216 root: config.linkModules ?
217 path.join(config.dir, 'server_modules') :
218 path.join(config.dir, 'node_modules'),
219 alias: aliasLoader
220 },
221 externals: externals,
222 resolve: {
223 root: root,
224 modulesDirectories: modulesDirectories,
225 extensions: extensions,
226 alias: alias,
227 fallback: fallback
228 },
229 plugins: plugins
230 };
231
232 if (config.debug) {
233 console.log('Webpack config:'.bold.blue);
234 console.log(util.inspect(webpackConfig, { depth: 10 }));
235 console.log();
236 }
237
238 return webpackConfig;
239}
240
241module.exports = makeAll;
\No newline at end of file