Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeTypeInspector.js: Add RTI build target + Examples integration #5817

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
5e8b2a6
Adding RTI transpiler to every target, I will fine-tune a bit later
kungfooman Nov 9, 2023
e8aa110
Merge branch 'main' into rti
kungfooman Nov 9, 2023
91dec96
name output build/playcanvas-rti and update packages
kungfooman Nov 9, 2023
d4bb80d
Merge branch 'main' into rti
kungfooman Nov 10, 2023
1ff92a2
Resolve RTI runtime and update to latest RTI rollup plugin
kungfooman Nov 10, 2023
82feb3a
Integrate RTI into Examples browser via `npm run develop:rti`
kungfooman Nov 10, 2023
53ab80c
only add node-resolve plugin for RTI
kungfooman Nov 10, 2023
af6366e
Don't type check framework/parsers/draco-worker.js (because a Worker …
kungfooman Nov 11, 2023
20a249a
npm i @runtime-type-inspector/plugin-rollup@1.0.6 (added `types.d.ts`…
kungfooman Nov 11, 2023
3cae39b
Merge branch 'main' into rti
kungfooman Nov 11, 2023
c89fde4
Add type reporting mode: never
kungfooman Nov 13, 2023
c1bab6b
Merge branch 'main' into rti
kungfooman Nov 28, 2023
b4cb08d
npm run develop:rti - Support live reload
kungfooman Nov 28, 2023
8fcb5e1
extras: JSDoc
kungfooman Dec 2, 2023
1557987
update RTI version, use index.rti.js for custom types/validations
kungfooman Dec 2, 2023
6472337
JSDoc fixes from Splat PR for extras type testing
kungfooman Dec 3, 2023
46daac2
fix lint
kungfooman Dec 3, 2023
5e668a0
Merge branch 'main' into rti
kungfooman Dec 6, 2023
c26a7c5
rollup.config.mjs: add rti target
kungfooman Dec 6, 2023
d18508c
Keep utils/rollup-build-target.mjs without RTI, using rollup-build-ta…
kungfooman Dec 6, 2023
eb75549
Fix lint
kungfooman Dec 6, 2023
5bd053b
little jsdoc fix
kungfooman Dec 17, 2023
e4a1433
npm install --save-dev @runtime-type-inspector/plugin-rollup@3.0.0
kungfooman Dec 30, 2023
2636752
Fix rollup/acorn error
kungfooman Dec 31, 2023
66c6492
Merge branch 'main' into rti
kungfooman Jan 1, 2024
e2f01a0
package-lock.json: npm install (changed jsdoc-typeof-plugin)
kungfooman Jan 1, 2024
73d1fca
npm update @runtime-type-inspector/transpiler (in root and examples/)
kungfooman Jan 1, 2024
bf397b2
npm update @runtime-type-inspector/plugin-rollup
kungfooman Jan 19, 2024
f3b1567
npm update @runtime-type-inspector/plugin-rollup
kungfooman Jan 20, 2024
1e9eb4f
Merge branch 'main' into rti
kungfooman Jan 25, 2024
cbb2d95
Update to @runtime-type-inspector/plugin-rollup@3.2.0
kungfooman Jan 25, 2024
9e35146
Merge branch 'main' into rti
kungfooman Jan 25, 2024
6e37268
Ignore node_modules for RTI to fix issue with DisplayAnything
kungfooman Jan 25, 2024
788331d
Merge branch 'main' into rti
kungfooman Feb 22, 2024
5837e5a
Bump RTI rollup plugin and integrate into examples again for `npm run…
kungfooman Feb 23, 2024
faa9c92
`cd examples && npm install` to fix `examples/package-lock.json`
kungfooman Feb 23, 2024
4f68608
Update RTI rollup plugin to 3.2.5
kungfooman May 13, 2024
add8005
Merge branch 'main' into rti
kungfooman May 14, 2024
78a6fd7
Reintegrate npm run build:rti
kungfooman May 14, 2024
e751a99
Reintegrate RTI after bunch of merge conflicts
kungfooman May 15, 2024
a305044
Merge branch 'main' into rti
kungfooman May 15, 2024
d8116b8
Fix package.json and package-lock.json from merge conflict
kungfooman May 15, 2024
4787e03
npm i --save-dev @runtime-type-inspector/plugin-rollup@3.2.6
kungfooman May 16, 2024
6272f07
Merge branch 'main' into rti
kungfooman May 18, 2024
cba932b
src/framework/anim/binder/anim-binder.js mini comment fix, you can te…
kungfooman May 18, 2024
ae4abb8
Fix custom types
kungfooman May 18, 2024
1dafa68
Merge branch 'main' into rti
kungfooman May 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build:thumbnails": "node ./scripts/thumbnails.mjs",
"clean": "node ./scripts/clean.mjs",
"develop": "cross-env NODE_ENV=development concurrently --kill-others \"npm run watch\" \"npm run serve\"",
"develop:rti": "RTI=on npm run develop",
"lint": "eslint --ext .js,.mjs .",
"serve": "serve dist -l 5000 --no-request-logging --config ../serve.json",
"watch": "npm run -s build:pre && cross-env NODE_ENV=development rollup -c -w"
Expand Down
7 changes: 6 additions & 1 deletion examples/rollup.config.js → examples/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import { generateStandalone } from './utils/plugins/rollup-generate-standalone.m
// engine rollup utils
import { treeshakeIgnore } from '../utils/plugins/rollup-treeshake-ignore.mjs';
import { buildTarget } from '../utils/rollup-build-target.mjs';
import { buildTargetRTI } from '../utils/rollup-build-target-rti.mjs';

// util functions
import { isModuleWithExternalDependencies } from './utils/utils.mjs';

const NODE_ENV = process.env.NODE_ENV ?? '';
const ENGINE_PATH = !process.env.ENGINE_PATH && NODE_ENV === 'development' ?
'../src/index.js' : process.env.ENGINE_PATH ?? '';
const { RTI = '' } = process.env;

const STATIC_FILES = [
// static main page src
Expand Down Expand Up @@ -95,6 +97,9 @@ function getEngineTargets() {
checkAppEngine();

const targets = [];
if (RTI === 'on') {
targets.push(buildTargetRTI('es', '../src/index.rti.js', 'dist/iframe/ENGINE_PATH'));
}
if (ENGINE_PATH) {
return targets;
}
Expand Down Expand Up @@ -148,7 +153,7 @@ export default [
skipWrite: true
},
treeshake: false,
plugins: [generateStandalone(NODE_ENV, ENGINE_PATH), copyStatic(NODE_ENV, STATIC_FILES)]
plugins: [generateStandalone(NODE_ENV, ENGINE_PATH, RTI), copyStatic(NODE_ENV, STATIC_FILES)]
},
{
// A debug build is ~2.3MB and a release build ~0.6MB
Expand Down
5 changes: 4 additions & 1 deletion examples/scripts/standalone-html.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ function generateExampleFile(categoryKebab, exampleNameKebab, setEngineType, fil

// engine
const engineType = process.env.ENGINE_PATH ? 'development' : process.env.NODE_ENV === 'development' ? 'debug' : setEngineType;
const engine = engineFor(engineType);
let engine = engineFor(engineType);
if (process.env.RTI === 'on') {
engine = './ENGINE_PATH/playcanvas.rti.mjs';
}
html = html.replace(/'@ENGINE'/g, JSON.stringify(engine));

if (/'@([A-Z0-9_]+)'/g.test(html)) {
Expand Down
5 changes: 3 additions & 2 deletions examples/utils/plugins/rollup-generate-standalone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const GREEN_OUT = '\x1b[32m';
*
* @param {string} nodeEnv - The node environment.
* @param {string} enginePath - The path to the engine.
* @param {string} rti - Whether to activate RuntimeTypeInspector.
* @returns {import('rollup').Plugin} The plugin.
*/
export function generateStandalone(nodeEnv, enginePath) {
export function generateStandalone(nodeEnv, enginePath, rti) {
return {
name: 'generate-standalone',
buildStart() {
Expand All @@ -23,7 +24,7 @@ export function generateStandalone(nodeEnv, enginePath) {
}
},
buildEnd() {
const cmd = `cross-env NODE_ENV=${nodeEnv} ENGINE_PATH=${enginePath} node ./scripts/standalone-html.mjs`;
const cmd = `cross-env NODE_ENV=${nodeEnv} ENGINE_PATH=${enginePath} RTI=${rti} node ./scripts/standalone-html.mjs`;
console.log(`${GREEN_OUT}${cmd}`);
execSync(cmd);
}
Expand Down
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@rollup/plugin-strip": "^3.0.4",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/pluginutils": "^5.1.0",
"@runtime-type-inspector/plugin-rollup": "^3.2.6",
"c8": "^9.1.0",
"chai": "^5.1.0",
"eslint": "^8.57.0",
Expand Down Expand Up @@ -150,6 +151,7 @@
"build:treenet": "npm run build target:umd treenet",
"build:treesun": "npm run build target:umd treesun",
"build:sourcemaps": "npm run build -- -m",
"build:rti": "rollup -c --environment target:rti",
"watch": "npm run build -- -w",
"watch:release": "npm run build target:release -- -w",
"watch:debug": "npm run build target:debug -- -w",
Expand Down
6 changes: 6 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'node:fs';
import { version, revision } from './utils/rollup-version-revision.mjs';
import { buildTarget } from './utils/rollup-build-target.mjs';
import { buildTargetRTI } from './utils/rollup-build-target-rti.mjs';

// unofficial package plugins
import dts from 'rollup-plugin-dts';
Expand Down Expand Up @@ -103,6 +104,11 @@ BUILD_TYPES.forEach((buildType) => {
});
});

if (envTarget === 'rti') {
targets.length = 0;
targets.push(buildTargetRTI('umd'), buildTargetRTI('es'));
}

if (envTarget === null || envTarget === 'types') {
targets.push(...TYPES_TARGET);
}
Expand Down
2 changes: 1 addition & 1 deletion src/framework/anim/binder/anim-binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AnimBinder {
* or string path.
* @returns {string} The locator encoded as a string.
* @example
* // returns 'spotLight/light/color.r'
* // returns 'spotLight/light/color/r'
* encode(['spotLight'], 'light', ['color', 'r']);
*/
static encode(entityPath, component, propertyPath) {
Expand Down
77 changes: 77 additions & 0 deletions src/index.rti.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export * from './index.js';
import { Vec2 } from './core/math/vec2.js';
import { Vec3 } from './core/math/vec3.js';
import { Vec4 } from './core/math/vec4.js';
import { Quat } from './core/math/quat.js';
import { Mat3 } from './core/math/mat3.js';
import { Mat4 } from './core/math/mat4.js';
import { customTypes, customValidations, validateNumber } from '@runtime-type-inspector/runtime';
import 'display-anything/src/style.js';
Object.assign(customTypes, {
AnimSetter(value) {
// Fix for type in ./framework/anim/evaluator/anim-target.js
// The AnimSetter type is not sufficient, just patching in the correct type here
if (value instanceof Function) {
return true;
}
return value?.set instanceof Function && value?.set instanceof Function;
},
AnimBinder(value) {
// Still using: @implements {AnimBinder}
// RTI doesn't take notice of that so far and we started removing `@implements` aswell:
// Testable via graphics/contact-hardening-shadows example.
return value?.constructor?.name?.endsWith('Binder');
},
ComponentData(value) {
// Used in src/framework/components/collision/trigger.js
// Why do we neither use @implements nor `extends` for such type?
// Testable via animation/locomotion example.
return value?.constructor?.name?.endsWith("ComponentData");
},
Renderer(value) {
// E.g. instance of `ForwardRenderer`
return value?.constructor?.name?.endsWith("Renderer");
},
IArguments(value) {
// Used in src/core/tags.js
// Testable via physics/offset-collision example.
return value[Symbol.iterator] instanceof Function;
}
});
// For quickly checking props of Vec2/Vec3/Vec4/Quat/Mat3/Mat4 without GC
const propsXY = ['x', 'y'];
const propsXYZ = ['x', 'y', 'z'];
const propsXYZW = ['x', 'y', 'z', 'w'];
const props9 = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const props16 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
/**
* `@ignoreRTI`
* @param {any} value - The value.
* @returns {boolean} Only false if we can find some NaN issues.
*/
function validate(value) {
/**
* @param {string|number} prop - Something like 'x', 'y', 'z', 'w', 0, 1, 2, 3, 4 etc.
* @returns {boolean} Wether prop is a valid number.
*/
const checkProp = (prop) => {
return validateNumber(value, prop);
};
if (value instanceof Vec2) {
return propsXY.every(checkProp);
}
if (value instanceof Vec3) {
return propsXYZ.every(checkProp);
}
if ((value instanceof Vec4) || (value instanceof Quat)) {
return propsXYZW.every(checkProp);
}
if (value instanceof Mat3) {
return props9.every(prop => validateNumber(value.data, prop));
}
if (value instanceof Mat4) {
return props16.every(prop => validateNumber(value.data, prop));
}
return true;
}
customValidations.push(validate);
54 changes: 54 additions & 0 deletions utils/rollup-build-target-rti.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import resolve from '@rollup/plugin-node-resolve';
import { engineLayerImportValidation } from './plugins/rollup-import-validation.mjs';
import { getBanner } from './rollup-get-banner.mjs';
import { runtimeTypeInspector } from '@runtime-type-inspector/plugin-rollup';

/** @typedef {import('rollup').RollupOptions} RollupOptions */
/** @typedef {import('rollup').OutputOptions} OutputOptions */
/** @typedef {import('rollup').ModuleFormat} ModuleFormat */
/** @typedef {import('@rollup/plugin-babel').RollupBabelInputPluginOptions} RollupBabelInputPluginOptions */
/** @typedef {import('@rollup/plugin-strip').RollupStripOptions} RollupStripOptions */

/**
* Configure a Runtime Type Inspector target that rollup is supposed to build.
*
* @param {'umd'|'es'} moduleFormat - The module format (subset of ModuleFormat).
* @param {string} input - The input file.
* @param {string} buildDir - The build dir.
* @returns {RollupOptions} Configuration for Runtime Type Inspector rollup target.
*/
function buildTargetRTI(moduleFormat, input = 'src/index.rti.js', buildDir = 'build') {
const banner = getBanner(' (RUNTIME-TYPE-INSPECTOR)');

const outputExtension = {
umd: '.js',
es: '.mjs'
};

const file = `${buildDir}/playcanvas.rti${outputExtension[moduleFormat]}`;

/** @type {OutputOptions} */
const outputOptions = {
banner,
format: moduleFormat,
indent: '\t',
name: 'pc',
file
};

return {
input,
output: outputOptions,
plugins: [
engineLayerImportValidation(input, true),
resolve(),
runtimeTypeInspector({
ignoredFiles: [
'node_modules',
'framework/parsers/draco-worker.js' // runs in Worker context without RTI
]
})
]
};
}
export { buildTargetRTI };