1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var colors = require('colors');
|
15 | var path = require('path');
|
16 | var webpack = require('webpack');
|
17 |
|
18 | var ExtractTextPlugin = require('extract-text-webpack-plugin');
|
19 | var util = require('util');
|
20 | var joinEntry = require('./lib/joinEntry');
|
21 | var statsPlugin = require('./lib/statsPlugin');
|
22 | var linkModules = require('./lib/linkModules');
|
23 | var getExcludedModules = require('./lib/getExcludedModules');
|
24 |
|
25 | function makeAll(configs) {
|
26 | if (Array.isArray(configs))
|
27 | return configs.map(make);
|
28 | else
|
29 | return make(configs);
|
30 | }
|
31 |
|
32 |
|
33 | function make(config) {
|
34 |
|
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 |
|
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 |
|
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 |
|
66 |
|
67 |
|
68 | loaders.push({
|
69 | test: /\.jsx?$/,
|
70 | loader: 'babel-loader?experimental=true&optional=bluebirdCoroutines',
|
71 | exclude: getExcludedModules(config)
|
72 | });
|
73 |
|
74 |
|
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 |
|
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 |
|
95 |
|
96 | var entry = config.entry;
|
97 |
|
98 |
|
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 |
|
111 |
|
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 |
|
141 |
|
142 | var plugins = [
|
143 |
|
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 |
|
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 |
|
162 | if (config.debug)
|
163 | plugins.push(statsPlugin(config));
|
164 |
|
165 |
|
166 |
|
167 |
|
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 |
|
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 |
|
241 | module.exports = makeAll; |
\ | No newline at end of file |