router-query/examples/compile_native.mjs
Version 663282b0ebbc.bb1 · MIT. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.
← Return to resource and package checksum
import { readFile, mkdir, writeFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { build, transform } from 'esbuild';
const plan=JSON.parse(await readFile('native-extraction-plan.json','utf8'));
await mkdir('output/modules',{recursive:true});
const compilation=[];
for(const block of plan.blocks){
if(block.language==='bash'){compilation.push({block:block.index,status:'not-executed-source-command'});continue;}
const raw=await readFile(`output/source/native-${String(block.index).padStart(2,'0')}.tsx`);
if(createHash('sha256').update(raw).digest('hex')!==block.sha256)throw Error('original block checksum mismatch');
try{const result=await transform(raw.toString(),{loader:'tsx',jsx:'automatic',format:'esm',logLevel:'silent'});if(block.index===12)await writeFile('output/modules/labelled-contrasts.mjs',result.code);compilation.push({block:block.index,status:'transpiled',full_typecheck:false});}
catch(error){compilation.push({block:block.index,status:'parse-rejected',contrast_block:plan.contrast_blocks.includes(block.index),errors:error.errors.map(x=>({text:x.text,line:x.location?.line})),full_typecheck:false});if(!plan.contrast_blocks.includes(block.index))throw error;}
}
for(const entry of plan.entries){
const file=`output/source/${entry.name}.tsx`;
if(createHash('sha256').update(await readFile(file)).digest('hex')!==entry.entry_sha256)throw Error('wrapper checksum mismatch');
await build({entryPoints:[file],outfile:`output/modules/${entry.name}.mjs`,bundle:true,packages:'external',platform:'node',format:'esm',jsx:'automatic',logLevel:'silent'});
}
await writeFile('native-compilation.json',JSON.stringify({checked_at:new Date().toISOString(),blocks:compilation,compiled_entries:plan.entries.length,full_typecheck:false},null,2)+'\n');
console.log(JSON.stringify({original_blocks:plan.blocks.length,compiled_entries:plan.entries.length,parse_counterexamples:compilation.filter(x=>x.status==='parse-rejected').map(x=>x.block),bash_commands_executed:0}));