From 8a473500048e2cb58538fe54e18b12e6d42101e4 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 23 Jun 2021 17:12:18 +0800 Subject: [PATCH 1/3] feat(admin): use Vite as middleware on dev close #293 --- package.json | 6 +-- src/admin/{ => app}/vite.config.ts | 8 +-- src/admin/index.ts | 80 ++++++++++++++++-------------- 3 files changed, 49 insertions(+), 45 deletions(-) rename src/admin/{ => app}/vite.config.ts (82%) diff --git a/package.json b/package.json index a6cea052b..5fb2ee3c6 100755 --- a/package.json +++ b/package.json @@ -30,14 +30,14 @@ ], "scripts": { "build": "rm -rf dist && siroc build", - "dev": "BUILD_ADMIN=true nuxt dev docs", + "dev": "ADMIN_DEV=true nuxt dev docs", "dev:nuxtjs": "nuxt dev nuxtjs.org", - "dev:admin": "vite --config src/admin/vite.config.ts", + "dev:admin": "vite --config src/admin/app/vite.config.ts", "generate": "nuxt generate --force-build docs", "generate:nuxtjs": "nuxt generate --force-build nuxtjs.org", "start": "nuxt start docs", "start:nuxtjs": "nuxt start nuxtjs.org", - "play": "BUILD_ADMIN=true nuxt dev playground", + "play": "ADMIN_DEV=true nuxt dev playground", "lint": "eslint --ext .ts,.js,.vue .", "prepare": "yarn link && yarn link docus", "release": "yarn test && standard-version && git push --follow-tags && npm publish", diff --git a/src/admin/vite.config.ts b/src/admin/app/vite.config.ts similarity index 82% rename from src/admin/vite.config.ts rename to src/admin/app/vite.config.ts index 13b0d5542..f3aea414f 100644 --- a/src/admin/vite.config.ts +++ b/src/admin/app/vite.config.ts @@ -1,13 +1,13 @@ -import { join, resolve } from 'path' +import { resolve } from 'path' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import WindiCSS from 'vite-plugin-windicss' import ViteIcons, { ViteIconsResolver } from 'vite-plugin-icons' import ViteComponents from 'vite-plugin-components' -import { dependencies, devDependencies } from '../../package.json' +import { dependencies, devDependencies } from '../../../package.json' const r = path => resolve(__dirname, path) -const root = join(__dirname, 'app') +const root = __dirname export default defineConfig({ root, @@ -20,7 +20,7 @@ export default defineConfig({ WindiCSS({ root, scan: { - include: [r('app/**/*')] + include: [r('**/*')] } }), ViteComponents({ diff --git a/src/admin/index.ts b/src/admin/index.ts index 2156e975c..d47cb0dda 100644 --- a/src/admin/index.ts +++ b/src/admin/index.ts @@ -1,9 +1,9 @@ -import { execSync } from 'child_process' import { join } from 'upath' import chalk from 'chalk' import { NuxtOptionsServer } from '@nuxt/types/config/server' import serveStatic from 'serve-static' import { Module } from '@nuxt/types' +import { createServer as createViteServer } from 'vite' import api from './api' export default function () { @@ -12,43 +12,6 @@ export default function () { process.options = nuxt.options process.previewUrl = 'http://localhost:4000' - addServerMiddleware({ - path: '/admin/api', - handler: api - }) - - // TODO: Implement Vite as server middleware/proxy, so we can remove this - // Build the admin - if (process.env.BUILD_ADMIN) execSync('npx vite --config src/admin/vite.config.ts build') - - this.addServerMiddleware({ - path: '/admin/', - handler: serveStatic(join(__dirname, 'app/dist')) - }) - - // TODO: Implement Vite as server middleware/proxy - // instead of using the built version. - /* if (process.env.ADMIN_DEV && false) { - // Start the admin in development - const viteServer = await createServer({ - configFile: false, - ...viteConfig - }) - await viteServer.listen() - const viteUrl = `http://localhost:${viteServer.config.server.port}` - - // TODO: handle when Array syntax - const proxy = (this.options.proxy = this.options.proxy || {}) - proxy['/admin/'] = { - target: viteUrl, - pathRewrite: { - '^/admin/': '/' - } - } - - this.requireModule('@nuxtjs/proxy') - } */ - // Add runtime plugin if (options.dev) this.options.plugins.push(require.resolve(join(__dirname, '/runtime/plugin'))) @@ -60,4 +23,45 @@ export default function () { chalk.bold('📝 Admin: ') + chalk.underline.yellow(`${process.previewUrl}/admin/`) ) }) + + if (process.env.ADMIN_DEV) { + // use Vite as middleware + const server = createViteServer({ + root: join(__dirname, 'app'), + server: { + middlewareMode: 'html' + }, + plugins: [ + { + name: 'docus:admin-api', + configureServer(server) { + server.middlewares.use('/api', api) + } + } + ] + }) + + nuxt.hook('render:setupMiddleware', async () => { + const middleware = (await server).middlewares + + // remove Vite's base middleware since it's already handled by connect route + // it's right before '/__open-in-editor' middleware + const viteBaseMiddlewareIndex = middleware.stack.findIndex(i => i.route === '/__open-in-editor') - 1 + if (viteBaseMiddlewareIndex >= 0) { + middleware.stack.splice(viteBaseMiddlewareIndex, 1) + } + + nuxt.server.app.use('/admin/', middleware) + }) + } else { + // use built dist + addServerMiddleware({ + path: '/admin/', + handle: serveStatic(join(__dirname, 'app/dist')) + }) + addServerMiddleware({ + path: '/admin/api', + handler: api + }) + } } From ece221f96e2f8890da681c32156e6ba5c6ac19e5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 23 Jun 2021 17:16:12 +0800 Subject: [PATCH 2/3] chore: fix build --- siroc.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/siroc.config.ts b/siroc.config.ts index af87168c2..be3a7cab2 100644 --- a/siroc.config.ts +++ b/siroc.config.ts @@ -10,7 +10,7 @@ export default defineSirocConfig({ 'build:extend'() { // eslint-disable-next-line no-console console.log('📝 Building the admin app') - execSync('npx vite --config src/admin/vite.config.ts build') + execSync('npx vite --config src/admin/app/vite.config.ts build', { stdio: 'inherit' }) } } }) From 92d0c8af74616af502f69d74d91a5ce253f867d4 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 23 Jun 2021 18:27:10 +0800 Subject: [PATCH 3/3] feat(admin): dark mode (#502) --- package.json | 3 ++- src/admin/app/components/AppHeader.vue | 13 ++--------- src/admin/app/components/AppHeaderLogo.vue | 14 ++++++++++-- src/admin/app/components/AppHeaderNav.vue | 19 ++++++++++++---- src/admin/app/components/Editor.vue | 7 ++---- src/admin/app/components/FilesTree.vue | 8 +++---- src/admin/app/components/Preview.vue | 2 +- src/admin/app/composables/dark.ts | 4 ++++ src/admin/app/composables/index.ts | 1 + src/admin/app/index.css | 8 +++++++ src/admin/app/index.html | 2 +- src/admin/app/pages/index.vue | 6 +++--- src/admin/app/pages/static.vue | 4 ++-- src/admin/app/vite.config.ts | 5 +++-- src/admin/app/windi.config.ts | 25 ++++++++++++++++++++++ yarn.lock | 8 +++++++ 16 files changed, 93 insertions(+), 36 deletions(-) create mode 100644 src/admin/app/composables/dark.ts create mode 100644 src/admin/app/windi.config.ts diff --git a/package.json b/package.json index 5fb2ee3c6..d05d37b48 100755 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "start:nuxtjs": "nuxt start nuxtjs.org", "play": "ADMIN_DEV=true nuxt dev playground", "lint": "eslint --ext .ts,.js,.vue .", - "prepare": "yarn link && yarn link docus", + "prepare": "yarn link && yarn link docus && vue-demi-switch 3 vue3", "release": "yarn test && standard-version && git push --follow-tags && npm publish", "test": "yarn lint && echo 'TODO tests'", "clean:nuxt": "rm -rf .nuxt docs/.nuxt playground/.nuxt nuxtjs.org/.nuxt", @@ -124,6 +124,7 @@ "@types/fs-extra": "^9.0.11", "@types/graceful-fs": "^4.1.5", "@types/node-fetch": "^2.5.10", + "@vueuse/core": "^5.0.3", "babel-eslint": "^10.1.0", "eslint": "^7.29.0", "eslint-config-prettier": "^8.3.0", diff --git a/src/admin/app/components/AppHeader.vue b/src/admin/app/components/AppHeader.vue index 73218349c..d0937b306 100644 --- a/src/admin/app/components/AppHeader.vue +++ b/src/admin/app/components/AppHeader.vue @@ -1,20 +1,11 @@ - diff --git a/src/admin/app/components/AppHeaderLogo.vue b/src/admin/app/components/AppHeaderLogo.vue index d4bf2e516..d44b7d039 100644 --- a/src/admin/app/components/AppHeaderLogo.vue +++ b/src/admin/app/components/AppHeaderLogo.vue @@ -1,7 +1,13 @@ + + diff --git a/src/admin/app/components/AppHeaderNav.vue b/src/admin/app/components/AppHeaderNav.vue index 9e37cb69c..85d9dd6d8 100644 --- a/src/admin/app/components/AppHeaderNav.vue +++ b/src/admin/app/components/AppHeaderNav.vue @@ -1,23 +1,34 @@ + + diff --git a/src/admin/app/components/Editor.vue b/src/admin/app/components/Editor.vue index 785077c23..efc04d081 100644 --- a/src/admin/app/components/Editor.vue +++ b/src/admin/app/components/Editor.vue @@ -1,8 +1,5 @@