feat(showcase): add preview option

This commit is contained in:
Haoyu Xu
2023-02-20 20:53:10 -05:00
parent 81b20e9426
commit bf5a154bc7
3 changed files with 49 additions and 17 deletions

View File

@@ -23,17 +23,12 @@ async function main() {
const op = process.argv[2]
let OPERATOR_NAMES = process.argv.slice(3);
const background = new Background()
await background.process()
const backgrounds = ['operator_bg.png', ...background.files]
const charwordTable = new CharwordTable()
directory()
/**
* Skip all, no need for OPERATOR_NAME
* build-all: build all assets
* directory: build directory.json
* directory: build directory
*/
switch (op) {
case 'directory':
@@ -45,6 +40,10 @@ async function main() {
OPERATOR_NAMES.push(key)
}
break
case 'preview':
assert(OPERATOR_NAMES.length !== 0, 'Please set the operator name.')
fork(path.join(__projetRoot, 'vite.config.js'), [op, OPERATOR_NAMES])
return
case 'charword':
await charwordTable.process()
process.exit(0)
@@ -54,6 +53,12 @@ async function main() {
assert(OPERATOR_NAMES.length !== 0, 'Please set the operator name.')
const background = new Background()
await background.process()
const backgrounds = ['operator_bg.png', ...background.files]
directory()
for (const OPERATOR_NAME of OPERATOR_NAMES) {
const OPERATOR_SOURCE_FOLDER = path.join(__projetRoot, __config.folder.operator)
const OPERATOR_RELEASE_FOLDER = path.join(__projetRoot, __config.folder.release, OPERATOR_NAME)

View File

@@ -8,6 +8,7 @@
"dev": "node aklive2d dev",
"build": "node aklive2d build",
"build-all": "node aklive2d build-all",
"preview": "node aklive2d preview",
"init": "node aklive2d init",
"readme": "node aklive2d readme",
"directory": "node aklive2d directory",

View File

@@ -1,16 +1,27 @@
import path from 'path'
import { fileURLToPath } from 'url'
import { defineConfig, loadEnv } from 'vite'
import { defineConfig } from 'vite'
import assert from 'assert'
import react from '@vitejs/plugin-react-swc'
import getConfig from './libs/config.js'
import { rmdir } from './libs/file.js'
global.__projetRoot = path.dirname(fileURLToPath(import.meta.url))
class ViteRunner {
#globalConfig = getConfig()
#mode
#baseViteConfig = {}
#baseViteConfig = {
configFile: false,
base: "",
server: {
host: '0.0.0.0',
},
build: {
emptyOutDir: false,
chunkSizeWarningLimit: 10000,
},
}
config() {
let result;
@@ -19,10 +30,16 @@ class ViteRunner {
switch (this.#mode) {
case 'directory':
result = this.directory()
const op = temp[2] || process.argv[3]
if (op !== 'preview') {
rmdir(path.resolve(__projetRoot, this.#globalConfig.folder.release, "_directory"))
rmdir(path.resolve(__projetRoot, this.#globalConfig.folder.release, "index.html"))
}
break
case 'dev':
case 'build':
case 'build-all':
case 'preview':
result = this.operator()
break
default:
@@ -41,6 +58,9 @@ class ViteRunner {
case 'build-all':
this.#build(viteConfig)
break
case 'preview':
this.#preview(viteConfig)
break
default:
return
}
@@ -66,18 +86,25 @@ class ViteRunner {
})()
}
#preview(viteConfig) {
; (async () => {
const { preview } = await import('vite')
const previewServer = await preview({
...viteConfig,
})
previewServer.printUrls()
})()
}
operator() {
const operatorName = process.env.O || process.argv[3]
assert(operatorName, 'Please set the operator name by using environment variable O.')
return {
configFile: false,
base: "",
...this.#baseViteConfig,
envDir: path.join(__projetRoot, this.#globalConfig.folder.operator, operatorName),
publicDir: path.resolve(__projetRoot, this.#globalConfig.folder.release, operatorName),
root: path.resolve(__projetRoot),
server: {
host: '0.0.0.0',
},
resolve: {
alias: {
'@': path.resolve(__projetRoot, './src'),
@@ -85,9 +112,8 @@ class ViteRunner {
},
},
build: {
...this.#baseViteConfig.build,
outDir: path.resolve(__projetRoot, this.#globalConfig.folder.release, operatorName),
emptyOutDir: false,
chunkSizeWarningLimit: 10000,
},
}
}
@@ -112,9 +138,9 @@ class ViteRunner {
},
},
build: {
...this.#baseViteConfig.build,
outDir: path.resolve(__projetRoot, this.#globalConfig.folder.release),
emptyOutDir: false,
chunkSizeWarningLimit: 10000,
assetsDir: '_directory',
rollupOptions: {
output: {
manualChunks: {