-
Notifications
You must be signed in to change notification settings - Fork 179
/
builder.js
92 lines (74 loc) · 2.07 KB
/
builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Builders for CSS, JS, and TS */
import { promises as fs } from 'node:fs'
import { join } from 'node:path'
import { resolve } from 'import-meta-resolve'
import { Features, bundleAsync } from 'lightningcss'
export async function getBuilder(is_esbuild) {
try {
return is_esbuild ? await import(resolve('esbuild', `file://${process.cwd()}/`)) : Bun
} catch {
throw 'Bundler not found. Please use Bun or install esbuild'
}
}
export async function buildJS(args) {
const { outdir, toname, minify, bundle } = args
const is_esbuild = args.esbuild || !process.isBun
const builder = await getBuilder(is_esbuild)
let ret
const opts = {
external: bundle ? ['../@nue/*', '/@nue/*'] : is_esbuild ? undefined : ['*'],
entryPoints: [args.path],
format: 'esm',
outdir,
bundle,
minify,
}
if (args.silent) opts.logLevel = 'silent'
if (toname) {
if (is_esbuild) {
delete opts.outdir
opts.outfile = join(outdir, toname)
} else {
opts.naming = toname
}
}
try {
ret = await builder.build(opts)
} catch (e) { ret = e }
// console.info(ret)
const error = parseError(ret)
if (error) throw error
}
export function parseError(buildResult) {
const { logs = [], errors = [] } = buildResult
let error
// Bun
if (logs.length) {
const [err] = logs
error = { text: err.message, ...err.position }
}
// esbuild
if (errors.length) {
const [err] = errors
error = { text: err.text, ...err.location }
}
if (error) {
error.title = error.text.includes('resolve') ? 'Import error' : 'Syntax error'
delete error.file
return error
}
}
export async function lightningCSS(filename, minify, opts = {}) {
let include = Features.Colors
if (opts.native_css_nesting) include |= Features.Nesting
try {
return (await bundleAsync({ filename, include, minify })).code?.toString()
} catch ({ fileName, loc, data }) {
throw {
title: 'CSS syntax error',
lineText: (await fs.readFile(fileName, 'utf-8')).split(/\r\n|\r|\n/)[loc.line - 1],
text: data.type,
...loc
}
}
}