From eb3b81f6213381528ca8b8327c8ef4285fddfcf7 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 7 Feb 2022 04:42:38 +0800 Subject: [PATCH] refa: enhance console builder --- build/frontend.ts | 57 ++++++++++++++++----------- plugins/frontend/builder/src/index.ts | 1 - plugins/frontend/console/src/http.ts | 3 +- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/build/frontend.ts b/build/frontend.ts index 785c709c39..e4d343c2dc 100644 --- a/build/frontend.ts +++ b/build/frontend.ts @@ -1,5 +1,5 @@ import { buildExtension } from '@koishijs/builder/src' -import { copyFile } from 'fs/promises' +import { createReadStream, createWriteStream } from 'fs' import { cwd, getPackages } from './utils' import vue from '@vitejs/plugin-vue' import vite from 'vite' @@ -14,12 +14,11 @@ function findModulePath(id: string) { } export async function build(root: string, config: vite.UserConfig) { - const { rollupOptions } = config.build || {} + const { rollupOptions = {} } = config.build || {} await vite.build({ root, build: { outDir: '../dist', - minify: 'esbuild', emptyOutDir: true, ...config.build, rollupOptions: { @@ -30,12 +29,13 @@ export async function build(root: string, config: vite.UserConfig) { root + '/client.js', root + '/components.js', ], - output: rollupOptions?.input ? { + output: { format: 'module', entryFileNames: '[name].js', - assetFileNames: '[name][extname]', + chunkFileNames: '[name].js', + assetFileNames: '[name].[ext]', ...rollupOptions.output, - } : undefined, + }, }, }, plugins: [vue()], @@ -51,6 +51,17 @@ export async function build(root: string, config: vite.UserConfig) { }) } +function pipe(src: string, dest: string) { + return new Promise((resolve, reject) => { + const readStream = createReadStream(src) + const writeStream = createWriteStream(dest, { flags: 'a' }) + readStream.on('error', reject) + writeStream.on('error', reject) + writeStream.on('close', resolve) + readStream.pipe(writeStream) + }) +} + async function buildConsole(folder: string) { const root = cwd + '/' + folder + '/client' const dist = cwd + '/' + folder + '/dist' @@ -58,24 +69,24 @@ async function buildConsole(folder: string) { // build for console main await build(root, { base: './' }) - await copyFile(findModulePath('vue') + '/dist/vue.runtime.esm-browser.prod.js', dist + '/vue.js') - - // build for console client entry - await build(cwd, { - build: { - outDir: dist, - emptyOutDir: false, - rollupOptions: { - input: { - 'client': root + '/client.ts', - 'vue-router': findModulePath('vue-router') + '/dist/vue-router.esm-browser.js', - 'components': cwd + '/plugins/frontend/components/client/index.ts', + await Promise.all([ + pipe(findModulePath('vue') + '/dist/vue.runtime.esm-browser.prod.js', dist + '/vue.js'), + pipe(cwd + '/plugins/frontend/components/dist/index.js', dist + '/components.js'), + pipe(cwd + '/plugins/frontend/components/dist/style.css', dist + '/index.css'), + build(cwd, { + build: { + outDir: dist, + emptyOutDir: false, + rollupOptions: { + input: { + 'client': root + '/client.ts', + 'vue-router': findModulePath('vue-router') + '/dist/vue-router.esm-browser.js', + }, + preserveEntrySignatures: 'strict', }, - treeshake: false, - preserveEntrySignatures: 'strict', }, - }, - }) + }), + ]) } ;(async () => { @@ -84,8 +95,6 @@ async function buildConsole(folder: string) { for (const folder of folders) { if (folder === 'plugins/frontend/console') { await buildConsole(folder) - } else if (folder === 'plugins/frontend/components') { - continue } else { await buildExtension(cwd + '/' + folder, { plugins: [{ diff --git a/plugins/frontend/builder/src/index.ts b/plugins/frontend/builder/src/index.ts index 4ffa6f154e..2068665228 100644 --- a/plugins/frontend/builder/src/index.ts +++ b/plugins/frontend/builder/src/index.ts @@ -11,7 +11,6 @@ export async function buildExtension(root: string, config: UserConfig = {}) { root, build: { outDir: 'dist', - minify: 'esbuild', assetsDir: '', emptyOutDir: true, lib: { diff --git a/plugins/frontend/console/src/http.ts b/plugins/frontend/console/src/http.ts index c226ab75e1..5749f7ffda 100644 --- a/plugins/frontend/console/src/http.ts +++ b/plugins/frontend/console/src/http.ts @@ -46,7 +46,7 @@ class HttpService extends DataService { if (devMode) { return Object.values(this.data).map(filename => '/vite/@fs/' + filename) } - const filenames: string[] = ['/components.css'] + const filenames: string[] = [] for (const name in this.data) { const baseDir = uiPath + '/' + name const localDir = this.data[name] @@ -113,6 +113,7 @@ class HttpService extends DataService { resolve: { alias: { '~/client': root + '/client.ts', + '~/components': '@koishijs/components', }, }, optimizeDeps: {