runtime-compiler 4.0.3 → 4.1.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 +42 -15
- package/build.d.ts +13 -0
- package/{build/index.js → build.js} +1 -1
- package/env/aot.js +1 -1
- package/env/build.js +1 -1
- package/env/index.js +1 -1
- package/globals.d.ts +2 -2
- package/globals.js +1 -1
- package/package.json +1 -1
- package/rolldown.d.ts +9 -0
- package/rolldown.js +1 -0
- package/build/index.d.ts +0 -9
- package/build/rolldown.d.ts +0 -3
- package/build/rolldown.js +0 -1
package/README.md
CHANGED
|
@@ -3,31 +3,58 @@ A code generation system.
|
|
|
3
3
|
# Usage
|
|
4
4
|
```ts
|
|
5
5
|
import { IS_AOT, IS_BUILD } from 'runtime-compiler/env';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
// Only emit code if not in AOT mode
|
|
9
|
-
IS_AOT || emit(`return()=>console.log("Hi")`);
|
|
6
|
+
import { evaluate } from 'runtime-compiler/globals';
|
|
10
7
|
|
|
11
8
|
// Only log when building
|
|
12
9
|
IS_BUILD && console.log('Building...');
|
|
13
10
|
|
|
14
|
-
//
|
|
15
|
-
const fn = evaluate<() => void>();
|
|
16
|
-
|
|
11
|
+
// Only emit code if not in AOT mode
|
|
12
|
+
const fn = evaluate<() => void>(IS_AOT || `return()=>console.log("Hi")`);
|
|
13
|
+
|
|
14
|
+
// Run only when not in build mode
|
|
15
|
+
IS_BUILD || fn();
|
|
17
16
|
```
|
|
18
17
|
|
|
19
|
-
Build with `rolldown
|
|
18
|
+
Build with `rolldown`:
|
|
20
19
|
```ts
|
|
21
20
|
...
|
|
22
|
-
import rtc from 'runtime-compiler/
|
|
21
|
+
import rtc from 'runtime-compiler/rolldown';
|
|
22
|
+
|
|
23
|
+
export default defineConfig({
|
|
24
|
+
plugins: [rtc()]
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Or Vite:
|
|
29
|
+
```ts
|
|
30
|
+
import rtc from 'runtime-compiler/rolldown';
|
|
23
31
|
|
|
24
32
|
export default defineConfig({
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
// Only run this plugin on build
|
|
34
|
+
build: {
|
|
35
|
+
rolldownOptions: {
|
|
36
|
+
plugins: [rtc()]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
If you have dependencies marked as external that uses `runtime-compiler`:
|
|
43
|
+
```ts
|
|
44
|
+
rtc({
|
|
45
|
+
// Return true if the package uses 'runtime-compiler' at startup time without pre-building.
|
|
46
|
+
useLoader: (externalPkg) => ...
|
|
30
47
|
});
|
|
31
48
|
```
|
|
32
49
|
|
|
33
|
-
|
|
50
|
+
## Limitation
|
|
51
|
+
Library code and startup code that uses `runtime-compiler` cannot be mixed together in the same bundle.
|
|
52
|
+
```ts
|
|
53
|
+
import { IS_AOT } from 'runtime-compiler/env';
|
|
54
|
+
import { evaluate } from 'runtime-compiler/globals';
|
|
55
|
+
|
|
56
|
+
const fn = evaluate<typeof console.log>(IS_AOT || `return console.log`)('Hi');
|
|
57
|
+
|
|
58
|
+
// AOT build will break this export
|
|
59
|
+
export const createFn = () => evaluate<typeof console.log>(IS_AOT || `return console.log`);
|
|
60
|
+
```
|
package/build.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const getPromiseResolvers: (res: any, rej: any) => void;
|
|
2
|
+
/**
|
|
3
|
+
* Requires code to not bundle "runtime-compiler/env" and "runtime-compiler/globals"
|
|
4
|
+
*/
|
|
5
|
+
export declare const runInWorker: (code: string) => Promise<string>;
|
|
6
|
+
/**
|
|
7
|
+
* Requires code to not bundle `runtime-compiler/env` and `runtime-compiler/globals`
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* writeFileSync('preload.js', compile(await runInWorker(code)));
|
|
11
|
+
* code = 'import"./preload.js";' + code;
|
|
12
|
+
*/
|
|
13
|
+
export declare const compile: (codes: string) => string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Worker}from"node:worker_threads";let curRes,curRej;export const getPromiseResolvers=(res,rej)=>{curRes=res;curRej=rej};let workerOptions={eval:!0};export const runInWorker=code=>{let p=new Promise(getPromiseResolvers),worker=new Worker(`import"runtime-compiler/env/build";${code};\nimport{__rtcpl_ct__}from"runtime-compiler/globals";import{parentPort as __rtcpl_pp__}from"node:worker_threads";__rtcpl_pp__.postMessage(__rtcpl_ct__)`,workerOptions);worker.once("message",curRes);worker.once("error",curRej);return p};export const compile=
|
|
1
|
+
import{Worker}from"node:worker_threads";let curRes,curRej;export const getPromiseResolvers=(res,rej)=>{curRes=res;curRej=rej};let workerOptions={eval:!0};export const runInWorker=code=>{let p=new Promise(getPromiseResolvers),worker=new Worker(`import"runtime-compiler/env/build";${code};\nimport{__rtcpl_ct__}from"runtime-compiler/globals";import{parentPort as __rtcpl_pp__}from"node:worker_threads";__rtcpl_pp__.postMessage(__rtcpl_ct__)`,workerOptions);worker.once("message",curRes);worker.once("error",curRej);return p};export const compile=codes=>`import"runtime-compiler/env/aot";import{__rtcpl_aot_fns__}from"runtime-compiler/globals";__rtcpl_aot_fns__.push(${codes});`;
|
package/env/aot.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{__rtcpl_aot__}from"./index.js";__rtcpl_aot__();
|
package/env/build.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{__rtcpl_build__}from"./index.js";__rtcpl_build__();
|
package/env/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export let IS_JIT=!0;export let IS_BUILD=!1;export let IS_AOT=!1;export const
|
|
1
|
+
export let IS_JIT=!0;export let IS_BUILD=!1;export let IS_AOT=!1;export const __rtcpl_build__=()=>{IS_JIT=!1;IS_BUILD=!0;IS_AOT=!1};export const __rtcpl_aot__=()=>{IS_JIT=!1;IS_BUILD=!1;IS_AOT=!0};
|
package/globals.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type Ref<T> = number & {
|
|
2
2
|
"~": T;
|
|
3
3
|
};
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const ref: <T>(value: T) => Ref<T>;
|
|
5
5
|
export declare const createRef: <T>() => Ref<T>;
|
|
6
6
|
export declare let evaluate: <T>(content?: string | boolean | null | undefined) => T;
|
|
7
|
-
export declare
|
|
7
|
+
export declare let evaluateAsync: <T>(content?: string | boolean | null | undefined) => Promise<T>;
|
|
8
8
|
export declare const deref: <T>(id: Ref<T>) => T;
|
package/globals.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{IS_AOT,IS_BUILD}from"./env/index.js";let __rtcpl_r__=[];export const
|
|
1
|
+
import{IS_AOT,IS_BUILD}from"./env/index.js";let __rtcpl_r__=[];export const ref=value=>__rtcpl_r__.push(value)-1;export const createRef=()=>__rtcpl_r__.push(void 0)-1;export let __rtcpl_ct__;export let evaluate=IS_AOT?_=>__rtcpl_aot_fns__.pop()(__rtcpl_r__):IS_BUILD?content=>{if("string"==typeof content){content=`$=>{${content}}`;__rtcpl_ct__=content+","+__rtcpl_ct__;return(0,eval)(content)(__rtcpl_r__)}__rtcpl_ct__="$=>{},"+__rtcpl_ct__}:content=>{if("string"==typeof content)return(0,eval)(`$=>{${content}}`)(__rtcpl_r__)};export let evaluateAsync=IS_AOT?async _=>__rtcpl_aot_fns__.pop()(__rtcpl_r__):IS_BUILD?async content=>{if("string"==typeof content){content=`async $=>{${content}}`;__rtcpl_ct__=content+","+__rtcpl_ct__;return(0,eval)(content)(__rtcpl_r__)}__rtcpl_ct__="async $=>{},"+__rtcpl_ct__}:async content=>{if("string"==typeof content)return(0,eval)(`async $=>{${content}}`)(__rtcpl_r__)};export const __rtcpl_aot_fns__=[];export const deref=id=>__rtcpl_r__[id];
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"runtime-compiler","version":"4.0
|
|
1
|
+
{"name":"runtime-compiler","version":"4.1.0","description":"A code generation system.","keywords":["rolldown-plugin","vite-plugin"],"homepage":"","license":"MIT","repository":"re-utils/runtime-compiler","type":"module","exports":{"./*":"./*.js","./env":"./env/index.js"}}
|
package/rolldown.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Plugin } from "rolldown";
|
|
2
|
+
export interface Options {
|
|
3
|
+
/**
|
|
4
|
+
* Function to check whether this external dependency uses `runtime-compiler` without bundling.
|
|
5
|
+
*/
|
|
6
|
+
useLoader?: (importName: string) => boolean;
|
|
7
|
+
}
|
|
8
|
+
declare const _default: (options?: Options) => Plugin;
|
|
9
|
+
export default _default;
|
package/rolldown.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{runInWorker,compile}from"./build.js";export const parseImportBindings=(code,start,end)=>{let startSpecs=code.indexOf("{",start),endSpecs=code.lastIndexOf("}",end);if(-1===start&&-1===end)throw new Error("Only named imports replacement is supported!");let bindings=[];for(let i=0,specifiers=code.slice(startSpecs+1,endSpecs-1).split(",");i<specifiers.length;i++){let specifier=specifiers[i].trim();if(""===specifier)continue;let parts=specifier.split(" as ",2);if(1===parts.length){let name=parts[0].trim();bindings.push({name,alias:name})}else bindings.push({name:parts[0].trim(),alias:parts[1].trim()})}return{bindings,start,end}};let EMPTY_IMPORT={bindings:[],start:0,end:0};export default((options={})=>({name:"rolldown-plugin-runtime-compiler",resolveId:{filter:{id:/^runtime-compiler\/(?:env|globals)$/},handler:()=>!1},async renderChunk(code,chunk){let{imports}=chunk,useLoader=options.useLoader&&imports.some(options.useLoader),globalsImport=null;if(useLoader)globalsImport=EMPTY_IMPORT;else{let globalsImportIdx=imports.indexOf("runtime-compiler/globals");if(-1===globalsImportIdx)return null;imports.splice(globalsImportIdx)}let envImport=null;{let envImportIdx=imports.indexOf("runtime-compiler/env");-1===envImportIdx?envImport=EMPTY_IMPORT:imports.splice(envImportIdx,1)}for(let start=0,eol=code.indexOf("\n");eol>-1&&(null==envImport||null==globalsImport);eol=code.indexOf("\n",start=eol+1))code.startsWith("import",start)&&(null==envImport&&code.endsWith('"runtime-compiler/env";',eol)?envImport=parseImportBindings(code,start,eol):null==globalsImport&&code.endsWith('"runtime-compiler/globals";',eol)&&(globalsImport=parseImportBindings(code,start,eol)));let codes=await runInWorker(code);if(useLoader){let fileName=chunk.fileName+"-rtc-aot-loader.js";this.emitFile({type:"prebuilt-chunk",code:compile(codes),fileName});let aotCode="import"+JSON.stringify(fileName)+";";for(let i=0,{bindings}=envImport;i<bindings.length;i++){let{name,alias}=bindings[i];aotCode+="IS_AOT"===name?`const ${alias}=true;`:"IS_BUILD"===name||"IS_JIT"===name?`const ${alias}=false;`:""}return aotCode+code.slice(0,envImport.start)+code.slice(envImport.end)}{let aotCode=`const __rtcpl_r__=[],__rtcpl_aot_fns__=[${codes}]`;for(let i=0,{bindings}=envImport;i<bindings.length;i++){let{name,alias}=bindings[i];aotCode+="IS_AOT"===name?`,${alias}=true`:"IS_BUILD"===name||"IS_JIT"===name?`,${alias}=false`:""}for(let i=0,{bindings}=globalsImport;i<bindings.length;i++){let{name,alias}=bindings[i];aotCode+="deref"===name?`,${alias}=i=>__rtcpl_r__[i]`:"evaluate"===name?`,${alias}=_=>__rtcpl_aot_fns__.pop()(__rtcpl_r__)`:"evaluateAsync"===name?`,${alias}=async _=>__rtcpl_aot_fns__.pop()(__rtcpl_r__)`:"createRef"===name?`,${alias}=()=>__rtcpl_r__.push(undefined)-1`:"ref"===name?`,${alias}=v=>__rtcpl_r__.push(v)-1`:""}return aotCode+";"+(envImport.start<globalsImport.start?code.slice(0,envImport.start)+code.slice(envImport.end,globalsImport.start)+code.slice(globalsImport.end):code.slice(0,globalsImport.start)+code.slice(globalsImport.end,envImport.start)+code.slice(envImport.end))}}}));
|
package/build/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export declare const getPromiseResolvers: (res: any, rej: any) => void;
|
|
2
|
-
/**
|
|
3
|
-
* Requires code to not bundle "runtime-compiler/env" and "runtime-compiler/globals"
|
|
4
|
-
*/
|
|
5
|
-
export declare const runInWorker: (code: string) => Promise<string[]>;
|
|
6
|
-
/**
|
|
7
|
-
* Requires code to not bundle "runtime-compiler/env" and "runtime-compiler/globals"
|
|
8
|
-
*/
|
|
9
|
-
export declare const compile: (code: string) => Promise<string>;
|
package/build/rolldown.d.ts
DELETED
package/build/rolldown.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{runInWorker}from"./index.js";let parseImportBindings=(code,start,end)=>{let startSpecs=code.indexOf("{",start),endSpecs=code.lastIndexOf("}",end);if(-1===start&&-1===end)throw new Error("Only named imports replacement is supported!");let bindings=[];for(let i=0,specifiers=code.slice(startSpecs+1,endSpecs-1).split(",");i<specifiers.length;i++){let specifier=specifiers[i].trim();if(""===specifier)continue;let parts=specifier.split(" as ",2);if(1===parts.length){let name=parts[0].trim();bindings.push({name,alias:name})}else bindings.push({name:parts[0].trim(),alias:parts[1].trim()})}return{bindings,start,end}};export default(()=>({name:"rolldown-plugin-runtime-compiler",resolveId:{filter:{id:/^runtime-compiler\/(?:env|globals)$/},handler:()=>!1},renderChunk:async(code,{imports})=>{let globalsImportIdx=imports.indexOf("runtime-compiler/globals");if(-1===globalsImportIdx)return null;imports.splice(globalsImportIdx);let envImportIdx=imports.indexOf("runtime-compiler/env"),envImport=null,globalsImport=null;-1===envImportIdx?envImport={bindings:[],start:0,end:0}:imports.splice(envImportIdx,1);for(let start=0,eol=code.indexOf("\n");eol>-1&&(null==envImport||null==globalsImport);eol=code.indexOf("\n",start=eol+1))code.startsWith("import",start)&&(code.endsWith('"runtime-compiler/env";',eol)?envImport=parseImportBindings(code,start,eol):code.endsWith('"runtime-compiler/globals";',eol)&&(globalsImport=parseImportBindings(code,start,eol)));let aotCode="const __rtcpl_r__=[],__rtcpl_aot_fns__=[";for(let codes=await runInWorker(code),i=codes.length-1;i>-1;i--)aotCode+=`$=>{${codes[i]}},`;aotCode+="]";for(let i=0,{bindings}=envImport;i<bindings.length;i++){let{name,alias}=bindings[i];aotCode+="IS_AOT"===name?`,${alias}=true`:"IS_BUILD"===name||"IS_JIT"===name?`,${alias}=false`:""}for(let i=0,{bindings}=globalsImport;i<bindings.length;i++){let{name,alias}=bindings[i];aotCode+="deref"===name?`,${alias}=i=>__rtcpl_r__[i]`:"evaluate"===name?`,${alias}=(_)=>__rtcpl_aot_fns__.pop()(__rtcpl_r__)`:"createRef"===name?`,${alias}=()=>__rtcpl_r__.push(undefined)-1`:"importRef"===name?`,${alias}=v=>__rtcpl_r__.push(v)-1`:""}aotCode+=";"+(envImport.start<globalsImport.start?code.slice(0,envImport.start)+code.slice(envImport.end,globalsImport.start)+code.slice(globalsImport.end):code.slice(0,globalsImport.start)+code.slice(globalsImport.end,envImport.start)+code.slice(envImport.end));return{code:aotCode}}}));
|