-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
135 lines (131 loc) · 4.07 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
/* eslint-disable @typescript-eslint/no-var-requires */
// Do this as the first thing so that any code reading it knows the right env.
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const path = require('path')
const webpack = require('webpack')
const WebpackBar = require('webpackbar')
const appConstants = require('./package.json')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const BitBarProgressPlugin = require('bitbar-webpack-progress-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const HtmlPlugin = require('html-webpack-plugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const isEnvDevelopment = process.env.NODE_ENV === 'development'
const isEnvProduction = process.env.NODE_ENV !== 'development'
module.exports = {
mode: isEnvDevelopment ? 'development' : 'production',
entry: './src/app/index.tsx',
devtool: isEnvDevelopment ? 'eval-source-map' : 'inline-source-map',
devServer: {
contentBase: './dist',
hot: true,
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
alias: {
'react-dom': '@hot-loader/react-dom',
},
},
module: {
rules: [
{
test: /\.(j|t)sx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
babelrc: false,
presets: [
[
'@babel/preset-env',
{ targets: { browsers: 'last 2 versions' } },
],
'@babel/preset-typescript',
'@babel/preset-react',
],
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-transform-runtime',
'react-hot-loader/babel',
],
},
},
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
output: {
filename: '[name].[hash].js',
path: path.resolve(__dirname, 'dist'),
},
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
module: 'empty',
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty',
},
// Turn off performance hints during development because we don't do any
// splitting or minification in interest of speed. These warnings become
// cumbersome.
performance: {
hints: false,
},
optimization: {
minimize: isEnvProduction,
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
},
},
},
},
plugins: [
new ForkTsCheckerWebpackPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new WebpackBar(),
new BitBarProgressPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new CleanWebpackPlugin({
verbose: true,
cleanOnceBeforeBuildPatterns: ['./dist'],
}),
new CopyPlugin({ patterns: [{ from: 'src/assets', to: '' }] }),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: isEnvDevelopment ? '[name].css' : '[name].[contenthash].css',
chunkFilename: isEnvDevelopment
? '[id].[name].css'
: '[id].[name].[contenthash].css',
}),
new webpack.SourceMapDevToolPlugin({
filename: '[name].js.map',
exclude: ['vendor.js', 'vendor.[hash].js'],
}),
new HtmlPlugin({
production: isEnvProduction,
inject: true,
filename: 'index.html',
title: appConstants.appName,
template: path.resolve(__dirname, 'src/app/index.ejs'),
favicon: path.resolve(__dirname, 'src/assets/favicon.ico'),
cache: isEnvProduction,
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
],
}