@@ -4,139 +4,141 @@ import type { BunPlugin } from 'bun'
4
4
import path from 'node:path'
5
5
import { buildWebComponents , cacheTemplate , checkCache , defaultConfig , extractVariables , processDirectives , readMarkdownFile } from '@stacksjs/stx'
6
6
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 ,
33
15
}
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
+ }
36
38
}
37
- }
38
39
39
- // Handler for .md files
40
- build . onLoad ( { filter : / \. m d $ / } , async ( { path : filePath } ) => {
41
- try {
40
+ // Handler for .md files
41
+ build . onLoad ( { filter : / \. m d $ / } , async ( { path : filePath } ) => {
42
+ try {
42
43
// 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 )
44
45
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 = `
47
48
export const content = ${ JSON . stringify ( htmlContent ) } ;
48
49
export const data = ${ JSON . stringify ( frontmatter ) } ;
49
50
export default content;
50
51
`
51
52
52
- return {
53
- contents : jsModule ,
54
- loader : 'js' ,
53
+ return {
54
+ contents : jsModule ,
55
+ loader : 'js' ,
56
+ }
55
57
}
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' } ";
61
62
export const data = {};
62
63
export default content;` ,
63
- loader : 'js' ,
64
+ loader : 'js' ,
65
+ }
64
66
}
65
- }
66
- } )
67
+ } )
67
68
68
- build . onLoad ( { filter : / \. s t x $ / } , async ( { path : filePath } ) => {
69
- try {
69
+ build . onLoad ( { filter : / \. s t x $ / } , async ( { path : filePath } ) => {
70
+ try {
70
71
// 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
+ }
83
85
}
84
86
}
85
- }
86
87
87
- const content = await Bun . file ( filePath ) . text ( )
88
+ const content = await Bun . file ( filePath ) . text ( )
88
89
89
- // Extract script and template sections
90
- const scriptMatch = content . match ( / < s c r i p t \b [ ^ > ] * > ( [ \s \S ] * ?) < \/ s c r i p t > / i)
91
- const scriptContent = scriptMatch ? scriptMatch [ 1 ] : ''
92
- const templateContent = content . replace ( / < s c r i p t \b [ ^ > ] * > [ \s \S ] * ?< \/ s c r i p t > / i, '' )
90
+ // Extract script and template sections
91
+ const scriptMatch = content . match ( / < s c r i p t \b [ ^ > ] * > ( [ \s \S ] * ?) < \/ s c r i p t > / i)
92
+ const scriptContent = scriptMatch ? scriptMatch [ 1 ] : ''
93
+ const templateContent = content . replace ( / < s c r i p t \b [ ^ > ] * > [ \s \S ] * ?< \/ s c r i p t > / i, '' )
93
94
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 > = {
96
97
// 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
+ }
105
106
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 )
108
109
109
- // Process template directives
110
- let output = templateContent
110
+ // Process template directives
111
+ let output = templateContent
111
112
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 )
114
115
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 ) )
117
118
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
+ }
123
125
}
124
- }
125
126
126
- return {
127
- contents : output ,
128
- loader : 'html' ,
127
+ return {
128
+ contents : output ,
129
+ loader : 'html' ,
130
+ }
129
131
}
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
+ }
136
138
}
137
- }
138
- } )
139
- } ,
139
+ } )
140
+ } ,
141
+ }
140
142
}
141
143
142
- export default plugin
144
+ export default stxPlugin
0 commit comments