-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathwebpack.config.js
160 lines (155 loc) · 5.54 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
const path = require('path');
const webpack = require('webpack');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const fs = require('fs-extra');
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
// const webpackDevClientEntry = require.resolve('react-dev-utils/webpackHotDevClient');
// const reactRefreshOverlayEntry = require.resolve('react-dev-utils/refreshOverlayInterop');
const isInteractive = process.stdout.isTTY;
const appPath = path.resolve(__dirname, './');
const appPublic = path.resolve(__dirname, 'public');
const appBuild = path.resolve(__dirname, 'build');
const appHtml = path.resolve(__dirname, 'src/index.html');
const appHtmlts = path.resolve(__dirname, 'src/index');
const assets = path.resolve(__dirname, 'src/assets');
function copyPublicFolder() {
console.log('------- copying files from public dir to build -------');
fs.copySync(appPublic, appBuild, {
dereference: true,
filter: file => file !== appHtml,
});
}
module.exports = (env) => {
const isProduction = env.NODE_ENV === 'production';
const isDevelopment = env.NODE_ENV === 'development';
const globalConfig = require('./config/config.json')[env.NODE_ENV];
const localConfig = require('./config/config.local.json')[env.NODE_ENV];
let envConfig = {
...globalConfig
}
!isProduction ? envConfig = {...envConfig, ...localConfig} : null;
isProduction && checkBrowsers(appPath, isInteractive).then(() => {
copyPublicFolder();
})
console.log('=========================', env.NODE_ENV, 'env_var', envConfig)
return {
mode: env.NODE_ENV,
entry: appHtmlts,
output: {
path: appBuild,
filename: 'bundle.js',
publicPath: '/'
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: {
loader: 'babel-loader',
query: {compact: false}
},
exclude: /node-modules/
},
{
test: /\.tsx?$/,
use: [{
loader: 'ts-loader',
options: {
transpileOnly: true
},
}],
exclude: /node-modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
exclude: /node-modules/
},
{
test: /\.(s[ac]ss)$/i,
use: ['style-loader', 'css-loader', 'sass-loader'],
exclude: /node-modules/
},
{
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
use: {
loader: 'url-loader',
options: {
esModule: false
}
}
},
{
test: /\.(png|jpeg|gif|svg|jpg)/,
use: [{
loader: 'file-loader',
options: {
outputPath: assets + '/images',
publicPath: '/images'
}
}]
}
]
},
optimization: {
minimize: isProduction,
minimizer: [
new TerserPlugin({
// test: /\.tsx(\?.*)?$/i
terserOptions: {
parse: {
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
comparisons: false,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
ascii_only: true,
},
},
sourceMap: isProduction ? false : true,
}),
],
},
resolve: {
extensions: ['.tsx', 'ts', '.js', '.jsx', '.json', '.css'],
alias: {
'~images': path.resolve(__dirname, 'src/assets/images'),
'Styles': path.resolve(__dirname, 'src/assets/style')
}
},
devServer: {
historyApiFallback: true,
compress: true,
hot: true,
port: 3000
},
devtool: isProduction
? 'source-map'
: 'cheap-module-source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebPackPlugin({
template: appHtml
}),
new MiniCssExtractPlugin({ // plugin for controlling how compiled css will be outputted and named
filename: "css/[name].css",
chunkFilename: "css/[id].css"
}),
new webpack.DefinePlugin({
env_config: JSON.stringify(envConfig)
})
]
}
}