-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
178 lines (175 loc) · 3.74 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
// Import required modules
const path = require('path')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')
const CSSExtractPlugin = require('mini-css-extract-plugin')
const MarkoPlugin = require('@marko/webpack/plugin').default
const SpawnServerPlugin = require('spawn-server-webpack-plugin')
const MinifyCSSPlugin = require('css-minimizer-webpack-plugin')
const config = require('./config')
const markoPlugin = new MarkoPlugin()
const { NODE_ENV = 'development' } = process.env
const isDev = NODE_ENV === 'development'
const isProd = !isDev
const filenameTemplate = `${isProd ? '' : `[name].`}[contenthash:8]`
const spawnedServer =
isDev &&
new SpawnServerPlugin({
args: [
'--enable-source-maps',
// Allow debugging spawned server with the INSPECT=1 env var
process.env.INSPECT && '--inspect',
].filter(Boolean),
})
module.exports = [
compiler({
name: 'browser',
target: 'web',
devtool: isProd
? 'cheap-module-source-map'
: 'eval-cheap-module-source-map',
entry: {
client: './app/client.js'
},
output: {
filename: `${filenameTemplate}.js`,
path: path.join(__dirname, 'dist/assets')
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: 3
}
},
devServer: isProd
? undefined
: {
hot: true,
static: false,
host: '0.0.0.0',
allowedHosts: 'all',
port: parseInt(process.env.PORT || config.port, 10),
headers: {
'Access-Control-Allow-Origin': '*'
},
...spawnedServer.devServerConfig
},
module: {
rules: [
{
test: /\.css$/,
use: [CSSExtractPlugin.loader, 'css-loader']
},
{
test: /\.(jpg|jpeg|gif|png|svg)$/,
type: 'asset'
}
]
},
plugins: [
markoPlugin.browser,
new webpack.DefinePlugin({
'typeof window': '\'object\''
}),
new CSSExtractPlugin({
filename: `${filenameTemplate}.css`,
ignoreOrder: true
}),
isProd && new MinifyCSSPlugin()
]
}),
compiler({
name: 'server',
target: 'async-node',
devtool: 'inline-nosources-cheap-module-source-map',
externals: [
// Exclude node_modules, but ensure non js files are bundled
// Eg: `.marko`, `.css`, etc.
nodeExternals({
allowlist: [/\.(?!(?:js|json)$)[^.]+$/]
})
],
optimization: {
minimize: false
},
entry: './app/server.js',
output: {
libraryTarget: 'commonjs2',
path: path.join(__dirname, 'dist'),
devtoolModuleFilenameTemplate: '[absolute-resource-path]'
},
module: {
rules: [
{
test: /\.(jpg|jpeg|gif|png|svg)$/,
generator: { emit: false },
type: 'asset/resource'
}
]
},
plugins: [
spawnedServer,
markoPlugin.server,
new webpack.IgnorePlugin({
resourceRegExp: /\.css$/
}),
new webpack.DefinePlugin({
'typeof window': '\'undefined\''
})
]
})
]
// Shared config for both server and client compilers
function compiler(options) {
return {
...options,
mode: isProd ? 'production' : 'development',
stats: isDev && 'minimal',
cache: {
type: 'filesystem'
},
output: {
...options.output,
publicPath: '/assets/',
assetModuleFilename: `${filenameTemplate}[ext][query]`
},
resolve: {
extensions: ['.js', '.json']
},
module: {
rules: [
{
test: /\.(scss)$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader'
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: () => [
require('autoprefixer')
]
}
}
},
{
loader: 'sass-loader'
}
]
},
...options.module.rules,
{
test: /\.marko$/,
loader: '@marko/webpack/loader'
}
]
},
plugins: options.plugins.filter(Boolean)
}
}