-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
executable file
·150 lines (142 loc) · 5.04 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
const buildConfig = require('./build.config');
const path = require('path');
const fs = require('fs');
const fs_extra = require('fs-extra');
const remove_dir = require('delete');
// Necesary webpack plugins.
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BrowserAsyncPlugin = require('browser-sync-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
// Phaser webpack config.
const phaserModule = path.join(__dirname, '/node_modules/phaser/');
const phaser = path.join(phaserModule, 'src/phaser.js');
// Take directory and entry file path from build.config.js file.
const game_directory = `${buildConfig.gameDir}`;
const entry_file_path = `${buildConfig.entryFilePath}`;
remove_dir.sync('./dist'); // Delete the dist folder every time we build the pack.
// Copy all the given assets to dist folder.
const copyFilesAndDirectory = (src, dest) => {
fs.exists(src, (exists) => {
if (!exists) return;
console.log(">>>>src---" + src, dest);
fs_extra.copySync(src, dest);
});
};
// Array of objects that are copied to dist folder from source folder.
let toCopy = [{
from: `${game_directory}/assets/audio`,
to: './dist/assets/audio'
},
{
from: `${game_directory}/assets/images`,
to: './dist/assets/images'
},
{
from: `${game_directory}/assets/bitmapfonts`,
to: './dist/assets/bitmapfonts'
},
{
from: `${game_directory}/assets/json`,
to: './dist/assets/json'
},
{
from: `${game_directory}/assets/spritesheets`,
to: './dist/assets/spritesheets'
},
{
from: `${game_directory}/assets/xml`,
to: './dist/assets/xml'
},
{
from: `${game_directory}/js/plugins`,
to: './dist/plugins'
},
];
// Copy all the assets from source to dist folder.
for (let i = 0; i < toCopy.length; i++) {
copyFilesAndDirectory(toCopy[i].from, toCopy[i].to);
}
// Export the webpack configuration.
module.exports = {
mode: buildConfig.mode, // When it is production by default the uglify plugin activated else it wont.
// Entry points to the chunk.
entry: {
app: [`${game_directory}/${entry_file_path}`],
vendor: [phaser]
},
// Emit the output chunks with respective entry name.
output: {
filename: '[name].[hash].js',
path: path.resolve(__dirname, 'dist'),
},
watch: true, // Helps to run browser-sync plugin.
// Add webpack plugins.
plugins: [
new HtmlWebpackPlugin({
filename: '../dist/index.html',
template: `./index.html`,
chunks: ['vendor', 'app'],
hash: true
}),
new BrowserAsyncPlugin({
host: process.env.IP || 'localhost',
port: process.env.PORT || 3000,
server: {
baseDir: ['./dist']
},
})
],
// Modules support.
module: {
rules: [{
test: /\.js$/,
use: ['babel-loader'],
include: path.join(__dirname, game_directory)
},
{
test: /\.ts$/,
use: ['babel-loader', 'awesome-typescript-loader'],
include: path.join(__dirname)
},
]
},
// Need to resolve the module that is being imported in the code.
resolve: {
extensions: ['.ts', '.js'],
},
// Optimize the code with options that makes code less in size.
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
test: /\.js(\?.*)?$/i,
terserOptions: {
output: {
comments: buildConfig.terserOutput.comments,
quote_keys: buildConfig.terserOutput.quote_keys,
keep_quoted_props: buildConfig.terserOutput.keep_quoted_props,
},
mangle: {
keep_fnames: buildConfig.mangle.keep_fnames,
keep_classnames: buildConfig.mangle.keep_classnames,
toplevel: buildConfig.mangle.toplevel,
safari10: buildConfig.mangle.safari10,
},
compress: {
arguments: buildConfig.compress.arguments,
collapse_vars: buildConfig.compress.collapse_vars,
conditionals: buildConfig.compress.conditionals,
arrows: buildConfig.compress.arrows,
unsafe_arrows: buildConfig.compress.unsafe_arrows,
loops: buildConfig.compress.loops,
toplevel: buildConfig.compress.toplevel,
reduce_funcs: buildConfig.compress.reduce_funcs,
reduce_vars: buildConfig.compress.reduce_vars,
join_vars: buildConfig.compress.join_vars,
drop_console: buildConfig.compress.drop_console
},
},
}),
],
},
};