Skip to content

Commit

Permalink
refa: enhance console builder
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 6, 2022
1 parent 3d1e488 commit eb3b81f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
57 changes: 33 additions & 24 deletions build/frontend.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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: {
Expand All @@ -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()],
Expand All @@ -51,31 +51,42 @@ export async function build(root: string, config: vite.UserConfig) {
})
}

function pipe(src: string, dest: string) {
return new Promise<void>((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'

// 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 () => {
Expand All @@ -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: [{
Expand Down
1 change: 0 additions & 1 deletion plugins/frontend/builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export async function buildExtension(root: string, config: UserConfig = {}) {
root,
build: {
outDir: 'dist',
minify: 'esbuild',
assetsDir: '',
emptyOutDir: true,
lib: {
Expand Down
3 changes: 2 additions & 1 deletion plugins/frontend/console/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HttpService extends DataService<string[]> {
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]
Expand Down Expand Up @@ -113,6 +113,7 @@ class HttpService extends DataService<string[]> {
resolve: {
alias: {
'~/client': root + '/client.ts',
'~/components': '@koishijs/components',
},
},
optimizeDeps: {
Expand Down

0 comments on commit eb3b81f

Please sign in to comment.