1
- import { exec } from 'child_process'
2
1
import { globby } from 'globby'
3
2
import fs from 'node:fs/promises'
4
3
import { register } from 'node:module'
@@ -7,10 +6,16 @@ import { createElement } from 'react'
7
6
import { renderToString } from 'react-dom/server'
8
7
import assert from 'node:assert'
9
8
import { match } from './util.js'
9
+ import { evaluate , build as _build } from './esbuild.js'
10
+ import { $ , DefaultLayout } from './util.js'
10
11
11
- register ( './jsx-loader.js' , import . meta. url )
12
- register ( './orga-loader.js' , import . meta. url )
13
- register ( './raw-loader.js' , import . meta. url )
12
+ const USE_NODE = false
13
+
14
+ if ( USE_NODE ) {
15
+ register ( './jsx-loader.js' , import . meta. url )
16
+ register ( './orga-loader.js' , import . meta. url )
17
+ register ( './raw-loader.js' , import . meta. url )
18
+ }
14
19
15
20
const defaultConfig = {
16
21
outDir : 'out' ,
@@ -58,12 +63,15 @@ async function iter(dirPath, context) {
58
63
59
64
let components = { ...context . components }
60
65
let Layout = context . Layout
61
- const ignore = context . ignore || / n o d e _ m o d u l e s /
66
+ let ignore = context . ignore || / n o d e _ m o d u l e s /
67
+ if ( ! Array . isArray ( ignore ) ) {
68
+ ignore = [ ignore ]
69
+ }
62
70
63
71
const files = await fs . readdir ( dirPath )
64
72
65
73
for ( const file of files ) {
66
- if ( match ( file , ignore ) ) {
74
+ if ( match ( file , ... ignore ) ) {
67
75
continue
68
76
}
69
77
const filePath = path . join ( dirPath , file )
@@ -103,7 +111,7 @@ async function iter(dirPath, context) {
103
111
104
112
// write regex to match .org and .tsx, .jsx files, javascript code only
105
113
106
- if ( match ( file , [ / \. ( o r g ) $ / , / \. ( j | t ) s x $ / ] ) ) {
114
+ if ( match ( file , / \. ( o r g ) $ / , / \. ( j | t ) s x $ / ) ) {
107
115
const module = await _import ( filePath )
108
116
const {
109
117
default : /** @type import('@orgajs/orgx').OrgContent */ Content ,
@@ -200,7 +208,11 @@ async function _import(...files) {
200
208
const file = found [ 0 ]
201
209
const fullPath = path . isAbsolute ( file ) ? file : path . join ( process . cwd ( ) , file )
202
210
const { mtime } = await fs . stat ( fullPath )
203
- return await import ( `${ fullPath } ?version=${ mtime . getTime ( ) } ` )
211
+ if ( USE_NODE ) {
212
+ return await import ( `${ fullPath } ?version=${ mtime . getTime ( ) } ` )
213
+ } else {
214
+ return await evaluate ( fullPath )
215
+ }
204
216
}
205
217
206
218
/**
@@ -217,46 +229,3 @@ export async function loadConfig() {
217
229
const config = await _import ( 'orga.config.(j|t)s' )
218
230
return { ...defaultConfig , ...config }
219
231
}
220
-
221
- /**
222
- * @param {string } cmd
223
- */
224
- async function $ ( cmd ) {
225
- return new Promise ( ( resolve , reject ) => {
226
- exec ( cmd , ( err , stdout , stderr ) => {
227
- if ( err ) {
228
- reject ( err )
229
- }
230
- if ( stderr ) {
231
- console . error ( stderr )
232
- }
233
- console . log ( stdout )
234
- resolve ( stdout )
235
- } )
236
- } )
237
- }
238
-
239
- /**
240
- * Default layout
241
- * @param {Object } props
242
- * @param {string|undefined } props.title
243
- * @param {import('react').ReactNode } props.children
244
- * @returns {React.JSX.Element }
245
- */
246
- function DefaultLayout ( { title, children } ) {
247
- return createElement (
248
- 'html' ,
249
- { lang : 'en' } ,
250
- createElement (
251
- 'head' ,
252
- { } ,
253
- createElement ( 'meta' , { charSet : 'utf-8' } ) ,
254
- createElement ( 'meta' , {
255
- name : 'viewport' ,
256
- content : 'width=device-width, initial-scale=1'
257
- } ) ,
258
- title && createElement ( 'title' , { } , title )
259
- ) ,
260
- createElement ( 'body' , { } , children )
261
- )
262
- }
0 commit comments