({
catchBuildError = false,
runServer = false,
watch = false,
page,
logHelper,
...options
}: BuildOptions = {})
| 177 | * Build the project and return the build result. |
| 178 | */ |
| 179 | export async function build({ |
| 180 | catchBuildError = false, |
| 181 | runServer = false, |
| 182 | watch = false, |
| 183 | page, |
| 184 | logHelper, |
| 185 | ...options |
| 186 | }: BuildOptions = {}) { |
| 187 | process.env.NODE_ENV = 'production'; |
| 188 | |
| 189 | options.config = await updateConfigForTest(options.config || {}, options.cwd); |
| 190 | |
| 191 | const rsbuild = await createRsbuild(options); |
| 192 | const getOutputFiles = collectOutputFiles(rsbuild); |
| 193 | |
| 194 | let buildError: Error | undefined; |
| 195 | let buildResult: RsbuildBuildResult | undefined; |
| 196 | |
| 197 | try { |
| 198 | buildResult = await rsbuild.build({ watch }); |
| 199 | } catch (error) { |
| 200 | buildError = error as Error; |
| 201 | buildError.message = stripAnsi(buildError.message); |
| 202 | |
| 203 | if (!catchBuildError) { |
| 204 | throw buildError; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | const { distPath } = rsbuild.context; |
| 209 | |
| 210 | let port = 0; |
| 211 | let server = { close: noop }; |
| 212 | |
| 213 | if (runServer || page) { |
| 214 | const result = await rsbuild.preview(); |
| 215 | port = result.port; |
| 216 | server = result.server; |
| 217 | } |
| 218 | |
| 219 | const getIndexBundle = async () => { |
| 220 | const [name, content] = |
| 221 | Object.entries(getOutputFiles()).find( |
| 222 | ([file]) => file.includes('index') && file.endsWith('.js'), |
| 223 | ) || []; |
| 224 | assert(name && content); |
| 225 | return content; |
| 226 | }; |
| 227 | |
| 228 | if (page) { |
| 229 | await gotoPage(page, { port }); |
| 230 | } |
| 231 | |
| 232 | return { |
| 233 | ...logHelper!, |
| 234 | distPath, |
| 235 | port, |
| 236 | stats: buildResult?.stats, |
searching dependent graphs…