63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
// https://nuxt.com/docs/api/configuration/nuxt-config
|
|
// 每次热更新都会执行defineNuxtConfig
|
|
import {exec} from "node:child_process";
|
|
import open, {apps} from "open";
|
|
import type {AddressInfo} from "node:net";
|
|
|
|
|
|
export default defineNuxtConfig({
|
|
compatibilityDate: '2024-11-01',
|
|
devtools: { enabled: true },
|
|
modules: ['@nuxt/eslint', '@nuxtjs/tailwindcss'],
|
|
devServer: {
|
|
host: '0.0.0.0',
|
|
},
|
|
hooks: {
|
|
'listen': (server) => {
|
|
startBroswer(server.address())
|
|
},
|
|
}
|
|
})
|
|
|
|
// 首次启动浏览器
|
|
async function startBroswer(address: AddressInfo | string | null) {
|
|
if(!process.env.START){
|
|
if (address && typeof address !== 'string') {
|
|
console.log('Hooks: Listen')
|
|
const host = address.address === '::' ? 'localhost' : address.address
|
|
const port = address.port
|
|
const url = `http://${host}:${port}`;
|
|
let resd;
|
|
try {
|
|
resd = await open(url, {
|
|
app: {
|
|
name: apps.chrome,
|
|
arguments: [
|
|
'--remote-debugging-port=9222',
|
|
'--auto-open-devtools-for-tabs'
|
|
]
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error((e as Error).message)
|
|
switch (process.platform) {
|
|
case 'win32':
|
|
console.log('Windows 系统');
|
|
exec(`start ${url}`)
|
|
break;
|
|
case 'darwin':
|
|
console.log('macOS 系统');
|
|
exec(`open ${url}`)
|
|
break;
|
|
case 'linux':
|
|
console.log('Linux 系统');
|
|
exec(`xdg-open ${url}`)
|
|
break;
|
|
default:
|
|
console.log('其他系统:', process.platform);
|
|
}
|
|
}
|
|
process.env.START = String(true)
|
|
}
|
|
}
|
|
} |