-
Notifications
You must be signed in to change notification settings - Fork 35
/
webpack.config.js
187 lines (174 loc) · 5.33 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// @ts-nocheck
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const workspace = path.resolve(__dirname);
const scriptOutputRoot = 'Assets/Scripts/Resources/scripts';
const polyfillOutputRoot = 'Assets/Scripts/Resources/polyfills';
const entries = {
'source-map-support': {
input: 'src/framework/polyfills/source-map-support.unity.ts',
path: polyfillOutputRoot,
filename: 'source-map-support.mjs',
externals: {
fs: 'global polyfill:fs',
path: 'global polyfill:path',
}
},
'console': {
input: 'src/framework/polyfills/console.unity.ts',
path: polyfillOutputRoot,
filename: 'console.mjs'
},
webapi: {
input: 'src/framework/webapi/index.unity.ts',
path: polyfillOutputRoot,
filename: 'webapi.mjs'
},
bundle: {
input: 'src/main.ts',
path: scriptOutputRoot,
filename: 'bundle.mjs'
},
'platform-editor': {
input: 'src/framework/unity/platform/platform.unity.editor.ts',
path: polyfillOutputRoot,
filename: 'platform.editor.mjs'
},
tinysdk: {
input: 'src/framework/tinysdk/index.ts',
path: polyfillOutputRoot,
filename: 'tinysdk.mjs',
tsConfigFile: 'tsconfig.json'
},
editor: {
input: 'src/editor.ts',
path: scriptOutputRoot,
filename: 'editor.bundle.mjs'
},
test: {
input: 'src/test/index.ts',
path: scriptOutputRoot,
filename: 'bundle.mjs'
},
};
entries['dev'] = [entries['platform-editor'], entries.tinysdk, entries.webapi, entries.bundle];
/** 生成版本信息 */
class HashGeneratorPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.hooks.afterEmit.tap('HashGenerator', (compilation) => {
fs.writeFileSync(this.options.output, JSON.stringify({ hash: compilation.fullHash, time: Date.now() }, undefined, '\t'), 'utf-8');
});
}
}
module.exports = (env) => {
if (!env) {
env = {
production: false,
analyze: false,
target: 'ES2020',
esbuild: true,
backend: 'v8',
entry: 'bundle'
};
}
env.production = JSON.parse(env.production || 'false');
env.analyze = JSON.parse(env.analyze || 'false');
env.esbuild = JSON.parse(env.esbuild || 'true');
env.entry = env.entry || 'bundle';
env.target = env.target || 'ES2020';
env.backend = env.backend || 'v8';
console.log('Compile config:', env);
const isNode = env.backend === 'node';
const isPollify = env.path === polyfillOutputRoot;
const tsConfigFile = path.join(workspace, 'tsconfig.json');
if (env.target) {
const tsconfig = JSON.parse(fs.readFileSync(tsConfigFile).toString('utf-8'));
tsconfig.compilerOptions.target = env.target;
fs.writeFileSync(tsConfigFile, JSON.stringify(tsconfig, undefined, '\t'));
}
const targets = entries[env.entry];
if (!targets) throw new Error(`未找到构建配置: ${env.entry}`);
const options = Array.isArray(targets) ? targets : [targets];
const ret = options.map(option => {
const target = {
target: 'es2020',
entry: [path.join(workspace, option.input)],
output: {
path: path.join(workspace, option.path),
filename: option.filename
},
module: {
rules: [
{ test: /\.(md|txt|glsl)$/, use: 'raw-loader' },
{ test: /\.ya?ml$/, use: 'yaml-loader' },
(env.esbuild ? { // ESBuild 构建
test: /\.(jsx?|tsx?)$/,
loader: 'esbuild-loader',
exclude: /(node_modules|bower_components)/,
options: { loader: 'tsx' }
}
// ts-loader 构建
: { test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ }
),
]
},
plugins: [
new HashGeneratorPlugin({ output: path.join(entries.bundle.path, 'version.json') }),
env.production ? null : new (require('webpackbar'))(),
env.analyze ? new (require('webpack-bundle-analyzer')).BundleAnalyzerPlugin() : null,
env.circularDetect ? new (require('circular-dependency-plugin'))(require('../tools/circular-dependency')) : null,
// 相当于 C++ 的宏定义,键名会被替换为值的字符串
new webpack.DefinePlugin({
BUILD_TIME: JSON.stringify(Date.now()),
PRODUCTION: JSON.stringify(env.production == true)
}),
// ESBuild 不会自动检查TS语法,这里添加相关插件
env.esbuild && option === entries.bundle ? new (require('fork-ts-checker-webpack-plugin'))() : null,
new webpack.SourceMapDevToolPlugin({
noSources: !env.production,
filename: env.production ? `${option.filename}.map` : undefined
})
].filter(p => p != null),
devtool: false,
resolve: {
extensions: ['.tsx', '.ts', '.js', 'glsl', 'md', 'txt'],
plugins: [
new (require('tsconfig-paths-webpack-plugin'))({ configFile: tsConfigFile }),
],
fallback: {
util: require.resolve('util/')
}
},
mode: env.production ? 'production' : 'development',
externals: [
{
csharp: 'global polyfill:csharp',
puerts: 'global polyfill:puerts',
path: 'global polyfill:path'
},
option.externals,
].filter(e => e),
};
if (isNode) {
target.target = 'node';
target.externals.push(nodeExternals({}));
} else {
target.plugins.push(new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'] }));
}
if (isPollify) {
target.output.library = { type: 'module' };
} else {
target.output.library = { type: 'global', name: '$entry' };
}
if (target.target === 'es2020') {
target.experiments = { outputModule: true };
}
return target;
});
return ret;
};