-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
98 lines (88 loc) · 2.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
const dotenv = require('dotenv');
const { cleanEnv, str } = require('envalid');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const package = require('./package.json');
/**
*
* @param {boolean} isProduction
* @returns {Record<string, string>}
*/
const getEnv = (isProduction) => {
const DOTENV_SUFFIX = isProduction ? '.production' : '';
const processEnv = {};
dotenv.config({ path: path.resolve(__dirname, `.env${DOTENV_SUFFIX}`), processEnv });
const env = cleanEnv(processEnv, {
LOGLEVER: str({ default: 'full' }),
CHANGES: str({ default: 'off' })
});
return env;
};
/**
*
* @param {Record<string, string>} env
* @returns {Record<string, string>}
*/
const envToSafe = (env) => {
return Object.entries(env).reduce((result, [key, value]) => {
return { ...result, [key]: typeof value === 'string' ? value : JSON.stringify(value) };
}, {});
};
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production';
const processEnv = getEnv(isProduction);
return {
mode: argv.mode,
entry: {
app: {
import: path.resolve(__dirname, 'src/index.tsx'),
},
},
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
extensionAlias: {
'.js': ['.js', '.ts'],
'.cjs': ['.cjs', '.cts'],
'.mjs': ['.mjs', '.mts'],
},
alias: {
_api: path.resolve(__dirname, './src/api'),
_components: path.resolve(__dirname, './src/components'),
_intl: path.resolve(__dirname, './src/intl'),
_page: path.resolve(__dirname, './src/page'),
_shell: path.resolve(__dirname, './src/shell'),
_state: path.resolve(__dirname, './src/state'),
_routes: path.resolve(__dirname, './src/routes.ts'),
_theme: path.resolve(__dirname, './src/theme.tsx'),
_eventBus: path.resolve(__dirname, './src/eventBus.ts'),
},
},
module: {
rules: [
{ test: /\.([cm]?tsx?)$/, loader: 'ts-loader' },
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
},
},
plugins: [
new webpack.EnvironmentPlugin(envToSafe(processEnv)),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './index.template.html'),
filename: '../index.html',
}),
],
};
};