@runtime-type-inspector/plugin-rollup 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/index.mjs +77 -0
- package/package.json +17 -0
package/README.md
ADDED
package/index.mjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {createFilter} from '@rollup/pluginutils';
|
|
2
|
+
import {parse } from '@babel/parser';
|
|
3
|
+
import {
|
|
4
|
+
addTypeChecks, expandType,ast2jsonForComparison, code2ast2code
|
|
5
|
+
} from '@runtime-type-inspector/transpiler';
|
|
6
|
+
/**
|
|
7
|
+
* Alternatively "import * as rti from ..." would also prevent "Unused external imports" warning...
|
|
8
|
+
* or keeping log of every single call during RTI parsing.
|
|
9
|
+
* @param {boolean} validateDivision
|
|
10
|
+
*/
|
|
11
|
+
function getHeader(validateDivision) {
|
|
12
|
+
let header = "import { assertType, youCanAddABreakpointHere";
|
|
13
|
+
if (validateDivision) {
|
|
14
|
+
header += ", validateDivision";
|
|
15
|
+
}
|
|
16
|
+
header += ", registerTypedef, registerClass } from '@runtime-type-inspector/runtime';\n";
|
|
17
|
+
// Prevent tree-shaking in UMD build so we can always "add a breakpoint here".
|
|
18
|
+
header += "export * from '@runtime-type-inspector/runtime';\n";
|
|
19
|
+
return header;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} left
|
|
23
|
+
* @param {string} right
|
|
24
|
+
*/
|
|
25
|
+
function compareAST(left, right) {
|
|
26
|
+
const l = parse(left , {sourceType: 'module'});
|
|
27
|
+
const r = parse(right, {sourceType: 'module'});
|
|
28
|
+
const ljson = ast2jsonForComparison(l);
|
|
29
|
+
const rjson = ast2jsonForComparison(r);
|
|
30
|
+
const test = ljson == rjson;
|
|
31
|
+
if (!test) {
|
|
32
|
+
console.warn(`AST is NOT equal`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @param {object} [options] - Optional options.
|
|
37
|
+
* @param {boolean} [options.enable] - Enable or disable entire plugin. Defaults to true.
|
|
38
|
+
* @param {boolean} [options.selftest] - Every once in a while Babel changes the AST, so we
|
|
39
|
+
* self-test Stringifier class to ensure its functionanlity.
|
|
40
|
+
* @param {string[]} [options.ignoredFiles] - Ignore certain files which operate in a different
|
|
41
|
+
* context, for example framework/parsers/draco-worker.js operates as WebWorker (without RTI).
|
|
42
|
+
* @returns {import('rollup').Plugin}
|
|
43
|
+
*/
|
|
44
|
+
function runtimeTypeInspector({enable = true, selftest = false, ignoredFiles} = {}) {
|
|
45
|
+
const filter = createFilter([
|
|
46
|
+
'**/*.js'
|
|
47
|
+
], []);
|
|
48
|
+
return {
|
|
49
|
+
name: 'runtime-type-inspector',
|
|
50
|
+
transform(code, id) {
|
|
51
|
+
if (!enable || !filter(id)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// Ignore files which are supposed to run in e.g. a Worker context without RTI
|
|
55
|
+
if (ignoredFiles?.some(_ => id.includes(_))) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (selftest) {
|
|
59
|
+
compareAST(code, code2ast2code(code));
|
|
60
|
+
}
|
|
61
|
+
const validateDivision = true;
|
|
62
|
+
code = getHeader(validateDivision) + code;
|
|
63
|
+
// todo expose options to rollup plugin
|
|
64
|
+
code = addTypeChecks(code, {
|
|
65
|
+
validateDivision,
|
|
66
|
+
// ignoreLocations: ['Tensor#constructor'], // todo
|
|
67
|
+
expandType,
|
|
68
|
+
filename: id,
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
code,
|
|
72
|
+
map: null
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export {runtimeTypeInspector};
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@runtime-type-inspector/plugin-rollup",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"module": "index.mjs",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"author": "Hermann Rolfes (kungfooman)",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@runtime-type-inspector/runtime": "^2.0.5",
|
|
14
|
+
"@runtime-type-inspector/transpiler": "^2.0.4",
|
|
15
|
+
"rollup": "^3.29.4"
|
|
16
|
+
}
|
|
17
|
+
}
|