-
Notifications
You must be signed in to change notification settings - Fork 12
/
webpack.config.js
99 lines (84 loc) · 2.3 KB
/
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
const { PATHS, HOST, PORT, THEME_NAME } = require("./env.config");
const utils = require("./scripts/utils");
const webpack = require("webpack");
const path = require("path");
const WriteFilePlugin = require("write-file-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ENV = utils.getEnv();
const WATCH = global.watch || false;
module.exports = {
entry: getEntry(),
output: {
path: PATHS.compiled(),
publicPath:
ENV === "production"
? "/"
: `http://${HOST}:${PORT}/wp-content/themes/${THEME_NAME}/`,
filename: "js/[name].js",
sourceMapFilename: "[file].map"
},
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
exclude: /node_modules/,
include: PATHS.src()
},
{
test: /\.css$/,
loader: getCssLoader()
}
]
},
devtool: ENV === "production" ? false : "inline-source-map",
plugins: getPlugins(ENV),
target: "web",
watch: WATCH
};
/*
CONFIG ENV DEFINITIONS
*/
function getEntry() {
const entry = {};
entry.main = [PATHS.src("js", "main.js")];
if (ENV === "development") entry.main.push("webpack-hot-middleware/client");
return entry;
}
function getPlugins(env) {
const plugins = [
new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify(env) }),
new CopyWebpackPlugin([{ from: PATHS.src("assets"), to: "assets" }], {
ignore: ["*.psd"]
})
];
switch (env) {
case "production":
plugins.push(new ExtractTextPlugin("main.css"));
// plugins.push(
// new webpack.optimize.UglifyJsPlugin({
// sourceMap: true,
// minimize: true,
// compress: { warnings: false }
// })
// );
break;
case "development":
plugins.push(new webpack.HotModuleReplacementPlugin());
plugins.push(new webpack.NoEmitOnErrorsPlugin());
plugins.push(new WriteFilePlugin());
break;
}
return plugins;
}
function getCssLoader() {
if (ENV === "production") {
return ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader?importLoaders=1&minimize=true!postcss-loader"
});
} else {
return "style-loader!css-loader?importLoaders=1!postcss-loader";
}
}