-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
99 lines (98 loc) · 2.8 KB
/
webpack.common.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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
module.exports = {
target: ['web', 'es5'],
entry: {
index: './src/index.tsx',
},
output: {
filename: '[name].js',
path: path.resolve(process.cwd(), 'dist'),
assetModuleFilename: 'assets/[hash][ext][query]',
clean: true,
},
cache: {
type: 'filesystem',
cacheDirectory: path.resolve(__dirname, './tmp/.webpack_cache_temp'),
},
resolve: {
alias: {
'@app': path.resolve(process.cwd(), 'src'),
'@tests': path.resolve(process.cwd(), 'tests'),
react: path.resolve('./node_modules/react'), // useful when you link local package to debug
},
// only these suffix file can import
extensions: ['.tsx', '.ts', '.js', '.svg', '.png', '.jpg', '.gif'],
// // point the module load path, can add extra path
// modules: [path.resolve(__dirname, './node_modules')],
},
module: {
rules: [
// {
// test: /\.(ts|tsx)$/,
// exclude: /(node_modules|tests)/,
// use: 'babel-loader?cacheDirectory',
// },
{
test: /\.(ts|tsx)$/,
exclude: /(node_modules|tests)/,
loader: 'ts-loader',
options: { configFile: 'tsconfig.build.json', projectReferences: true },
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/i,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 8 * 1024,
},
},
generator: {
filename: 'images/[name][ext]',
},
// // url-loader and file-loader is deprecated since webpack 5 and css-loader 6
// use: [
// {
// loader: 'url-loader',
// options: {
// limit: 10 * 1024, // 10kb,
// outputPath: '/assets/',
// // publicPath: 'https://cdn.abc.com'
// },
// },
// ],
},
{
test: /\.(ttf|woff|woff2)/i,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 8 * 1024,
},
},
generator: {
filename: 'fonts/[name][ext]',
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
title: 'index',
filename: 'index.html',
template: 'public/index.html',
chunks: ['index'], // only enject index.js to template
}),
new webpack.ProvidePlugin({
process: 'process/browser',
}),
// // define different enject var, if use ts, then you need declare these varaible in type file, can only enject js code snippet
// new webpack.DefinePlugin({
// API_BASE_URL:
// process.env.NODE_ENV === 'production'
// ? JSON.stringify('https://api.prod.com')
// : JSON.stringify('https://api.example.com'),
// }),
],
};