-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-webpack-config.js
175 lines (167 loc) · 5.35 KB
/
make-webpack-config.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
var path = require("path");
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var StatsPlugin = require("stats-webpack-plugin");
var loadersByExtension = require("./config/loadersByExtension");
// var UglifyJsPlugin = require("uglify-loader")
module.exports = function(options) {
var entry = {
main: reactEntry("main"),
// second: reactEntry("second")
};
var loaders = {
"coffee": "coffee-redux-loader",
"jsx": options.hotComponents ? ["react-hot-loader", "babel-loader?stage=0"] : "babel-loader?stage=0",
"json": "json-loader",
"js": {
loader: "babel-loader?stage=0",
include: path.join(__dirname, "app")
},
"json5": "json5-loader",
"txt": "raw-loader",
"png|jpg|jpeg|gif|svg": "url-loader?limit=10000",
"woff|woff2": "url-loader?limit=100000",
"ttf|eot": "file-loader",
"wav|mp3": "file-loader",
"html": "html-loader",
"md|markdown": ["html-loader", "markdown-loader"]
};
var cssLoader = options.minimize ? "css-loader" : "css-loader?localIdentName=[path][name]---[local]---[hash:base64:5]";
var stylesheetLoaders = {
"css": cssLoader,
"less": [cssLoader, "less-loader"],
"styl": [cssLoader, "stylus-loader"],
"scss|sass": [cssLoader, "sass-loader"]
};
var additionalLoaders = [
// { test: /some-reg-exp$/, loader: "any-loader" }
];
var alias = {
};
var aliasLoader = {
};
var externals = [
];
var modulesDirectories = ["web_modules", "node_modules"];
var extensions = ["", ".web.js", ".js", ".jsx"];
var root = path.join(__dirname, "app");
var publicPath = options.devServer ?
"http://localhost:2992/_assets/" :
"/_assets/";
var output = {
path: path.join(__dirname, "build", options.prerender ? "prerender" : "public"),
publicPath: publicPath,
headers: { "Access-Control-Allow-Origin": "*" },
filename: "[name].js" + (options.longTermCaching && !options.prerender ? "?[chunkhash]" : ""),
chunkFilename: (options.devServer ? "[id].js" : "[name].js") + (options.longTermCaching && !options.prerender ? "?[chunkhash]" : ""),
sourceMapFilename: "debugging/[file].map",
libraryTarget: options.prerender ? "commonjs2" : undefined,
pathinfo: options.debug || options.prerender
};
var excludeFromStats = [
/node_modules[\\\/]react(-router)?[\\\/]/,
/node_modules[\\\/]items-store[\\\/]/
];
var plugins = [
new webpack.PrefetchPlugin("react"),
new webpack.PrefetchPlugin("react/lib/ReactComponentBrowserEnvironment")
];
if(options.prerender) {
plugins.push(new StatsPlugin(path.join(__dirname, "build", "stats.prerender.json"), {
chunkModules: true,
exclude: excludeFromStats
}));
aliasLoader["react-proxy$"] = "react-proxy/unavailable";
aliasLoader["react-proxy-loader$"] = "react-proxy-loader/unavailable";
externals.push(
/^react(\/.*)?$/,
/^reflux(\/.*)?$/,
"superagent",
"async"
);
plugins.push(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }));
} else {
plugins.push(new StatsPlugin(path.join(__dirname, "build", "stats.json"), {
chunkModules: true,
exclude: excludeFromStats
}));
}
// if(options.uglify) {
// plugins.push(new webpack.optimize.UglifyJsPlugin({ compress: {warnings: false}, mangle: true }));
// }
if(options.commonsChunk) {
plugins.push(new webpack.optimize.CommonsChunkPlugin("commons", "commons.js" + (options.longTermCaching && !options.prerender ? "?[chunkhash]" : "")));
}
var asyncLoader = {
test: require("./app/routeHandlers/async").map(function(name) {
return path.join(__dirname, "app", "routeHandlers", name);
}),
loader: options.prerender ? "react-proxy-loader/unavailable" : "react-proxy-loader"
};
function reactEntry(name) {
return (options.prerender ? "./config/prerender?" : "./config/app?") + name;
}
Object.keys(stylesheetLoaders).forEach(function(ext) {
var loaders = stylesheetLoaders[ext];
if(Array.isArray(loaders)) loaders = loaders.join("!");
if(options.prerender) {
stylesheetLoaders[ext] = loaders.replace(/^css-loader/, "css-loader/locals");
} else if(options.separateStylesheet) {
stylesheetLoaders[ext] = ExtractTextPlugin.extract("style-loader", loaders);
} else {
stylesheetLoaders[ext] = "style-loader!" + loaders;
}
});
if(options.separateStylesheet && !options.prerender) {
plugins.push(new ExtractTextPlugin("[name].css" + (options.longTermCaching ? "?[contenthash]" : "")));
}
if(options.minimize && !options.prerender) {
plugins.push(
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
}),
new webpack.optimize.DedupePlugin()
);
}
if(options.minimize) {
plugins.push(
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
}),
new webpack.NoErrorsPlugin()
);
}
return {
entry: entry,
output: output,
target: options.prerender ? "node" : "web",
module: {
loaders: [asyncLoader].concat(loadersByExtension(loaders)).concat(loadersByExtension(stylesheetLoaders)).concat(additionalLoaders)
},
devtool: options.devtool,
debug: options.debug,
resolveLoader: {
root: path.join(__dirname, "node_modules"),
alias: aliasLoader
},
externals: externals,
resolve: {
root: root,
modulesDirectories: modulesDirectories,
extensions: extensions,
alias: alias
},
plugins: plugins,
devServer: {
headers: { "Access-Control-Allow-Origin": "*" },
stats: {
cached: false,
exclude: excludeFromStats
}
}
};
};