-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrollup-build-target.mjs
263 lines (232 loc) · 8.07 KB
/
rollup-build-target.mjs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// official package plugins
import resolve from '@rollup/plugin-node-resolve';
import strip from '@rollup/plugin-strip';
import swcPlugin from '@rollup/plugin-swc';
// unofficial package plugins
import jscc from 'rollup-plugin-jscc';
import { visualizer } from 'rollup-plugin-visualizer';
// custom plugins
import { shaderChunks } from './plugins/rollup-shader-chunks.mjs';
import { engineLayerImportValidation } from './plugins/rollup-import-validation.mjs';
import { spacesToTabs } from './plugins/rollup-spaces-to-tabs.mjs';
import { dynamicImportLegacyBrowserSupport, dynamicImportBundlerSuppress } from './plugins/rollup-dynamic.mjs';
import { treeshakeIgnore } from './plugins/rollup-treeshake-ignore.mjs';
import { version, revision } from './rollup-version-revision.mjs';
import { getBanner } from './rollup-get-banner.mjs';
import { swcOptions } from './rollup-swc-options.mjs';
import { dirname, resolve as pathResolve } from 'path';
import { fileURLToPath } from 'url';
// Find path to the repo root
// @ts-ignore import.meta not allowed by tsconfig module:es6, but it works
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = pathResolve(__dirname, '..');
/** @typedef {import('rollup').RollupOptions} RollupOptions */
/** @typedef {import('rollup').OutputOptions} OutputOptions */
/** @typedef {import('rollup').ModuleFormat} ModuleFormat */
/** @typedef {import('@rollup/plugin-strip').RollupStripOptions} RollupStripOptions */
const TREESHAKE_IGNORE_REGEXES = [
/polyfill/
];
const STRIP_FUNCTIONS = [
'Debug.assert',
'Debug.assertDeprecated',
'Debug.assertDestroyed',
'Debug.call',
'Debug.deprecated',
'Debug.warn',
'Debug.warnOnce',
'Debug.error',
'Debug.errorOnce',
'Debug.log',
'Debug.logOnce',
'Debug.removed',
'Debug.trace',
'DebugHelper.setName',
'DebugHelper.setLabel',
'DebugHelper.setDestroyed',
'DebugGraphics.toString',
'DebugGraphics.clearGpuMarkers',
'DebugGraphics.pushGpuMarker',
'DebugGraphics.popGpuMarker',
'WebgpuDebug.validate',
'WebgpuDebug.memory',
'WebgpuDebug.internal',
'WebgpuDebug.end',
'WebgpuDebug.endShader',
'WorldClustersDebug.render'
];
const BANNER = {
debug: ' (DEBUG)',
release: ' (RELEASE)',
profiler: ' (PROFILE)',
min: ' (RELEASE)'
};
const OUT_PREFIX = {
debug: 'playcanvas.dbg',
release: 'playcanvas',
profiler: 'playcanvas.prf',
min: 'playcanvas.min'
};
const HISTORY = new Map();
/**
* @param {'debug'|'release'|'profiler'} buildType - The build type.
* @param {boolean} isUMD - Whether the build is for UMD.
* @returns {object} - The JSCC options.
*/
function getJSCCOptions(buildType, isUMD) {
const options = {
debug: {
values: {
_CURRENT_SDK_VERSION: version,
_CURRENT_SDK_REVISION: revision,
_IS_UMD: +isUMD,
_DEBUG: 1,
_PROFILER: 1
},
asloader: false,
keepLines: true
},
release: {
values: {
_CURRENT_SDK_VERSION: version,
_CURRENT_SDK_REVISION: revision,
_IS_UMD: +isUMD
},
asloader: false
},
profiler: {
values: {
_CURRENT_SDK_VERSION: version,
_CURRENT_SDK_REVISION: revision,
_IS_UMD: +isUMD,
_PROFILER: 1
},
asloader: false
}
};
return options[buildType];
}
/**
* @returns {OutputOptions['plugins']} - The output plugins.
*/
function getOutPlugins() {
const plugins = [
];
if (process.env.treemap) {
plugins.push(visualizer({
filename: 'treemap.html',
brotliSize: true,
gzipSize: true
}));
}
if (process.env.treenet) {
plugins.push(visualizer({
filename: 'treenet.html',
template: 'network'
}));
}
if (process.env.treesun) {
plugins.push(visualizer({
filename: 'treesun.html',
template: 'sunburst'
}));
}
return plugins;
}
/**
* Build a target that Rollup is supposed to build (bundled and unbundled).
*
* @param {object} options - The build target options.
* @param {'umd'|'esm'} options.moduleFormat - The module format.
* @param {'debug'|'release'|'profiler'|'min'} options.buildType - The build type.
* @param {'unbundled'|'bundled'} [options.bundleState] - The bundle state.
* @param {string} [options.input] - Only used for examples to change it to `../src/index.js`.
* @param {string} [options.dir] - Only used for examples to change the output location.
* @returns {RollupOptions[]} Rollup targets.
*/
function buildTarget({ moduleFormat, buildType, bundleState, input = 'src/index.js', dir = 'build' }) {
const isUMD = moduleFormat === 'umd';
const isDebug = buildType === 'debug';
const isMin = buildType === 'min';
const bundled = isUMD || isMin || bundleState === 'bundled';
const targets = [];
// bundle from unbundled
if (bundled && HISTORY.has(`${buildType}-${moduleFormat}-false`)) {
const unbundled = HISTORY.get(`${buildType}-${moduleFormat}-false`);
/**
* @type {RollupOptions}
*/
const target = {
input: `${unbundled.output.dir}/src/index.js`,
output: {
banner: getBanner(BANNER[buildType]),
format: 'es',
indent: '\t',
sourcemap: isDebug && 'inline',
name: 'pc',
preserveModules: false,
file: `${dir}/${OUT_PREFIX[buildType]}.mjs`
}
};
HISTORY.set(`${buildType}-${moduleFormat}-true`, target);
targets.push(target);
return targets;
}
// minify from release build
if (isMin && HISTORY.has(`release-${moduleFormat}-true`)) {
const release = HISTORY.get(`release-${moduleFormat}-true`);
/**
* @type {RollupOptions}
*/
const target = {
input: release.output.file,
plugins: [
swcPlugin({ swc: swcOptions(isDebug, isUMD, isMin) })
],
output: {
plugins: getOutPlugins(),
file: `${dir}/${OUT_PREFIX[buildType]}${isUMD ? '.js' : '.mjs'}`
},
context: isUMD ? 'this' : undefined
};
HISTORY.set(`${buildType}-${moduleFormat}-${bundled}`, target);
targets.push(target);
return targets;
}
/**
* @type {RollupOptions}
*/
const target = {
input,
output: {
banner: bundled ? getBanner(BANNER[buildType]) : undefined,
plugins: isMin ? getOutPlugins() : undefined,
format: isUMD ? 'umd' : 'es',
indent: '\t',
sourcemap: bundled && isDebug && 'inline',
name: 'pc',
preserveModules: !bundled,
preserveModulesRoot: !bundled ? rootDir : undefined,
file: bundled ? `${dir}/${OUT_PREFIX[buildType]}${isUMD ? '.js' : '.mjs'}` : undefined,
dir: !bundled ? `${dir}/${OUT_PREFIX[buildType]}` : undefined,
entryFileNames: chunkInfo => `${chunkInfo.name.replace(/node_modules/g, 'modules')}.js`
},
plugins: [
resolve(),
jscc(getJSCCOptions(isMin ? 'release' : buildType, isUMD)),
isUMD ? treeshakeIgnore(TREESHAKE_IGNORE_REGEXES) : undefined,
isUMD ? dynamicImportLegacyBrowserSupport() : undefined,
!isDebug ? shaderChunks() : undefined,
isDebug ? engineLayerImportValidation(input) : undefined,
!isDebug ? strip({ functions: STRIP_FUNCTIONS }) : undefined,
swcPlugin({ swc: swcOptions(isDebug, isUMD, isMin) }),
!isUMD ? dynamicImportBundlerSuppress() : undefined,
!isDebug ? spacesToTabs() : undefined
]
};
HISTORY.set(`${buildType}-${moduleFormat}-${bundled}`, target);
targets.push(target);
return targets;
}
export { buildTarget };