-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
167 lines (163 loc) · 4.26 KB
/
webpack.common.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
const CWD = process.cwd()
const path = require("path")
const webpack = require("webpack")
const BundleTracker = require("webpack-bundle-tracker")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const TerserJSPlugin = require("terser-webpack-plugin")
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin")
const packageJson = require(path.resolve(CWD, "package.json"))
const HOST = process.env.HOST || "127.0.0.1"
const DEBUG = process.env.NODE_ENV !== "production"
const HTTPS = !!process.env.HTTPS
function cssLoader(firstLoader) {
return [
DEBUG ? "style-loader" : MiniCssExtractPlugin.loader,
"css-loader?sourceMap",
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require("autoprefixer")()],
sourceMap: true,
},
},
},
]
.concat(firstLoader || [])
.filter(function (el) {
return !!el
})
}
module.exports = {
mode: DEBUG ? "development" : "production",
context: path.join(CWD, "app", "static", "app"),
devtool: "source-map",
entry: {
main: "./main.js",
},
output: {
path: path.resolve("./static/app/"),
publicPath: DEBUG
? "http" + (HTTPS ? "s" : "") + "://" + HOST + ":4000/"
: (process.env.STATIC_URL || "/static/") + "app/",
filename: DEBUG ? "[name].js" : "[name]-[contenthash].js",
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: [
[
"@babel/preset-env",
{
modules: false,
// debug: true,
targets: packageJson.browserslist,
useBuiltIns: "usage",
corejs: "3",
},
],
],
plugins: [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties",
],
cacheDirectory: path.resolve(CWD, "tmp"),
sourceType: "unambiguous",
},
},
],
},
{
test: /\.css$/,
use: cssLoader(),
},
{
test: /\.scss$/,
use: cssLoader({
loader: "sass-loader",
options: {
sassOptions: {
includePaths: [path.resolve(path.join(CWD, "node_modules"))],
},
},
}),
},
{
test: /\.less$/,
use: cssLoader("less-loader"),
},
{
test: /\.(png|woff|woff2|svg|eot|ttf|otf|gif|jpe?g)$/,
use: [
{
loader: "url-loader",
options: {
limit: 500,
// ManifestStaticFilesStorage reuse.
name: "[path][name].[md5:hash:hex:12].[ext]",
// No need to emit files in production, collectstatic does it.
},
},
],
},
],
},
resolve: {
extensions: [".js", ".jsx"],
modules: ["app/static/app/", "node_modules", "."],
alias: {},
},
plugins: [
DEBUG ? null : new CleanWebpackPlugin(),
DEBUG
? null
: new MiniCssExtractPlugin({
filename: "[name]-[contenthash].css",
}),
new BundleTracker({
filename: "./static/webpack-stats-" + (DEBUG ? "dev" : "prod") + ".json",
}),
DEBUG
? new webpack.NamedModulesPlugin()
: new webpack.HashedModuleIdsPlugin(),
].filter(function (el) {
return !!el
}),
devServer: {
contentBase: false,
inline: true,
quiet: false,
https: HTTPS,
disableHostCheck: true,
headers: { "Access-Control-Allow-Origin": "*" },
host: HOST,
port: 4000,
},
performance: {
// No point warning in development, since HMR / CSS bundling blows up
// the asset / entrypoint size anyway.
hints: DEBUG ? false : "warning",
},
optimization: {
minimizer: [
new TerserJSPlugin({}),
new OptimizeCSSAssetsPlugin({
cssProcessorPluginOptions: {
preset: [
"default",
{
svgo: false,
},
],
},
}),
],
},
}