-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
executable file
·139 lines (129 loc) · 3.22 KB
/
webpack.config.ts
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
import 'webpack-dev-server'; // types
import webpack from 'webpack';
import path from 'path';
import HTMLWebpackPlugin from 'html-webpack-plugin';
import dotenv from 'dotenv';
import ESLintPlugin from 'eslint-webpack-plugin';
import SpeedMeasurePlugin from 'speed-measure-webpack-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import { existsSync } from 'fs';
import TerserPlugin from 'terser-webpack-plugin';
const env = process.env.TARGET_ENV;
const envPath = path.join(__dirname, `.env.${env}`);
if (!existsSync(envPath))
{
throw new Error(
'Could not find file ' + envPath + '. Did you specify TARGET_ENV?'
);
}
dotenv.config({ path: envPath });
const runEslint = process.env.LINT || env !== 'local';
const smp = new SpeedMeasurePlugin();
const inspectLoader =
{
loader: 'inspect-loader',
options:
{
callback: (inspect : any) => console.log(inspect.arguments),
},
};
const config = (_ : any, argv : any) => smp.wrap(
{
devtool: argv.mode === 'development' && 'eval-cheap-source-map',
entry: './src/main.tsx',
output:
{
path: path.resolve(__dirname, 'tmp'),
filename: 'bundle.js',
},
mode: argv.mode,
optimization:
{
minimize: true,
usedExports: true,
minimizer: [new TerserPlugin()],
},
resolve:
{
extensions: ['.ts', '.tsx', '.js', '.jsx', '.scss'],
modules:
[
path.resolve(__dirname, './src/'),
path.resolve(__dirname, './node_modules'),
],
},
module:
{
rules:
[
{
test: /\.(woff|woff2|png|jpg|gif|svg)/,
use: 'file-loader',
},
{
test: /\.(ts|tsx|js|jsx)$/,
// exclude everything in node_modules except dashboard-api/...
exclude: /node_modules/,
sideEffects: false,
use:
[
...(process.env.INSPECT ? [inspectLoader] : []),
'babel-loader',
{
loader: 'ts-loader',
options:
{
allowTsInNodeModules: true,
// speeds up compilation time (tsc will be executed when building)
transpileOnly: true,
},
},
],
},
{
test: /\.(css|scss)$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
sideEffects: true,
},
],
},
devServer:
{
allowedHosts: [
'label.localhost',
],
historyApiFallback: true,
watchFiles: [
'./src/**',
'./webpack.config.ts',
'./node_modules/@unologin/react-ui/.!(node_modules/**)',
],
},
plugins:
[
...(process.env.ANALYZE ?
[new BundleAnalyzerPlugin({ openAnalyzer: false }) as any] : []
),
new HTMLWebpackPlugin(
{
template: 'public/index.html',
filename: 'index.html',
inject: false,
},
),
...(runEslint ?
[new ESLintPlugin(
{
extensions: ['ts', 'tsx'],
},
)] : []
),
new webpack.DefinePlugin(
{
'process.env': JSON.stringify(process.env),
},
),
],
},
);
export default config;