-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.babel.js
152 lines (136 loc) · 3.26 KB
/
webpack.config.babel.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
import { join } from 'path'
import {
DefinePlugin,
HotModuleReplacementPlugin,
} from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import {
name,
version,
description,
keywords,
} from './package.json'
const {
NODE_ENV = 'production',
PROJECT_NAME = name || 'game',
PROJECT_DESCRIPTION = description || '',
PROJECT_KEYWORDS = keywords || '',
BUILD_DIR = 'build',
WEBPACK_DEVTOOL,
WEBPACK_DEV_SERVER_HOST = '0.0.0.0',
WEBPACK_DEV_SERVER_PORT = 3001,
WEBPACK_PUBLIC_PATH = './',
WEBPACK_HASH_LENGTH = 10,
} = process.env
const isDevelopment = NODE_ENV === 'development'
const buildDirAbsolutePath = join(__dirname, BUILD_DIR)
const entry = {
[PROJECT_NAME]: join(__dirname, 'src/index.js'),
};
const output = {
path: buildDirAbsolutePath,
filename: isDevelopment ?
`assets/[name]-[hash:${WEBPACK_HASH_LENGTH}].js` :
`assets/[name]-[chunkhash:${WEBPACK_HASH_LENGTH}].js`,
publicPath: isDevelopment ? '/' : WEBPACK_PUBLIC_PATH,
};
const plugins = {
common: [
new DefinePlugin({
IS_DEVELOPMENT: JSON.stringify(isDevelopment),
[`${PROJECT_NAME}_VERSION`]: version && JSON.stringify(version),
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.ejs',
title: PROJECT_NAME,
meta: {
description: PROJECT_DESCRIPTION,
keywords: PROJECT_KEYWORDS,
},
inject: 'body',
cache: true,
minify: !isDevelopment && {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
},
}),
],
development: [
new HotModuleReplacementPlugin(),
],
production: [],
};
const devServer = {
host: WEBPACK_DEV_SERVER_HOST,
port: WEBPACK_DEV_SERVER_PORT,
contentBase: buildDirAbsolutePath,
hot: true,
stats: { colors: true },
};
const fileNameFormat = `[name]-[hash:${WEBPACK_HASH_LENGTH}].[ext]`
const getFileLoader = (location) => ({
loader: 'file-loader',
options: {
name: `assets/${location}/${fileNameFormat}`,
},
})
const graphicsLoader = getFileLoader('graphics')
const rules = {
common: [{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader'],
}, {
test: /\.(mp3|ogg|m4a|aac)$/,
use: [getFileLoader('audio')],
}, {
test: /\.(ttf|woff)$/,
use: [getFileLoader('fonts')],
},{
test: /\.xml$/,
use: [getFileLoader('data')],
}],
development: [{
test: /\.(png|jpg|jpeg|gif)$/,
use: [graphicsLoader],
}],
production: [{
test: /\.(png|jpg|jpeg|gif)$/,
use: [
graphicsLoader, {
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
quality: 90,
},
optipng: {
optimizationLevel: 6,
},
gifsicle: {
interlaced: true,
},
},
},
],
}],
};
export default {
mode: NODE_ENV,
entry,
output,
devtool:
WEBPACK_DEVTOOL ||
(isDevelopment ? 'cheap-eval-source-map' : undefined),
plugins: isDevelopment ?
[...plugins.common, ...plugins.development] :
[...plugins.common, ...plugins.production],
module: {
rules: isDevelopment ?
[...rules.common, ...rules.development] :
[...rules.common, ...rules.production],
},
devServer,
};