-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rollup-build-target.mjs
192 lines (170 loc) · 5.58 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
// 1st party Rollup plugins
import { babel } from '@rollup/plugin-babel';
import strip from '@rollup/plugin-strip';
import terser from '@rollup/plugin-terser';
// 3rd party Rollup plugins
import jscc from 'rollup-plugin-jscc';
import { visualizer } from 'rollup-plugin-visualizer';
// custom Rollup plugins
import { shaderChunks } from './rollup-shader-chunks.mjs';
import { engineLayerImportValidation } from './rollup-import-validation.mjs';
import { spacesToTabs } from './rollup-spaces-to-tabs.mjs';
import { dynamicImportLegacyBrowserSupport, dynamicImportViteSupress } from './rollup-dynamic-import-transform.mjs';
import { version, revision } from './rollup-version-revision.mjs';
import { getBanner } from './rollup-get-banner.mjs';
import { es5Options } from './rollup-es5-options.mjs';
import { moduleOptions } from './rollup-module-options.mjs';
/** @typedef {import('rollup').RollupOptions} RollupOptions */
/** @typedef {import('rollup').OutputOptions} OutputOptions */
/** @typedef {import('rollup').ModuleFormat} ModuleFormat */
/** @typedef {import('@rollup/plugin-strip').RollupStripOptions} RollupStripOptions */
const stripFunctions = [
'Debug.assert',
'Debug.assertDeprecated',
'Debug.assertDestroyed',
'Debug.call',
'Debug.deprecated',
'Debug.warn',
'Debug.warnOnce',
'Debug.error',
'Debug.errorOnce',
'Debug.log',
'Debug.logOnce',
'Debug.trace',
'DebugHelper.setName',
'DebugHelper.setLabel',
`DebugHelper.setDestroyed`,
'DebugGraphics.toString',
'DebugGraphics.clearGpuMarkers',
'DebugGraphics.pushGpuMarker',
'DebugGraphics.popGpuMarker',
'WebgpuDebug.validate',
'WebgpuDebug.memory',
'WebgpuDebug.internal',
'WebgpuDebug.end',
'WorldClustersDebug.render'
];
/**
* Build a target that rollup is supposed to build.
*
* @param {'debug'|'release'|'profiler'|'min'} buildType - The build type.
* @param {'es5'|'es6'} moduleFormat - The module format.
* @param {string} input - Only used for Examples to change it to `../src/index.js`.
* @param {string} [buildDir] - Only used for examples to change the output location.
* @param {Boolean} [shouldBundle] - Whether the target should be bundled.
* @returns {RollupOptions} One rollup target.
*/
function buildTarget(buildType, moduleFormat, input = 'src/index.js', buildDir = 'build', shouldBundle = true) {
const banner = {
debug: ' (DEBUG)',
release: ' (RELEASE)',
profiler: ' (PROFILE)',
min: ' (RELEASE)'
};
const outputPlugins = {
release: [],
min: [
terser()
]
};
if (process.env.treemap) {
outputPlugins.min.push(visualizer({
filename: 'treemap.html',
brotliSize: true,
gzipSize: true
}));
}
if (process.env.treenet) {
outputPlugins.min.push(visualizer({
filename: 'treenet.html',
template: 'network'
}));
}
if (process.env.treesun) {
outputPlugins.min.push(visualizer({
filename: 'treesun.html',
template: 'sunburst'
}));
}
const outputFile = {
debug: 'playcanvas.dbg',
release: 'playcanvas',
profiler: 'playcanvas.prf',
min: 'playcanvas.min'
};
const outputExtension = {
es5: '.js',
es6: shouldBundle ? '.mjs' : ''
};
/** @type {Record<string, ModuleFormat>} */
const outputFormat = {
es5: 'umd',
es6: 'es'
};
const sourceMap = {
debug: 'inline',
release: null
};
/** @type {OutputOptions} */
const outputOptions = {
banner: (moduleFormat === 'es5' || shouldBundle) ? getBanner(banner[buildType]) : undefined,
plugins: outputPlugins[buildType || outputPlugins.release],
format: outputFormat[moduleFormat],
indent: '\t',
sourcemap: shouldBundle && (sourceMap[buildType] || sourceMap.release),
name: 'pc',
preserveModules: !shouldBundle
};
const loc = `${buildDir}/${outputFile[buildType]}${outputExtension[moduleFormat]}`;
outputOptions[shouldBundle ? 'file' : 'dir'] = loc;
const sdkVersion = {
_CURRENT_SDK_VERSION: version,
_CURRENT_SDK_REVISION: revision
};
const jsccOptions = {
debug: {
values: {
...sdkVersion,
_DEBUG: 1,
_PROFILER: 1
},
keepLines: true
},
release: {
values: sdkVersion
},
profiler: {
values: {
...sdkVersion,
_PROFILER: 1
}
}
};
/**
* @type {RollupStripOptions}
*/
const stripOptions = {
functions: stripFunctions
};
const babelOptions = {
es5: es5Options(buildType),
es6: moduleOptions(buildType)
};
const jsccParam = jsccOptions[buildType] || jsccOptions.release;
if (moduleFormat === 'es5') jsccParam.values._IS_UMD = 1;
return {
input,
output: outputOptions,
plugins: [
jscc(jsccParam),
moduleFormat === 'es5' ? dynamicImportLegacyBrowserSupport() : undefined,
shaderChunks({ enabled: buildType !== 'debug' }),
engineLayerImportValidation(input, buildType === 'debug'),
buildType !== 'debug' ? strip(stripOptions) : undefined,
babel(babelOptions[moduleFormat]),
moduleFormat === 'es6' && buildType !== 'debug' ? dynamicImportViteSupress() : undefined,
spacesToTabs(buildType !== 'debug')
]
};
}
export { buildTarget };