@@ -3,27 +3,53 @@ title: 构建
33description : UI 部分的构建说明
44---
55
6- 在 [ halo-dev/plugin-starter ] ( https://github.com/halo-dev/plugin-starter ) 模板中 ,我们已经配置好了 UI 的构建工具和流程,此文档主要说明一些构建细节以及其他可能的构建选项。
6+ 在 [ halo-dev/create-halo-plugin ] ( https://github.com/halo-dev/create-halo-plugin ) 工具中 ,我们已经配置好了 UI 的构建工具和流程,此文档主要说明一些构建细节以及其他可能的构建选项。
77
88## 原理
99
10- Halo 插件的 UI 部分(Console / UC)的实现方式其实很简单,本质上就是构建一个结构固定的大对象,交给 Halo 去解析,其中包括全局注册的组件、路由定义、扩展点等。在 [ halo-dev/plugin-starter ] ( https://github.com/halo-dev/plugin-starter ) 模板中 ,我们使用 ` index.ts ` 作为入口文件,并在构建之后将 ` main.js ` 和 ` style.css ` 放到插件项目的 ` src/main/resources/console ` 目录中,后续 Halo 在内部会自动合并所有插件的 ` main.js ` 和 ` style.css ` 文件,并生成最终的 ` bundle.js ` 和 ` bundle.css ` 文件,然后在 Console 和 UC 中加载这两个资源并解析。
10+ Halo 插件的 UI 部分(Console / UC)的实现方式其实很简单,本质上就是构建一个结构固定的大对象,交给 Halo 去解析,其中包括全局注册的组件、路由定义、扩展点等。在 [ halo-dev/create-halo-plugin ] ( https://github.com/halo-dev/create-halo-plugin ) 工具创建的项目中 ,我们使用 ` index.ts ` 作为入口文件,并在构建之后将 ` main.js ` 和 ` style.css ` 放到插件项目的 ` src/main/resources/console ` 目录中,后续 Halo 在内部会自动合并所有插件的 ` main.js ` 和 ` style.css ` 文件,并生成最终的 ` bundle.js ` 和 ` bundle.css ` 文件,然后在 Console 和 UC 中加载这两个资源并解析。
1111
12- 所以本质上,我们只需要使用支持将 ` index.ts ` 编译为 ` main.js ` 和 ` style.css ` 的工具,然后输出到插件项目的 ` src/main/resources/console ` 目录中即可,在 [ halo-dev/plugin-starter ] ( https://github.com/halo-dev/plugin-starter ) 模板中可以看到 ,我们提供了一个名为 ` @halo-dev/ui-plugin-bundler-kit ` 的库,这个库包含了 [ Vite] ( https://vite.dev/ ) 和 [ Rsbuild] ( https://rsbuild.dev/ ) 的预配置,插件项目只需要通过简单的配置即可使用。
12+ 所以本质上,我们只需要使用支持将 ` index.ts ` 编译为 ` main.js ` 和 ` style.css ` 的工具,然后输出到插件项目的 ` src/main/resources/console ` 目录中即可,在 [ halo-dev/create-halo-plugin ] ( https://github.com/halo-dev/create-halo-plugin ) 的模板中可以看到 ,我们提供了一个名为 ` @halo-dev/ui-plugin-bundler-kit ` 的库,这个库包含了 [ Vite] ( https://vite.dev/ ) 和 [ Rsbuild] ( https://rsbuild.dev/ ) 的预配置,插件项目只需要通过简单的配置即可使用。
1313
1414## @halo-dev/ui-plugin-bundler-kit
1515
1616在这个库中,我们提供了三个预配置,分别是:
1717
18- 1 . ` viteConfig ` : Vite 的预配置, [ halo-dev/plugin-starter ] ( https://github.com/halo-dev/plugin-starter ) 中默认使用的配置
18+ 1 . ` viteConfig ` : Vite 的预配置
19192 . ` rsbuildConfig ` : Rsbuild 的预配置
20203 . ` HaloUIPluginBundlerKit ` :已过时,迁移方式可以参考下面的文档
2121
2222### viteConfig
2323
2424#### 使用
2525
26- 如果你想要使用 Vite 构建 UI 部分,那么使用 ` viteConfig ` 即可,并且已经在 [ halo-dev/plugin-starter] ( https://github.com/halo-dev/plugin-starter ) 中配置,直接使用即可。
26+ 如果你在通过 [ halo-dev/create-halo-plugin] ( https://github.com/halo-dev/create-halo-plugin ) 创建项目时没有选择使用 Vite 作为 UI 的构建工具,那么可以通过以下方式改为使用 Vite。
27+
28+ 安装依赖:
29+
30+ ``` bash
31+ pnpm install @halo-dev/ui-plugin-bundler-kit@2.21.1 vite -D
32+ ```
33+
34+ 创建 vite.config.ts:
35+
36+ ``` js
37+ import { viteConfig } from " @halo-dev/ui-plugin-bundler-kit" ;
38+
39+ export default viteConfig ()
40+ ```
41+
42+ 更新 package.json:
43+
44+ ``` json
45+ {
46+ "type" : " module" ,
47+ "scripts" : {
48+ "dev" : " vite build --watch --mode=development" ,
49+ "build" : " vite build"
50+ }
51+ }
52+ ```
2753
2854#### 配置
2955
@@ -83,15 +109,15 @@ Rsbuild 是基于 Rspack 开发的上层构建工具,其优势在于兼容 Web
83109
84110#### 使用
85111
86- 因为在 [halo- dev/ plugin - starter ](https: // github.com/halo-dev/plugin-starter) 中,默认采用 Vite 构建,所以如果想要使用 Rsbuild 构建,需要手动配置,以下是切换到 Rsbuild 的过程:
112+ 如果你在通过 [halo- dev/ create - halo - plugin ](https: // github.com/halo-dev/create-halo-plugin) 创建项目时没有选择使用 Rsbuild 作为 UI 的构建工具,那么可以通过以下方式改为使用 Rsbuild。
87113
88114安装依赖:
89115
90116` ` ` bash
91117pnpm install @halo-dev/ui-plugin-bundler-kit@2.21.1 @rsbuild/core -D
92118` ` `
93119
94- 创建 rsbuild .config .mjs :
120+ 创建 rsbuild .config .ts :
95121
96122` ` ` js
97123import { rsbuildConfig } from "@halo-dev/ui-plugin-bundler-kit";
@@ -255,116 +281,99 @@ export default definePlugin({
255281
2562823. 更新项目根目录的 ` build.gradle` 文件
257283
258- ` ` ` diff
284+ ` ` ` gradle
259285 plugins {
260286 id 'java'
261- - id "com.github.node-gradle.node" version "7.0.2"
262- - id "io.freefair.lombok" version "8.0.1"
263- - id "run.halo.plugin.devtools" version "0.2.0"
264- + id "io.freefair.lombok" version "8.13"
265- + id "run.halo.plugin.devtools" version "0.6.0"
287+ id "io.freefair.lombok" version "8.13"
288+ id "run.halo.plugin.devtools" version "0.6.0"
266289 }
267-
290+
268291 group 'run.halo.starter'
269- -sourceCompatibility = JavaVersion.VERSION_17
270-
292+
271293 repositories {
272294 mavenCentral()
273- - maven { url 'https://s01.oss.sonatype.org/content/repositories/releases' }
274- - maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' }
275- - maven { url 'https://repo.spring.io/milestone' }
276295 }
277-
296+
278297 dependencies {
279- - implementation platform('run.halo.tools.platform:plugin:2.20.0-SNAPSHOT')
280- + implementation platform('run.halo.tools.platform:plugin:2.21.0')
298+ implementation platform('run.halo.tools.platform:plugin:2.21.0')
281299 compileOnly 'run.halo.app:api'
282-
300+
283301 testImplementation 'run.halo.app:api'
284302 testImplementation 'org.springframework.boot:spring-boot-starter-test'
285- + testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
303+ testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
286304 }
287-
305+
288306 test {
289307 useJUnitPlatform()
290308 }
291-
292- -tasks.withType(JavaCompile).configureEach {
293- - options.encoding = "UTF-8"
294- -}
295- -
296- -node {
297- - nodeProjectDir = file("${ project .projectDir } /ui")
298- +java {
299- + toolchain {
300- + languageVersion = JavaLanguageVersion.of(21)
301- + }
309+
310+ java {
311+ toolchain {
312+ languageVersion = JavaLanguageVersion.of(21)
313+ }
314+ }
315+
316+ tasks.withType(JavaCompile).configureEach {
317+ options.encoding = "UTF-8"
318+ options.release = 21
302319 }
303-
304- -tasks.register('buildFrontend', PnpmTask) {
305- - args = ['build']
306- - dependsOn('installDepsForUI')
307- +tasks.withType(JavaCompile).configureEach {
308- + options.encoding = "UTF-8"
309- + options.release = 21
320+
321+ tasks.register('processUiResources', Copy) {
322+ from project(':ui').layout.buildDirectory.dir('dist')
323+ into layout.buildDirectory.dir('resources/main/console')
324+ dependsOn project(':ui').tasks.named('assemble')
325+ shouldRunAfter tasks.named('processResources')
310326 }
311-
312- -tasks.register('installDepsForUI', PnpmTask) {
313- - args = ['install']
314- +tasks.register('processUiResources', Copy) {
315- + from project(':ui').tasks.named('buildFrontend')
316- + into layout.buildDirectory.dir('resources/main/console')
327+
328+ tasks.named('classes') {
329+ dependsOn tasks.named('processUiResources')
317330 }
318-
319- -build {
320- - // build frontend before build
321- - tasks.named('compileJava').configure {
322- - dependsOn('buildFrontend')
323- - }
324- +tasks.named('processResources', ProcessResources) {
325- + dependsOn tasks.named('processUiResources')
331+
332+ halo {
333+ version = '2.21'
326334 }
327335 ` ` `
328336
3293374. 在 ui 或者 console 目录新建 ` build.gradle` 文件,内容如下:
330338
331339 ` ` ` gradle
332340 plugins {
333- id 'base'
334- id "com.github.node-gradle.node" version "7.1.0"
341+ id 'base'
342+ id "com.github.node-gradle.node" version "7.1.0"
335343 }
336344
337345 group 'run.halo.starter.ui'
338346
339347 tasks.register('buildFrontend', PnpmTask) {
340- args = ['build']
341- dependsOn tasks.named('pnpmInstall')
342- inputs.dir(layout.projectDirectory.dir('src'))
343- inputs.files(fileTree(
344- dir: layout.projectDirectory,
345- includes: ['*.cjs', '*.ts', '*.js', '*.json', '*.yaml']))
346- outputs.dir(layout.buildDirectory.dir('dist'))
347- shouldRunAfter(tasks.named('check'))
348+ group = 'build'
349+ description = 'Builds the UI project using pnpm.'
350+ args = ['build']
351+ dependsOn tasks.named('pnpmInstall')
352+ inputs.dir(layout.projectDirectory.dir('src'))
353+ inputs.files(fileTree(
354+ dir: layout.projectDirectory,
355+ includes: ['*.cjs', '*.ts', '*.js', '*.json', '*.yaml']))
356+ outputs.dir(layout.buildDirectory.dir('dist'))
348357 }
349358
350- tasks.register('checkFrontend', PnpmTask) {
351- args = ['test:unit']
352- dependsOn tasks.named('pnpmInstall')
359+ tasks.register('pnpmCheck', PnpmTask) {
360+ group = 'verification'
361+ description = 'Runs unit tests using pnpm.'
362+ args = ['test:unit']
363+ dependsOn tasks.named('pnpmInstall')
353364 }
354365
355366 tasks.named('check') {
356- dependsOn tasks.named('checkFrontend ')
367+ dependsOn tasks.named('pnpmCheck ')
357368 }
358369
359- tasks.named('build ') {
360- dependsOn tasks.named('buildFrontend')
370+ tasks.named('assemble ') {
371+ dependsOn tasks.named('buildFrontend')
361372 }
362373 ` ` `
363374
364375进行此变更的主要目的是保证 UI 构建的产物不直接输出到源码目录的 resources 目录中,而是通过 Gradle 构建插件时复制到 ` src/main/resources/console` 目录中。
365376
366- 完整变更过程可参考:[halo- dev/ plugin- starter#52 ](https: // github.com/halo-dev/plugin-starter/pull/52)
367-
368377如果你不想使用新的 Gradle 构建配置,也可以修改 viteConfig 或 rsbuildConfig 的输出目录,和旧版本保持一致:
369378
370379viteConfig:
0 commit comments