Skip to content

Commit b99a0e0

Browse files
committed
ci: replace esbuild with rspack
1 parent 35dd767 commit b99a0e0

3 files changed

Lines changed: 98 additions & 19 deletions

File tree

builder/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
},
1111
"devDependencies": {
1212
"@npmcli/package-json": "^7.0.5",
13+
"@rspack/core": "^1.7.11",
1314
"@types/node": "^24.12.2",
1415
"@types/npmcli__package-json": "^4.0.4",
1516
"@typescript-eslint/eslint-plugin": "^8.58.0",
1617
"@typescript-eslint/parser": "^8.58.0",
1718
"@typescriptprime/parsing": "^2.0.1",
1819
"chokidar": "^5.0.0",
19-
"esbuild": "^0.28.0",
2020
"eslint": "^10.2.0",
21+
"memfs": "^4.57.1",
2122
"tldts": "^7.0.27",
2223
"tsx": "^4.21.0",
2324
"typescript": "^6.0.2",

builder/source/build.ts

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import * as ESBuild from 'esbuild'
1+
import * as RsPack from '@rspack/core'
2+
import * as Memfs from 'memfs'
23
import * as Zod from 'zod'
34
import * as Process from 'node:process'
45
import * as Path from 'node:path'
56
import PackageJson from '@npmcli/package-json'
67
import { CreateBanner } from './banner/index.js'
78
import { SafeInitCwd } from './utils/safe-init-cwd.js'
9+
import { RunCompiler } from './utils/awaited-rspack.js'
810

911
export type BuildOptions = {
1012
Minify: boolean
@@ -45,25 +47,87 @@ export async function Build(OptionsParam?: BuildOptions): Promise<void> {
4547
}
4648
})
4749

48-
const WorkerCode = await ESBuild.build({
49-
entryPoints: [Path.resolve(ProjectRoot, 'userscript', 'source', 'ocr-worker.ts')],
50-
bundle: true,
51-
minify: Options.Minify,
52-
write: false,
53-
target: ['es2024', 'chrome119', 'firefox142', 'safari26']
54-
})
50+
const TypescriptLoader = {
51+
test: /\.ts$/,
52+
exclude: ['/node_modules/'],
53+
loader: 'builtin:swc-loader',
54+
options: {
55+
jsc: {
56+
parser: {
57+
syntax: 'typescript',
58+
decorators: true,
59+
dynamicImport: true
60+
}
61+
}
62+
},
63+
type: 'javascript/auto'
64+
} satisfies RsPack.RuleSetRule
5565

56-
await ESBuild.build({
57-
entryPoints: [Path.resolve(ProjectRoot, 'userscript', 'source', 'index.ts')],
58-
bundle: true,
59-
minify: Options.Minify,
60-
outfile: `${ProjectRoot}/dist/NamuLink${Options.BuildType === 'development' ? '.dev' : ''}.user.js`,
61-
banner: {
62-
js: Banner
66+
const WorkerContainer = Memfs.createFsFromVolume(new Memfs.Volume())
67+
const WorkerBuilder = RsPack.rspack({
68+
entry: [Path.resolve(ProjectRoot, 'userscript', 'source', 'ocr-worker.ts')],
69+
mode: Options.BuildType,
70+
optimization: {
71+
minimize: Options.Minify
72+
},
73+
target: ['es2024', 'webworker'],
74+
output: {
75+
path: Path.resolve('/'),
76+
filename: 'ocr-worker.js'
6377
},
64-
target: ['es2024', 'chrome119', 'firefox142', 'safari26'],
65-
define: {
66-
__OCR_WORKER_CODE__: JSON.stringify(WorkerCode.outputFiles[0].text)
78+
resolve: {
79+
extensions: ['.ts', '.js', '.json'],
80+
extensionAlias: {
81+
'.js': ['.ts', '.js']
82+
}
83+
},
84+
module: {
85+
rules: [TypescriptLoader]
6786
}
6887
})
88+
//@ts-expect-error https://github.com/web-infra-dev/rspack/issues/5091
89+
WorkerBuilder.outputFileSystem = WorkerContainer
90+
await RunCompiler(WorkerBuilder)
91+
92+
const MainBuilder = RsPack.rspack({
93+
entry: [Path.resolve(ProjectRoot, 'userscript', 'source', 'index.ts')],
94+
mode: Options.BuildType,
95+
optimization: {
96+
minimize: Options.Minify,
97+
minimizer: [
98+
new RsPack.SwcJsMinimizerRspackPlugin({
99+
minimizerOptions: {
100+
format: {
101+
comments: 'all'
102+
}
103+
}
104+
})
105+
]
106+
},
107+
plugins: [
108+
new RsPack.BannerPlugin({
109+
banner: Banner,
110+
raw: true
111+
}),
112+
new RsPack.DefinePlugin({
113+
__OCR_WORKER_CODE__: JSON.stringify(WorkerContainer.readFileSync(Path.resolve('/', 'ocr-worker.js'), 'utf-8'))
114+
})
115+
],
116+
output: {
117+
path: Path.resolve(ProjectRoot, 'dist'),
118+
filename: `NamuLink${Options.BuildType === 'development' ? '.dev' : ''}.user.js`
119+
},
120+
resolve: {
121+
extensions: ['.ts', '.js', '.json'],
122+
extensionAlias: {
123+
'.js': ['.ts', '.js']
124+
}
125+
},
126+
module: {
127+
rules: [TypescriptLoader]
128+
},
129+
target: ['es2024', 'web']
130+
})
131+
132+
await RunCompiler(MainBuilder)
69133
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as RsPack from '@rspack/core'
2+
3+
export async function RunCompiler(Compiler: RsPack.Compiler): Promise<RsPack.Stats> {
4+
return new Promise((Resolve, Reject) => {
5+
Compiler.run((Err, Stats) => {
6+
if (Err) return Reject(Err)
7+
if (!Stats) return Reject(new Error('No stats returned'))
8+
if (Stats.hasErrors()) {
9+
return Reject(new Error(Stats.toString({ all: false, errors: true })))
10+
}
11+
Resolve(Stats)
12+
})
13+
})
14+
}

0 commit comments

Comments
 (0)