Skip to content

Commit 99d76a3

Browse files
authored
release orga-build (#251)
* add source map for esbuild * migrate orga-build to esbuild * upgrade changeset * change version to 0.0.1 * add changeset * fix build
1 parent 4c7ee61 commit 99d76a3

14 files changed

+593
-165
lines changed

.changeset/moody-sheep-carry.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'orga-build': patch
3+
'@orgajs/esbuild': patch
4+
---
5+
6+
release orga-build, add source map to @orgajs/esbuild

docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@codemirror/language": "^6.10.8",
2020
"@codemirror/state": "^6.5.2",
2121
"@codemirror/view": "^6.36.2",
22-
"@orgajs/build": "workspace:^",
22+
"orga-build": "workspace:^",
2323
"@orgajs/editor": "workspace:^",
2424
"@orgajs/orgx": "workspace:^",
2525
"codemirror": "^6.0.1",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "orgajs",
33
"private": true,
44
"devDependencies": {
5-
"@changesets/cli": "^2.26.2",
5+
"@changesets/cli": "^2.28.1",
66
"@types/node": "^22.13.4",
77
"prettier": "^2.8.8",
88
"turbo": "^1.10.13",

packages/esbuild/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import fs from 'node:fs/promises'
99
import { VFile } from 'vfile'
1010
import { compile } from '@orgajs/orgx'
11+
import { SourceMapGenerator } from 'source-map'
1112

1213
const name = '@orgajs/esbuild'
1314

@@ -51,7 +52,7 @@ function esbuild(options) {
5152
)
5253

5354
const file = new VFile({ path: data.path, value: document })
54-
const code = await compile(file, options)
55+
const code = await compile(file, { ...options, SourceMapGenerator })
5556
const contents =
5657
String(code) +
5758
'\n' +

packages/esbuild/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"author": "Xiaoxing Hu <[email protected]>",
2222
"dependencies": {
2323
"@orgajs/orgx": "workspace:^",
24+
"source-map": "^0.7.4",
2425
"vfile": "^6.0.3"
2526
},
2627
"devDependencies": {

packages/orga-build/cli.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import { argv } from 'node:process'
44
import { parseArgs } from 'node:util'
55
import { watch } from './lib/watch.js'
6-
import { build, loadConfig, clean } from './lib/build.js'
6+
import { loadConfig, clean } from './lib/build.js'
7+
import { build } from './lib/esbuild/index.js'
78
import { serve } from './lib/serve.js'
89

910
const { values, positionals } = parseArgs({

packages/orga-build/lib/build.js

+20-51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { exec } from 'child_process'
21
import { globby } from 'globby'
32
import fs from 'node:fs/promises'
43
import { register } from 'node:module'
@@ -7,10 +6,16 @@ import { createElement } from 'react'
76
import { renderToString } from 'react-dom/server'
87
import assert from 'node:assert'
98
import { match } from './util.js'
9+
import { evaluate, build as _build } from './esbuild.js'
10+
import { $, DefaultLayout } from './util.js'
1011

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+
}
1419

1520
const defaultConfig = {
1621
outDir: 'out',
@@ -58,12 +63,15 @@ async function iter(dirPath, context) {
5863

5964
let components = { ...context.components }
6065
let Layout = context.Layout
61-
const ignore = context.ignore || /node_modules/
66+
let ignore = context.ignore || /node_modules/
67+
if (!Array.isArray(ignore)) {
68+
ignore = [ignore]
69+
}
6270

6371
const files = await fs.readdir(dirPath)
6472

6573
for (const file of files) {
66-
if (match(file, ignore)) {
74+
if (match(file, ...ignore)) {
6775
continue
6876
}
6977
const filePath = path.join(dirPath, file)
@@ -103,7 +111,7 @@ async function iter(dirPath, context) {
103111

104112
// write regex to match .org and .tsx, .jsx files, javascript code only
105113

106-
if (match(file, [/\.(org)$/, /\.(j|t)sx$/])) {
114+
if (match(file, /\.(org)$/, /\.(j|t)sx$/)) {
107115
const module = await _import(filePath)
108116
const {
109117
default: /** @type import('@orgajs/orgx').OrgContent */ Content,
@@ -200,7 +208,11 @@ async function _import(...files) {
200208
const file = found[0]
201209
const fullPath = path.isAbsolute(file) ? file : path.join(process.cwd(), file)
202210
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+
}
204216
}
205217

206218
/**
@@ -217,46 +229,3 @@ export async function loadConfig() {
217229
const config = await _import('orga.config.(j|t)s')
218230
return { ...defaultConfig, ...config }
219231
}
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-
}

packages/orga-build/lib/esbuild.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import assert from 'assert'
2+
import * as esbuild from 'esbuild'
3+
import esbuildOrga from '@orgajs/esbuild'
4+
5+
/**
6+
* Evaluate an org/jsx/tsx/ts file. Returns as a module.
7+
* It's like a dynamic import, but without relying on nodejs.
8+
* @param {string} filePath
9+
* @returns {Promise<any>}
10+
*/
11+
export async function evaluate(filePath) {
12+
const result = await esbuild.build({
13+
entryPoints: [filePath],
14+
bundle: true,
15+
format: 'esm',
16+
platform: 'node',
17+
target: 'esnext',
18+
jsx: 'automatic',
19+
write: false,
20+
plugins: [esbuildOrga()],
21+
loader: {
22+
'.jsx': 'jsx',
23+
'.tsx': 'tsx'
24+
}
25+
})
26+
27+
const files = result.outputFiles
28+
assert(files.length === 1, 'Expected only one output file')
29+
const code = files[0].text
30+
return await new Function(
31+
`return import("data:application/javascript,${encodeURIComponent(code)}")`
32+
)()
33+
}
34+
35+
/**
36+
* @param {string} pattern
37+
*/
38+
export async function build(pattern) {
39+
return await esbuild.build({
40+
entryPoints: [pattern],
41+
entryNames: '[dir]/_/[name]',
42+
bundle: true,
43+
format: 'esm',
44+
platform: 'node',
45+
target: 'esnext',
46+
jsx: 'automatic',
47+
// write: false,
48+
outdir: '.build',
49+
plugins: [esbuildOrga()],
50+
loader: {
51+
'.jsx': 'jsx',
52+
'.tsx': 'tsx'
53+
}
54+
})
55+
}
56+
57+
export async function b() {}

0 commit comments

Comments
 (0)