vue-config.js配置,我們前面搭建好腳手架會發現,這個對比2.x版本少了很多東西,沒有build的配置,也沒有webpack的配置,那么問題來了,我們如何去開發我們的項目呢,比如設置代理,打包配置等問題呢?
vue cli3.0項目中需要配置其他參數時,需要新建文件'vue.config.js',(這文件名是固定這么寫的),與package.json在同一級目錄下。
module.exports = { // 項目部署的基礎路徑 // 我們默認假設你的應用將會部署在域名的根部, // 比如 https://www.my-app.com/ // 如果你的應用時部署在一個子路徑下,那么你需要在這里 // 指定子路徑。比如,如果你的應用部署在 // https://www.foobar.com/my-app/ // 那么將這個值改為 `/my-app/` publicPath: process.env.NODE_ENV === "production" ? "./" : "/", /*這個是我存放在github在線預覽的Reader項目*/ // chainWebpack: config => { // config // .module // .rule("images") // .test(/\.(jpg|png|gif)$/) // .use("url-loader") // .loader("url-loader") // .options({ // limit:10, // publicPath: 'https://oss.xx.com/img' , // outputPath: 'img', // name: '[name].[ext]', // }) // .end(); // }, // 將構建好的文件輸出到哪里(或者說將編譯的文件) outputDir: 'dist', // 放置靜態資源的地方 (js/css/img/font/...) assetsDir: '', // 用於多頁配置,默認是 undefined pages: { index: { // 入口文件 entry: 'src/main.js', /*這個是根入口文件*/ // 模板文件 template: 'public/index.html', // 輸出文件 filename: 'index.html', // 頁面title title: 'Index Page' }, sec: { entry: 'src/main.js', template: 'public/index.html', filename: 'sec.html', title: 'sec Page' }, // 簡寫格式 // 模板文件默認是 `public/subpage.html` // 如果不存在,就是 `public/index.html`. // 輸出文件默認是 `subpage.html`. subpage: 'src/main.js' /*注意這個是*/ }, // 是否在保存的時候使用 `eslint-loader` 進行檢查。 // 有效的值:`ture` | `false` | `"error"` // 當設置為 `"error"` 時,檢查出的錯誤會觸發編譯失敗。 lintOnSave: true, // 使用帶有瀏覽器內編譯器的完整構建版本 // 查閱 https://cn.vuejs.org/v2/guide/installation.html#運行時-編譯器-vs-只包含運行時 runtimeCompiler: false, // babel-loader 默認會跳過 node_modules 依賴。 // 通過這個選項可以顯式轉譯一個依賴。 transpileDependencies: [/* string or regex */], // 是否為生產環境構建生成 source map? productionSourceMap: true, // 調整內部的 webpack 配置。 // 查閱 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/webpack.md chainWebpack: (config) => { config .module .rule("images") .test(/\.(jpg|png|gif)$/) .use("url-loader") .loader("url-loader") .options({ limit:10, publicPath: 'https://oss.xx.com/img' , outputPath: 'img', name: '[name].[ext]', }) .end(); }, configureWebpack: () => {}, // CSS 相關選項 css: { // 將組件內的 CSS 提取到一個單獨的 CSS 文件 (只用在生產環境中) // 也可以是一個傳遞給 `extract-text-webpack-plugin` 的選項對象 extract: true, // 是否開啟 CSS source map? sourceMap: false, // 為預處理器的 loader 傳遞自定義選項。比如傳遞給 // sass-loader 時,使用 `{ sass: { ... } }`。 loaderOptions: {}, // 為所有的 CSS 及其預處理文件開啟 CSS Modules。 // 這個選項不會影響 `*.vue` 文件。 modules: false }, // 在生產環境下為 Babel 和 TypeScript 使用 `thread-loader` // 在多核機器下會默認開啟。 parallel: require('os').cpus().length > 1, // PWA 插件的選項。 // 查閱 https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-plugin-pwa pwa: {}, // 配置 webpack-dev-server 行為。 devServer: { open: process.platform === 'darwin', host: '0.0.0.0', port: 8080, https: false, hotOnly: false, // 查閱 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/cli-service.md#配置代理 proxy: { '/api': { target: 'http://localhost:8880', changeOrigin: true, secure: false, // ws: true, pathRewrite: { '^/api': '/static/mock' // 請求數據路徑別名,這里是注意將static/mock放入public文件夾 } } }, before: app => {} }, // 三方插件的選項 pluginOptions: { // ... } }
多頁面配置:

// 用於多頁配置,默認是 undefined pages: { index: { // 入口文件 entry: 'src/main.js', /*這個是根入口文件*/ // 模板文件 template: 'public/index.html', // 輸出文件 filename: 'index.html', // 頁面title title: 'Index Page' }, sec: { entry: 'src/main.js', template: 'public/index.html', filename: 'sec.html', title: 'sec Page' }, // 簡寫格式 // 模板文件默認是 `public/subpage.html` // 如果不存在,就是 `public/index.html`. // 輸出文件默認是 `subpage.html`. subpage: 'src/main.js' /*注意這個是*/ },
打包后圖片文件的引用路徑改為cdn路徑

chainWebpack: (config) => { config .module .rule("images") .test(/\.(jpg|png|gif)$/) .use("url-loader") .loader("url-loader") .options({ limit:10, publicPath: 'https://oss.xx.com/img' , outputPath: 'img', name: '[name].[ext]', }) .end(); },
但是這樣配置的話, 不管開發還是生產環境下都會將引用路徑修改為cdn路徑, 而我們的需求是只在生產環境下使用cdn, 開發環境下使用相對路徑,
因此使用process.env.NODE_ENV判斷項目環境
這里我們把相關選項寫在了url-loader里, url-loader本身的作用是將圖片引用方式轉換為base64的內聯引用方式,
通過配置limit, 可使文件大小小於此limit值(單位為byte)的文件轉換為base64格式, 大於此limit的, 會執行options中的fallback配置項插件,
fallback默認值為file-loader, 而且url-loader的options配置項也會被傳遞給file-loader.(查看文檔)
最終代碼:
chainWebpack: config => { config .module .rule("images") .test(/\.(jpg|png|gif)$/) .use("url-loader") .loader("url-loader") .options({ limit:10, // 以下配置項用於配置file-loader // 根據環境使用cdn或相對路徑 publicPath: process.env.NODE_ENV === 'production' ? 'https://oss.xx.com/img' : './', // 將圖片打包到dist/img文件夾下, 不配置則打包到dist文件夾下 outputPath: 'img', // 配置打包后圖片文件名 name: '[name].[ext]', }) .end(); }