-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
185 lines (167 loc) · 4.46 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
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
var path = require('path')
var webpack = require('webpack')
var merge = require('webpack-merge')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var MiniCssExtractPlugin = require('mini-css-extract-plugin')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var TerserPlugin = require('terser-webpack-plugin')
var OptimizeCSSAssetsPlugin = require('css-minimizer-webpack-plugin')
console.log('WEBPACK GO!')
// detemine build env
var TARGET_ENV = process.env.npm_lifecycle_event === 'build' ? 'production' : 'development'
// common webpack config
var commonConfig = {
output: {
path: path.resolve(__dirname, 'dist/'),
filename: '[contenthash].js'
},
resolve: {
modules: ['node_modules'],
extensions: ['.js', '.elm']
},
module: {
noParse: /\.elm$/,
rules: [
{
test: /\.(eot|svg|ttf|woff(2)?)(\?v=\d+\.\d+\.\d+)?/,
type: 'asset/inline'
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'front', 'static', 'index.html'),
inject: 'body',
filename: 'index.html'
}),
new webpack.EnvironmentPlugin({
NODE_ENV: 'development'
})
]
}
// additional webpack settings for prod env (when invoked via 'npm run dev')
if (TARGET_ENV === 'development') {
console.log('Serving locally...')
module.exports = merge(commonConfig, {
entry: [
path.join(__dirname, 'front', 'static', 'index.js')
],
devServer: {
static: [
{
directory: path.join(__dirname, 'front', 'static'),
watch: true
},
{
directory: path.join(__dirname, 'front', 'static', 'documents'),
watch: true
}
],
hot: true,
host: '127.0.0.1',
port: 8080
},
mode: 'development',
module: {
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: 'elm-reloader'
},
{
loader: 'elm-webpack-loader',
options: {
debug: true
}
}
]
},
{
test: /\.(css|scss)$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader', options: { sourceMap: true } },
{ loader: 'sass-loader', options: { sourceMap: true } }
]
}
]
}
})
}
// additional webpack settings for prod env (when invoked via 'npm run build')
if (TARGET_ENV === 'production') {
console.log('Building for prod...')
module.exports = merge(commonConfig, {
entry: path.join(__dirname, 'front', 'static', 'index.js'),
mode: 'production',
module: {
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: {
loader: 'elm-webpack-loader',
options: {
optimize: true
}
}
},
{
test: /\.(css|scss)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader'
]
}
]
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
pure_funcs: ['F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'],
pure_getters: true,
keep_fargs: false,
unsafe_comps: true,
unsafe: true
}
}
}),
new OptimizeCSSAssetsPlugin({})
]
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.join(__dirname, 'front', 'static', 'img'),
to: path.join('static', 'img')
},
{
from: path.join(__dirname, 'front', 'static', 'documents', 'xaviermaso.pdf'),
to: 'xaviermaso.pdf'
},
{
from: path.join(__dirname, 'front', 'static', 'documents', 'internship_report_2018.pdf'),
to: 'internship_report_2018.pdf'
},
{
from: path.join(__dirname, 'front', 'static', 'img', 'favicon.ico')
}
]
}),
// extract CSS into a separate file
new MiniCssExtractPlugin({
filename: '[contenthash].css',
chunkFilename: '[id].[contenthash].css'
})
]
})
}