-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrollup-types-fixup.mjs
274 lines (266 loc) · 9.79 KB
/
rollup-types-fixup.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
import * as fs from 'node:fs';
const GREEN_OUT = '\x1b[32m';
const BOLD_OUT = '\x1b[1m';
const REGULAR_OUT = '\x1b[22m';
const TYPES_PATH = './build/playcanvas/src';
const STANDARD_MAT_PROPS = [
['alphaFade', 'boolean'],
['ambient', 'Color'],
['anisotropy', 'number'],
['aoIntensity', 'number'],
['aoMap', 'Texture|null'],
['aoMapChannel', 'string'],
['aoMapOffset', 'Vec2'],
['aoMapRotation', 'number'],
['aoMapTiling', 'Vec2'],
['aoMapUv', 'number'],
['aoDetailMap', 'Texture|null'],
['aoDetailMapChannel', 'string'],
['aoDetailMapOffset', 'Vec2'],
['aoDetailMapRotation', 'number'],
['aoDetailMapTiling', 'Vec2'],
['aoDetailMapUv', 'number'],
['aoDetailMode', 'string'],
['aoVertexColor', 'boolean'],
['aoVertexColorChannel', 'string'],
['bumpiness', 'number'],
['clearCoat', 'number'],
['clearCoatBumpiness', 'number'],
['clearCoatGlossInvert', 'boolean'],
['clearCoatGlossMap', 'Texture|null'],
['clearCoatGlossMapChannel', 'string'],
['clearCoatGlossMapOffset', 'Vec2'],
['clearCoatGlossMapRotation', 'number'],
['clearCoatGlossMapTiling', 'Vec2'],
['clearCoatGlossMapUv', 'number'],
['clearCoatGlossVertexColor', 'boolean'],
['clearCoatGlossVertexColorChannel', 'string'],
['clearCoatGloss', 'number'],
['clearCoatMap', 'Texture|null'],
['clearCoatMapChannel', 'string'],
['clearCoatMapOffset', 'Vec2'],
['clearCoatMapRotation', 'number'],
['clearCoatMapTiling', 'Vec2'],
['clearCoatMapUv', 'number'],
['clearCoatNormalMap', 'Texture|null'],
['clearCoatNormalMapOffset', 'Vec2'],
['clearCoatNormalMapRotation', 'number'],
['clearCoatNormalMapTiling', 'Vec2'],
['clearCoatNormalMapUv', 'number'],
['clearCoatVertexColor', 'boolean'],
['clearCoatVertexColorChannel', 'string'],
['cubeMap', 'Texture|null'],
['cubeMapProjection', 'number'],
['cubeMapProjectionBox', 'BoundingBox'],
['diffuse', 'Color'],
['diffuseDetailMap', 'Texture|null'],
['diffuseDetailMapChannel', 'string'],
['diffuseDetailMapOffset', 'Vec2'],
['diffuseDetailMapRotation', 'number'],
['diffuseDetailMapTiling', 'Vec2'],
['diffuseDetailMapUv', 'number'],
['diffuseDetailMode', 'string'],
['diffuseMap', 'Texture|null'],
['diffuseMapChannel', 'string'],
['diffuseMapOffset', 'Vec2'],
['diffuseMapRotation', 'number'],
['diffuseMapTiling', 'Vec2'],
['diffuseMapUv', 'number'],
['diffuseVertexColor', 'boolean'],
['diffuseVertexColorChannel', 'string'],
['emissive', 'Color'],
['emissiveIntensity', 'number'],
['emissiveMap', 'Texture|null'],
['emissiveMapChannel', 'string'],
['emissiveMapOffset', 'Vec2'],
['emissiveMapRotation', 'number'],
['emissiveMapTiling', 'Vec2'],
['emissiveMapUv', 'number'],
['emissiveVertexColor', 'boolean'],
['emissiveVertexColorChannel', 'string'],
['enableGGXSpecular', 'boolean'],
['envAtlas', 'Texture|null'],
['fresnelModel', 'number'],
['gloss', 'number'],
['glossInvert', 'boolean'],
['glossMap', 'Texture|null'],
['glossMapChannel', 'string'],
['glossMapOffset', 'Vec2'],
['glossMapRotation', 'number'],
['glossMapTiling', 'Vec2'],
['glossMapUv', 'number'],
['glossVertexColor', 'boolean'],
['glossVertexColorChannel', 'string'],
['heightMap', 'Texture|null'],
['heightMapChannel', 'string'],
['heightMapFactor', 'number'],
['heightMapOffset', 'Vec2'],
['heightMapRotation', 'number'],
['heightMapTiling', 'Vec2'],
['heightMapUv', 'number'],
['lightMap', 'Texture|null'],
['lightMapChannel', 'string'],
['lightMapOffset', 'Vec2'],
['lightMapRotation', 'number'],
['lightMapTiling', 'Vec2'],
['lightMapUv', 'number'],
['lightVertexColor', 'boolean'],
['lightVertexColorChannel', 'string'],
['metalness', 'number'],
['metalnessMap', 'Texture|null'],
['metalnessMapChannel', 'string'],
['metalnessMapOffset', 'Vec2'],
['metalnessMapRotation', 'number'],
['metalnessMapTiling', 'Vec2'],
['metalnessMapUv', 'number'],
['metalnessVertexColor', 'boolean'],
['metalnessVertexColorChannel', 'string'],
['normalDetailMap', 'Texture|null'],
['normalDetailMapBumpiness', 'number'],
['normalDetailMapOffset', 'Vec2'],
['normalDetailMapRotation', 'number'],
['normalDetailMapTiling', 'Vec2'],
['normalDetailMapUv', 'number'],
['normalMap', 'Texture|null'],
['normalMapOffset', 'Vec2'],
['normalMapRotation', 'number'],
['normalMapTiling', 'Vec2'],
['normalMapUv', 'number'],
['occludeDirect', 'number'],
['occludeSpecular', 'number'],
['occludeSpecularIntensity', 'number'],
['opacity', 'number'],
['opacityDither', 'string'],
['opacityShadowDither', 'string'],
['opacityFadesSpecular', 'boolean'],
['opacityMap', 'Texture|null'],
['opacityMapChannel', 'string'],
['opacityMapOffset', 'Vec2'],
['opacityMapRotation', 'number'],
['opacityMapTiling', 'Vec2'],
['opacityMapUv', 'number'],
['opacityVertexColor', 'boolean'],
['opacityVertexColorChannel', 'string'],
['pixelSnap', 'boolean'],
['reflectivity', 'number'],
['refraction', 'number'],
['refractionIndex', 'number'],
['dispersion', 'number'],
['shadowCatcher', 'boolean'],
['specular', 'Color'],
['specularMap', 'Texture|null'],
['specularMapChannel', 'string'],
['specularMapOffset', 'Vec2'],
['specularMapRotation', 'number'],
['specularMapTiling', 'Vec2'],
['specularMapUv', 'number'],
['specularTint', 'boolean'],
['specularVertexColor', 'boolean'],
['specularVertexColorChannel', 'string'],
['specularityFactor', 'number'],
['specularityFactorMap', 'Texture|null'],
['specularityFactorMapChannel', 'string'],
['specularityFactorMapOffset', 'Vec2'],
['specularityFactorMapRotation', 'number'],
['specularityFactorMapTiling', 'Vec2'],
['specularityFactorMapUv', 'number'],
['useSheen', 'boolean'],
['sheen', 'Color'],
['sheenMap', 'Texture|null'],
['sheenMapChannel', 'string'],
['sheenMapOffset', 'Vec2'],
['sheenMapRotation', 'number'],
['sheenMapTiling', 'Vec2'],
['sheenMapUv', 'number'],
['sheenVertexColor', 'boolean'],
['sheenVertexColorChannel', 'string'],
['sphereMap', 'Texture|null'],
['twoSidedLighting', 'boolean'],
['useFog', 'boolean'],
['useTonemap', 'boolean'],
['useLighting', 'boolean'],
['useMetalness', 'boolean'],
['useMetalnessSpecularColor', 'boolean'],
['useSkybox', 'boolean']
];
const REPLACEMENTS = [{
path: `${TYPES_PATH}/scene/materials/standard-material.d.ts`,
replacement: {
transformer: (contents) => {
// Find the jsdoc block description using eg "@property {Type} {name}"
return contents.replace('reset(): void;', `reset(): void;
${STANDARD_MAT_PROPS.map((prop) => {
const typeDefinition = `@property {${prop[1]}} ${prop[0]}`;
const typeDescriptionIndex = contents.match(typeDefinition);
const typeDescription = typeDescriptionIndex ?
contents.slice(typeDescriptionIndex.index + typeDefinition.length, contents.indexOf('\n * @property', typeDescriptionIndex.index + typeDefinition.length)) :
'';
// Strip newlines, asterisks, and tabs from the type description
const cleanTypeDescription = typeDescription
.trim()
.replace(/[\n\t*]/g, ' ') // remove newlines, tabs, and asterisks
.replace(/\s+/g, ' '); // collapse whitespace
const jsdoc = cleanTypeDescription ? `/** ${cleanTypeDescription} */` : '';
return `\t${jsdoc}\n\tset ${prop[0]}(arg: ${prop[1]});\n\tget ${prop[0]}(): ${prop[1]};\n\n`;
}).join('')}`
);
},
footer: `
import { Color } from '../../core/math/color.js';
import { Vec2 } from '../../core/math/vec2.js';
import { BoundingBox } from '../../core/shape/bounding-box.js';
import { Texture } from '../../platform/graphics/texture.js';
`
}
}, {
path: `${TYPES_PATH}/framework/script/script-type.d.ts`,
replacement: {
from: 'get enabled(): boolean;',
to: `get enabled(): boolean;
/**
* Called when script is about to run for the first time.
*/
initialize?(): void;
/**
* Called after all initialize methods are executed in the same tick or enabling chain of actions.
*/
postInitialize?(): void;
/**
* Called for enabled (running state) scripts on each tick.
* @param dt - The delta time in seconds since the last frame.
*/
update?(dt: number): void;
/**
* Called for enabled (running state) scripts on each tick, after update.
* @param dt - The delta time in seconds since the last frame.
*/
postUpdate?(dt: number): void;
/**
* Called when a ScriptType that already exists in the registry gets redefined. If the new
* ScriptType has a \`swap\` method in its prototype, then it will be executed to perform
* hot-reload at runtime.
* @param old - Old instance of the scriptType to copy data to the new instance.
*/
swap?(old: ScriptType): void;
`
}
}];
export function typesFixup() {
return {
name: 'types-fixup',
buildStart() {
REPLACEMENTS.forEach((item) => {
const { from, to, footer, transformer } = item.replacement;
let contents = fs.readFileSync(item.path, 'utf-8');
if (transformer) {
contents = transformer(contents);
} else {
contents = contents.replace(from, to);
}
contents += footer ?? '';
fs.writeFileSync(item.path, contents, 'utf-8');
console.log(`${GREEN_OUT}type fixed ${BOLD_OUT}${item.path}${REGULAR_OUT}`);
});
}
};
}