-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.base.ts
166 lines (162 loc) · 5.33 KB
/
webpack.base.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Configuration, DefinePlugin } from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import * as dotenv from 'dotenv'
import { styleLoadersArray, lessRule } from './config/style'
const { ModuleFederationPlugin } = require('webpack').container
const path = require('path')
const WebpackBar = require('webpackbar')
const envConfig = dotenv.config({
path: path.resolve(__dirname, `./env/.env.${process.env.BASE_ENV}`)
})
const baseConfig: Configuration = {
entry: path.join(__dirname, './src/index.tsx'), // 入口文件
// 打包出口文件
output: {
filename: 'static/js/[name].[chunkhash:8].js', // 每个输出js的名称
path: path.join(__dirname, './dist'), // 打包结果输出路径
clean: true, // webpack4需要配置clean-webpack-plugin来删除dist文件,webpack5内置了
publicPath: '/', // 打包后文件的公共前缀路径
environment: {
asyncFunction: true
}
},
// loader 配置
module: {
rules: [
{
test: /.(ts|tsx)$/, // 匹配.ts, tsx文件
exclude: /node_modules/,
use: ['babel-loader']
// use: ["thread-loader", "babel-loader"] 当项目规模到达一定程度可以考虑开启多进程打包
},
{
test: /.css$/, // 匹配 css 文件
use: styleLoadersArray
},
lessRule,
{
test: /\.(png|jpe?g|gif|svg)$/i, // 匹配图片文件
type: 'asset', // type选择asset
parser: {
dataUrlCondition: {
maxSize: 10 * 1024 // 小于10kb转base64
}
},
generator: {
filename: 'static/fonts/[name].[contenthash:8][ext]' // 加上[contenthash:8]
}
},
{
test: /.(woff2?|eot|ttf|otf)$/, // 匹配字体图标文件
type: 'asset', // type选择asset
parser: {
dataUrlCondition: {
maxSize: 10 * 1024 // 小于10kb转base64
}
},
generator: {
filename: 'static/fonts/[name].[contenthash:8][ext]' // 加上[contenthash:8]
}
},
{
test: /.(mp4|webm|ogg|mp3|wav|flac|aac)$/, // 匹配媒体文件
type: 'asset', // type选择asset
parser: {
dataUrlCondition: {
maxSize: 10 * 1024 // 小于10kb转base64
}
},
generator: {
filename: 'static/fonts/[name].[contenthash:8][ext]' // 加上[contenthash:8]
}
},
{
// 匹配json文件
test: /\.json$/,
loader: 'json-loader',
type: 'javascript/auto', // 将json文件视为文件类型
generator: {
// 这里专门针对json文件的处理
filename: 'static/json/[name].[hash][ext][query]'
}
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.less', '.css'],
alias: {
'@': path.join(__dirname, './src')
},
modules: [path.resolve(__dirname, './node_modules')]
},
// plugins
plugins: [
new HtmlWebpackPlugin({
title: 'webpack5-react-ts',
filename: 'index.html',
inject: true, // 自动注入静态资源
hash: true,
cache: false,
// 复制 'index.html' 文件,并自动引入打包输出的所有资源(js/css)
template: path.join(__dirname, './public/index.html'),
// 压缩html资源
minify: {
removeAttributeQuotes: true,
collapseWhitespace: true, // 去空格
removeComments: true, // 去注释
minifyJS: true, // 在脚本元素和事件属性中缩小JavaScript(使用UglifyJS)
minifyCSS: true // 缩小CSS样式元素和样式属性
},
nodeModules: path.resolve(__dirname, './node_modules')
}),
new ModuleFederationPlugin({
name: 'microApp-project',
remotes: {
// project_container: 'project_container@http://127.0.0.1:9091/remoteEntry.js'
// 'project_container@https://micro-frontend-container-88g.pages.dev/remoteEntry.js'
// 为什么要这样写?详见: https://github.com/module-federation/module-federation-examples/issues/1142
project_container: `promise new Promise(resolve => {
const remoteUrl = '${process.env.REMOTE_URL}/remoteEntry.js'
const script = document.createElement('script')
script.src = remoteUrl
script.onload = () => {
const proxy = {
get: (request) => window.project_container.get(request),
init: (arg) => {
try {
return window.project_container.init(arg)
} catch(e) {
console.log('remote container already initialized')
}
}
}
resolve(proxy)
}
document.head.appendChild(script);
})
`
},
shared: {
// 统一 react 等版本,避免重复加载
react: {
singleton: true
// eager: true
},
'react-dom': {
singleton: true
// eager: true
}
}
}),
new DefinePlugin({
'process.env': JSON.stringify(envConfig.parsed),
'process.env.BASE_ENV': JSON.stringify(process.env.BASE_ENV),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new WebpackBar()
].filter(Boolean),
cache: {
type: 'filesystem' // 使用文件缓存
}
}
export default baseConfig