forked from rokoroku/react-mobx-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
135 lines (131 loc) · 3.33 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
const path = require("path");
const webpack = require('webpack');
// variables
let dotenv = require('dotenv').config({ path: __dirname + '/.env' });
let sourcePath = path.join(__dirname, './src');
let outPath = path.join(__dirname, './build');
let isDeployedApp = (process.env.REACT_APP_ENVIRONMENT === "staging" || process.env.REACT_APP_ENVIRONMENT === "production");
// plugins
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
// defining exports
module.exports = {
context: sourcePath,
entry: {
app: './main.tsx',
},
output: {
path: outPath,
filename: isDeployedApp ? 'bundle.[id].js' : 'bundle.[fullhash].[id].js',
chunkFilename: isDeployedApp ? 'chunk.[id].js' : 'chunk.[fullhash].[id].js',
assetModuleFilename: isDeployedApp ? 'assets.[id].[ext]' : 'assets.[fullhash].[id].[ext]'
},
target: 'web',
resolve: {
extensions: ['.js', '.ts', '.tsx'],
// 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'],
alias: {
app: path.resolve(__dirname, 'src/app/'),
assets: path.resolve(__dirname, 'src/assets/')
}
},
module: {
rules: [
/*
* Typsecript files.
*/
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
!isDeployedApp && {
loader: require.resolve('babel-loader'),
options: {
plugins: [require.resolve('react-refresh/babel')],
},
},
'ts-loader'
].filter(Boolean),
},
/*
* JavaScript files.
*/
{
enforce: 'pre',
test: /\.js$/,
loader: 'source-map-loader'
},
/*
* CSS Files.
*/
{
test: /\.(sa|sc|c)ss$/,
use: [
'style-loader',
'css-loader'
]
},
/*
* misc Files.
*/
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.(bmp|mp3|mp4|ogg|wav|eot|ttf|woff|woff2)$/,
loader: 'file-loader'
},
{
test: /\.(svg|png|jpg|jpeg|gif)$/,
loader: 'file-loader',
options: {
esModule: false
}
}
]
},
performance: {
hints: false,
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/
}
},
minSize: 10000,
maxSize: 25000,
},
minimize: true
},
plugins: [
new webpack.DefinePlugin({
"process.env": JSON.stringify(dotenv.parsed)
}),
new MiniCssExtractPlugin({
filename: isDeployedApp ? 'styles.[id].css' : 'styles.[fullhash].[id].css'
}),
new HtmlWebpackPlugin({
template: 'assets/index.html',
}),
...(!isDeployedApp ? [new ReactRefreshWebpackPlugin()] : []),
],
devServer: {
static: sourcePath,
hot: true,
historyApiFallback: {
disableDotRule: true
},
client: {
logging: 'warn',
},
},
// https://webpack.js.org/configuration/devtool/
devtool: isDeployedApp ? 'hidden-source-map' : 'eval-cheap-module-source-map'
};