详解见官方 vue cli 配置参考
# Vue.config.js 配置注释
| // Vue.config.js 配置选项 | |
| module.exports = { | |
|     //  基本路径 | |
| publicPath: "./", | |
|     //  构建时的输出目录 | |
| outputDir: "dist", | |
|     //  放置静态资源的目录 | |
| assetsDir: "static", | |
|     //  html 的输出路径 | |
| indexPath: "index.html", | |
|     // 文件名哈希 | |
| filenameHashing: true, | |
|     // 用于多页配置,默认是 undefined | |
| pages: { | |
| index: { | |
|             //page 的入口文件 | |
| entry: 'src/index/main.js', | |
|             // 模板文件 | |
| template: 'public/index.html', | |
|             // 在 dist/index.html 的输出文件 | |
| filename: 'index.html', | |
|             // 当使用页面 title 选项时, | |
|             //template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title> | |
| title: 'Index Page', | |
|             // 在这个页面中包含的块,默认情况下会包含 | |
|             // 提取出来的通用 chunk 和 vendor chunk。 | |
| chunks: ['chunk-vendors', 'chunk-common', 'index'] | |
| }, | |
|         // 当使用只有入口的字符串格式时, | |
|         // 模板文件默认是 `public/subpage.html` | |
|         // 如果不存在,就回退到 `public/index.html`。 | |
|         // 输出文件默认是 `subpage.html`。 | |
| subpage: 'src/subpage/main.js' | |
| }, | |
|     //  是否在保存的时候使用 `eslint-loader` 进行检查。 | |
| lintOnSave: true, | |
|     //  是否使用带有浏览器内编译器的完整构建版本 | |
| runtimeCompiler: false, | |
|     //  babel-loader 默认会跳过 node_modules 依赖。 | |
| transpileDependencies: [ /* string or regex */ ], | |
|     //  是否为生产环境构建生成 source map? | |
| productionSourceMap: false, | |
|     //  设置生成的 HTML 中 <link rel="stylesheet"> 和 <script> 标签的 crossorigin 属性。 | |
| crossorigin: "", | |
|     //  在生成的 HTML 中的 <link rel="stylesheet"> 和 <script> 标签上启用 Subresource Integrity (SRI)。 | |
| integrity: false, | |
|     //  调整内部的 webpack 配置 | |
| configureWebpack: () => {}, //(Object | Function) | |
| chainWebpack: () => {}, | |
|     // 配置 webpack-dev-server 行为。 | |
| devServer: { | |
| 	    //open 在 devServer 启动且第一次构建完成时,自动用我们的系统的默认浏览器去打开要开发的网页 | |
| open: process.platform === 'darwin', | |
|         // 默认是 localhost。如果你希望服务器外部可访问,指定如下 host: '0.0.0.0',设置之后之后可以访问 ip 地址 | |
| host: '0.0.0.0', | |
| port: 8080, | |
|         //hot 配置是否启用模块的热替换功能,devServer 的默认行为是在发现源代码被变更后,通过自动刷新整个页面来做到事实预览,开启 hot 后,将在不刷新整个页面的情况下通过新模块替换老模块来做到实时预览。 | |
| hot: true, | |
| https: false, | |
|         //hot 和 hotOnly 的区别是在某些模块不支持热更新的情况下,前者会自动刷新页面,后者不会刷新页面,而是在控制台输出热更新失败 | |
| hotOnly: false, | |
|         // 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/cli-service.md# 配置代理 | |
|         // 设置代理 | |
| proxy: { | |
| '/api': { | |
| target: "http://app.rmsdmedia.com", | |
|                 // 是否跨域 | |
| changeOrigin: true, | |
|                 //false 为 http 访问,true 为 https 访问 | |
| secure: false, | |
| pathRewrite: { | |
| "^/api": "" // 重写接口 | |
|                 } | |
| }, | |
| '/foo': { | |
| target: '<other_url>' | |
|             } | |
| }, // string | Object | |
| before: app => {} | |
| }, | |
|     // CSS 相关选项 | |
| css: { | |
|         // 将组件内的 CSS 提取到一个单独的 CSS 文件 (只用在生产环境中) | |
|         // 也可以是一个传递给 `extract-text-webpack-plugin` 的选项对象 | |
| extract: true, | |
|         // 是否开启 CSS source map? | |
| sourceMap: false, | |
|         // 为预处理器的 loader 传递自定义选项。比如传递给 | |
|         // Css-loader 时,使用 `{Css: { ...} }`。 | |
| loaderOptions: { | |
| css: { | |
|                 // 这里的选项会传递给 css-loader | |
| }, | |
| postcss: { | |
|                 // 这里的选项会传递给 postcss-loader | |
|             } | |
| }, | |
|         // 为所有的 CSS 及其预处理文件开启 CSS Modules。 | |
|         // 这个选项不会影响 `*.vue` 文件。 | |
| modules: false | |
| }, | |
|     // 在生产环境下为 Babel 和 TypeScript 使用 `thread-loader` | |
|     // 在多核机器下会默认开启。 | |
| parallel: require('os').cpus().length > 1, | |
|     // PWA 插件的选项。 | |
|     // 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli-plugin-pwa/README.md | |
| pwa: {}, | |
|     // 第三方插件的选项 | |
| pluginOptions: { | |
|         // ... | |
|     } | |
| } | 
# 个人常用配置
| 'use strict' | |
| const path = require('path') | |
| const defaultSettings = require('./src/settings.js') | |
| function resolve(dir) { | |
| return path.join(__dirname, dir) | |
| } | |
| const name = defaultSettings.title || 'vue Element Admin' // page title | |
| // If your port is set to 80, | |
| // use administrator privileges to execute the command line. | |
| // For example, Mac: sudo npm run | |
| // You can change the port by the following method: | |
| // port = 9527 npm run dev OR npm run dev --port = 9527 | |
| const port = process.env.port || process.env.npm_config_port || 9527 // dev port | |
| // All configuration item explanations can be find in https://cli.vuejs.org/config/ | |
| module.exports = { | |
|   /** | |
| * You will need to set publicPath if you plan to deploy your site under a sub path, | |
| * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/, | |
| * then publicPath should be set to "/bar/". | |
| * In most cases please use '/' !!! | |
| * Detail: https://cli.vuejs.org/config/#publicpath | |
| */ | |
| publicPath: '/', | |
| outputDir: 'dist', | |
| assetsDir: 'static', | |
| lintOnSave: process.env.NODE_ENV === 'development', | |
| productionSourceMap: false, | |
| devServer: { | |
| port: port, | |
| open: true, | |
| overlay: { | |
| warnings: false, | |
| errors: true | |
| }, | |
| before: require('./mock/mock-server.js') | |
| }, | |
| configureWebpack: { | |
|     // provide the app's title in webpack's name field, so that | |
|     // it can be accessed in index.html to inject the correct title. | |
| name: name, | |
| resolve: { | |
| alias: { | |
| '@': resolve('src') | |
|       } | |
|     } | |
| }, | |
| chainWebpack(config) { | |
|     // it can improve the speed of the first screen, it is recommended to turn on preload | |
|     // it can improve the speed of the first screen, it is recommended to turn on preload | |
| config.plugin('preload').tap(() => [ | |
|       { | |
| rel: 'preload', | |
|         // to ignore runtime.js | |
|         // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171 | |
| fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/], | |
| include: 'initial' | |
|       } | |
| ]) | |
|     // when there are many pages, it will cause too many meaningless requests | |
| config.plugins.delete('prefetch') | |
|     // set svg-sprite-loader | |
|     config.module | |
| .rule('svg') | |
| .exclude.add(resolve('src/icons')) | |
| .end() | |
|     config.module | |
| .rule('icons') | |
| .test(/\.svg$/) | |
| .include.add(resolve('src/icons')) | |
| .end() | |
| .use('svg-sprite-loader') | |
| .loader('svg-sprite-loader') | |
| .options({ | |
| symbolId: 'icon-[name]' | |
| }) | |
| .end() | |
| config | |
| .when(process.env.NODE_ENV !== 'development', | |
| config => { | |
| config | |
| .plugin('ScriptExtHtmlWebpackPlugin') | |
| .after('html') | |
| .use('script-ext-html-webpack-plugin', [{ | |
|             // `runtime` must same as runtimeChunk name. default is `runtime` | |
| inline: /runtime\..*\.js$/ | |
| }]) | |
| .end() | |
| config | |
| .optimization.splitChunks({ | |
| chunks: 'all', | |
| cacheGroups: { | |
| libs: { | |
| name: 'chunk-libs', | |
| test: /[\\/]node_modules[\\/]/, | |
| priority: 10, | |
| chunks: 'initial' // only package third parties that are initially dependent | |
| }, | |
| elementUI: { | |
| name: 'chunk-elementUI', // split elementUI into a single package | |
| priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app | |
| test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm | |
| }, | |
| commons: { | |
| name: 'chunk-commons', | |
| test: resolve('src/components'), // can customize your rules | |
| minChunks: 3, // minimum common number | |
| priority: 5, | |
| reuseExistingChunk: true | |
|                 } | |
|               } | |
| }) | |
|           // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk | |
| config.optimization.runtimeChunk('single') | |
|         } | |
|       ) | |
|   } | |
| } | 
