Skip to content

Commit 4d06adc

Browse files
committed
chore: wip
1 parent 96794b7 commit 4d06adc

37 files changed

+412
-394
lines changed

packages/bun-plugin/src/index.ts

Lines changed: 105 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,139 +4,141 @@ import type { BunPlugin } from 'bun'
44
import path from 'node:path'
55
import { buildWebComponents, cacheTemplate, checkCache, defaultConfig, extractVariables, processDirectives, readMarkdownFile } from '@stacksjs/stx'
66

7-
export const plugin: BunPlugin = {
8-
name: 'bun-plugin-stx',
9-
async setup(build) {
10-
// Extract options from config or use defaults
11-
const options: StxOptions = {
12-
...defaultConfig,
13-
...(build.config as any)?.stx,
14-
}
15-
16-
// Track all dependencies for web component building
17-
const allDependencies = new Set<string>()
18-
19-
// Get web components output path
20-
const webComponentsPath = options.webComponents?.enabled
21-
? `./${path.relative(path.dirname(build.config?.outdir || 'dist'), options.webComponents.outputDir || 'dist/web-components')}`
22-
: '/web-components'
23-
24-
// Build web components if enabled
25-
const builtComponents: string[] = []
26-
if (options.webComponents?.enabled) {
27-
try {
28-
const components = await buildWebComponents(options, allDependencies)
29-
builtComponents.push(...components)
30-
if (options.debug && components.length > 0) {
31-
console.log(`Successfully built ${components.length} web components`)
32-
}
7+
export function stxPlugin(userOptions?: StxOptions): BunPlugin {
8+
return {
9+
name: 'bun-plugin-stx',
10+
async setup(build) {
11+
// Merge user options with defaults
12+
const options: StxOptions = {
13+
...defaultConfig,
14+
...userOptions,
3315
}
34-
catch (error) {
35-
console.error('Failed to build web components:', error)
16+
17+
// Track all dependencies for web component building
18+
const allDependencies = new Set<string>()
19+
20+
// Get web components output path
21+
const webComponentsPath = options.webComponents?.enabled
22+
? `./${path.relative(path.dirname(build.config?.outdir || 'dist'), options.webComponents.outputDir || 'dist/web-components')}`
23+
: '/web-components'
24+
25+
// Build web components if enabled
26+
const builtComponents: string[] = []
27+
if (options.webComponents?.enabled) {
28+
try {
29+
const components = await buildWebComponents(options, allDependencies)
30+
builtComponents.push(...components)
31+
if (options.debug && components.length > 0) {
32+
console.log(`Successfully built ${components.length} web components`)
33+
}
34+
}
35+
catch (error) {
36+
console.error('Failed to build web components:', error)
37+
}
3638
}
37-
}
3839

39-
// Handler for .md files
40-
build.onLoad({ filter: /\.md$/ }, async ({ path: filePath }) => {
41-
try {
40+
// Handler for .md files
41+
build.onLoad({ filter: /\.md$/ }, async ({ path: filePath }) => {
42+
try {
4243
// Process the markdown file with frontmatter
43-
const { content: htmlContent, data: frontmatter } = await readMarkdownFile(filePath, options)
44+
const { content: htmlContent, data: frontmatter } = await readMarkdownFile(filePath, options)
4445

45-
// Create a module that exports both the rendered HTML and the frontmatter data
46-
const jsModule = `
46+
// Create a module that exports both the rendered HTML and the frontmatter data
47+
const jsModule = `
4748
export const content = ${JSON.stringify(htmlContent)};
4849
export const data = ${JSON.stringify(frontmatter)};
4950
export default content;
5051
`
5152

52-
return {
53-
contents: jsModule,
54-
loader: 'js',
53+
return {
54+
contents: jsModule,
55+
loader: 'js',
56+
}
5557
}
56-
}
57-
catch (error: any) {
58-
console.error('Markdown Processing Error:', error)
59-
return {
60-
contents: `export const content = "Error processing markdown: ${error.message?.replace(/"/g, '\\"') || 'Unknown error'}";
58+
catch (error: any) {
59+
console.error('Markdown Processing Error:', error)
60+
return {
61+
contents: `export const content = "Error processing markdown: ${error.message?.replace(/"/g, '\\"') || 'Unknown error'}";
6162
export const data = {};
6263
export default content;`,
63-
loader: 'js',
64+
loader: 'js',
65+
}
6466
}
65-
}
66-
})
67+
})
6768

68-
build.onLoad({ filter: /\.stx$/ }, async ({ path: filePath }) => {
69-
try {
69+
build.onLoad({ filter: /\.stx$/ }, async ({ path: filePath }) => {
70+
try {
7071
// Track dependencies for caching
71-
const dependencies = new Set<string>()
72-
73-
// Check for cached content if caching is enabled
74-
if (options.cache && options.cachePath) {
75-
const cachedOutput = await checkCache(filePath, options)
76-
if (cachedOutput) {
77-
if (options.debug) {
78-
console.log(`Using cached version of ${filePath}`)
79-
}
80-
return {
81-
contents: cachedOutput,
82-
loader: 'html',
72+
const dependencies = new Set<string>()
73+
74+
// Check for cached content if caching is enabled
75+
if (options.cache && options.cachePath) {
76+
const cachedOutput = await checkCache(filePath, options)
77+
if (cachedOutput) {
78+
if (options.debug) {
79+
console.log(`Using cached version of ${filePath}`)
80+
}
81+
return {
82+
contents: cachedOutput,
83+
loader: 'html',
84+
}
8385
}
8486
}
85-
}
8687

87-
const content = await Bun.file(filePath).text()
88+
const content = await Bun.file(filePath).text()
8889

89-
// Extract script and template sections
90-
const scriptMatch = content.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i)
91-
const scriptContent = scriptMatch ? scriptMatch[1] : ''
92-
const templateContent = content.replace(/<script\b[^>]*>[\s\S]*?<\/script>/i, '')
90+
// Extract script and template sections
91+
const scriptMatch = content.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i)
92+
const scriptContent = scriptMatch ? scriptMatch[1] : ''
93+
const templateContent = content.replace(/<script\b[^>]*>[\s\S]*?<\/script>/i, '')
9394

94-
// Create a sandbox environment to execute the script
95-
const context: Record<string, any> = {
95+
// Create a sandbox environment to execute the script
96+
const context: Record<string, any> = {
9697
// Add some useful globals
97-
__filename: filePath,
98-
__dirname: path.dirname(filePath),
99-
// Add stx config info
100-
__stx: {
101-
webComponentsPath,
102-
builtComponents,
103-
},
104-
}
98+
__filename: filePath,
99+
__dirname: path.dirname(filePath),
100+
// Add stx config info
101+
__stx: {
102+
webComponentsPath,
103+
builtComponents,
104+
},
105+
}
105106

106-
// Execute script content to extract variables
107-
await extractVariables(scriptContent, context, filePath)
107+
// Execute script content to extract variables
108+
await extractVariables(scriptContent, context, filePath)
108109

109-
// Process template directives
110-
let output = templateContent
110+
// Process template directives
111+
let output = templateContent
111112

112-
// Process all directives
113-
output = await processDirectives(output, context, filePath, options, dependencies)
113+
// Process all directives
114+
output = await processDirectives(output, context, filePath, options, dependencies)
114115

115-
// Track dependencies for this file
116-
dependencies.forEach(dep => allDependencies.add(dep))
116+
// Track dependencies for this file
117+
dependencies.forEach(dep => allDependencies.add(dep))
117118

118-
// Cache the processed output if caching is enabled
119-
if (options.cache && options.cachePath) {
120-
await cacheTemplate(filePath, output, dependencies, options)
121-
if (options.debug) {
122-
console.log(`Cached template ${filePath} with ${dependencies.size} dependencies`)
119+
// Cache the processed output if caching is enabled
120+
if (options.cache && options.cachePath) {
121+
await cacheTemplate(filePath, output, dependencies, options)
122+
if (options.debug) {
123+
console.log(`Cached template ${filePath} with ${dependencies.size} dependencies`)
124+
}
123125
}
124-
}
125126

126-
return {
127-
contents: output,
128-
loader: 'html',
127+
return {
128+
contents: output,
129+
loader: 'html',
130+
}
129131
}
130-
}
131-
catch (error: any) {
132-
console.error('stx Plugin Error:', error)
133-
return {
134-
contents: `<!DOCTYPE html><html><body><h1>stx Rendering Error</h1><pre>${error.message || String(error)}</pre></body></html>`,
135-
loader: 'html',
132+
catch (error: any) {
133+
console.error('stx Plugin Error:', error)
134+
return {
135+
contents: `<!DOCTYPE html><html><body><h1>stx Rendering Error</h1><pre>${error.message || String(error)}</pre></body></html>`,
136+
loader: 'html',
137+
}
136138
}
137-
}
138-
})
139-
},
139+
})
140+
},
141+
}
140142
}
141143

142-
export default plugin
144+
export default stxPlugin

packages/bun-plugin/test/plugin-setup.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('BUN-PLUGIN: Plugin Setup & Configuration', () => {
3030
const result = await Bun.build({
3131
entrypoints: [testFile],
3232
outdir: OUTPUT_DIR,
33-
plugins: [plugin],
33+
plugins: [plugin()],
3434
})
3535

3636
expect(result.success).toBe(true)
@@ -50,7 +50,7 @@ describe('BUN-PLUGIN: Plugin Setup & Configuration', () => {
5050
const result = await Bun.build({
5151
entrypoints: [testFile],
5252
outdir: OUTPUT_DIR,
53-
plugins: [plugin],
53+
plugins: [plugin()],
5454
stx: {
5555
debug: true,
5656
cache: false,
@@ -65,8 +65,9 @@ describe('BUN-PLUGIN: Plugin Setup & Configuration', () => {
6565

6666
test('should register plugin in Bun build', async () => {
6767
// Test that the plugin name is correctly registered
68-
expect(plugin.name).toBe('bun-plugin-stx')
69-
expect(typeof plugin.setup).toBe('function')
68+
const pluginInstance = plugin()
69+
expect(pluginInstance.name).toBe('bun-plugin-stx')
70+
expect(typeof pluginInstance.setup).toBe('function')
7071
})
7172

7273
test('should handle web components configuration', async () => {
@@ -82,7 +83,7 @@ describe('BUN-PLUGIN: Plugin Setup & Configuration', () => {
8283
const result = await Bun.build({
8384
entrypoints: [testFile],
8485
outdir: OUTPUT_DIR,
85-
plugins: [plugin],
86+
plugins: [plugin()],
8687
stx: {
8788
webComponents: {
8889
enabled: true,
@@ -116,7 +117,7 @@ describe('BUN-PLUGIN: Plugin Setup & Configuration', () => {
116117
const result = await Bun.build({
117118
entrypoints: [testFile],
118119
outdir: OUTPUT_DIR,
119-
plugins: [plugin],
120+
plugins: [plugin()],
120121
stx: {
121122
// Intentionally invalid config for testing - should not cause errors
122123
invalidOption: 'invalid',
@@ -135,7 +136,7 @@ describe('BUN-PLUGIN: Plugin Setup & Configuration', () => {
135136
const result = await Bun.build({
136137
entrypoints: [testFile],
137138
outdir: OUTPUT_DIR,
138-
plugins: [plugin],
139+
plugins: [plugin()],
139140
// No config provided
140141
})
141142

packages/stx/bin/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ else {
734734
compile?: boolean
735735
root?: string
736736
verbose?: boolean
737+
port?: string | number
738+
timeout?: string | number
737739
}) => {
738740
try {
739741
// Validate port parameter if provided

packages/stx/src/types/bun.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/stx/test/basic/stx-bun-serve.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('stx with Bun.serve direct imports', () => {
8686
path.join(TEMPLATES_DIR, "dashboard.stx")
8787
],
8888
outdir: path.join(import.meta.dir, "dist"),
89-
plugins: [stxPlugin]
89+
plugins: [stxPlugin()]
9090
});
9191
9292
// Get HTML content

packages/stx/test/basic/stx-dom.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('stx DOM Interaction Tests', () => {
7777
const result = await Bun.build({
7878
entrypoints: [template],
7979
outdir: OUTPUT_DIR,
80-
plugins: [stxPlugin],
80+
plugins: [stxPlugin()],
8181
})
8282

8383
expect(result.success).toBe(true)
@@ -117,7 +117,7 @@ describe('stx DOM Interaction Tests', () => {
117117
const result = await Bun.build({
118118
entrypoints: [template],
119119
outdir: OUTPUT_DIR,
120-
plugins: [stxPlugin],
120+
plugins: [stxPlugin()],
121121
})
122122

123123
const htmlOutput = result.outputs.find(o => o.path.endsWith('.html'))
@@ -192,7 +192,7 @@ describe('stx DOM Interaction Tests', () => {
192192
const result = await Bun.build({
193193
entrypoints: [eventTemplate],
194194
outdir: OUTPUT_DIR,
195-
plugins: [stxPlugin],
195+
plugins: [stxPlugin()],
196196
})
197197

198198
const htmlOutput = result.outputs.find(o => o.path.endsWith('.html'))

packages/stx/test/basic/stx-import.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function createTestServer(routes: Record<string, string>) {
2424
const result = await Bun.build({
2525
entrypoints: [template],
2626
outdir: OUTPUT_DIR,
27-
plugins: [stxPlugin],
27+
plugins: [stxPlugin()],
2828
})
2929

3030
const outputs = result.outputs || []

packages/stx/test/basic/stx-integration.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('stx Template Integration Tests', () => {
6868
const result = await Bun.build({
6969
entrypoints: [template],
7070
outdir: OUTPUT_DIR,
71-
plugins: [stxPlugin],
71+
plugins: [stxPlugin()],
7272
})
7373

7474
// Verify build was successful
@@ -132,7 +132,7 @@ describe('stx Template Integration Tests', () => {
132132
const result = await Bun.build({
133133
entrypoints: [template],
134134
outdir: OUTPUT_DIR,
135-
plugins: [stxPlugin],
135+
plugins: [stxPlugin()],
136136
})
137137

138138
expect(result.success).toBe(true)
@@ -192,7 +192,7 @@ describe('stx Template Integration Tests', () => {
192192
const result = await Bun.build({
193193
entrypoints: [conditionalTemplate],
194194
outdir: OUTPUT_DIR,
195-
plugins: [stxPlugin],
195+
plugins: [stxPlugin()],
196196
})
197197

198198
expect(result.success).toBe(true)

packages/stx/test/basic/stx-server.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class MockServer {
4444
const result = await Bun.build({
4545
entrypoints: [template],
4646
outdir: OUTPUT_DIR,
47-
plugins: [stxPlugin],
47+
plugins: [stxPlugin()],
4848
define: {
4949
'process.env.NODE_ENV': this.development ? '"development"' : '"production"',
5050
},

0 commit comments

Comments
 (0)