-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): enhance build workflow
- Loading branch information
Showing
28 changed files
with
260 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { buildExtension } from '@koishijs/builder/src' | ||
import { copyFile } from 'fs/promises' | ||
import { cwd, getPackages } from './utils' | ||
import vue from '@vitejs/plugin-vue' | ||
import vite from 'vite' | ||
import cac from 'cac' | ||
|
||
const { args } = cac().help().parse() | ||
|
||
function findModulePath(id: string) { | ||
const path = require.resolve(id).replace(/\\/g, '/') | ||
const keyword = `/node_modules/${id}/` | ||
return path.slice(0, path.indexOf(keyword)) + keyword.slice(0, -1) | ||
} | ||
|
||
export async function build(root: string, config: vite.UserConfig) { | ||
const { rollupOptions } = config.build || {} | ||
await vite.build({ | ||
root, | ||
build: { | ||
outDir: '../dist', | ||
minify: 'esbuild', | ||
emptyOutDir: true, | ||
...config.build, | ||
rollupOptions: { | ||
...rollupOptions, | ||
external: [ | ||
root + '/vue.js', | ||
root + '/vue-router.js', | ||
root + '/client.js', | ||
], | ||
output: rollupOptions?.input ? { | ||
format: 'module', | ||
entryFileNames: '[name].js', | ||
...rollupOptions.output, | ||
} : undefined, | ||
}, | ||
}, | ||
plugins: [vue()], | ||
resolve: { | ||
alias: { | ||
'vue': root + '/vue.js', | ||
'vue-router': root + '/vue-router.js', | ||
'./client': root + '/client.js', | ||
'../client': root + '/client.js', | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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', | ||
}, | ||
treeshake: false, | ||
preserveEntrySignatures: 'strict', | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
;(async () => { | ||
const folders = await getPackages(args) | ||
|
||
for (const folder of folders) { | ||
if (folder === 'plugins/frontend/console') { | ||
await buildConsole(folder) | ||
} else { | ||
await buildExtension(cwd + '/' + folder, { | ||
plugins: [{ | ||
name: 'fuck-echarts', | ||
renderChunk(code, chunk) { | ||
if (chunk.fileName.includes('echarts')) { | ||
return code.replace(/\bSymbol(?!\.toStringTag)/g, 'FuckSymbol') | ||
} | ||
}, | ||
}], | ||
}) | ||
} | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,41 @@ | ||
/* eslint-disable quote-props */ | ||
|
||
import * as vite from 'vite' | ||
import pluginVue from '@vitejs/plugin-vue' | ||
import { build, mergeConfig, UserConfig } from 'vite' | ||
import { existsSync } from 'fs' | ||
import vue from '@vitejs/plugin-vue' | ||
|
||
export async function build(root: string, config: vite.UserConfig) { | ||
const { rollupOptions } = config.build || {} | ||
await vite.build({ | ||
...config, | ||
export async function buildExtension(root: string, config: UserConfig = {}) { | ||
if (!existsSync(root + '/client')) return | ||
|
||
return build(mergeConfig({ | ||
root, | ||
build: { | ||
outDir: '../dist', | ||
outDir: 'dist', | ||
minify: 'esbuild', | ||
assetsDir: '', | ||
emptyOutDir: true, | ||
...config.build, | ||
lib: { | ||
entry: root + '/client/index.ts', | ||
formats: ['es'], | ||
}, | ||
rollupOptions: { | ||
...rollupOptions, | ||
external: [root + '/vue.js', root + '/vue-router.js', root + '/client.js'], | ||
output: rollupOptions?.input ? { | ||
format: 'module', | ||
entryFileNames: '[name].js', | ||
globals: { | ||
[root + '/vue.js']: 'Vue', | ||
[root + '/vue-router.js']: 'VueRouter', | ||
[root + '/client.js']: 'KoishiClient', | ||
}, | ||
...rollupOptions.output, | ||
} : undefined, | ||
external: [ | ||
root + '/vue.js', | ||
root + '/vue-router.js', | ||
root + '/client.js', | ||
], | ||
output: { | ||
format: 'iife', | ||
}, | ||
}, | ||
}, | ||
plugins: [pluginVue(), ...config.plugins || []], | ||
plugins: [vue()], | ||
resolve: { | ||
alias: { | ||
'vue': root + '/vue.js', | ||
'vue-router': root + '/vue-router.js', | ||
'~/client': root + '/client.js', | ||
...config.resolve?.alias, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
export function buildExtension(root: string) { | ||
return build(root, { | ||
build: { | ||
outDir: 'dist', | ||
assetsDir: '', | ||
minify: 'esbuild', | ||
rollupOptions: { | ||
input: root + '/client/index.ts', | ||
output: { | ||
format: 'iife', | ||
}, | ||
}, | ||
}, | ||
}) | ||
}, config)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.