使用命令
在一個 Vue CLI 項目中,@vue/cli-service 安裝了一個名爲 vue-cli-service 的命令。你可以在 npm scripts 中以 vue-cli-service、或者從終端中以 ./node_modules/.bin/vue-cli-service 訪問這個命令。
這是你使用默認 preset 的項目的 package.json:
{
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build"
}
}
你可以通過 npm 或 Yarn 調用這些 script:
npm run serve
# OR
yarn serve
如果你可以使用 npx (最新版的 npm 應該已經自帶),也可以直接這樣調用命令:
npx vue-cli-service serve
vue-cli-service serve
用法:vue-cli-service serve [options] [entry]
選項:
--open 在服務器啓動時打開瀏覽器
--copy 在服務器啓動時將 URL 複製到剪切版
--mode 指定環境模式 (默認值:development)
--host 指定 host (默認值:0.0.0.0)
--port 指定 port (默認值:8080)
--https 使用 https (默認值:false)
vue-cli-service serve
命令會啓動一個開發服務器 (基於 webpack-dev-server) 並附帶開箱即用的模塊熱重載 (Hot-Module-Replacement)。
除了通過命令行參數,你也可以使用 vue.config.js
裏的 –devServer 字段配置開發服務器。
命令行參數 [entry]
將被指定爲唯一入口,而非額外的追加入口。嘗試使用 [entry]
覆蓋 config.pages
中的 entry
將可能引發錯誤。
vue-cli-service build
用法:vue-cli-service build [options] [entry|pattern]
選項:
--mode 指定環境模式 (默認值:production)
--dest 指定輸出目錄 (默認值:dist)
--modern 面向現代瀏覽器帶自動回退地構建應用
--target app | lib | wc | wc-async (默認值:app)
--name 庫或 Web Components 模式下的名字 (默認值:package.json 中的 "name" 字段或入口文件名)
--no-clean 在構建項目之前不清除目標目錄
--report 生成 report.html 以幫助分析包內容
--report-json 生成 report.json 以幫助分析包內容
--watch 監聽文件變化
vue-cli-service build
會在 dist/
目錄產生一個可用於生產環境的包,帶有 JS/CSS/HTML 的壓縮,和爲更好的緩存而做的自動的 vendor chunk splitting。它的 chunk manifest 會內聯在 HTML 裏。
這裏還有一些有用的命令參數:
--modern
使用現代模式構建應用,爲現代瀏覽器交付原生支持的 ES2015 代碼,並生成一個兼容老瀏覽器的包用來自動回退。
--target
允許你將項目中的任何組件以一個庫或 Web Components 組件的方式進行構建。更多細節請查閱構建目標。
--report
和 --report-json
會根據構建統計生成報告,它會幫助你分析包中包含的模塊們的大小。
vue-cli-service inspect
用法:vue-cli-service inspect [options] [...paths]
選項:
--mode 指定環境模式 (默認值:development)
你可以使用 vue-cli-service inspect
來審查一個 Vue CLI 項目的 webpack config。
查看所有的可用命令
有些 CLI 插件會向 vue-cli-service 注入額外的命令。例如 @vue/cli-plugin-eslint 會注入 vue-cli-service lint 命令。你可以運行以下命令查看所有注入的命令:
npx vue-cli-service help
你也可以這樣學習每個命令可用的選項:
npx vue-cli-service help [command]