UNPKG

esbuild-peggy

Version:

esbuild plugin for importing PEG.js / Peggy parsers directly in JavaScript / TypeScript

4 lines (3 loc) 687 B
import l from"node:fs/promises";import r from"peggy";var c="pegjs-loader",p="file",g=/\.peg(js|gy)$/,f=(s={})=>({name:c,setup(i){i.onLoad({filter:g,namespace:p},async n=>{let o=await l.readFile(n.path,"utf8"),a={output:"source",...s};try{return{contents:`export default ${r.generate(o,a)}`}}catch(e){if(e!=null&&e instanceof r.GrammarError){let t=e;return{errors:[{text:t?.message,location:{file:n.path,line:t?.location?.start?.line??0,column:(t?.location?.start?.column??1)-1,length:(t?.location?.end?.offset??1)-(t?.location?.start?.offset??0),lineText:o.split(` `).slice(t?.location?.start?.line??0,t?.location?.start?.line).join(` `)}}]}}else throw e}})}});export{f as peggyPlugin};