bun-plugin-stx 0.2.10 → 0.2.12

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 CHANGED
@@ -539,25 +539,27 @@ Web components support several configuration options:
539
539
 
540
540
  ## TypeScript Support
541
541
 
542
- stx includes TypeScript declarations for importing .stx files. Make sure your `tsconfig.json` includes the necessary configuration:
542
+ stx ships TypeScript declarations as part of the package — `*.stx` and
543
+ `*.md` module declarations, the runtime globals injected into
544
+ `<script client>` blocks (`state`, `derived`, `useStore`, `useHead`, etc.),
545
+ and the `window.stx` registry interface. They're picked up automatically
546
+ through the package's `types` entry as soon as `@stacksjs/stx` is installed.
547
+
548
+ You do **not** need to create your own `stx.d.ts` file. Just include
549
+ `.stx` files in your `tsconfig.json`:
543
550
 
544
551
  ```jsonc
545
552
  {
546
553
  "compilerOptions": {
547
- // ... your other options
548
- "types": ["bun"]
554
+ "types": ["bun"],
555
+ "moduleResolution": "bundler"
549
556
  },
550
- "files": ["src/stx.d.ts"],
551
- "include": ["**/*.ts", "**/*.d.ts", "*.stx", "./**/*.stx"]
557
+ "include": ["**/*.ts", "**/*.stx"]
552
558
  }
553
559
  ```
554
560
 
555
- Create a declaration file (`src/stx.d.ts`):
556
-
557
- ```ts
558
- // Allow importing .stx files
559
- declare module '*.stx';
560
- ```
561
+ If you have an old `stx.d.ts` declaring `*.stx` modules or runtime globals
562
+ as a workaround, it's now redundant and can be deleted.
561
563
 
562
564
  ## Server-Side Rendering Optimizations
563
565
 
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  // src/index.ts
3
3
  import path2 from "path";
4
- import { buildWebComponents, cacheTemplate, checkCache, defaultConfig, extractVariables, processClientScript, processDirectives, readMarkdownFile } from "@stacksjs/stx";
4
+ import { buildWebComponents, cacheTemplate, checkCache, classifyScript, defaultConfig, extractVariables, processClientScript, processDirectives, readMarkdownFile } from "@stacksjs/stx";
5
5
 
6
6
  // src/watch.ts
7
7
  import fs from "fs";
@@ -42,10 +42,10 @@ function createWatcher(options = {}) {
42
42
  const pendingChanges = [];
43
43
  const handleChanges = debounce(() => {
44
44
  if (pendingChanges.length > 0 && options.onChange) {
45
- for (const event of pendingChanges) {
45
+ const batch = pendingChanges.splice(0);
46
+ for (const event of batch) {
46
47
  options.onChange(event);
47
48
  }
48
- pendingChanges.length = 0;
49
49
  }
50
50
  }, debounceDelay);
51
51
  function watchDirectory(dirPath) {
@@ -234,13 +234,13 @@ async function startWatchMode(args) {
234
234
  }
235
235
  }
236
236
  });
237
- process.on("SIGINT", () => {
237
+ process.once("SIGINT", () => {
238
238
  console.log(`
239
239
  \x1B[36m[stx]\x1B[0m Stopping watch mode...`);
240
240
  watcher.close();
241
241
  process.exit(0);
242
242
  });
243
- process.on("SIGTERM", () => {
243
+ process.once("SIGTERM", () => {
244
244
  watcher.close();
245
245
  process.exit(0);
246
246
  });
@@ -386,19 +386,18 @@ export { content as default };
386
386
  const serverScripts = [];
387
387
  const signalsScripts = [];
388
388
  let scriptMatch;
389
- const usesSignalsAPI = (content2) => /\b(?:state|derived|effect|batch|ref|reactive|computed|watch|watchEffect)\s*(?:<[^>]*>)?\s*\(/.test(content2);
390
389
  while ((scriptMatch = scriptRegex.exec(content)) !== null) {
391
- const attrs = scriptMatch[1];
392
- const scriptContent = scriptMatch[2];
393
- const fullScript = scriptMatch[0];
394
- const isClientScript = attrs.includes("client") || attrs.includes('type="module"') || attrs.includes("src=");
395
- const isSignalsScript = usesSignalsAPI(scriptContent);
396
- if (isSignalsScript) {
397
- signalsScripts.push(fullScript);
398
- } else if (isClientScript) {
399
- clientScripts.push(fullScript);
400
- } else {
401
- serverScripts.push(scriptContent);
390
+ const classified = classifyScript(scriptMatch[1], scriptMatch[2], scriptMatch[0]);
391
+ switch (classified.type) {
392
+ case "signals":
393
+ signalsScripts.push(classified.fullTag);
394
+ break;
395
+ case "client":
396
+ clientScripts.push(classified.fullTag);
397
+ break;
398
+ case "server":
399
+ serverScripts.push(classified.content);
400
+ break;
402
401
  }
403
402
  }
404
403
  let templateContent = workingContent;
@@ -431,7 +430,7 @@ export { content as default };
431
430
  const contentMatch = fullScript.match(/<script\b[^>]*>([\s\S]*?)<\/script>/);
432
431
  if (!contentMatch)
433
432
  return fullScript;
434
- return processClientScript(contentMatch[1], { eventBindings });
433
+ return processClientScript(contentMatch[1], { eventBindings, templateContent: output });
435
434
  });
436
435
  const scriptsHtml = transformedScripts.join(`
437
436
  `);