runtime-compiler 4.1.0 → 4.2.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 +24 -7
- package/build.d.ts +2 -3
- package/build.js +1 -1
- package/{globals.d.ts → index.d.ts} +2 -2
- package/index.js +1 -0
- package/package.json +1 -1
- package/rolldown.d.ts +11 -3
- package/rolldown.js +1 -1
- package/globals.js +0 -1
- package/utils.d.ts +0 -8
- package/utils.js +0 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ A code generation system.
|
|
|
3
3
|
# Usage
|
|
4
4
|
```ts
|
|
5
5
|
import { IS_AOT, IS_BUILD } from 'runtime-compiler/env';
|
|
6
|
-
import { evaluate } from 'runtime-compiler
|
|
6
|
+
import { evaluate } from 'runtime-compiler';
|
|
7
7
|
|
|
8
8
|
// Only log when building
|
|
9
9
|
IS_BUILD && console.log('Building...');
|
|
@@ -41,17 +41,34 @@ export default defineConfig({
|
|
|
41
41
|
|
|
42
42
|
If you have dependencies marked as external that uses `runtime-compiler`:
|
|
43
43
|
```ts
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
// package: external-pkg
|
|
45
|
+
import { IS_AOT } from 'runtime-compiler/env';
|
|
46
|
+
import { evaluate } from 'runtime-compiler';
|
|
47
|
+
|
|
48
|
+
export const fn = evaluate(IS_AOT || '()=>console.log("Hi")');
|
|
49
|
+
|
|
50
|
+
// index.ts
|
|
51
|
+
import { fn } from 'external-pkg';
|
|
52
|
+
fn();
|
|
53
|
+
|
|
54
|
+
// build
|
|
55
|
+
export default {
|
|
56
|
+
...,
|
|
57
|
+
external: ['external-pkg', ...],
|
|
58
|
+
plugins: [
|
|
59
|
+
rtc({
|
|
60
|
+
// Chunks that import 'external-pkg' will output loader instead
|
|
61
|
+
useLoader: (pkg) => pkg === 'external-pkg';
|
|
62
|
+
});
|
|
63
|
+
]
|
|
64
|
+
}
|
|
48
65
|
```
|
|
49
66
|
|
|
50
67
|
## Limitation
|
|
51
|
-
Library code and startup code that uses `runtime-compiler` cannot be mixed together in the same
|
|
68
|
+
Library code and startup code that uses `runtime-compiler` cannot be mixed together in the same chunk.
|
|
52
69
|
```ts
|
|
53
70
|
import { IS_AOT } from 'runtime-compiler/env';
|
|
54
|
-
import { evaluate } from 'runtime-compiler
|
|
71
|
+
import { evaluate } from 'runtime-compiler';
|
|
55
72
|
|
|
56
73
|
const fn = evaluate<typeof console.log>(IS_AOT || `return console.log`)('Hi');
|
|
57
74
|
|
package/build.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
export declare const getPromiseResolvers: (res: any, rej: any) => void;
|
|
2
1
|
/**
|
|
3
|
-
* Requires code to not bundle "runtime-compiler/env" and "runtime-compiler
|
|
2
|
+
* Requires code to not bundle "runtime-compiler/env" and "runtime-compiler"
|
|
4
3
|
*/
|
|
5
4
|
export declare const runInWorker: (code: string) => Promise<string>;
|
|
6
5
|
/**
|
|
7
|
-
* Requires code to not bundle `runtime-compiler/env` and `runtime-compiler
|
|
6
|
+
* Requires code to not bundle `runtime-compiler/env` and `runtime-compiler`
|
|
8
7
|
*
|
|
9
8
|
* @example
|
|
10
9
|
* writeFileSync('preload.js', compile(await runInWorker(code)));
|
package/build.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Worker}from"node:worker_threads";let
|
|
1
|
+
import{Worker}from"node:worker_threads";let workerOptions={eval:!0};export const runInWorker=code=>new Promise((res,rej)=>{let worker=new Worker(`import"runtime-compiler/env/build";${code};\nimport{__rtcpl_ct__}from"runtime-compiler";import{parentPort as __rtcpl_pp__}from"node:worker_threads";__rtcpl_pp__.postMessage(__rtcpl_ct__)`,workerOptions);worker.once("message",msg=>{res(msg);worker.terminate()});worker.once("error",rej)});export const compile=codes=>`import"runtime-compiler/env/aot";import{__rtcpl_aot_fns__ as a}from"runtime-compiler";a.push(${codes})`;
|
|
@@ -3,6 +3,6 @@ export type Ref<T> = number & {
|
|
|
3
3
|
};
|
|
4
4
|
export declare const ref: <T>(value: T) => Ref<T>;
|
|
5
5
|
export declare const createRef: <T>() => Ref<T>;
|
|
6
|
-
export declare let evaluate: <T>(content?:
|
|
7
|
-
export declare let evaluateAsync: <T>(content?:
|
|
6
|
+
export declare let evaluate: <T>(content?: any) => T;
|
|
7
|
+
export declare let evaluateAsync: <T>(content?: any) => Promise<T>;
|
|
8
8
|
export declare const deref: <T>(id: Ref<T>) => T;
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
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)throw new Error("Expected a string for content in JIT & BUILD mode");content=`$=>{${content}}`;__rtcpl_ct__=content+","+__rtcpl_ct__;return(0,eval)('"use strict";'+content)(__rtcpl_r__)}:content=>{if("string"!=typeof content)throw new Error("Expected a string for content in JIT & BUILD mode");return(0,eval)(`"use strict";$=>{${content}}`)(__rtcpl_r__)};export let evaluateAsync=IS_AOT?evaluate:IS_BUILD?content=>{if("string"!=typeof content)throw new Error("Expected a string for content in JIT & BUILD mode");content=`async $=>{${content}}`;__rtcpl_ct__=content+","+__rtcpl_ct__;return(0,eval)('"use strict";'+content)(__rtcpl_r__)}:content=>{if("string"!=typeof content)throw new Error("Expected a string for content in JIT & BUILD mode");return(0,eval)(`"use strict";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.
|
|
1
|
+
{"name":"runtime-compiler","version":"4.2.0","description":"A code generation system.","keywords":["rolldown-plugin","vite-plugin"],"homepage":"","license":"MIT","repository":"re-utils/runtime-compiler","type":"module","exports":{"./*":"./*.js",".":"./index.js","./env":"./env/index.js"}}
|
package/rolldown.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import type { Plugin } from "rolldown";
|
|
2
|
-
|
|
2
|
+
type RenderChunk = Exclude<Plugin["renderChunk"] & {}, (...args: any[]) => any>;
|
|
3
|
+
declare const _default: (options?: {
|
|
3
4
|
/**
|
|
4
5
|
* Function to check whether this external dependency uses `runtime-compiler` without bundling.
|
|
5
6
|
*/
|
|
6
7
|
useLoader?: (importName: string) => boolean;
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Choose what chunk to run `renderChunk`.
|
|
10
|
+
*/
|
|
11
|
+
filter?: RenderChunk["filter"];
|
|
12
|
+
/**
|
|
13
|
+
* Choose the order to run `renderChunk`. Defaults to `pre`.
|
|
14
|
+
*/
|
|
15
|
+
order?: RenderChunk["order"];
|
|
16
|
+
}) => Plugin;
|
|
9
17
|
export default _default;
|
package/rolldown.js
CHANGED
|
@@ -1 +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
|
|
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 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$)/},handler:()=>!1},renderChunk:{filter:options.filter,order:options.order??"pre",async handler(code,chunk){let{imports}=chunk,useLoader=options.useLoader&&imports.some(options.useLoader),envImport=null;{let envImportIdx=imports.indexOf("runtime-compiler/env");-1===envImportIdx?envImport=EMPTY_IMPORT:imports.splice(envImportIdx,1)}let globalsImport=null;if(useLoader)globalsImport=EMPTY_IMPORT;else{let globalsImportIdx=imports.indexOf("runtime-compiler");if(-1===globalsImportIdx){if(envImport===EMPTY_IMPORT)return null;globalsImport=EMPTY_IMPORT}else imports.splice(globalsImportIdx,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";',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||"evaluateAsync"===name?`,${alias}=()=>__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/globals.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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/utils.d.ts
DELETED
package/utils.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const toExpr=str=>str;export const toId=toExpr;
|