refactor(playground): extract logic

This commit is contained in:
三咲智子 Kevin Deng 2023-11-30 05:54:27 +08:00
parent 50e919581b
commit e41429f3ef
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
6 changed files with 71 additions and 41 deletions

View File

@ -3,7 +3,7 @@
"version": "0.0.0", "version": "0.0.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "tsx ./setup/vite.ts", "dev": "node ./setup/vite.js",
"build": "vite build" "build": "vite build"
}, },
"dependencies": { "dependencies": {
@ -11,7 +11,6 @@
}, },
"devDependencies": { "devDependencies": {
"@vitejs/plugin-vue": "^4.5.0", "@vitejs/plugin-vue": "^4.5.0",
"tsx": "^4.6.0",
"vite": "^5.0.2", "vite": "^5.0.2",
"vite-node": "^0.34.6", "vite-node": "^0.34.6",
"vite-plugin-inspect": "^0.7.42" "vite-plugin-inspect": "^0.7.42"

View File

@ -1,10 +1,12 @@
// @ts-check
import path from 'node:path' import path from 'node:path'
import type { Plugin } from 'vite'
const dirname = path.dirname(new URL(import.meta.url).pathname) const dirname = path.dirname(new URL(import.meta.url).pathname)
const resolve = (p: string) => path.resolve(dirname, '../../packages', p) const resolve = (/** @type {string} */ p) =>
path.resolve(dirname, '../../packages', p)
export function DevPlugin(): Plugin { /** @returns {import('vite').Plugin} */
export function DevPlugin() {
return { return {
name: 'dev-plugin', name: 'dev-plugin',
config() { config() {

View File

@ -1,16 +1,20 @@
// @ts-check
import { createServer, createLogger } from 'vite' import { createServer, createLogger } from 'vite'
import { ViteNodeServer } from 'vite-node/server' import { ViteNodeServer } from 'vite-node/server'
import { ViteNodeRunner } from 'vite-node/client' import { ViteNodeRunner } from 'vite-node/client'
import { reload } from 'vite-node/hmr' import { reload } from 'vite-node/hmr'
import { installSourcemapsSupport } from 'vite-node/source-map' import { installSourcemapsSupport } from 'vite-node/source-map'
import { DevPlugin } from './dev' import { DevPlugin } from './dev.js'
const logger = createLogger(undefined, { export const logger = createLogger(undefined, {
prefix: '[vite-node]', prefix: '[vite-node]',
allowClearScreen: false allowClearScreen: false
}) })
export async function setupViteNode(onUpdate: () => void) { /**
* @param {() => void} onUpdate
*/
export async function setupViteNode(onUpdate) {
const server = await createServer({ const server = await createServer({
configFile: false, configFile: false,
optimizeDeps: { disabled: true }, optimizeDeps: { disabled: true },

58
playground/setup/vite.js Normal file
View File

@ -0,0 +1,58 @@
// @ts-check
import path from 'node:path'
import { createServer, version } from 'vite'
import { setupViteNode, logger } from './vite-node.js'
import colors from 'picocolors'
import minimist from 'minimist'
const dirname = path.dirname(new URL(import.meta.url).pathname)
main()
async function main() {
logger.info('Starting server...', { timestamp: true })
const runner = await setupViteNode(async () => {
const VuePlugin = await getVuePlugin()
server.config.inlineConfig.plugins = [VuePlugin]
server.restart()
})
const VuePlugin = await getVuePlugin()
const server = await startViteServer({
plugins: [VuePlugin]
})
async function getVuePlugin() {
/** @type { typeof import('./vue-plugin') } */
const mod = await runner.executeId(path.resolve(dirname, 'vue-plugin.ts'))
return mod.VuePlugin
}
}
/**
* @param {import('vite').InlineConfig} inlineConfig
*/
async function startViteServer(inlineConfig) {
const args = minimist(process.argv.slice(2))
const server = await createServer({
configFile: args.c || args.config,
logLevel: args.l || args.logLevel,
optimizeDeps: { force: args.force },
server: {
host: args.host,
port: args.port,
open: args.open,
cors: args.cors,
strictPort: args.strictPort
},
...inlineConfig
})
await server.listen()
server.config.logger.info(
`\n ${colors.green(`${colors.bold('VITE')} v${version}`)}\n`
)
server.printUrls()
server.bindCLIShortcuts({
print: true
})
return server
}

View File

@ -1,30 +0,0 @@
import path from 'node:path'
import { createServer } from 'vite'
import { setupViteNode } from './vite-node'
const dirname = path.dirname(new URL(import.meta.url).pathname)
main()
async function main() {
const runner = await setupViteNode(async () => {
const VuePlugin = await getVuePlugin()
server.config.inlineConfig.plugins = [VuePlugin]
server.restart()
})
const VuePlugin = await getVuePlugin()
const server = await createServer({
plugins: [VuePlugin]
})
await server.listen()
server.printUrls()
server.bindCLIShortcuts({
print: true
})
async function getVuePlugin() {
const file = path.resolve(dirname, 'vue-plugin.ts')
const mod = (await runner.executeId(file)) as typeof import('./vue-plugin')
return mod.VuePlugin
}
}

View File

@ -446,9 +446,6 @@ importers:
'@vitejs/plugin-vue': '@vitejs/plugin-vue':
specifier: ^4.5.0 specifier: ^4.5.0
version: 4.5.0(vite@5.0.2)(vue@packages+vue) version: 4.5.0(vite@5.0.2)(vue@packages+vue)
tsx:
specifier: ^4.6.0
version: 4.6.0
vite: vite:
specifier: ^5.0.2 specifier: ^5.0.2
version: 5.0.2(@types/node@20.10.0)(terser@5.22.0) version: 5.0.2(@types/node@20.10.0)(terser@5.22.0)