-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
117 lines (109 loc) · 2.87 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
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const dotenv = require('dotenv');
const Dotenv = require('dotenv-webpack');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
checkEnvironmentVariables();
const config = {
mode: 'production',
entry: path.resolve(__dirname, 'src/index.tsx'),
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].[chunkhash].js',
publicPath: '/',
clean: true,
},
devtool: false,
module: {
rules: [
{
test: /\.ts(x?)$/,
loader: 'ts-loader',
exclude: /(test)/,
},
{
test: /\.(ttf|eot|svg|png|jpe?g|gif)(\?[\s\S]+)?$/,
exclude: /(test)/,
type: 'asset/resource',
},
{
test: /\.css$/,
exclude: /(test)/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: {
mode: 'local',
auto: true,
exportGlobals: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
localIdentContext: path.resolve(__dirname, 'src'),
},
sourceMap: true,
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
title: 'Booktracker',
template: path.resolve(__dirname, 'public/index.html'),
favicon: path.resolve(__dirname, 'public/favicon.ico'),
}),
new Dotenv({
defaults: true,
systemvars: true,
}),
],
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin()],
},
};
module.exports = (_, argv) => {
const isEnvProduction = process.env.ENVIRONMENT === 'production';
if (isEnvProduction) {
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'bundle-report.html',
}),
);
} else {
config.mode = 'development';
config.devtool = 'eval-source-map';
config.devServer = {
port: 8080,
historyApiFallback: true,
server: 'http',
hot: true,
};
config.stats = {
errorDetails: true,
};
}
return config;
};
function checkEnvironmentVariables() {
dotenv.config();
const ENVIRONMENT = process.env.ENVIRONMENT;
const missingOtherEnvVariables = Object.entries({
ENVIRONMENT,
}).filter(([, value]) => !value);
if (missingOtherEnvVariables.length) {
const names = missingOtherEnvVariables
.map(([name]) => ` ${name}`)
.join('\n');
console.warn(`Missing optional environment variables: \n${names}`);
}
}