Skip to content

Commit c029ed7

Browse files
committed
Move frontend build to esbuild
Also remove unocss from sandbox
1 parent bde82e7 commit c029ed7

File tree

18 files changed

+513
-1311
lines changed

18 files changed

+513
-1311
lines changed

build.ts

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import * as path from 'node:path'
2+
import * as url from 'node:url'
3+
import * as fs from 'node:fs'
4+
import * as assert from 'node:assert/strict'
5+
import * as threads from 'node:worker_threads'
6+
import * as esb from 'esbuild'
7+
import ts from 'typescript'
8+
9+
type Worker_Data = {
10+
is_dev: boolean,
11+
dist_dirname: string,
12+
ts_entries: string[],
13+
}
14+
15+
const filename = url.fileURLToPath(import.meta.url)
16+
// const dirname = path.dirname(filename)
17+
18+
export const DEFAULT_EXTERNAL_DEPS: string[] = [
19+
'solid-js',
20+
'solid-js/*',
21+
'@solid-devtools/shared/*',
22+
]
23+
24+
export function get_external_deps_from_pkg(pkg_filename: string): string[] {
25+
let pkg = JSON.parse(fs.readFileSync(pkg_filename, 'utf-8')) as any
26+
return [
27+
...DEFAULT_EXTERNAL_DEPS,
28+
...Object.keys({...pkg?.peerDependencies, ...pkg?.dependencies}),
29+
]
30+
}
31+
32+
export function get_is_dev_from_args(): boolean {
33+
return process.argv.includes('--watch')
34+
}
35+
36+
export function get_common_esbuild_options(is_dev: boolean, dist_dirname: string): esb.BuildOptions {
37+
return {
38+
platform: 'browser',
39+
format: 'esm',
40+
target: 'esnext',
41+
sourcemap: is_dev,
42+
outdir: dist_dirname,
43+
bundle: true,
44+
splitting: true,
45+
treeShaking: !is_dev,
46+
logLevel: is_dev ? 'debug' : 'warning',
47+
}
48+
}
49+
50+
function main() {
51+
52+
if (threads.isMainThread)
53+
return
54+
55+
/* Worker - runs the ts program */
56+
57+
const data = threads.workerData as Worker_Data
58+
59+
const port = threads.parentPort
60+
assert.ok(port != null)
61+
62+
const options = get_tsc_options(data.dist_dirname)
63+
64+
/* Watch - never terminates */
65+
if (data.is_dev) {
66+
const host = ts.createWatchCompilerHost(
67+
data.ts_entries,
68+
options,
69+
ts.sys,
70+
undefined,
71+
report_diagnostic,
72+
report_watch_status_changed,
73+
)
74+
ts.createWatchProgram(host)
75+
}
76+
/* Emit once and exit */
77+
else {
78+
let begin = performance.now()
79+
ts.createProgram(data.ts_entries, options).emit()
80+
// eslint-disable-next-line no-console
81+
console.log(`DTS complete in ${(performance.now()-begin).toFixed(2)}ms`)
82+
process.exit(0)
83+
}
84+
}
85+
86+
export async function build(
87+
options: esb.BuildOptions[],
88+
ts_entries: string[],
89+
is_dev: boolean,
90+
dist_dirname: string = path.join(process.cwd(), `dist`),
91+
): Promise<void> {
92+
93+
/* Clear dist when building to prod */
94+
if (!is_dev) {
95+
fs.rmSync(dist_dirname, {recursive: true, force: true})
96+
}
97+
98+
const worker = new threads.Worker(filename, {
99+
workerData: {is_dev, dist_dirname, ts_entries} satisfies Worker_Data
100+
})
101+
102+
worker.on('error', (error) => {
103+
// eslint-disable-next-line no-console
104+
console.error(`Worker error:`, error)
105+
})
106+
107+
/* Watch - never terminates */
108+
if (is_dev) {
109+
for (const option of options) {
110+
esb.context(option)
111+
.then(ctx => ctx.watch())
112+
}
113+
}
114+
/* Build once - wait for all to finish */
115+
else {
116+
let begin = performance.now()
117+
await Promise.all(options.map(option => esb.build(option)))
118+
// eslint-disable-next-line no-console
119+
console.log(`JS built in ${(performance.now()-begin).toFixed(2)}ms`)
120+
}
121+
}
122+
123+
const format_host: ts.FormatDiagnosticsHost = {
124+
getCurrentDirectory: () => process.cwd(),
125+
getCanonicalFileName: (fileName) => fileName,
126+
getNewLine: () => ts.sys.newLine
127+
}
128+
129+
function report_diagnostic(diagnostic: ts.Diagnostic) {
130+
// eslint-disable-next-line no-console
131+
console.error(ts.formatDiagnostic(diagnostic, format_host))
132+
}
133+
function report_diagnostics(diagnostics: ts.Diagnostic[]) {
134+
// eslint-disable-next-line no-console
135+
console.error(ts.formatDiagnostics(diagnostics, format_host))
136+
}
137+
function report_watch_status_changed(diagnostic: ts.Diagnostic) {
138+
// eslint-disable-next-line no-console
139+
console.info(ts.formatDiagnostic(diagnostic, format_host))
140+
}
141+
142+
export function get_tsc_options(dist_dirname: string): ts.CompilerOptions {
143+
144+
let ts_config_file = ts.findConfigFile(process.cwd(), ts.sys.fileExists, 'tsconfig.json')
145+
if (!ts_config_file) throw Error('tsconfig.json not found')
146+
147+
let {config, error} = ts.readConfigFile(ts_config_file, ts.sys.readFile)
148+
if (error) {
149+
report_diagnostic(error)
150+
}
151+
152+
let {options, errors} = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd())
153+
if (errors.length > 0) {
154+
report_diagnostics(errors)
155+
}
156+
157+
return {
158+
...options,
159+
outDir: dist_dirname,
160+
emitDeclarationOnly: true,
161+
noEmit: false,
162+
noEmitOnError: false,
163+
declaration: true,
164+
sourceMap: true,
165+
}
166+
}
167+
168+
169+
main()

examples/sandbox/index.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@
66
<meta name="theme-color" content="#000000" />
77
<link rel="shortcut icon" type="image/ico" href="/src/assets/favicon.ico" />
88
<title>Sandbox Playground</title>
9-
<style>
10-
body {
11-
background: #121212;
12-
color: #efefef;
13-
padding: 24px;
14-
padding-top: 82px;
15-
margin: 0;
16-
}
17-
</style>
189
</head>
1910

2011
<body>

examples/sandbox/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
"preview": "vite preview"
1212
},
1313
"devDependencies": {
14-
"@babel/plugin-syntax-typescript": "^7.23.3",
15-
"@unocss/preset-typography": "0.65.1",
1614
"solid-devtools": "workspace:^"
1715
},
1816
"dependencies": {

examples/sandbox/src/Recursive.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,34 @@ const Node: Component<NodeType> = props => {
4646
const addNode = useContext(RecursiveContext)
4747

4848
return (
49-
<div class="b-2 rounded-lg p-1 space-y-1 m-1">
49+
<div style={{
50+
"border": "1px solid #1e293b",
51+
"border-radius": "8px",
52+
"padding": "0.5rem",
53+
"margin": "1rem",
54+
"gap": "0.5rem"
55+
}}>
5056
<p>{nodeSignals.url.join(' > ')}</p>
5157
<For each={childrenProps.children}>
52-
{node => {
53-
const childProps = mergeProps(
54-
{
55-
parents: [...nodeSignals.parents, nodeProps.id],
56-
url: nodeSignals.url,
57-
},
58-
node,
59-
)
60-
return <Node {...childProps} />
61-
}}
58+
{node => {
59+
const childProps = mergeProps(
60+
{
61+
parents: [...nodeSignals.parents, nodeProps.id],
62+
url: nodeSignals.url,
63+
},
64+
node,
65+
)
66+
return <Node {...childProps} />
67+
}}
6268
</For>
6369
<button
64-
class="rounded-full bg-slate-300 p-x-2"
65-
onClick={e => addNode(nodeProps.id, nodeSignals.parents)}
70+
style={{
71+
"border-radius": "9999px",
72+
"background-color": "#1e293b"
73+
}}
74+
onClick={e => addNode(nodeProps.id, nodeSignals.parents)}
6675
>
67-
Add item
76+
Add item
6877
</button>
6978
</div>
7079
)

examples/sandbox/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import App from './App.tsx'
33
import {Overlay} from './Overlay.tsx'
44
import {ThemeProvider} from './Theme.tsx'
55

6-
import 'uno.css'
6+
import "./styles.css"
77

88
function Main() {
99
return (

examples/sandbox/src/styles.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
body {
2+
background: #121212;
3+
color: #efefef;
4+
padding: 24px;
5+
padding-top: 82px;
6+
margin: 0;
7+
}
8+
button {
9+
color: inherit;
10+
}

examples/sandbox/vite.config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import devtools from 'solid-devtools/vite'
2-
import {presetTypography} from 'unocss'
3-
import Unocss from 'unocss/vite'
42
import {defineConfig} from 'vite'
53
import solid from 'vite-plugin-solid'
64

@@ -20,9 +18,6 @@ export default defineConfig(mode => {
2018
},
2119
}),
2220
solid({hot: true, dev: true}),
23-
Unocss({
24-
presets: [presetTypography()],
25-
}),
2621
],
2722
define: {
2823
'process.env.EXT': JSON.stringify(is_ext),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"tsup-preset-solid": "^2.2.0",
4949
"turbo": "^1.10.16",
5050
"typescript": "^5.7.2",
51-
"unocss": "0.65.1",
52-
"@unocss/cli": "0.65.1",
51+
"@unocss/core": "0.65.1",
52+
"@unocss/preset-uno": "0.65.1",
5353
"vite": "6.0.3",
5454
"vite-plugin-solid": "^2.11.0",
5555
"vitest": "^2.1.8"

0 commit comments

Comments
 (0)