-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
229 lines (223 loc) · 7.61 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
const webpack = require('webpack');
const path = require('path');
const nodeSass = require('node-sass');
const bourbon = require('bourbon');
const Dotenv = require('dotenv-webpack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
// variables
// isProduction: is this a prod-like environment (stage, prod)
const isProduction = (process.argv.indexOf('-p') >= 0 ||
process.env.NODE_ENV === 'production') &&
process.env.NODE_ENV !== 'dev';
const runBundleReport = (process.env.RUN_BUNDLE_REPORT === 'true');
const commitHash = require('child_process')
.execSync('git rev-parse --short HEAD').toString()
.trim();
const rootPath = path.join(__dirname);
const sourcePath = path.join(__dirname, 'src');
const assetsPath = path.join(__dirname, 'src', 'assets');
const publicPath = path.join(__dirname, 'public');
const outPath = path.join(__dirname, 'dist');
// plugins
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
// functions
const encodebase64 = (str) => {
const buffer = new Buffer.from(str.getValue());
return nodeSass.types.String(buffer.toString('base64'));
};
module.exports = {
context: sourcePath,
entry: {
app: './index.tsx',
},
output: {
path: outPath,
filename: 'static/js/[name].bundle.[hash].js',
chunkFilename: 'static/js/[name].[chunkhash].js',
publicPath: '/',
},
target: 'web',
devtool: isProduction ? false : 'eval-source-map',
resolve: {
extensions: ['.js', '.ts', '.tsx', '.scss'],
// Fix webpack's default behavior to not load packages with jsnext:main module
// (jsnext:main directs not usually distributable es6 format, but es6 sources)
mainFields: ['module', 'browser', 'main'],
modules: ['node_modules', sourcePath],
},
externals: [ 'aws-sdk', 'commonjs2 firebase-admin' ],
module: {
rules: [
// .ts, .tsx
{
test: /^.*\.tsx?$/,
exclude: /^.*\.(spec|test)\.tsx?$/,
use: [
isProduction && {
loader: 'babel-loader',
options: {plugins: ['react-hot-loader/babel']},
},
{
loader: 'ts-loader',
options: {
onlyCompileBundledFiles: true,
transpileOnly: true
},
}
].filter(Boolean),
},
// css
{
test: /\.s?css$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
"css-modules-typescript-loader",
{
loader: 'css-loader',
query: {
modules: true,
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: [
require('postcss-import')({addDependencyTo: webpack}),
require('postcss-url')(),
require('postcss-preset-env')({stage: 4}),
require('postcss-reporter')(),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: !isProduction,
implementation: require('sass'),
sassOptions: {
functions: {
encodebase64: encodebase64,
includePaths: [assetsPath].concat(bourbon.includePaths),
},
},
},
},
].filter(Boolean),
},
// static assets
{
test: /\.(eot|ttf|otf|woff|woff2)$/,
loader: 'file-loader',
options: {
name: 'static/[path][name].[ext]',
}
},
{
test: /\.(jpg|gif)$/,
loader: 'file-loader',
options: {
name: `static/[path][name].[${commitHash}].[ext]`,
}
},
{
test: /\.png$/,
use: [
{
loader: 'url-loader',
options: {
limit: 6000,
name: `static/[path][name].${commitHash}.[ext]`,
}
}
],
},
{
test: /\.svg$/,
use: [
{loader: "babel-loader"},
{
loader: "react-svg-loader",
options: {jsx: true}
}
]
},
{
test: /\.(ogg|wav|mp3)$/,
loader: 'file-loader',
include: [path.resolve(__dirname, 'src/assets/sounds')],
options: {
name: `static/media/[path][name].[ext]`
}
}
],
},
optimization: {
splitChunks: {
name: true,
cacheGroups: {
commons: {
chunks: 'initial',
minChunks: 2,
},
vendors: {
test: /[\\/]node_modules[\\/]/,
chunks: 'all',
priority: -10,
},
},
},
runtimeChunk: true,
usedExports: true,
},
plugins: [
new ForkTsCheckerWebpackPlugin({
typescript: {
configFile: path.join(rootPath, 'tsconfig.json')
}
}),
new webpack.EnvironmentPlugin({
NODE_ENV: 'development', // use 'development' unless process.env.NODE_ENV is defined
DEBUG: false,
COMMIT_HASH: commitHash,
DISABLE_IN_PROGRESS_FEATURES: false,
}),
new Dotenv(),
new CleanWebpackPlugin(),
new ManifestPlugin(),
new MiniCssExtractPlugin({
filename: 'static/css/[name].[contenthash].css',
disable: !isProduction,
}),
new HtmlWebpackPlugin({
template: path.join(publicPath, 'index.html'),
favicon: path.join(publicPath, 'favicon.ico'),
}),
runBundleReport ? new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)({
analyzerMode: 'static',
}) : null,
].filter(x => x !== null),
devServer: {
contentBase: sourcePath,
disableHostCheck: true,
headers: {
"Access-Control-Allow-Origin": "*",
},
historyApiFallback: {
disableDotRule: true,
},
host: '0.0.0.0',
inline: true,
port: 5000,
stats: 'minimal',
},
node: {
// workaround for webpack-dev-server issue
// https://github.com/webpack/webpack-dev-server/issues/60#issuecomment-103411179
fs: 'empty',
net: 'empty',
},
};