|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const isWin = process.platform === 'win32' | 
|---|
|  |  |  | const appPath = slash(path.resolve(process.cwd(), 'app')) | 
|---|
|  |  |  | const runGoServer = () => { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const getPort: () => Promise<string> = () => { | 
|---|
|  |  |  | const child: ChildProcess = crossSpawn( | 
|---|
|  |  |  | isWin | 
|---|
|  |  |  | ? '..\\node_modules\\lmes-create-widget\\www' | 
|---|
|  |  |  | : '../node_modules/lmes-create-widget/www', | 
|---|
|  |  |  | ? '..\\node_modules\\lmes-create-widget\\port' | 
|---|
|  |  |  | : '../node_modules/lmes-create-widget/port', | 
|---|
|  |  |  | [], //添加端口和环境变量参数 | 
|---|
|  |  |  | { | 
|---|
|  |  |  | cwd: appPath, | 
|---|
|  |  |  | shell: true, | 
|---|
|  |  |  | env: { | 
|---|
|  |  |  | ENV: 'production', | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | let output = '' | 
|---|
|  |  |  | return new Promise((resolve, reject) => { | 
|---|
|  |  |  | // 获取子进程的标准输出 | 
|---|
|  |  |  | child.stdout?.on('data', (data) => { | 
|---|
|  |  |  | output += data.toString() | 
|---|
|  |  |  | }) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | child.stderr?.on('data', (data) => { | 
|---|
|  |  |  | console.error('stderr:', data.toString()) | 
|---|
|  |  |  | }) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | child.on('close', (code) => { | 
|---|
|  |  |  | if (code === 0) { | 
|---|
|  |  |  | const port = output.split('\n')[0] | 
|---|
|  |  |  | console.log('PROJECT_PORT:', port) | 
|---|
|  |  |  | resolve(port) | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | reject(code) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const runGoServer = (projectPort: string) => { | 
|---|
|  |  |  | const child: ChildProcess = crossSpawn( | 
|---|
|  |  |  | isWin | 
|---|
|  |  |  | ? `..\\node_modules\\lmes-create-widget\\www ${projectPort}` | 
|---|
|  |  |  | : `../node_modules/lmes-create-widget/www ${projectPort}`, | 
|---|
|  |  |  | [], //添加端口和环境变量参数 | 
|---|
|  |  |  | { | 
|---|
|  |  |  | stdio: 'inherit', | 
|---|
|  |  |  | cwd: appPath, | 
|---|
|  |  |  | 
|---|
|  |  |  | process.exit(0) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | const run = () => { | 
|---|
|  |  |  | const run = (projectPort: string) => { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | runGoServer() | 
|---|
|  |  |  | runGoServer(projectPort) | 
|---|
|  |  |  | let lastPressTime = Date.now() | 
|---|
|  |  |  | process.on('SIGINT', async () => { | 
|---|
|  |  |  | const currentTime = Date.now() | 
|---|
|  |  |  | if (lastPressTime && currentTime - lastPressTime < 2000) { | 
|---|
|  |  |  | killGoServer(8800) | 
|---|
|  |  |  | killGoServer(Number(projectPort)) | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | lastPressTime = Date.now() | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | process.on('SIGTERM', () => { | 
|---|
|  |  |  | console.log('进程被 kill 终止...') | 
|---|
|  |  |  | killGoServer(8800) | 
|---|
|  |  |  | killGoServer(Number(projectPort)) | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | } catch (error) { | 
|---|
|  |  |  | console.error(error) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | run() | 
|---|
|  |  |  | // @ts-ignore | 
|---|
|  |  |  | export default defineConfig(({ mode }) => { | 
|---|
|  |  |  | export default defineConfig(async ({ mode }) => { | 
|---|
|  |  |  | const projectPort = await getPort() | 
|---|
|  |  |  | run(projectPort) | 
|---|
|  |  |  | const env = loadEnv(mode, __dirname) | 
|---|
|  |  |  | console.info('[mode]', mode) | 
|---|
|  |  |  | console.info('[env]', env) | 
|---|
|  |  |  | 
|---|
|  |  |  | // requireTransform({}), | 
|---|
|  |  |  | vueJsx(), | 
|---|
|  |  |  | vue({ | 
|---|
|  |  |  | // @ts-ignore | 
|---|
|  |  |  | reactivityTransform: path.resolve(__dirname, 'src'), | 
|---|
|  |  |  | }), | 
|---|
|  |  |  | VueTypeImports(), | 
|---|
|  |  |  | 
|---|
|  |  |  | ws: true, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | '/projectApi': { | 
|---|
|  |  |  | target: env.VITE_PROJECT_API_URL, | 
|---|
|  |  |  | target: `http://127.0.0.1:${projectPort}`, | 
|---|
|  |  |  | changeOrigin: true, | 
|---|
|  |  |  | ws: true, | 
|---|
|  |  |  | }, | 
|---|