bun-plugin-stx 0.2.0 → 0.2.4

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/dist/index.js CHANGED
@@ -1,20 +1,22 @@
1
1
  // @bun
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
- `);R=R.replace(/(<\/body>)/i,`${c}
4
- $1`)}else R+=`
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
-
11
- export { content, data };
12
- export { content as default };
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
- var data = {};
16
-
17
- export { content, data };
18
- export { content as default };
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};
2
+ import {
3
+ createMiddleware,
4
+ createRoute,
5
+ createWatcher,
6
+ readMarkdownFile,
7
+ src_default,
8
+ startWatchMode,
9
+ stxPlugin,
10
+ watchAndBuild
11
+ } from "./chunk-ymdk9x8z.js";
12
+ import"./chunk-eecw9x2f.js";
13
+ export {
14
+ watchAndBuild,
15
+ stxPlugin,
16
+ startWatchMode,
17
+ readMarkdownFile,
18
+ src_default as default,
19
+ createWatcher,
20
+ createRoute,
21
+ createMiddleware
22
+ };
package/dist/preload.js CHANGED
@@ -1,2 +1,10 @@
1
1
  // @bun
2
- import{a as c}from"./index.js";import"./chunk-mjj2b826.js";import"./chunk-h0s48z3v.js";var{plugin:j}=globalThis.Bun;j(c());console.log("\u2705 stx plugin loaded - .stx files can now be imported");
2
+ import {
3
+ src_default
4
+ } from "./chunk-ymdk9x8z.js";
5
+ import"./chunk-eecw9x2f.js";
6
+
7
+ // src/preload.ts
8
+ var {plugin } = globalThis.Bun;
9
+ plugin(src_default());
10
+ console.log("\u2705 stx plugin loaded - .stx files can now be imported");
package/dist/serve.d.ts CHANGED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Start the STX development server
3
+ * @param options Server options with patterns and port
4
+ */
5
+ export declare function serve(options: ServeOptions): Promise<void>;
6
+ export declare interface ServeOptions {
7
+ patterns: string[]
8
+ port?: number
9
+ componentsDir?: string
10
+ layoutsDir?: string
11
+ partialsDir?: string
12
+ }