runtime-compiler 4.1.1 → 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 +18 -5
- package/build.d.ts +0 -1
- package/build.js +1 -1
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/package.json +1 -1
- package/rolldown.d.ts +1 -1
- package/rolldown.js +1 -1
package/README.md
CHANGED
|
@@ -42,17 +42,30 @@ export default defineConfig({
|
|
|
42
42
|
If you have dependencies marked as external that uses `runtime-compiler`:
|
|
43
43
|
```ts
|
|
44
44
|
// package: external-pkg
|
|
45
|
+
import { IS_AOT } from 'runtime-compiler/env';
|
|
45
46
|
import { evaluate } from 'runtime-compiler';
|
|
46
47
|
|
|
48
|
+
export const fn = evaluate(IS_AOT || '()=>console.log("Hi")');
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
}
|
|
52
65
|
```
|
|
53
66
|
|
|
54
67
|
## Limitation
|
|
55
|
-
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.
|
|
56
69
|
```ts
|
|
57
70
|
import { IS_AOT } from 'runtime-compiler/env';
|
|
58
71
|
import { evaluate } from 'runtime-compiler';
|
package/build.d.ts
CHANGED
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})`;
|
package/index.d.ts
CHANGED
|
@@ -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
CHANGED
|
@@ -1 +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?
|
|
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
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))}}}}));
|