Vue3.0的vue.config.js配置

module.exports = {
  baseUrl: "./", //1.默认为 "/":部署在一个域名的根路径上  ; 2. "./":所有的资源都会被链接为相对路径,这样打出来的包可以被部署在任意路径
  outputDir: "dist", //默认为 "dist",指打包后的资源放置的路径,放在dist文件夹下
  assetsDir: "static", //默认为:'' ,放置打包后生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
  indexPath: "index.html", //Default: 'index.html' ,指定生成的 index.html 的输出路径 (相对于 outputDir)
  filenameHashing: true, //Default: true ,生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存
  // pages:undefined,//在 multi-page 模式下构建应用
  lintOnSave: true, //Type: boolean|'error';Default: true; true:将 lint 错误输出为编译警告;'error':错误输出会导致编译失败
  runtimeCompiler: false, //Default: false, 设置为 true 后你就可以在 Vue 组件中使用 template 选项了,但是这会让你的应用额外增加 10kb 左右
  // transpileDependencies:[],//Default: [], 默认情况下 babel-loader 会忽略所有 node_modules 中的文件
  productionSourceMap: false, //Default: true, 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  // crossorigin: undefined, //Default: undefined, 设置生成的 HTML 中 <link rel="stylesheet"> 和 <script> 标签的 crossorigin 属性。
  // integrity: false, //Default: false,在生成的 HTML 中的 <link rel="stylesheet"> 和 <script> 标签上启用 Subresource Integrity
	configureWebpack:{
		devServer:{
			before(app){//相当于搭建了一个小型的服务器
				app.get("/api/getUsers",function (req,res) {
					res.json({
						code:"S",
						data:[{id:1,name:"bob",age:23},{id:2,name:"jack",age:24}]
					})
				})
				app.post("/api/setUsers",function (req,res) {
					res.json({
						code:200,
						msg:"修改成功"
					})
				})
			}
		}
	},//Type: Object | Function ;修改最终的配置,可以模拟服务器接口
  // chainWebpack:function(){},//允许对内部的 webpack 配置进行更细粒度的修改。
  css: {
    modules: false, //Default: false, 设置为 true 后你就可以去掉文件名中的 .module 并将所有的 *.(css|scss|sass|less|styl(us)?) 文件视为 CSS Modules 模块。
    sourceMap: false //Default: false, 是否为 CSS 开启 source map。设置为 true 之后可能会影响构建的性能。
    // extract: false, //Default: 生产环境下是 true,开发环境下是 false.是否将组件中的 CSS 提取至一个独立的 CSS 文件中
    // loaderOptions: {} //Default: {}, 向 CSS 相关的 loader 传递选项
  },
  chainWebpack: config => {
    config.resolve.alias
      .set("@", resolve("src"))
      .set("@v", resolve("src/views"))
      .set("@c", resolve("src/components")); /* 别名配置 */
    config.optimization.runtimeChunk("single");
  },
  devServer: {
        overlay:{   //去除eslint报警告
            warning:false,
            error:true
        },
        open: true,//启动默认浏览器打开
        host: "127.0.0.1",
        port: 8080,
        https: false,
        proxy: {//配置跨域
            '/api': {
                target: 'http://api.xxx.com/api/',//接口地址
                ws: true,
                changOrigin: true,//允许跨域
                secure: false,//是否支持https
                pathRewrite: {
                    '^/api': '' // /api表示http://xxx.com/api/
                }
            }
            
        }
    },

  // parallel: require("os").cpus().length > 1, //Default: require('os').cpus().length > 1, 是否为 Babel 或 TypeScript 使用 thread-loader
  // pwa:{},//向 PWA 插件传递选项
  pluginOptions: {},//可以用来传递任何第三方插件选项
};

常用配置

baseUrl: "./", //防止打包后,文件找不到
assetsDir: "static",//把打包文件放到static文件夹内
productionSourceMap: false,//加速生产环境构建,减小打包后包体积
devServer:{proxy:'http://api.test.com'} //根据个人需要配置该项

vue.config.js常用配置示例

module.exports = {
    //根据屏幕大小自动缩放适配
    css: {
        loaderOptions: {
            css: {
                // options here will be passed to css-loader
            },
            postcss: {
                // options here will be passed to postcss-loader
                plugins: [require('postcss-px2rem')({
                    remUnit: 192,  //屏幕适配
                })]
            }
        }
    },
    
    lintOnSave:false, //取消eslint检测
    productionSourceMap: false,  //设为false可减小打包后包体积,默认为true
    publicPath: '/cm/',  //配置域名根路径,从localhost/#/配置后成localhost/cm#/,注意路由应使用hash 
    devServer: {
        overlay:{
            warning:false,
            error:true
        },
        host: '0.0.0.0',
        port: 8060,  
        // open: true,  //自动打开浏览器
    },
    assetsDir: "static"  //设置打包后静态资源放在static文件夹里
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章