vue中的config目录下index.js解读

vue的config目录下index.js

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

// 用于处理路径统一的问题
const path = require('path')

module.exports = {
 // 开发环境的配置
 dev: {
 // Paths
 assetsSubDirectory: 'static', // 静态资源文件夹
 assetsPublicPath: '/', // 发布路径
 // 一般解决跨域请求api
 proxyTable: {
 '/api': {
 target: 'http://api.douban.com/v2', // 目标url
 changeOrigin: true, // 是否跨域
 pathRewrite: {
 '^/api': '' // 可以使用 /api 等价于 http://api.douban.com/v2
 }
 }
 },

 // Various Dev Server settings
 host: 'localhost', // can be overwritten by process.env.HOST
 port: 8080, // dev-server的端口号,可以自行更改
 autoOpenBrowser: false, // 是否自定代开浏览器
 errorOverlay: true, // 查询错误
 notifyOnErrors: true, // 通知错误
 poll: false, // poll轮询,webpack为我们提供devserver是可以监控文件改动的,有些情况下却不能工作,可以设置一个轮询解决

 
 /**
 * Source Maps
 */

 // https://webpack.js.org/configuration/devtool/#development
 devtool: 'cheap-module-eval-source-map', // webpack用于方便调试的配置

 // If you have problems debugging vue-files in devtools,
 // set this to false - it *may* help
 // https://vue-loader.vuejs.org/en/options.html#cachebusting
 cacheBusting: true, // devtool的配置当文件名插入新的hash导致清除缓存时是否生成source maps,默认为true

 cssSourceMap: true // 是否开启cssSourceMap
 },
 // 生产编译环境下的一些配置
 build: {
 // 下面是相对路径的拼接
 index: path.resolve(__dirname, '../dist/index.html'),

 // 下面定义的是静态资源的根目录 也就是dist目录
 assetsRoot: path.resolve(__dirname, '../dist'),
 assetsSubDirectory: 'static',
 assetsPublicPath: '/', // 下面定义的是静态资源的公开路径,也就是真正的引用路径

 /**
 * Source Maps
 */

 productionSourceMap: true,
 // https://webpack.js.org/configuration/devtool/#production
 devtool: '#source-map',

 // Gzip off by default as many popular static hosts such as
 // Surge or Netlify already gzip all static assets for you.
 // Before setting to `true`, make sure to:
 // npm install --save-dev compression-webpack-plugin
 productionGzip: false, // 是否在生产环境中压缩代码,如果要压缩必须安装compression-webpack-plugin
 productionGzipExtensions: ['js', 'css'], // 定义要压缩哪些类型的文件

 // Run the build command with an extra argument to
 // View the bundle analyzer report after build finishes:
 // `npm run build --report`
 // Set to `true` or `false` to always turn it on or off
 bundleAnalyzerReport: process.env.npm_config_report // 是否开启打包后的分析报告
 }
}

config中的 index.js配置项

config中index.js文件是用来配置开发环境和生产环境的配置参数 

index.js:

const path = require('path')
 
module.exports = {
  build: {       // production 环境
    env: require('./prod.env'), // 使用 config/prod.env.js 中定义的编译环境
    index: path.resolve(__dirname, '../dist/index.html'),//编译输入的 index.html 文件, __dirname 是node的一个全局变量,获得当前文件所在目录的完整目录名
    assetsRoot: path.resolve(__dirname, '../dist'),// 编译输出的静态资源路径
    assetsSubDirectory: 'static',// 编译输出的二级目录
    assetsPublicPath: '/',// 编译发布的根目录,可配置为资源服务器域名或 CDN 域名
    productionSourceMap: false,// 是否开启 cssSourceMap
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,// 是否开启 gzip
    productionGzipExtensions: ['js', 'css'],// 需要使用 gzip 压缩的文件扩展名
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  },
  dev: {// dev 环境
    env: require('./dev.env'),// 使用 config/dev.env.js 中定义的编译环境
    port: process.env.PORT || 8080,// 运行测试页面的端口
    autoOpenBrowser: true,//自动打开浏览器
    assetsSubDirectory: 'static',// 编译输出的二级目录
    assetsPublicPath: '/', // 编译发布的根目录,可配置为资源服务器域名或 CDN 域名
    proxyTable: {}, // 需要 proxyTable 代理的接口(可跨域)
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false // 是否开启 cssSourceMap
  }
}

config/prod.env.js :

module.exports = {
  NODE_ENV: '"production"'
}

config/dev.env.js

onst merge = require('webpack-merge')
const prodEnv = require('./prod.env')
 
module.exports = merge(prodEnv, {
  NODE_ENV: '"development"'
})

关于cssSourceMap的作用是,随着代码增多,我们需要对代码进行压缩。

代码压缩后进行调bug定位将非常困难,于是引入sourcemap记录压缩前后的位置信息记录,当产生错误时直接定位到未压缩前的位置,将大大的方便我们调试。

总结

作者:牛犊6原文地址:https://blog.csdn.net/weixin_45793819/article/details/115272365

%s 个评论

要回复文章请先登录注册