From c4955b2c76d0c8b4c0ad49d560acfd68a749326f Mon Sep 17 00:00:00 2001 From: DarkSky Date: Sat, 21 Feb 2026 08:19:07 +0800 Subject: [PATCH] feat: improve admin build --- .../backend/server/src/__tests__/app/selfhost.e2e.ts | 4 ++-- packages/backend/server/src/core/selfhost/static.ts | 2 +- packages/frontend/admin/package.json | 2 +- tools/cli/bin/cli.js | 1 + tools/cli/src/bundle.ts | 10 ++++++++-- tools/cli/src/webpack/html-plugin.ts | 5 ++++- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/backend/server/src/__tests__/app/selfhost.e2e.ts b/packages/backend/server/src/__tests__/app/selfhost.e2e.ts index 088dbdaaeb..bf7a74bd0b 100644 --- a/packages/backend/server/src/__tests__/app/selfhost.e2e.ts +++ b/packages/backend/server/src/__tests__/app/selfhost.e2e.ts @@ -97,7 +97,7 @@ test('should always return static asset files', async t => { t.is(res.text, "const name = 'affine'"); res = await request(t.context.app.getHttpServer()) - .get('/main.b.js') + .get('/admin/main.b.js') .expect(200); t.is(res.text, "const name = 'affine-admin'"); @@ -119,7 +119,7 @@ test('should always return static asset files', async t => { t.is(res.text, "const name = 'affine'"); res = await request(t.context.app.getHttpServer()) - .get('/main.b.js') + .get('/admin/main.b.js') .expect(200); t.is(res.text, "const name = 'affine-admin'"); diff --git a/packages/backend/server/src/core/selfhost/static.ts b/packages/backend/server/src/core/selfhost/static.ts index a04c248a7c..5faf292045 100644 --- a/packages/backend/server/src/core/selfhost/static.ts +++ b/packages/backend/server/src/core/selfhost/static.ts @@ -52,7 +52,7 @@ export class StaticFilesResolver implements OnModuleInit { // serve all static files app.use( - basePath, + basePath + '/admin', serveStatic(join(staticPath, 'admin'), { redirect: false, index: false, diff --git a/packages/frontend/admin/package.json b/packages/frontend/admin/package.json index df71544e5d..50df8bfa47 100644 --- a/packages/frontend/admin/package.json +++ b/packages/frontend/admin/package.json @@ -74,7 +74,7 @@ "scripts": { "build": "affine bundle", "dev": "affine bundle --dev", - "update-shadcn": "shadcn-ui add -p src/components/ui" + "update-shadcn": "yarn dlx shadcn@latest add -p src/components/ui" }, "exports": { "./*": "./src/*.ts", diff --git a/tools/cli/bin/cli.js b/tools/cli/bin/cli.js index be4c9ecf24..60c59c1d5b 100755 --- a/tools/cli/bin/cli.js +++ b/tools/cli/bin/cli.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node import { spawnSync } from 'node:child_process'; spawnSync('yarn', ['r', 'affine.ts', ...process.argv.slice(2)], { diff --git a/tools/cli/src/bundle.ts b/tools/cli/src/bundle.ts index 182a56b578..43a0756434 100644 --- a/tools/cli/src/bundle.ts +++ b/tools/cli/src/bundle.ts @@ -88,7 +88,11 @@ function getWebpackBundleConfigs(pkg: Package): webpack.MultiConfiguration { switch (pkg.name) { case '@affine/admin': { return [ - createWebpackHTMLTargetConfig(pkg, pkg.srcPath.join('index.tsx').value), + createWebpackHTMLTargetConfig( + pkg, + pkg.srcPath.join('index.tsx').value, + { selfhostPublicPath: '/admin/' } + ), ] as webpack.MultiConfiguration; } case '@affine/web': @@ -158,7 +162,9 @@ function getRspackBundleConfigs(pkg: Package): MultiRspackOptions { switch (pkg.name) { case '@affine/admin': { return [ - createRspackHTMLTargetConfig(pkg, pkg.srcPath.join('index.tsx').value), + createRspackHTMLTargetConfig(pkg, pkg.srcPath.join('index.tsx').value, { + selfhostPublicPath: '/admin/', + }), ] as MultiRspackOptions; } case '@affine/web': diff --git a/tools/cli/src/webpack/html-plugin.ts b/tools/cli/src/webpack/html-plugin.ts index 85c8e34f81..e6286122d4 100644 --- a/tools/cli/src/webpack/html-plugin.ts +++ b/tools/cli/src/webpack/html-plugin.ts @@ -79,6 +79,7 @@ const currentDir = Path.dir(import.meta.url); export interface CreateHTMLPluginConfig { filename?: string; additionalEntryForSelfhost?: boolean; + selfhostPublicPath?: string; injectGlobalErrorHandler?: boolean; emitAssetsManifest?: boolean; } @@ -206,6 +207,7 @@ export function createHTMLPlugins( ): WebpackPluginInstance[] { const publicPath = getPublicPath(BUILD_CONFIG); const htmlPluginOptions = getHTMLPluginOptions(BUILD_CONFIG); + const selfhostPublicPath = config.selfhostPublicPath ?? '/'; const plugins: WebpackPluginInstance[] = []; plugins.push( @@ -269,9 +271,10 @@ export function createHTMLPlugins( new HTMLPlugin({ ...htmlPluginOptions, chunks: ['index'], + publicPath: selfhostPublicPath, meta: { 'env:isSelfHosted': 'true', - 'env:publicPath': '/', + 'env:publicPath': selfhostPublicPath, }, filename: 'selfhost.html', templateParameters: {