-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmake-webpack-config.js
More file actions
118 lines (99 loc) · 2.52 KB
/
make-webpack-config.js
File metadata and controls
118 lines (99 loc) · 2.52 KB
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
/*
* Webpack server configuration
*
* For dev, config is set up for serving the webpack-dev-server, which will watch for changes and recompile as required if
* the subfolder /webpack-dev-server/ is visited. Visiting the root will not automatically reload.
*
* For production, config is set up for serving the distribution version. It will be compiled to dist/ by default
*/
const webpack = require("webpack");
const path = require("path");
/**
* http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
*/
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function buildConfig(options) {
const dev = options.dev;
// Include js-common as source path to be process by babel loader (jsx and ES6 support)
const currentSrc = escapeRegExp(path.join(__dirname, "src"));
// Entry
let entry;
if (!dev) {
entry = "./src/script/tooltip.jsx";
} else {
console.log("is dev");
entry = ["webpack/hot/only-dev-server", "./src/script/example-tooltip.jsx"];
}
// Output
const output = {
publicPath: "/assets/",
filename: "tooltip.bundle.js"
};
if (!dev) {
output.path = "dist/assets/";
}
// Plugins
let plugins;
if (!dev) {
plugins = [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin()
];
} else {
plugins = [new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin()];
}
// js/jsx loader
const jsLoader = {
test: /\.js$/,
include: currentSrc
};
if (!dev) {
jsLoader.loader = "babel-loader";
} else {
jsLoader.loaders = ["react-hot-loader", "babel-loader"];
}
const config = {
cache: dev,
debug: dev,
entry: entry,
output: output,
stats: {
colors: true,
reasons: dev
},
resolve: {
extensions: ["", ".js"]
},
module: {
preLoaders: [
{
test: /\.(js|jsx)$/,
include: currentSrc,
loader: "eslint"
}
],
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" },
{ test: /\.(jpe?g|png|gif)$/, loader: "url-loader?limit=10000" },
{ test: /\.jsx$/, exclude: /node_modules/, loaders: dev ? ["react-hot-loader", "babel-loader"] : ["babel-loader"] }
]
},
plugins: plugins,
headers: {
"Access-Control-Allow-Origin": "*"
}
};
// Add sourceMap for dev
if (dev) {
config.devtool = "source-map";
}
return config;
}
module.exports = function(options) {
console.log(buildConfig(options));
return buildConfig(options);
};