forked from playcanvas/engine
/
rollup.config.mjs
425 lines (368 loc) · 10.9 KB
/
rollup.config.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
// 1st party Rollup plugins
import { createFilter } from '@rollup/pluginutils';
import { babel } from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import strip from '@rollup/plugin-strip';
import terser from '@rollup/plugin-terser';
// 3rd party Rollup plugins
import dts from 'rollup-plugin-dts';
import jscc from 'rollup-plugin-jscc';
import { visualizer } from 'rollup-plugin-visualizer';
function getVersion() {
const text = fs.readFileSync('./package.json', 'utf8');
const json = JSON.parse(text);
return json.version;
}
function getRevision() {
let revision;
try {
revision = execSync('git rev-parse --short HEAD').toString().trim();
} catch (e) {
revision = 'unknown';
}
return revision;
}
const version = getVersion();
const revision = getRevision();
console.log(`Building PlayCanvas Engine v${version} revision ${revision}`);
function getBanner(config) {
return [
'/**',
' * @license',
' * PlayCanvas Engine v' + version + ' revision ' + revision + config,
' * Copyright 2011-' + new Date().getFullYear() + ' PlayCanvas Ltd. All rights reserved.',
' */'
].join('\n');
}
function spacesToTabs(enable) {
const filter = createFilter([
'**/*.js'
], []);
return {
transform(code, id) {
if (!enable || !filter(id)) return undefined;
return {
code: code.replace(/\G {4}/g, '\t'),
map: null
};
}
};
}
// Validate and print warning if an engine module on a lower level imports module on a higher level
function engineLayerImportValidation(rootFile, enable) {
const folderLevels = {
'core': 0,
'platform': 1,
'scene': 2,
'framework': 3
};
let rootPath;
return {
buildStart() {
rootPath = path.parse(path.resolve(rootFile)).dir;
},
resolveId(imported, importer) {
if (enable) {
// skip non-relative paths, those are not our imports, for example 'rollupPluginBabelHelpers.js'
if (importer && imported && imported.includes('./')) {
// convert importer path
const importerDir = path.parse(importer).dir;
const relImporter = path.dirname(path.relative(rootPath, importer));
const folderImporter = relImporter.split(path.sep)[0];
const levelImporter = folderLevels[folderImporter];
// convert imported path
const absImported = path.resolve(path.join(importerDir, imported));
const relImported = path.dirname(path.relative(rootPath, absImported));
const folderImported = relImported.split(path.sep)[0];
const levelImported = folderLevels[folderImported];
if (levelImporter < levelImported) {
console.log(`(!) Incorrect import: [${path.relative(rootPath, importer)}] -> [${imported}]`);
}
}
}
// we don't process imports, return null to allow chaining
return null;
}
};
}
function shaderChunks(enable) {
const filter = createFilter([
'**/*.vert.js',
'**/*.frag.js'
], []);
return {
transform(code, id) {
if (!enable || !filter(id)) return undefined;
code = code.replace(/\/\* glsl \*\/\`((.|\r|\n)*)\`/, (match, glsl) => {
// Remove carriage returns
glsl = glsl.replace(/\r/g, '');
// 4 spaces to tabs
glsl = glsl.replace(/ {4}/g, '\t');
// Remove comments
glsl = glsl.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '');
// Trim all whitespace from line endings
glsl = glsl.split('\n').map(line => line.trimEnd()).join('\n');
// Restore final new line
glsl += '\n';
// Comment removal can leave an empty line so condense 2 or more to 1
glsl = glsl.replace(/\n{2,}/g, '\n');
// Remove new line character at the start of the string
if (glsl.length > 1 && glsl[0] === '\n') {
glsl = glsl.substr(1);
}
return JSON.stringify(glsl);
});
return {
code: code,
map: null
};
}
};
}
const es5Options = {
babelHelpers: 'bundled',
babelrc: false,
comments: false,
compact: false,
minified: false,
presets: [
[
'@babel/preset-env', {
loose: true,
modules: false,
targets: {
ie: '11'
}
}
]
]
};
const moduleOptions = {
babelHelpers: 'bundled',
babelrc: false,
comments: false,
compact: false,
minified: false,
presets: [
[
'@babel/preset-env', {
bugfixes: true,
loose: true,
modules: false,
targets: {
esmodules: true
}
}
]
]
};
const stripFunctions = [
'Debug.assert',
'Debug.assertDeprecated',
'Debug.call',
'Debug.deprecated',
'Debug.warn',
'Debug.warnOnce',
'Debug.error',
'Debug.errorOnce',
'Debug.log',
'Debug.logOnce',
'Debug.trace',
'DebugHelper.setName',
'DebugHelper.setLabel',
'DebugGraphics.toString',
'DebugGraphics.pushGpuMarker',
'DebugGraphics.popGpuMarker',
'WorldClustersDebug.render'
];
// buildType is: 'debug', 'release', 'profiler', 'min'
// moduleFormat is: 'es5', 'es6'
function buildTarget(buildType, moduleFormat) {
const banner = {
debug: ' (DEBUG PROFILER)',
release: '',
profiler: ' (PROFILER)',
min: null
};
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: 'build/playcanvas.dbg',
release: 'build/playcanvas',
profiler: 'build/playcanvas.prf',
min: 'build/playcanvas.min'
};
const outputExtension = {
es5: '.js',
es6: '.mjs'
};
const outputFormat = {
es5: 'umd',
es6: 'es'
};
const sourceMap = {
debug: 'inline',
release: null
};
const outputOptions = {
banner: banner[buildType] && getBanner(banner[buildType] || banner.release),
plugins: outputPlugins[buildType || outputPlugins.release],
format: outputFormat[moduleFormat],
indent: '\t',
sourcemap: sourceMap[buildType] || sourceMap.release,
name: 'pc',
preserveModules: moduleFormat === 'es6'
};
outputOptions[moduleFormat === 'es6' ? 'dir' : 'file'] = `${outputFile[buildType]}${outputExtension[moduleFormat]}`;
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
}
}
};
const stripOptions = {
debug: {
functions: []
},
release: {
functions: stripFunctions
}
};
const babelOptions = {
es5: es5Options,
es6: moduleOptions
};
const rootFile = 'src/index.js';
return {
input: rootFile,
output: outputOptions,
plugins: [
jscc(jsccOptions[buildType] || jsccOptions.release),
shaderChunks(buildType !== 'debug'),
engineLayerImportValidation(rootFile, buildType === 'debug'),
strip(stripOptions[buildType] || stripOptions.release),
babel(babelOptions[moduleFormat]),
spacesToTabs(buildType !== 'debug')
]
};
}
function scriptTarget(name, input, output) {
return {
input: input,
output: {
name: name,
banner: getBanner(''),
file: output || input.replace('.mjs', '.js'),
format: 'umd',
indent: '\t',
globals: { playcanvas: 'pc' }
},
plugins: [
resolve(),
babel(es5Options),
spacesToTabs()
],
external: ['playcanvas'],
cache: false
};
}
function scriptTargetEs6(name, input, output) {
return {
input: input,
output: {
name: name,
banner: getBanner(''),
dir: output,
format: 'es',
indent: '\t',
preserveModules: true
},
plugins: [
resolve(),
babel(moduleOptions),
spacesToTabs()
],
external: ['playcanvas', 'fflate']
};
}
const target_extras = [
scriptTarget('pcx', 'extras/index.js', 'build/playcanvas-extras.js'),
scriptTargetEs6('pcx', 'extras/index.js', 'build/playcanvas-extras.mjs'),
scriptTarget('VoxParser', 'scripts/parsers/vox-parser.mjs')
];
const target_types = {
input: 'types/index.d.ts',
output: [{
file: 'build/playcanvas.d.ts',
footer: 'export as namespace pc;',
format: 'es'
}],
plugins: [
dts()
]
};
export default (args) => {
let targets = [];
const envTarget = process.env.target ? process.env.target.toLowerCase() : null;
if (envTarget === 'types') {
targets.push(target_types);
} else if (envTarget === 'extras') {
targets = targets.concat(target_extras);
} else {
['release', 'debug', 'profiler', 'min'].forEach((t) => {
['es5', 'es6'].forEach((m) => {
if (envTarget === null || envTarget === t || envTarget === m || envTarget === `${t}_${m}`) {
targets.push(buildTarget(t, m));
}
});
});
if (envTarget === null) {
// no targets specified, build them all
targets = targets.concat(target_extras);
}
}
return targets;
};