generated from pavle-goloskokovic/phaser3-webpack5-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.ts
147 lines (141 loc) · 4.64 KB
/
webpack.common.ts
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
import { resolve } from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import {description, tagId, title} from './src/ts/game.config';
import pkg from './package.json';
// boolean indicating if current build is dev or prod
const prod: boolean = module.parent.id.includes('.prod');
const banner = '\nCopyright (c) ' + new Date().getFullYear() + ' ' + pkg.author + '\n';
const htmlminOptions = {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyJS: {
compress: false,
mangle: false
},
minifyCSS: true
} as HtmlWebpackPlugin.MinifyOptions;
const assetsRule = (
test: webpack.RuleSetRule['test'],
folderName: 'images' | 'fonts' | 'audio'
): webpack.RuleSetRule =>
{
return {
test,
type: 'asset/resource',
generator: {
filename: `assets/${folderName}/[name]${
prod ? '.[contenthash]' : ''
}[ext][query]`
}
};
};
export default <webpack.Configuration>{
entry: {
// defaults to ./src
// Here the application starts executing
// and webpack starts bundling
game: resolve(__dirname, 'src/ts/game.ts') // string | object | array
},
target: ['web', 'es5'],
output: {
// options related to how webpack emits results
filename: `[name]${ prod ? '.[contenthash]' : '' }.js`, // string
// the filename template for entry chunks
path: resolve(__dirname, 'docs'), // string
// the target directory for all output files
// must be an absolute path (use the Node.js path module)
// publicPath: "/assets/", // string
// the url to the output directory resolved relative to the HTML page
clean: true
},
optimization: {
moduleIds: 'deterministic',
runtimeChunk: false
},
module: {
// configuration regarding modules
rules: [
// rules for modules (configure loaders, parser options, etc.)
{
test: /\.ts$/,
loader: 'ts-loader',
options: {
onlyCompileBundledFiles: true
},
exclude: /node_modules/
},
{
test: /\.ejs$/,
use: {
loader: 'ejs-compiled-loader',
options: {
htmlmin: true,
htmlminOptions
}
}
},
{
test: /\.css$/i,
use: [
prod ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader'
]
},
/**
* Fonts
*/
assetsRule(/\.(woff|woff2|eot|ttf|otf)$/i, 'fonts'),
assetsRule(/assets\\fonts\\.+\.xml$/i, 'fonts'),
/**
* Images
*/
assetsRule(/\.(png|svg|jpg|jpeg|gif)$/i, 'images'),
/**
* Audio
*/
assetsRule(/\.(mp3|ogg)$/i, 'audio')
]
},
resolve: {
extensions: ['.ts', '.js'], // .js for Phaser imports
// extensions that are used
alias: {
'phaser': resolve(__dirname,
// 'node_modules/phaser/src/phaser-core.js')
'node_modules/phaser/dist/phaser-arcade-physics.min.js')
}
},
plugins: [
new webpack.DefinePlugin({
'typeof CANVAS_RENDERER': JSON.stringify(true),
'typeof WEBGL_RENDERER': JSON.stringify(true),
'typeof WEBGL_DEBUG': JSON.stringify(false),
'typeof FEATURE_SOUND': JSON.stringify(true),
'typeof PLUGIN_CAMERA3D': JSON.stringify(false),
'typeof PLUGIN_FBINSTANT': JSON.stringify(false)
}),
new webpack.BannerPlugin({
banner,
entryOnly: true
}),
new HtmlWebpackPlugin({
template: './src/ejs/index.ejs',
templateParameters: {
banner: `\n${banner.replace('\n', '\n ')}\n`,
tagId: prod ? tagId : null,
title,
description
},
minify: prod ? htmlminOptions : 'auto'
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// all options are optional
filename: `style${ prod ? '.[contenthash]' : '' }.css`,
chunkFilename: `style${ prod ? '.[contenthash]' : '' }.css`
})
]
};