-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
257 lines (211 loc) · 7.23 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// config:
// entry: entrypoint file
// target: 'node' or 'client'
// server: true/false for webpack-dev-server
// devtool: specify webpack devtool
// commonsChunk: split common files into commons.js chunk
// longTermCaching: use hash name with files
// minify: uglify and dedupe
// debug (bool): output debug info
// dir: (string): absolute path to app dir
// hot: (bool): use hot reloading
// port (number): webpack port
var colors = require('colors');
var path = require('path');
var webpack = require('webpack');
// var ReactStylePlugin = require('react-style-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var util = require('util');
var joinEntry = require('./lib/joinEntry');
var statsPlugin = require('./lib/statsPlugin');
var linkModules = require('./lib/linkModules');
function makeAll(configs) {
if (Array.isArray(configs))
return configs.map(make);
else
return make(configs);
}
// makes from a single config object
function make(config) {
// defaults
config.env = config.env || 'development';
config.debug = process.env.DEBUG || config.debug;
config.dir = process.env.DIR || config.dir;
config.target = process.env.TARGET || config.target;
config.hostname = config.hostname || 'localhost';
config.platform = config.platform;
// target
var node = config.target === 'node';
var web = config.target === 'web';
if (config.debug)
console.log("Making webpack config with:\n".bold.blue, config, "\n");
if (config.linkModules)
linkModules(config, config.dir + '/server_modules');
// LOADERS
var loaders = [
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.(png|jpg|jpeg|gif)$/, loader: 'url-loader?limit=10000&name=[name].[ext]' },
{ test: /\.svg$/, loader: 'raw-loader' },
{ test: /\.html$/, loader: 'file-loader?name=[name].[ext]' },
{ test: /\.worker\.js$/, loader: 'worker-loader?inline=true' }
]
.concat(config.loaders || []);
if (config.hot)
loaders.push({ test: /\.jsx$/, loader: 'react-hot' });
// if (node)
// loaders.push({ test: /\.jsx?$/, loader: ReactStylePlugin.loader() });
try {
var entry = require(config.dir + '/package.json')["main"];
var entryDir = path.normalize(path.dirname(config.dir + '/' + entry));
}
catch (e) {}
loaders.push({
test: /\.jsx?$/,
loader: 'babel-loader?{"stage": 0, "optional": ["bluebirdCoroutines"]}',
include: entryDir || config.dir + '/app',
exclude: /node_modules/
});
// style loaders
var cssLoader = 'css-loader!autoprefixer-loader?browsers=last 2 version';
var stylesheetLoaders = [
{ test: /\.css$/, loader: cssLoader },
{ test: /\.styl$/, loader: cssLoader + '!stylus-loader' }
];
// various ways of handling stylesheet requires
stylesheetLoaders.forEach(function(stylesheetLoader) {
var loader = stylesheetLoader.loader;
if (node)
stylesheetLoader.loader = 'null-loader';
else if (config.separateStylesheet)
stylesheetLoader.loader = ExtractTextPlugin.extract('style-loader', loader);
else
stylesheetLoader.loader = 'style-loader!' + loader;
});
// WEBPACK CONFIG
var entry = config.entry;
// allow shorthand for single entry
if (typeof entry === 'string') {
entry = { main: entry };
}
var alias = config.alias || {};
var aliasLoader = config.aliasLoader || {};
var externals = config.externals || [];
var modulesDirectories = config.modulesDirectories || [
'node_modules',
'web_modules',
'server_modules',
// this adds a shorthand so you can require anything in ./app
// without using relative paths
'app'
];
var extensions = config.extensions || ['', '.web.js', '.js', '.jsx'];
var root = config.root || [path.join(config.dir)];
var fallback = (config.fallback || ['node_modules', 'server_modules']).map(function(moduleDir) {
return config.dir + '/' + moduleDir
})
var output = {
path: path.join(config.dir, 'build',
node ? 'prerender' : config.platform || 'public'),
filename: '[name].js' +
(config.longTermCaching ? '?[chunkhash]' : ''),
chunkFilename: (config.commonsChunk ? '[name].js' : '[id].js') +
(config.longTermCaching ? '?[chunkhash]' : ''),
publicPath: '/',
sourceMapFilename: 'debugging/[file].map',
libraryTarget: node ? 'commonjs2' : undefined,
pathinfo: config.debug
};
// PLUGINS
var plugins = [
// set process.env for modules
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(config.env),
TARGET: JSON.stringify(config.target),
PLATFORM: JSON.stringify(config.platform || 'web'),
DISABLE_RAF: JSON.stringify(config.disableRAFBatching)
}
})
];
// prefetch
var prefetches = config.prefetch ||
['react', 'react/lib/ReactComponentBrowserEnvironment'];
prefetches.forEach(function(prefetch) {
plugins.push(new webpack.PrefetchPlugin(prefetch));
});
// outputs build stats to ./build/stats.json
if (config.debug)
plugins.push(statsPlugin(config));
// todo: awaiting new version of react-style
// if (config.separateStylesheet)
// plugins.push(new ReactStylePlugin('bundle.css'));
if (node) {
aliasLoader['react-proxy$'] = 'react-proxy/unavailable';
externals.push(/^react(\/.*)?$/);
plugins.push(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }));
}
if (config.hot) {
plugins.push(new webpack.HotModuleReplacementPlugin());
plugins.push(new webpack.NoErrorsPlugin());
entry = joinEntry('webpack/hot/only-dev-server', entry);
}
if (config.commonsChunk)
plugins.push(
new webpack.optimize.CommonsChunkPlugin('commons', 'commons.js' +
(config.longTermCaching && !node ? '?[chunkhash]' : '')));
if (config.server && web)
entry = joinEntry('webpack-dev-server/client?http://' + config.hostname + ':' + (config.port || 3011), entry);
if (config.separateStylesheet)
plugins.push(new ExtractTextPlugin('[name].css'));
if (config.minify)
plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.optimize.DedupePlugin()
);
// globals will be in every module
if (config.globals)
plugins.push(new webpack.ProvidePlugin(config.globals));
// user defined plugins
if(config.plugins)
config.plugins.forEach(function(plugin){
plugins.push(plugin)
});
// RETURN
var webpackConfig = {
hostname: config.hostname,
entry: entry,
output: output,
target: config.target,
module: Object.assign({
loaders: loaders.concat(stylesheetLoaders)
}, config.module),
devtool: config.devtool || 'eval',
debug: config.debug,
resolveLoader: {
root: config.linkModules ?
path.join(config.dir, 'server_modules') :
path.join(config.dir, 'node_modules'),
alias: aliasLoader
},
externals: externals,
resolve: {
root: root,
modulesDirectories: modulesDirectories,
extensions: extensions,
alias: alias,
fallback: fallback
},
plugins: plugins
};
if (config.debug) {
console.log('Webpack config:'.bold.blue);
console.log(util.inspect(webpackConfig, { depth: 10 }));
console.log();
}
return webpackConfig;
}
module.exports = makeAll;