-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
342 lines (325 loc) · 12.6 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Template for webpack.config.js in Fable projects
// Find latest version in https://github.com/fable-compiler/webpack-config-template
// In most cases, you'll only need to edit the CONFIG object (after dependencies)
// See below if you need better fine-tuning of Webpack options
// Dependencies. Also required: core-js, fable-loader, fable-compiler, @babel/core,
// @babel/preset-env, babel-loader, sass, sass-loader, css-loader, style-loader, file-loader, resolve-url-loader
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
var CONFIG = {
// The tags to include the generated JS and CSS will be automatically injected in the HTML template
// See https://github.com/jantimon/html-webpack-plugin
indexHtmlTemplate: './src/Game/index.html',
fsharpEntry: './src/Game/Game.fsproj',
cssEntry: './src/Game/style.scss',
outputDir: './src/Game/deploy',
assetsDir: './src/Game/public',
devServerPort: 8080,
// When using webpack-dev-server, you may need to redirect some calls
// to a external API server. See https://webpack.js.org/configuration/dev-server/#devserver-proxy
devServerProxy: {
// redirect requests that start with /api/ to the server on port 8085
'/api/**': {
target: 'http://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
changeOrigin: true
},
// redirect websocket requests that start with /socket/ to the server on the port 8085
'/socket/**': {
target: 'http://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
ws: true
},
'/login': {
target: 'http://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
ws: true
},
'/auth': {
target: 'http://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
ws: true
},
'/auth/**': {
target: 'http://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
ws: true
}
},
// Use babel-preset-env to generate JS compatible with most-used browsers.
// More info at https://babeljs.io/docs/en/next/babel-preset-env.html
babel: {
presets: [
['@babel/preset-env', {
modules: false,
// This adds polyfills when needed. Requires core-js dependency.
// See https://babeljs.io/docs/en/babel-preset-env#usebuiltins
// Note that you still need to add custom polyfills if necessary (e.g. whatwg-fetch)
useBuiltIns: 'usage',
corejs: 3
}]
],
}
}
// If we're running the webpack-dev-server, assume we're in development mode
var isProduction = !process.argv.find(v => v.indexOf('webpack-dev-server') !== -1);
console.log('Bundling for ' + (isProduction ? 'production' : 'development') + '...');
// The HtmlWebpackPlugin allows us to use a template for the index.html page
// and automatically injects <script> or <link> tags for generated bundles.
var commonPlugins = [
new HtmlWebpackPlugin({
filename: 'game/index.html',
template: resolve(CONFIG.indexHtmlTemplate),
excludeChunks: ['join', 'joinstyle']
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: resolve('./src/Join/index.html'),
excludeChunks: ['game', 'style']
}),
];
var bgaPlugins = [
new HtmlWebpackPlugin({
filename: 'game/bga.html',
template: resolve('./src/BgaGame/index.html'),
excludeChunks: ['join', 'game', 'joinstyle']
})
];
module.exports = [{
// In development, split the JavaScript and CSS files in order to
// have a faster HMR support. In production bundle styles together
// with the code because the MiniCssExtractPlugin will extract the
// CSS in a separate files.
entry: isProduction ? {
game: [resolve(CONFIG.fsharpEntry),
resolve(CONFIG.cssEntry)],
join: [resolve('./src/Join/Join.fsproj'),
resolve('./src/Join/join-style.scss')],
} : {
game: [resolve(CONFIG.fsharpEntry)],
join: [resolve('./src/Join/Join.fsproj')],
joinstyle: [resolve('./src/Join/join-style.scss')],
style: [resolve(CONFIG.cssEntry)],
},
// Add a hash to the output file name in production
// to prevent browser caching if code changes
output: {
path: resolve(CONFIG.outputDir),
filename: isProduction ? '[name].[hash].js' : '[name].js'
},
mode: isProduction ? 'production' : 'development',
devtool: isProduction ? 'source-map' : 'eval-source-map',
optimization: {
splitChunks: {
chunks: 'all'
},
},
// Besides the HtmlPlugin, we use the following plugins:
// PRODUCTION
// - MiniCssExtractPlugin: Extracts CSS from bundle to a different file
// To minify CSS, see https://github.com/webpack-contrib/mini-css-extract-plugin#minimizing-for-production
// - CopyWebpackPlugin: Copies static assets to output directory
// DEVELOPMENT
// - HotModuleReplacementPlugin: Enables hot reloading when code changes without refreshing
plugins: isProduction ?
commonPlugins.concat([
new MiniCssExtractPlugin({ filename: '[name].[hash].css' }),
new CopyWebpackPlugin([{ from: resolve(CONFIG.assetsDir) }]),
])
: commonPlugins.concat([
new webpack.HotModuleReplacementPlugin(),
]),
resolve: {
// See https://github.com/fable-compiler/Fable/issues/1490
symlinks: false,
alias: {
fonts: path.resolve(__dirname, "src/Game/public/font"),
img: path.resolve(__dirname, "src/Game/public/img")
}
},
// Configuration for webpack-dev-server
devServer: {
publicPath: '/',
contentBase: resolve(CONFIG.assetsDir),
host: '0.0.0.0',
port: CONFIG.devServerPort,
proxy: CONFIG.devServerProxy,
historyApiFallback: {
rewrites: [
{ from: /^\/game\/.*/, to: '/game/index.html' },
]
},
hot: true,
inline: true
},
// - fable-loader: transforms F# into JS
// - babel-loader: transforms JS to old syntax (compatible with old browsers)
// - sass-loaders: transforms SASS/SCSS into JS
// - file-loader: Moves files referenced in the code (fonts, images) into output folder
module: {
rules: [
{
test: /\.fs(x|proj)?$/,
use: {
loader: 'fable-loader',
options: {
babel: CONFIG.babel
}
}
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: CONFIG.babel
},
},
{
test: /\.(sass|scss|css)$/,
use: [
isProduction
? MiniCssExtractPlugin.loader
: 'style-loader',
'css-loader',
{
loader: 'resolve-url-loader',
},
{
loader: 'sass-loader',
options: { implementation: require('sass') }
}
],
},
{
test: /\.(png|jpg|jpeg|gif|svg)(\?.*)?$/,
use: [isProduction ? 'file-loader?name=/img/[name].[ext]' : 'file-loader?name=/img/[name].[ext]']
},
{
test: /fa-.*\.(woff|woff2|ttf|eot)(\?.*)?$/,
use: ['file-loader']
},
{
test: /\.(woff|woff2|ttf|eot)(\?.*)?$/,
exclude: /fa-.*\.(woff|woff2|ttf|eot)(\?.*)?$/,
use: [isProduction ? 'file-loader?name=/font/[name].[ext]' : 'file-loader?name=/font/[name].[ext]']
}
]
}
},
// {
// /// BGA
// entry: isProduction ? {
// crazy: [resolve('./src/BgaGame/BGAGame.fsproj'),
// resolve(CONFIG.cssEntry)],
// } : {
// crazy: [resolve('./src/BgaGame/BGAGame.fsproj')],
// style: [resolve(CONFIG.cssEntry)],
// },
// // Add a hash to the output file name in production
// // to prevent browser caching if code changes
// output: {
// path: resolve("./bga-deploy/"),
// filename: isProduction ? 'modules/[name].js' : 'modules/[name].js',
// //libraryTarget: 'var',
// //library: 'crazy'
// },
// mode: isProduction ? 'production' : 'development',
// devtool: isProduction ? 'source-map' : 'eval-source-map',
// optimization: {
// splitChunks: {
// chunks: 'all'
// },
// },
// // Besides the HtmlPlugin, we use the following plugins:
// // PRODUCTION
// // - MiniCssExtractPlugin: Extracts CSS from bundle to a different file
// // To minify CSS, see https://github.com/webpack-contrib/mini-css-extract-plugin#minimizing-for-production
// // - CopyWebpackPlugin: Copies static assets to output directory
// // DEVELOPMENT
// // - HotModuleReplacementPlugin: Enables hot reloading when code changes without refreshing
// plugins: isProduction ?
// bgaPlugins.concat([
// new MiniCssExtractPlugin({ filename: 'crazyfarmers.css' }),
// new CopyWebpackPlugin([{ from: resolve(CONFIG.assetsDir) }]),
// new CopyWebpackPlugin([{ from: resolve('./bga') }]),
// ])
// : bgaPlugins.concat([
// new webpack.HotModuleReplacementPlugin(),
// ]),
// resolve: {
// // See https://github.com/fable-compiler/Fable/issues/1490
// symlinks: false,
// alias: {
// fonts: path.resolve(__dirname, "src/Game/public/font"),
// img: path.resolve(__dirname, "src/Game/public/img")
// }
// },
// // // Configuration for webpack-dev-server
// // devServer: {
// // publicPath: '/1/',
// // contentBase: resolve(CONFIG.assetsDir),
// // host: '0.0.0.0',
// // port: CONFIG.devServerPort,
// // proxy: CONFIG.devServerProxy,
// // historyApiFallback: {
// // rewrites: [
// // { from: /^\/game\/.*/, to: '/game/index.html' },
// // ]
// // },
// // hot: true,
// // inline: true
// // },
// // - fable-loader: transforms F# into JS
// // - babel-loader: transforms JS to old syntax (compatible with old browsers)
// // - sass-loaders: transforms SASS/SCSS into JS
// // - file-loader: Moves files referenced in the code (fonts, images) into output folder
// module: {
// rules: [
// {
// test: /\.fs(x|proj)?$/,
// use: {
// loader: 'fable-loader',
// options: {
// babel: CONFIG.babel
// }
// }
// },
// {
// test: /\.js$/,
// exclude: /node_modules/,
// use: {
// loader: 'babel-loader',
// options: CONFIG.babel
// },
// },
// {
// test: /\.(sass|scss|css)$/,
// use: [
// isProduction
// ? MiniCssExtractPlugin.loader
// : 'style-loader',
// 'css-loader',
// {
// loader: 'resolve-url-loader',
// },
// {
// loader: 'sass-loader',
// options: { implementation: require('sass') }
// }
// ],
// },
// {
// test: /\.(png|jpg|jpeg|gif|svg)(\?.*)?$/,
// use: [isProduction ? 'file-loader?name=./img/[name].[ext]' : 'file-loader?name=[name].[ext]']
// },
// {
// test: /\.(woff|woff2|ttf|eot)(\?.*)?$/,
// use: [isProduction ? 'file-loader?name=./font/[name].[ext]' : 'file-loader?name=[name].[ext]']
// }
// ]
// }
// }
]
;
function resolve(filePath) {
return path.isAbsolute(filePath) ? filePath : path.join(__dirname, filePath);
}