兼容性注意 Vite 需要 nodejs版本 >= 12.0.0
使用 NPM:
$ npm init vite@latest
使用 Yarn:
$ yarn create vite
你還可以通過(guò)附加的命令行選項(xiàng)直接指定項(xiàng)目名稱和你想要使用的模板。例如,要構(gòu)建一個(gè) Vite + Vue 項(xiàng)目,運(yùn)行:
# npm 6.x
npm init vite@latest my-vue-app --template vue
# npm 7+, 需要額外的雙橫線:
npm init vite@latest my-vue-app -- --template vue
# yarn
yarn create vite my-vue-app --template vue
# pnpm
pnpm create vite my-vue-app -- --template vue
查看 create-vite 以獲取每個(gè)模板的更多細(xì)節(jié):vanilla,vanilla-ts,vue,vue-ts,react,react-ts,preact,preact-ts,lit,lit-ts,svelte,svelte-ts。
在安裝了 Vite 的項(xiàng)目中,可以在 npm scripts 中使用 vite 可執(zhí)行文件,或者直接使用 npx vite 運(yùn)行它。下面是通過(guò)腳手架創(chuàng)建的 Vite 項(xiàng)目中默認(rèn)的 npm scripts
{
"scripts": {
"dev": "vite", // 啟動(dòng)開(kāi)發(fā)服務(wù)器,別名:`vite dev`,`vite serve`
"build": "vite build", // 為生產(chǎn)環(huán)境構(gòu)建產(chǎn)物
"preview": "vite preview" // 本地預(yù)覽生產(chǎn)構(gòu)建產(chǎn)物
}
}
可以指定額外的命令行選項(xiàng),如 --port 或 --https。運(yùn)行 npx vite --help 獲得完整的命令行選項(xiàng)列表。