generated from Teddy95/webapp-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
180 lines (166 loc) · 4.28 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
// Import required modules
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const WebpackPreBuildPlugin = require('pre-build-webpack')
const { getUserAgentRegExp } = require('browserslist-useragent-regexp')
const config = require('./config')
// Get node environment
const { NODE_ENV } = process.env
const isDev = NODE_ENV === 'development'
const env = isDev ? 'development' : 'production'
// Define pre-build function
const preBuild = () => {
// Build user agent regexp file for outdatedBrowser.js script
var fileContent = 'module.exports = ' + getUserAgentRegExp({
browsers: fs.readFileSync(path.resolve(__dirname, '.browserslistrc')).toString().replace('\r', '').split('\n').filter(Boolean).join(' and '),
allowHigherVersions: true
})
var file = path.resolve(__dirname, 'dist', 'supportedBrowsers.js')
if (!fs.existsSync(path.resolve(__dirname, 'dist'))) {
fs.mkdirSync(path.resolve(__dirname, 'dist'))
}
fs.writeFile(file, fileContent, (err) => {
if (err) {
throw err
}
})
}
// Define webpack rules
const javascriptRule = {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
const markoRule = {
test: /\.marko?$/,
use: ['babel-loader', '@marko/webpack/loader']
}
const styleRule = {
test: /\.(sass|scss|css)$/,
use: isDev ? [
'css-hot-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
] : [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
}
const vectorRule = {
test: /\.svg/,
loader: 'svg-url-loader'
}
const imageRule = {
test: /\.(jpg|jpeg|gif|png|ico)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[name]-[hash:8].[ext]'
}
}
]
}
const mediaRule = {
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac|m4a)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'media/[name]-[hash:8].[ext]'
}
}
]
}
const fontRule = {
test: /\.(woff|woff2|ttf|eot)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'webfonts/[name]-[hash:8].[ext]'
}
}
]
}
// Webpack config for browser
const client = {
target: 'web',
name: 'Client',
mode: env,
entry: {
app: isDev ? [
'webpack-hot-middleware/client?reload=true',
'./app/client.js'
] : ['./app/client.js'],
outdatedBrowser: ['./app/scripts/outdatedBrowser.js']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: config.path + '/assets/'
},
devServer: isDev ? {
contentBase: path.join(__dirname, 'dist'),
historyApiFallback: true,
overlay: true,
hot: true,
stats: 'minimal',
} : undefined,
devtool: isDev ? 'source-map' : undefined,
resolve: {
extensions: ['.json', '.js', '.marko']
},
module: {
rules: [javascriptRule, markoRule, styleRule, vectorRule, imageRule, mediaRule, fontRule]
},
plugins: [
new WebpackPreBuildPlugin(() => {
preBuild()
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
isDev && new webpack.HotModuleReplacementPlugin()
].filter(Boolean)
}
// Webpack config for server
const server = {
name: 'Server',
mode: env,
target: 'node',
node: {
__filename: true,
__dirname: true
},
externals: [nodeExternals({
whitelist: ['marko', /\.marko$/]
})],
entry: {
server: ['./app/server.js']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.json', '.js', '.marko']
},
module: {
rules: [javascriptRule, markoRule, vectorRule, imageRule]
}
}
// Export configurations
module.exports = [
server,
client
]