forked from AwesomeXR/xr-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
160 lines (147 loc) · 4.92 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
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
const path = require('path');
const { Logger } = require('ah-logger');
const webpack = require('webpack');
const { version } = require('./package.json');
const TerserPlugin = require('terser-webpack-plugin');
const logger = new Logger('webpack.config');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const _ = require('lodash');
// const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const AntdMapToken = require('./src/ThemeToken.json');
const dotenv = require('dotenv');
// 读取 .env 文件
dotenv.config();
const ENV = process.env;
ENV.version = version;
logger.info(`ENV -> \n${JSON.stringify(ENV, null, 2)}`);
const config = {
module: {
rules: [
{
test: /\.worker\.ts$/,
use: [{ loader: 'worker-loader', options: { inline: 'fallback' } }],
},
{
// 处理: BREAKING CHANGE: The request 'process/browser' failed to resolve only because it was resolved as fully specified
test: /\.m?js/,
resolve: { fullySpecified: false },
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
configFile: ENV.MODE === 'development' ? 'tsconfig.json' : 'tsconfig.build.json',
},
},
],
},
{ test: /\.svg$/, use: ['svg-react-loader'] },
{
test: /\.(css|less)$/,
use: [
'style-loader',
'css-loader',
{
loader: 'less-loader',
options: {
lessOptions: { javascriptEnabled: true, strictMath: false, modifyVars: AntdMapToken },
},
},
],
},
{
test: /\.(env|dds|png|jpg|zip|babylon|vol|hdr|d\.ts)$/i,
type: 'asset/resource',
},
{
test: /\.(xml)$/i,
type: 'asset/source', // 导入为文本
},
{
test: /\.js$/,
loader: 'string-replace-loader',
options: {
multiple: [
// fix: [[Bug] marked.Renderer is not a constructor · Issue #3035 · microsoft/monaco-editor](https://github.com/microsoft/monaco-editor/issues/3035)
{
search: '(__marked_exports || exports)',
replace: `(typeof exports !== 'undefined' ? exports : __marked_exports)`,
},
// 去掉 antd button 的 wave 效果(性能考虑)
{
search: 'function isUnBorderedButtonType(type) {',
replace: `function isUnBorderedButtonType(type) { return true;`,
},
],
},
},
],
},
output: {
library: { type: 'umd', name: ['XR', '[name]'] },
},
externals: {
react: { root: 'React', commonjs2: 'react', commonjs: 'react', amd: 'react' },
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom',
},
'react-dom/server': {
root: 'ReactDOMServer',
commonjs2: 'react-dom/server',
commonjs: 'react-dom/server',
amd: 'react-dom/server',
},
lodash: { root: '_', commonjs2: 'lodash', commonjs: 'lodash', amd: 'lodash' },
// antd: 'antd', 和 XUI alias 冲突
typescript: { root: 'ts', commonjs2: 'typescript', commonjs: 'typescript', amd: 'typescript' },
'ali-oss': { root: 'OSS', commonjs2: 'ali-oss', commonjs: 'ali-oss', amd: 'ali-oss' },
codemirror: { root: 'CodeMirror', commonjs2: 'codemirror', commonjs: 'codemirror', amd: 'codemirror' },
dayjs: { root: 'dayjs', commonjs2: 'dayjs', commonjs: 'dayjs', amd: 'dayjs' },
},
plugins: [
new webpack.DefinePlugin({
..._.mapValues(ENV, v => JSON.stringify(v)),
}),
// [Won’t work in browser because of process.cwd()? · Issue #8 · browserify/path-browserify](https://github.com/browserify/path-browserify/issues/8)
new webpack.ProvidePlugin({ process: 'process/browser' }),
// new MonacoWebpackPlugin({ languages: ['javascript', 'typescript', 'css', 'html', 'json'] }),
],
resolve: {
extensions: ['.tsx', '.ts', '.js'],
fallback: {
crypto: false,
fs: false,
path: require.resolve('path-browserify'),
buffer: require.resolve('buffer'),
},
alias: {
antd$: path.resolve(__dirname, 'src/common/component/XUI/index.ts'),
// 处理本地开发时 npm link 软连接造成 ah-flow-node 等加载多份的问题
'xr-core$': path.resolve(__dirname, 'node_modules/xr-core'),
'ah-flow-node$': path.resolve(__dirname, 'node_modules/ah-flow-node'),
},
},
stats: {
// warningsFilter: /export .* was not found in/,
errorDetails: true,
},
optimization: {
minimize: ENV.MODE == 'production',
splitChunks: { chunks: 'async', minChunks: 1 },
minimizer: [
new TerserPlugin({
terserOptions: {
compress: { drop_debugger: true },
},
}),
],
},
};
module.exports = { config, ENV, logger };