vue中config目录下index.js源码分析

前端开发   发布日期:2023年09月15日   浏览次数:487

这篇文章主要介绍“vue中config目录下index.js源码分析”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue中config目录下index.js源码分析”文章能帮助大家解决问题。

vue的config目录下index.js

  1. 'use strict'
  2. // Template version: 1.3.1
  3. // see http://vuejs-templates.github.io/webpack for documentation.
  4. // 用于处理路径统一的问题
  5. const path = require('path')
  6. module.exports = {
  7. // 开发环境的配置
  8. dev: {
  9. // Paths
  10. assetsSubDirectory: 'static', // 静态资源文件夹
  11. assetsPublicPath: '/', // 发布路径
  12. // 一般解决跨域请求api
  13. proxyTable: {
  14. '/api': {
  15. target: 'http://api.douban.com/v2', // 目标url
  16. changeOrigin: true, // 是否跨域
  17. pathRewrite: {
  18. '^/api': '' // 可以使用 /api 等价于 http://api.douban.com/v2
  19. }
  20. }
  21. },
  22. // Various Dev Server settings
  23. host: 'localhost', // can be overwritten by process.env.HOST
  24. port: 8080, // dev-server的端口号,可以自行更改
  25. autoOpenBrowser: false, // 是否自定代开浏览器
  26. errorOverlay: true, // 查询错误
  27. notifyOnErrors: true, // 通知错误
  28. poll: false, // poll轮询,webpack为我们提供devserver是可以监控文件改动的,有些情况下却不能工作,可以设置一个轮询解决
  29. /**
  30. * Source Maps
  31. */
  32. // https://webpack.js.org/configuration/devtool/#development
  33. devtool: 'cheap-module-eval-source-map', // webpack用于方便调试的配置
  34. // If you have problems debugging vue-files in devtools,
  35. // set this to false - it *may* help
  36. // https://vue-loader.vuejs.org/en/options.html#cachebusting
  37. cacheBusting: true, // devtool的配置当文件名插入新的hash导致清除缓存时是否生成source maps,默认为true
  38. cssSourceMap: true // 是否开启cssSourceMap
  39. },
  40. // 生产编译环境下的一些配置
  41. build: {
  42. // 下面是相对路径的拼接
  43. index: path.resolve(__dirname, '../dist/index.html'),
  44. // 下面定义的是静态资源的根目录 也就是dist目录
  45. assetsRoot: path.resolve(__dirname, '../dist'),
  46. assetsSubDirectory: 'static',
  47. assetsPublicPath: '/', // 下面定义的是静态资源的公开路径,也就是真正的引用路径
  48. /**
  49. * Source Maps
  50. */
  51. productionSourceMap: true,
  52. // https://webpack.js.org/configuration/devtool/#production
  53. devtool: '#source-map',
  54. // Gzip off by default as many popular static hosts such as
  55. // Surge or Netlify already gzip all static assets for you.
  56. // Before setting to `true`, make sure to:
  57. // npm install --save-dev compression-webpack-plugin
  58. productionGzip: false, // 是否在生产环境中压缩代码,如果要压缩必须安装compression-webpack-plugin
  59. productionGzipExtensions: ['js', 'css'], // 定义要压缩哪些类型的文件
  60. // Run the build command with an extra argument to
  61. // View the bundle analyzer report after build finishes:
  62. // `npm run build --report`
  63. // Set to `true` or `false` to always turn it on or off
  64. bundleAnalyzerReport: process.env.npm_config_report // 是否开启打包后的分析报告
  65. }
  66. }

config中的 index.js配置项

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

index.js:

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

config/prod.env.js :

  1. module.exports = {
  2. NODE_ENV: '"production"'
  3. }

config/dev.env.js

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

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

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

以上就是vue中config目录下index.js源码分析的详细内容,更多关于vue中config目录下index.js源码分析的资料请关注九品源码其它相关文章!