runtime-compiler 2.1.5 → 3.0.1
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/derive.d.ts +59 -0
- package/derive.js +1 -0
- package/index.d.ts +32 -44
- package/index.js +1 -1
- package/package.json +1 -1
- package/call.d.ts +0 -14
- package/call.js +0 -1
- package/scope.d.ts +0 -4
- package/scope.js +0 -1
package/derive.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Expression, type Value } from "./index.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Derive a value from other expressions.
|
|
4
|
+
* Use in `default` and `build` mode.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const newValue = derive(
|
|
8
|
+
* ['Math.random()' as Expression<number>],
|
|
9
|
+
* (rand) => rand * 10
|
|
10
|
+
* ); // '$[0](Math.random())'
|
|
11
|
+
*
|
|
12
|
+
* // In hydrate mode
|
|
13
|
+
* injectExternal((rand) => rand * 10);
|
|
14
|
+
*/
|
|
15
|
+
export declare const derive: <
|
|
16
|
+
const Expressions extends Expression<any>[],
|
|
17
|
+
R
|
|
18
|
+
>(values: Expressions, fn: (...args: { [K in keyof Expressions] : Expressions[K]["~type"] }) => R) => Value<R>;
|
|
19
|
+
/**
|
|
20
|
+
* Derive a value from other expressions.
|
|
21
|
+
* Use in `default` and `build` mode.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const newValue = deriveAsync(
|
|
25
|
+
* ['await fetch("http://example.com")' as Expression<Response>],
|
|
26
|
+
* async (res) => await res.text()
|
|
27
|
+
* ); // 'await $[0](await fetch("http://example.com"))'
|
|
28
|
+
*
|
|
29
|
+
* // In hydrate mode
|
|
30
|
+
* injectExternal((rand) => rand * 10);
|
|
31
|
+
*/
|
|
32
|
+
export declare const deriveAsync: <
|
|
33
|
+
const Expressions extends Expression<any>[],
|
|
34
|
+
R
|
|
35
|
+
>(values: Expressions, fn: (...args: { [K in keyof Expressions] : Expressions[K]["~type"] }) => Promise<R>) => Expression<R>;
|
|
36
|
+
/**
|
|
37
|
+
* Describe an injected call.
|
|
38
|
+
*/
|
|
39
|
+
export declare class InjectedCall<
|
|
40
|
+
const Args extends any[],
|
|
41
|
+
const R
|
|
42
|
+
> {
|
|
43
|
+
args: Expression<any>[];
|
|
44
|
+
fn: (...args: Args) => R;
|
|
45
|
+
constructor(args: Expression<any>[], fn: (...args: Args) => R);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* @param values
|
|
49
|
+
* @param fn
|
|
50
|
+
*/
|
|
51
|
+
export declare const inject: <
|
|
52
|
+
const Expressions extends Expression<any>[],
|
|
53
|
+
Args extends any[],
|
|
54
|
+
R
|
|
55
|
+
>(values: Expressions, fn: (...args: [...{ [K in keyof Expressions] : Expressions[K]["~type"] }, ...Args]) => R) => InjectedCall<Args, R>;
|
|
56
|
+
/**
|
|
57
|
+
* Inject the external function, cache the access to a local dependency and run with provided args.
|
|
58
|
+
*/
|
|
59
|
+
export declare const cacheCall: <R>(fn: (...args: any[]) => R, args: string) => Value<R>;
|
package/derive.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{injectDependency,injectExternal}from"./index.js";export let derive=(e,a)=>injectExternal(a)+`(`+e.join()+`)`;export let deriveAsync=(e,a)=>`await `+injectExternal(a)+`(`+e.join()+`)`;export class InjectedCall{args;fn;constructor(e,i){this.args=e;this.fn=i}}export let inject=(e,i)=>new InjectedCall(e,i);export let cacheCall=(a,o)=>injectDependency(injectExternal(a))+`(`+o+`)`;
|
package/index.d.ts
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
declare const _2: unique symbol;
|
|
4
|
-
export type LocalValue<T> = string & {
|
|
5
|
-
[_]: T
|
|
1
|
+
export type Expression<T> = string & {
|
|
2
|
+
"~type": T
|
|
6
3
|
};
|
|
7
|
-
export type
|
|
8
|
-
|
|
4
|
+
export type Value<T> = Expression<T> & {
|
|
5
|
+
"~value": 0
|
|
6
|
+
};
|
|
7
|
+
export type Identifier<T> = Value<T> & {
|
|
8
|
+
"~id": 0
|
|
9
9
|
};
|
|
10
10
|
export type ExportedDependency<T> = number & {
|
|
11
|
-
|
|
11
|
+
"~type": T
|
|
12
12
|
};
|
|
13
|
-
export type InferDependency<T extends {
|
|
14
|
-
[_]: any
|
|
15
|
-
}> = T[typeof _];
|
|
16
13
|
interface InjectDependencyFn {
|
|
17
|
-
<T>(val:
|
|
18
|
-
<T>(val: string):
|
|
14
|
+
<T>(val: Expression<T>): Identifier<T>;
|
|
15
|
+
<T>(val: string): Identifier<T>;
|
|
19
16
|
}
|
|
20
17
|
/**
|
|
21
18
|
* Inject a local dependency.
|
|
@@ -25,56 +22,47 @@ export declare const injectDependency: InjectDependencyFn;
|
|
|
25
22
|
/**
|
|
26
23
|
* Export a local dependency.
|
|
27
24
|
* Use in `default` and `build` mode.
|
|
28
|
-
* @param
|
|
25
|
+
* @param value
|
|
29
26
|
*/
|
|
30
|
-
export declare const exportDependency: <T>(
|
|
27
|
+
export declare const exportDependency: <T>(value: Expression<T>) => ExportedDependency<T>;
|
|
31
28
|
/**
|
|
32
|
-
* Mark a slot
|
|
29
|
+
* Mark a slot to export a dependency.
|
|
33
30
|
* Use in `hydrate` mode.
|
|
34
31
|
*/
|
|
35
32
|
export declare const markExported: <T>() => ExportedDependency<T>;
|
|
36
33
|
/**
|
|
37
|
-
* Get the value of
|
|
34
|
+
* Get the value of an exported dependency.
|
|
38
35
|
* @param idx
|
|
39
36
|
*/
|
|
40
37
|
export declare const getDependency: <T>(idx: ExportedDependency<T>) => T;
|
|
41
38
|
/**
|
|
42
|
-
* Add extra code after dependency building
|
|
43
|
-
*/
|
|
44
|
-
export declare const addExtraCode: (str: string) => void;
|
|
45
|
-
/**
|
|
46
39
|
* Inject an external dependency.
|
|
47
40
|
*/
|
|
48
|
-
export declare const
|
|
41
|
+
export declare const injectExternal: <T>(val: T) => Value<T>;
|
|
49
42
|
/**
|
|
50
|
-
*
|
|
43
|
+
* Evaluate to statements instead of a function.
|
|
51
44
|
* Use in `default` and `build` mode.
|
|
52
45
|
*/
|
|
53
|
-
export declare const
|
|
54
|
-
/**
|
|
55
|
-
* Equivalent to calling `evaluate`/`evaluateSync` in `default` or `build` mode.
|
|
56
|
-
* Return the args that needs to be passed in.
|
|
57
|
-
* Use in `hydrate` mode.
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* (() => {
|
|
61
|
-
* // Built content
|
|
62
|
-
* })(finishHydration())
|
|
63
|
-
*/
|
|
64
|
-
export declare const hydrate: () => any[];
|
|
46
|
+
export declare const evaluateToStatements: () => string;
|
|
65
47
|
/**
|
|
66
|
-
*
|
|
67
|
-
* Use in `
|
|
48
|
+
* Evaluate code to a function.
|
|
49
|
+
* Use in `build` mode.
|
|
68
50
|
*/
|
|
69
|
-
export declare const
|
|
51
|
+
export declare const evaluateToFn: (extraCode: string) => Expression<(...args: any[]) => any>;
|
|
70
52
|
/**
|
|
71
|
-
*
|
|
72
|
-
* Use in `default`
|
|
53
|
+
* Run evaluated code.
|
|
54
|
+
* Use in `default` mode.
|
|
73
55
|
*/
|
|
74
|
-
export declare const
|
|
56
|
+
export declare const evaluate: (extraCode: string) => any;
|
|
75
57
|
/**
|
|
76
|
-
*
|
|
77
|
-
* Use in `
|
|
58
|
+
* Hydrate a built function.
|
|
59
|
+
* Use in `hydrate` mode.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* hydratedCode += `
|
|
63
|
+
* import { hydrate } from 'runtime-compiler';
|
|
64
|
+
* const compiled = hydrate(${evaluateToFn(app)});
|
|
65
|
+
* `;
|
|
78
66
|
*/
|
|
79
|
-
export declare const
|
|
67
|
+
export declare const hydrate: <T>(compiledFn: (externalDependencies: any[]) => T) => T;
|
|
80
68
|
export {};
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
let externals=[];let localDeps=``;let localDepsCnt=0;let exportDeps=``;export let injectDependency=e=>{localDeps+=`,$`+localDepsCnt+`=`+e;return`$`+ localDepsCnt++};export let exportDependency=d=>(exportDeps+=`;$[`+externals.length+`]=`+d,externals.push(null)-1);export let markExported=()=>externals.push(null)-1;export let getDependency=d=>externals[d];export let injectExternal=d=>`$[`+(externals.push(d)-1)+`]`;export let evaluateToStatements=()=>localDeps.length===0?exportDeps:`let `+localDeps.slice(1)+exportDeps;export let evaluateToFn=e=>`$=>{var _`+localDeps+exportDeps+e+`}`;export let evaluate=f=>{try{return Function(`$`,`var _`+localDeps+exportDeps+f)(externals)}finally{localDeps=``;localDepsCnt=0;exportDeps=``}};export let hydrate=d=>d(externals);
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"runtime-compiler","version":"
|
|
1
|
+
{"name":"runtime-compiler","version":"3.0.1","description":"Universal compiler system","keywords":[],"repository":{},"homepage":"","license":"MIT","type":"module","exports":{".":"./index.js","./config":"./config/index.js","./utils":"./utils.js","./derive":"./derive.js","./config/mode/build":"./config/mode/build.js","./config/loader/build":"./config/loader/build.js","./config/mode/hydrate":"./config/mode/hydrate.js","./config/loader/hydrate":"./config/loader/hydrate.js"}}
|
package/call.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { InferDependency, LocalDependency } from "./index.ts";
|
|
2
|
-
export declare const sym: unique symbol;
|
|
3
|
-
/**
|
|
4
|
-
* Inject dependency to an fn for later compilation
|
|
5
|
-
*/
|
|
6
|
-
export declare const inject: <
|
|
7
|
-
const Deps extends LocalDependency<any>[],
|
|
8
|
-
const Args extends any[],
|
|
9
|
-
const Ret
|
|
10
|
-
>(deps: Deps, fn: (...args: [...{ [K in keyof Deps] : InferDependency<Deps[K]> }, ...Args]) => Ret) => ((...args: Args) => Ret);
|
|
11
|
-
/**
|
|
12
|
-
* Get fn injected dependency list
|
|
13
|
-
*/
|
|
14
|
-
export declare const getDeps: (fn: any) => LocalDependency<any>[] | undefined;
|
package/call.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let sym=Symbol();export let inject=(t,n)=>(n[sym]=t,n);export let getDeps=t=>t[sym];
|
package/scope.d.ts
DELETED
package/scope.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let init=()=>[0];export let nextId=e=>`l`+ e[0]++;export let idCount=e=>e[0];
|