当我们需要和后台分离部署的时候,必须配置config/index.js:

用vue-cli 自动构建的目录里面  (环境变量及其基本变量的配置)

  1. var path = require('path')
  2.  
  3. module.exports = {
  4. build: {
  5. index: path.resolve(__dirname, 'dist/index.html'),
  6. assetsRoot: path.resolve(__dirname, 'dist'),
  7. assetsSubDirectory: 'static',
  8. assetsPublicPath: '/',
  9. productionSourceMap: true
  10. },
  11. dev: {
  12. port: 8080,
  13. proxyTable: {}
  14. }
  15. }

  

在'build'部分,我们有以下选项:

build.index

必须是本地文件系统上的绝对路径。

index.html (带着插入的资源路径) 会被生成。

如果你在后台框架中使用此模板,你可以编辑index.html路径指定到你的后台程序生成的文件。例如Rails程序,可以是app/views/layouts/application.html.erb,或者Laravel程序,可以是resources/views/index.blade.php

build.assetsRoot

必须是本地文件系统上的绝对路径。

应该指向包含应用程序的所有静态资产的根目录。public/ 对应Rails/Laravel。

build.assetsSubDirectory

被webpack编译处理过的资源文件都会在这个build.assetsRoot目录下,所以它不可以混有其它可能在build.assetsRoot里面有的文件。例如,假如build.assetsRoot参数是/path/to/distbuild.assetsSubDirectory 参数是 static, 那么所以webpack资源会被编译到path/to/dist/static目录。

每次编译前,这个目录会被清空,所以这个只能放编译出来的资源文件。

static/目录的文件会直接被在构建过程中,直接拷贝到这个目录。这意味着是如果你改变这个规则,所有你依赖于static/中文件的绝对地址,都需要改变。

build.assetsPublicPath【资源的根目录】

这个是通过http服务器运行的url路径。在大多数情况下,这个是根目录(/)。如果你的后台框架对静态资源url前缀要求,你仅需要改变这个参数。在内部,这个是被webpack当做output.publicPath来处理的。

后台有要求的话一般要加上./ 或者根据具体目录添加,不然引用不到静态资源

build.productionSourceMap

在构建生产环境版本时是否开启source map。

dev.port

开发服务器监听的特定端口

dev.proxyTable

定义开发服务器的代理规则。

项目中配置的config/index.js,有dev和production两种环境的配置 以下介绍的是production环境下的webpack配置的理解

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

下面是vue中的build/webpack.base.conf.js

  1. //引入依赖模块
  2. var path = require('path')
  3. var config = require('../config') // 获取配置
  4. var utils = require('./utils')
  5. var projectRoot = path.resolve(__dirname, '../')
  6.  
  7. var env = process.env.NODE_ENV
  8. // check env & config/index.js to decide weither to enable CSS Sourcemaps for the
  9. // various preprocessor loaders added to vue-loader at the end of this file
  10. var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)/* 是否在 dev 环境下开启 cssSourceMap ,在 config/index.js 中可配置 */
  11. var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)/* 是否在 production 环境下开启 cssSourceMap ,在 config/index.js 中可配置 */
  12. var useCssSourceMap = cssSourceMapDev || cssSourceMapProd /* 最终是否使用 cssSourceMap */
  13.  
  14. module.exports = {
  15. entry: { // 配置webpack编译入口
  16. app: './src/main.js'
  17. },
  18. output: { // 配置webpack输出路径和命名规则
  19. path: config.build.assetsRoot, // webpack输出的目标文件夹路径(例如:/dist)
  20. publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath, // webpack编译输出的发布路径(判断是正式环境或者开发环境等)
  21. filename: '[name].js' // webpack输出bundle文件命名格式,基于文件的md5生成Hash名称的script来防止缓存
  22. },
  23. resolve: {
  24. extensions: ['', '.js', '.vue', '.scss'], //自动解析确定的拓展名,使导入模块时不带拓展名
  25. fallback: [path.join(__dirname, '../node_modules')],
  26. alias: { // 创建import或require的别名,一些常用的,路径长的都可以用别名
  27. 'vue$': 'vue/dist/vue',
  28. 'src': path.resolve(__dirname, '../src'),
  29. 'assets': path.resolve(__dirname, '../src/assets'),
  30. 'components': path.resolve(__dirname, '../src/components'),
  31. 'scss_vars': path.resolve(__dirname, '../src/styles/vars.scss')
  32. }
  33. },
  34. resolveLoader: {
  35. fallback: [path.join(__dirname, '../node_modules')]
  36. },
  37. module: {
  38. loaders: [
  39. {
  40. test: /\.vue$/, // vue文件后缀
  41. loader: 'vue' //使用vue-loader处理
  42. },
  43. {
  44. test: /\.js$/,
  45. loader: 'babel',
  46. include: projectRoot,
  47. exclude: /node_modules/
  48. },
  49. {
  50. test: /\.json$/,
  51. loader: 'json'
  52. },
  53. {
  54. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  55. loader: 'url',
  56. query: {
  57. limit: 10000,
  58. name: utils.assetsPath('img/[name].[hash:7].[ext]')
  59. }
  60. },
  61. {
  62. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  63. loader: 'url',
  64. query: {
  65. limit: 10000,
  66. name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
  67. }
  68. }
  69. ]
  70. },
  71. vue: { // .vue 文件配置 loader 及工具 (autoprefixer)
  72. loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }), //// 调用cssLoaders方法返回各类型的样式对象(css: loader)
  73. postcss: [
  74. require('autoprefixer')({
  75. browsers: ['last 2 versions']
  76. })
  77. ]
  78. }
  79. }

  webpack.prod.conf.js 生产环境下的配置文件

  1. var path = require('path')
  2. var config = require('../config')
  3. var utils = require('./utils')
  4. var webpack = require('webpack')
  5. var merge = require('webpack-merge')// 一个可以合并数组和对象的插件
  6. var baseWebpackConfig = require('./webpack.base.conf')
  7. // 用于从webpack生成的bundle中提取文本到特定文件中的插件
  8. // 可以抽取出css,js文件将其与webpack输出的bundle分离
  9. var ExtractTextPlugin = require('extract-text-webpack-plugin') //如果我们想用webpack打包成一个文件,css js分离开,需要这个插件
  10. var HtmlWebpackPlugin = require('html-webpack-plugin')// 一个用于生成HTML文件并自动注入依赖文件(link/script)的webpack插件
  11. var env = config.build.env
  12. // 合并基础的webpack配置
  13. var webpackConfig = merge(baseWebpackConfig, {
  14. // 配置样式文件的处理规则,使用styleLoaders
  15. module: {
  16. loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true })
  17. },
  18. devtool: config.build.productionSourceMap ? '#source-map' : false, // 开启source-map,生产环境下推荐使用cheap-source-map或source-map,后者得到的.map文件体积比较大,但是能够完全还原以前的js代码
  19. output: {
  20. path: config.build.assetsRoot,// 编译输出目录
  21. filename: utils.assetsPath('js/[name].[chunkhash].js'), // 编译输出文件名格式
  22. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') // 没有指定输出名的文件输出的文件名格式
  23. },
  24. vue: { // vue里的css也要单独提取出来
  25. loaders: utils.cssLoaders({ // css加载器,调用了utils文件中的cssLoaders方法,用来返回针对各类型的样式文件的处理方式,
  26. sourceMap: config.build.productionSourceMap,
  27. extract: true
  28. })
  29. },
  30. // 重新配置插件项
  31. plugins: [
  32. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  33. // 位于开发环境下
  34. new webpack.DefinePlugin({
  35. 'process.env': env
  36. }),
  37. new webpack.optimize.UglifyJsPlugin({// 丑化压缩代码
  38. compress: {
  39. warnings: false
  40. }
  41. }),
  42. new webpack.optimize.OccurenceOrderPlugin(),
  43. // extract css into its own file
  44. new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')), // 抽离css文件
  45. // generate dist index.html with correct asset hash for caching.
  46. // you can customize output by editing /index.html
  47. // see https://github.com/ampedandwired/html-webpack-plugin
  48. // filename 生成网页的HTML名字,可以使用/来控制文件文件的目录结构,最
  49. // 终生成的路径是基于webpac配置的output.path的
  50. new HtmlWebpackPlugin({
  51. // 生成html文件的名字,路径和生产环境下的不同,要与修改后的publickPath相结合,否则开启服务器后页面空白
  52. filename: config.build.index,
  53. // 源文件,路径相对于本文件所在的位置
  54. template: 'index.html',
  55. inject: true,// 要把<script>标签插入到页面哪个标签里(body|true|head|false)
  56. minify: {
  57. removeComments: true,
  58. collapseWhitespace: true,
  59. removeAttributeQuotes: true
  60. // more options:
  61. // https://github.com/kangax/html-minifier#options-quick-reference
  62. },
  63. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  64. chunksSortMode: 'dependency'
  65. }),
  66. // 如果文件是多入口的文件,可能存在,重复代码,把公共代码提取出来,又不会重复下载公共代码了
  67. // (多个页面间会共享此文件的缓存)
  68. // CommonsChunkPlugin的初始化常用参数有解析?
  69. // name: 这个给公共代码的chunk唯一的标识
  70. // filename,如何命名打包后生产的js文件,也是可以用上[name]、[hash]、[chunkhash]
  71. // minChunks,公共代码的判断标准:某个js模块被多少个chunk加载了才算是公共代码
  72. // split vendor js into its own file
  73. new webpack.optimize.CommonsChunkPlugin({
  74. name: 'vendor',
  75. minChunks: function (module, count) {
  76. // any required modules inside node_modules are extracted to vendor
  77. return (
  78. module.resource &&
  79. /\.js$/.test(module.resource) &&
  80. module.resource.indexOf(
  81. path.join(__dirname, '../node_modules')
  82. ) === 0
  83. )
  84. }
  85. }),
  86. // extract webpack runtime and module manifest to its own file in order to
  87. // prevent vendor hash from being updated whenever app bundle is updated
  88. new webpack.optimize.CommonsChunkPlugin({ // 为组件分配ID,通过这个插件webpack可以分析和优先考虑使用最多的模块,并为它们分配最小的ID
  89. name: 'manifest',
  90. chunks: ['vendor']
  91. })
  92. ]
  93. })
  94. // gzip模式下需要引入compression插件进行压缩
  95. if (config.build.productionGzip) {
  96. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  97.  
  98. webpackConfig.plugins.push(
  99. new CompressionWebpackPlugin({
  100. asset: '[path].gz[query]',
  101. algorithm: 'gzip',
  102. test: new RegExp(
  103. '\\.(' +
  104. config.build.productionGzipExtensions.join('|') +
  105. ')$'
  106. ),
  107. threshold: 10240,
  108. minRatio: 0.8
  109. })
  110. )
  111. }
  112.  
  113. module.exports = webpackConfig

  

vue 中build/build.js页面

  1. // https://github.com/shelljs/shelljs
  2. require('./check-versions')() // 检查 Node 和 npm 版本
  3. require('shelljs/global') // 使用了 shelljs 插件,可以让我们在 node 环境的 js 中使用 shell
  4. env.NODE_ENV = 'production'
  5.  
  6. var path = require('path')
  7. var config = require('../config') // 加载 config.js
  8. var ora = require('ora') // 一个很好看的 loading 插件
  9. var webpack = require('webpack') // 加载 webpack
  10. var webpackConfig = require('./webpack.prod.conf') // 加载 webpack.prod.conf
  11.  
  12. console.log( // 输出提示信息 ~ 提示用户请在 http 服务下查看本页面,否则为空白页
  13. ' Tip:\n' +
  14. ' Built files are meant to be served over an HTTP server.\n' +
  15. ' Opening index.html over file:// won\'t work.\n'
  16. )
  17.  
  18. var spinner = ora('building for production...') // 使用 ora 打印出 loading + log
  19. spinner.start() // 开始 loading 动画
  20.  
  21. /* 拼接编译输出文件路径 */
  22. var assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory)
  23. rm('-rf', assetsPath) /* 删除这个文件夹 (递归删除) */
  24. mkdir('-p', assetsPath) /* 创建此文件夹 */
  25. cp('-R', 'static/*', assetsPath) /* 复制 static 文件夹到我们的编译输出目录 */
  26.  
  27. webpack(webpackConfig, function (err, stats) { // 开始 webpack 的编译
  28. // 编译成功的回调函数
  29. spinner.stop()
  30. if (err) throw err
  31. process.stdout.write(stats.toString({
  32. colors: true,
  33. modules: false,
  34. children: false,
  35. chunks: false,
  36. chunkModules: false
  37. }) + '\n')
  38. })

项目入口,由package.json 文件可以看出

  1. "scripts": {
  2. "dev": "node build/dev-server.js",
  3. "build": "node build/build.js",
  4. "watch": "node build/build-watch.js"
  5. },

  当我们执行 npm run dev / npm run build  / npm run watch时运行的是 node build/dev-server.js 或 node build/build.js 或node build/build-watch.js

node build/build-watch.js 是我配置的载production环境的配置基础上在webpack的配置模块加上 watch:true  便可实现代码的实时编译

对vue中 默认的 config/index.js:配置的详细理解 -【以及webpack配置的理解】-config配置的目的都是为了服务webpack的配置,给不同的编译条件提供配置的更多相关文章

  1. vue中config/index.js:配置的详细理解

    当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面  (环境变量及其基本变量的配置) var path = require('path') ...

  2. 原生js中获取this与鼠标对象以及vue中默认的鼠标对象参数

    1.通过原生js获取this对象 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  3. vue-cli脚手架npm相关文件解读(9)config/index.js

    系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...

  4. vue -- config index.js 配置文件详解

    此文章介绍vue-cli脚手架config目录下index.js配置文件 此配置文件是用来定义开发环境和生产环境中所需要的参数 关于注释 当涉及到较复杂的解释我将通过标识的方式(如(1))将解释写到单 ...

  5. vue-cli下面的config/index.js注解 webpack.base.conf.js注解

    config/indexjs详解上代码: 'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io ...

  6. Vue中结合Flask与Node.JS的异步加载功能实现文章的分页效果

    你好!欢迎阅读我的博文,你可以跳转到我的个人博客网站,会有更好的排版效果和功能. 此外,本篇博文为本人Pushy原创,如需转载请注明出处:http://blog.pushy.site/posts/15 ...

  7. config/index.js

    // see http://vuejs-templates.github.io/webpack for documentation.var path = require('path') module. ...

  8. vue中html、css、js 分离

    在正常的创建和引用vue文件都是html.css.js三者在一起的,这样写起来虽然方便了,但是页面比较大或者代码比较多的情况下,即使使用组件有时代码也比较多,简单来说查找不变不利于编程,大的来说影像优 ...

  9. webpack / vue项目 config/index.js配置(用于配置webpack服务器代理)

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

随机推荐

  1. YaoLingJump开发者日志(八)V1.1版本完成

    跳跃吧瑶玲下载连接 官网下载(网站服务器不支持10M以上的文件上传-_-||) 百度网盘下载 介绍   忙里偷闲,把之前的工作整理了一下完成V1.1版本,下面是更新! (1)去掉了积分榜. (2)增加 ...

  2. PAT L2-028 秀恩爱分得快

    https://pintia.cn/problem-sets/994805046380707840/problems/994805054698012672 古人云:秀恩爱,分得快. 互联网上每天都有大 ...

  3. Tiny4412 LED 程序

    package cn.hyc.led; import android.os.Bundle; import android.app.Activity; import android.view.Menu; ...

  4. bpf移植到3.10

    bpf_common.h中显示的是/usr/src/linux-headersXXXX/include/uapi/linux 竟然会识别系统的挂载选项:

  5. js 事件阻止传播方法,准确定位事件源

    1事件冒泡 在目标元素获得机会处理事件后,事件模型检查目标元素的父元素,看是否为同类型事件建立了处理程序.如果是,则也调用父元素的处理程序.在这之后,再检查其父元素,然后父元素,然后父元素...持续不 ...

  6. BZOJ 1406 密码箱(数论)

    很简洁的题目.求出x^2%n=1的所有x<=n的值. n<=2e9. 直接枚举x一定是超时的. 看看能不能化成有性质的式子. 有 (x+1)(x-1)%n==0,设n=a*b,那么一定有x ...

  7. Codeforces Gym 101142 C. CodeCoder vs TopForces(思维+图论)

    题意: 每个人有两个积分CC和TF 第i个人能战胜第j个人的条件满足下面两个条件中的一个即可 1.CCi > CCj 或 TFi > TFj 2.i能战胜k,k能战胜j. 题解: 先按CC ...

  8. CentOS 装hadoop3.0.3 版本踩坑

    1.but there is no HDFS_NAMENODE_USER defined. Aborting operation. [root@xcff sbin]# ./start-dfs.sh S ...

  9. sd卡的访问

    一般再访问sd卡前都要获取sd卡的路径,以防止不同的厂商有不同的路径配置.Android提供了Environment类来获取系统当前sd卡路径. Log.d(TAG, Environment.getE ...

  10. HDOJ.2084 数塔(DP)

    数塔 点我挑战题目 题意分析 DP的思想,自上而下计算. [这几天比较忙 有空补上] 代码总览 /* Title:HDOJ.2084 Author:pengwill Date:2017-1-14 */ ...