]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(playground): extract logic
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 29 Nov 2023 21:54:27 +0000 (05:54 +0800)
committer三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 29 Nov 2023 22:19:35 +0000 (06:19 +0800)
playground/package.json
playground/setup/dev.js [moved from playground/setup/dev.ts with 91% similarity]
playground/setup/vite-node.js [moved from playground/setup/vite-node.ts with 86% similarity]
playground/setup/vite.js [new file with mode: 0644]
playground/setup/vite.ts [deleted file]
pnpm-lock.yaml

index ec563071c22ff8e315ae32f53e4891547f07fbd3..02f0f75b070c0ecb6d6b0e0b77419dbe450a7698 100644 (file)
@@ -3,7 +3,7 @@
   "version": "0.0.0",
   "type": "module",
   "scripts": {
-    "dev": "tsx ./setup/vite.ts",
+    "dev": "node ./setup/vite.js",
     "build": "vite build"
   },
   "dependencies": {
@@ -11,7 +11,6 @@
   },
   "devDependencies": {
     "@vitejs/plugin-vue": "^4.5.0",
-    "tsx": "^4.6.0",
     "vite": "^5.0.2",
     "vite-node": "^0.34.6",
     "vite-plugin-inspect": "^0.7.42"
similarity index 91%
rename from playground/setup/dev.ts
rename to playground/setup/dev.js
index 98dd7c839dd3d0084147045f6dbd85f528088efa..f68ac01a783161f2ab41dce6eedff9b0f864a1ca 100644 (file)
@@ -1,10 +1,12 @@
+// @ts-check
 import path from 'node:path'
-import type { Plugin } from 'vite'
 
 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 {
     name: 'dev-plugin',
     config() {
similarity index 86%
rename from playground/setup/vite-node.ts
rename to playground/setup/vite-node.js
index 162f5c07c9c0243e79ea060f133469674f145452..ac956b9735504751e6801f5134f89ebe395483f9 100644 (file)
@@ -1,16 +1,20 @@
+// @ts-check
 import { createServer, createLogger } from 'vite'
 import { ViteNodeServer } from 'vite-node/server'
 import { ViteNodeRunner } from 'vite-node/client'
 import { reload } from 'vite-node/hmr'
 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]',
   allowClearScreen: false
 })
 
-export async function setupViteNode(onUpdate: () => void) {
+/**
+ * @param {() => void} onUpdate
+ */
+export async function setupViteNode(onUpdate) {
   const server = await createServer({
     configFile: false,
     optimizeDeps: { disabled: true },
diff --git a/playground/setup/vite.js b/playground/setup/vite.js
new file mode 100644 (file)
index 0000000..053c536
--- /dev/null
@@ -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
+}
diff --git a/playground/setup/vite.ts b/playground/setup/vite.ts
deleted file mode 100644 (file)
index d85e8ba..0000000
+++ /dev/null
@@ -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
-  }
-}
index 365eb1e3d2f2176f57b1dcfde833981c691016ac..612cd719e6fd5d7230f8a4f84a7a9a32eb651dd7 100644 (file)
@@ -446,9 +446,6 @@ importers:
       '@vitejs/plugin-vue':
         specifier: ^4.5.0
         version: 4.5.0(vite@5.0.2)(vue@packages+vue)
-      tsx:
-        specifier: ^4.6.0
-        version: 4.6.0
       vite:
         specifier: ^5.0.2
         version: 5.0.2(@types/node@20.10.0)(terser@5.22.0)