bun-plugin-stx 0.1.16 → 0.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.
Files changed (2) hide show
  1. package/dist/index.js +10 -10
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,20 +1,20 @@
1
1
  // @bun
2
- import{b as E,c as k,d as N,e as g,f as P,g as F}from"./chunk-mjj2b826.js";import"./chunk-h0s48z3v.js";import u from"path";import q from"fs";import L from"path";var i=new Map;async function b(j,_){try{let Z=L.resolve(_.cachePath),V=L.join(Z,`${w(j)}.html`),K=L.join(Z,`${w(j)}.meta.json`);if(!await k(V)||!await k(K))return null;let U=await Bun.file(K).text(),Q=JSON.parse(U);if(Q.cacheVersion!==_.cacheVersion)return null;if((await q.promises.stat(j)).mtime.getTime()>Q.mtime)return null;for(let H of Q.dependencies)if(await k(H)){if((await q.promises.stat(H)).mtime.getTime()>Q.mtime)return null}else return null;return await Bun.file(V).text()}catch(Z){return console.warn(`Cache error for ${j}:`,Z),null}}async function D(j,_,Z,V){try{let K=L.resolve(V.cachePath);await q.promises.mkdir(K,{recursive:!0});let U=L.join(K,`${w(j)}.html`),Q=L.join(K,`${w(j)}.meta.json`),J=await q.promises.stat(j);await Bun.write(U,_);let H={sourcePath:j,mtime:J.mtime.getTime(),dependencies:Array.from(Z),cacheVersion:V.cacheVersion,generatedAt:Date.now()};await Bun.write(Q,JSON.stringify(H,null,2)),i.set(j,{output:_,mtime:J.mtime.getTime(),dependencies:Z})}catch(K){console.warn(`Failed to cache template ${j}:`,K)}}function w(j){return new Bun.CryptoHasher("sha1").update(j).digest("hex").substring(0,16)}var __dirname="/home/runner/work/stx/stx/packages/stx/src";var{serve:a}=globalThis.Bun;import M from"fs";import S from"path";async function n(j={}){let{port:_=3000,root:Z=".",stxOptions:V={},watch:K=!0,onRequest:U,routes:Q={},middleware:J=[],on404:H,onError:W}=j,T=S.resolve(Z),I=new Map;async function y($){let G=M.statSync($),z=`${$}:${G.mtimeMs}`,Y=I.get(z);if(Y)return Y.content;let X=await Bun.file($).text(),B=X.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i),h=B?B[1]:"",p=X.match(/<script\b[^>]*>[\s\S]*?<\/script>/gi)||[],l=X.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi,""),f={__filename:$,__dirname:S.dirname($)};await N(h,f,$);let R=await E(l,f,$,V,new Set),C=[...p];if(C.length>0)if(R.match(/(<\/body>)/i)){let c=C.join(`
2
+ import{b as E,c as M,d as F,e as g,f as P,g as N}from"./chunk-mjj2b826.js";import"./chunk-h0s48z3v.js";import u from"path";import q from"fs";import L from"path";var i=new Map;async function b(z,j){try{let Z=L.resolve(j.cachePath),V=L.join(Z,`${w(z)}.html`),K=L.join(Z,`${w(z)}.meta.json`);if(!await M(V)||!await M(K))return null;let _=await Bun.file(K).text(),Q=JSON.parse(_);if(Q.cacheVersion!==j.cacheVersion)return null;if((await q.promises.stat(z)).mtime.getTime()>Q.mtime)return null;for(let J of Q.dependencies)if(await M(J)){if((await q.promises.stat(J)).mtime.getTime()>Q.mtime)return null}else return null;return await Bun.file(V).text()}catch(Z){return console.warn(`Cache error for ${z}:`,Z),null}}async function C(z,j,Z,V){try{let K=L.resolve(V.cachePath);await q.promises.mkdir(K,{recursive:!0});let _=L.join(K,`${w(z)}.html`),Q=L.join(K,`${w(z)}.meta.json`),H=await q.promises.stat(z);await Bun.write(_,j);let J={sourcePath:z,mtime:H.mtime.getTime(),dependencies:Array.from(Z),cacheVersion:V.cacheVersion,generatedAt:Date.now()};await Bun.write(Q,JSON.stringify(J,null,2)),i.set(z,{output:j,mtime:H.mtime.getTime(),dependencies:Z})}catch(K){console.warn(`Failed to cache template ${z}:`,K)}}function w(z){return new Bun.CryptoHasher("sha1").update(z).digest("hex").substring(0,16)}var{serve:n}=globalThis.Bun;import k from"fs";import S from"path";async function r(z={}){let{port:j=3000,root:Z=".",stxOptions:V={},watch:K=!0,onRequest:_,routes:Q={},middleware:H=[],on404:J,onError:W}=z,y=S.resolve(Z),A=new Map;async function T($){let G=k.statSync($),U=`${$}:${G.mtimeMs}`,Y=A.get(U);if(Y)return Y.content;let X=await Bun.file($).text(),B=X.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i),m=B?B[1]:"",p=X.match(/<script\b[^>]*>[\s\S]*?<\/script>/gi)||[],l=X.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi,""),f={__filename:$,__dirname:S.dirname($)};await F(m,f,$);let R=await E(l,f,$,V,new Set),D=[...p];if(D.length>0)if(R.match(/(<\/body>)/i)){let c=D.join(`
3
3
  `);R=R.replace(/(<\/body>)/i,`${c}
4
4
  $1`)}else R+=`
5
- ${C.join(`
6
- `)}`;return I.set(z,{content:R,mtime:G.mtimeMs}),R}async function A($){let G=M.statSync($),z=`${$}:${G.mtimeMs}`,Y=I.get(z);if(Y)return Y.content;let{content:X}=await F($,V);return I.set(z,{content:X,mtime:G.mtimeMs}),X}function O($){let G=$.startsWith("/")?$.slice(1):$,z=[G,`${G}.stx`,`${G}.md`,`${G}.html`,S.join(G,"index.stx"),S.join(G,"index.md"),S.join(G,"index.html")];for(let Y of z){let X=S.join(T,Y);if(M.existsSync(X)&&M.statSync(X).isFile())return X}return null}async function m($){let G=new URL($.url),z=async()=>{if(Q[G.pathname])return await Q[G.pathname]($);if(U){let X=await U($);if(X)return X}let Y=O(G.pathname);if(!Y){if(H)return await H($);return new Response("Not Found",{status:404})}try{let X,B="text/html";if(Y.endsWith(".stx"))X=await y(Y);else if(Y.endsWith(".md"))X=await A(Y);else if(Y.endsWith(".html"))X=await Bun.file(Y).text();else if(Y.endsWith(".css"))X=await Bun.file(Y).text(),B="text/css";else if(Y.endsWith(".js"))X=await Bun.file(Y).text(),B="text/javascript";else if(Y.endsWith(".json"))X=await Bun.file(Y).text(),B="application/json";else return new Response(Bun.file(Y));return new Response(X,{headers:{"Content-Type":B}})}catch(X){if(W)return await W(X,$);return new Response(`<h1>Error</h1><pre>${X.message}
7
- ${X.stack}</pre>`,{status:500,headers:{"Content-Type":"text/html"}})}};for(let Y=J.length-1;Y>=0;Y--){let X=J[Y],B=z;z=async()=>await X($,B)}return await z()}let x=a({port:_,fetch:m}),v=null;if(K)v=M.watch(T,{recursive:!0},($,G)=>{if(G&&(G.endsWith(".stx")||G.endsWith(".md")||G.endsWith(".html")))I.clear()});return{server:x,url:x.url.toString(),stop(){if(v)v.close();x.stop()}}}function r(j){return j}function s(j){return j}function o(j){return{name:"bun-plugin-stx",async setup(_){let Z={...P,...j},V=new Set,K=Z.webComponents?.enabled?`./${u.relative(u.dirname(_.config?.outdir||"dist"),Z.webComponents.outputDir||"dist/web-components")}`:"/web-components",U=[];if(Z.webComponents?.enabled)try{let Q=await g(Z,V);if(U.push(...Q),Z.debug&&Q.length>0)console.log(`Successfully built ${Q.length} web components`)}catch(Q){console.error("Failed to build web components:",Q)}_.onLoad({filter:/\.md$/},async({path:Q})=>{try{let{content:J,data:H}=await F(Q,Z);return{contents:`// ${Q}
8
- var content = ${JSON.stringify(J)};
9
- var data = ${JSON.stringify(H)};
5
+ ${D.join(`
6
+ `)}`;return A.set(U,{content:R,mtime:G.mtimeMs}),R}async function O($){let G=k.statSync($),U=`${$}:${G.mtimeMs}`,Y=A.get(U);if(Y)return Y.content;let{content:X}=await N($,V);return A.set(U,{content:X,mtime:G.mtimeMs}),X}function I($){let G=$.startsWith("/")?$.slice(1):$,U=[G,`${G}.stx`,`${G}.md`,`${G}.html`,S.join(G,"index.stx"),S.join(G,"index.md"),S.join(G,"index.html")];for(let Y of U){let X=S.join(y,Y);if(k.existsSync(X)&&k.statSync(X).isFile())return X}return null}async function h($){let G=new URL($.url),U=async()=>{if(Q[G.pathname])return await Q[G.pathname]($);if(_){let X=await _($);if(X)return X}let Y=I(G.pathname);if(!Y){if(J)return await J($);return new Response("Not Found",{status:404})}try{let X,B="text/html";if(Y.endsWith(".stx"))X=await T(Y);else if(Y.endsWith(".md"))X=await O(Y);else if(Y.endsWith(".html"))X=await Bun.file(Y).text();else if(Y.endsWith(".css"))X=await Bun.file(Y).text(),B="text/css";else if(Y.endsWith(".js"))X=await Bun.file(Y).text(),B="text/javascript";else if(Y.endsWith(".json"))X=await Bun.file(Y).text(),B="application/json";else return new Response(Bun.file(Y));return new Response(X,{headers:{"Content-Type":B}})}catch(X){if(W)return await W(X,$);return new Response(`<h1>Error</h1><pre>${X.message}
7
+ ${X.stack}</pre>`,{status:500,headers:{"Content-Type":"text/html"}})}};for(let Y=H.length-1;Y>=0;Y--){let X=H[Y],B=U;U=async()=>await X($,B)}return await U()}let x=n({port:j,fetch:h}),v=null;if(K)v=k.watch(y,{recursive:!0},($,G)=>{if(G&&(G.endsWith(".stx")||G.endsWith(".md")||G.endsWith(".html")))A.clear()});return{server:x,url:x.url.toString(),stop(){if(v)v.close();x.stop()}}}function s(z){return z}function o(z){return z}function e(z){return{name:"bun-plugin-stx",async setup(j){let Z={...P,...z},V=new Set,K=Z.webComponents?.enabled?`./${u.relative(u.dirname(j.config?.outdir||"dist"),Z.webComponents.outputDir||"dist/web-components")}`:"/web-components",_=[];if(Z.webComponents?.enabled)try{let Q=await g(Z,V);if(_.push(...Q),Z.debug&&Q.length>0)console.log(`Successfully built ${Q.length} web components`)}catch(Q){console.error("Failed to build web components:",Q)}j.onLoad({filter:/\.md$/},async({path:Q})=>{try{let{content:H,data:J}=await N(Q,Z);return{contents:`// ${Q}
8
+ var content = ${JSON.stringify(H)};
9
+ var data = ${JSON.stringify(J)};
10
10
 
11
11
  export { content, data };
12
12
  export { content as default };
13
- `,loader:"js"}}catch(J){console.error("Markdown Processing Error:",J);let H=`<!DOCTYPE html><html><body><h1>Markdown Error</h1><pre>${J.message||"Unknown error"}</pre></body></html>`;return{contents:`// ${Q}
14
- var content = ${JSON.stringify(H)};
13
+ `,loader:"js"}}catch(H){console.error("Markdown Processing Error:",H);let J=`<!DOCTYPE html><html><body><h1>Markdown Error</h1><pre>${H.message||"Unknown error"}</pre></body></html>`;return{contents:`// ${Q}
14
+ var content = ${JSON.stringify(J)};
15
15
  var data = {};
16
16
 
17
17
  export { content, data };
18
18
  export { content as default };
19
- `,loader:"js"}}}),_.onLoad({filter:/\.stx$/},async({path:Q})=>{try{let J=new Set;if(Z.cache&&Z.cachePath){let O=await b(Q,Z);if(O){if(Z.debug)console.log(`Using cached version of ${Q}`);return{contents:O,loader:"html"}}}let H=await Bun.file(Q).text(),W=H.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i),T=W?W[1]:"",I=H.replace(/<script\b[^>]*>[\s\S]*?<\/script>/i,""),y={__filename:Q,__dirname:u.dirname(Q),__stx:{webComponentsPath:K,builtComponents:U}};await N(T,y,Q);let A=I;if(A=await E(A,y,Q,Z,J),J.forEach((O)=>V.add(O)),Z.cache&&Z.cachePath){if(await D(Q,A,J,Z),Z.debug)console.log(`Cached template ${Q} with ${J.size} dependencies`)}return{contents:A,loader:"html"}}catch(J){return console.error("stx Plugin Error:",J),{contents:`<!DOCTYPE html><html><body><h1>stx Rendering Error</h1><pre>${J.message||String(J)}</pre></body></html>`,loader:"html"}}})}}}var R4=o;export{o as stxPlugin,n as serve,F as readMarkdownFile,R4 as default,s as createRoute,r as createMiddleware};
20
- export{R4 as a};
19
+ `,loader:"js"}}}),j.onLoad({filter:/\.stx$/},async({path:Q})=>{try{let H=new Set;if(Z.cache&&Z.cachePath){let I=await b(Q,Z);if(I){if(Z.debug)console.log(`Using cached version of ${Q}`);return{contents:I,loader:"html"}}}let J=await Bun.file(Q).text(),W=J.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i),y=W?W[1]:"",A=J.replace(/<script\b[^>]*>[\s\S]*?<\/script>/i,""),T={__filename:Q,__dirname:u.dirname(Q),__stx:{webComponentsPath:K,builtComponents:_}};await F(y,T,Q);let O=A;if(O=await E(O,T,Q,Z,H),H.forEach((I)=>V.add(I)),Z.cache&&Z.cachePath){if(await C(Q,O,H,Z),Z.debug)console.log(`Cached template ${Q} with ${H.size} dependencies`)}return{contents:O,loader:"html"}}catch(H){return console.error("stx Plugin Error:",H),{contents:`<!DOCTYPE html><html><body><h1>stx Rendering Error</h1><pre>${H.message||String(H)}</pre></body></html>`,loader:"html"}}})}}}var X7=e;export{e as stxPlugin,r as serve,N as readMarkdownFile,X7 as default,o as createRoute,s as createMiddleware};
20
+ export{X7 as a};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.1.16",
4
+ "version": "0.2.0",
5
5
  "description": "A Bun plugin that allows for using Laravel Blade-like syntax.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -67,6 +67,6 @@
67
67
  "typecheck": "bun tsc --noEmit"
68
68
  },
69
69
  "dependencies": {
70
- "@stacksjs/stx": "0.1.15"
70
+ "@stacksjs/stx": "0.1.16"
71
71
  }
72
72
  }