1. 程式人生 > >對vue中 預設的 config/index.js:配置的詳細理解 -【以及webpack配置的理解】-config配置的目的都是為了服務webpack的配置

對vue中 預設的 config/index.js:配置的詳細理解 -【以及webpack配置的理解】-config配置的目的都是為了服務webpack的配置

複製於:https://www.cnblogs.com/whkl-m/p/6627864.html

 

當我們需要和後臺分離部署的時候,必須配置config/index.js:

 

用vue-cli 自動構建的目錄裡面  (環境變數及其基本變數的配置)

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

  

在'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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 //引入依賴模組 var  path = require( 'path' ) var  config = require( '../config' // 獲取配置 var  utils = require( './utils' ) var  projectRoot = path.resolve(__dirname,  '../' )   var  env = process.env.NODE_ENV // check env & config/index.js to decide weither to enable CSS Sourcemaps for the // various preprocessor loaders added to vue-loader at the end of this file var  cssSourceMapDev = (env ===  'development'  && config.dev.cssSourceMap) /* 是否在 dev 環境下開啟 cssSourceMap ,在 config/index.js 中可配置 */ var  cssSourceMapProd = (env ===  'production'  && config.build.productionSourceMap) /* 是否在 production 環境下開啟 cssSourceMap ,在 config/index.js 中可配置 */ var  useCssSourceMap = cssSourceMapDev || cssSourceMapProd  /* 最終是否使用 cssSourceMap */   module.exports = {    entry: {    // 配置webpack編譯入口      app:  './src/main.js'     },    output: {     // 配置webpack輸出路徑和命名規則      path: config.build.assetsRoot,  // webpack輸出的目標資料夾路徑(例如:/dist)      publicPath: process.env.NODE_ENV ===  'production'  ? config.build.assetsPublicPath : config.dev.assetsPublicPath,   // webpack編譯輸出的釋出路徑(判斷是正式環境或者開發環境等)      filename:  '[name].js'    // webpack輸出bundle檔案命名格式,基於檔案的md5生成Hash名稱的script來防止快取    },    resolve: {      extensions: [ '' '.js' '.vue' '.scss' ],   //自動解析確定的拓展名,使匯入模組時不帶拓展名      fallback: [path.join(__dirname,  '../node_modules' )],      alias: {   // 建立import或require的別名,一些常用的,路徑長的都可以用別名        'vue$' 'vue/dist/vue' ,        'src' : path.resolve(__dirname,  '../src' ),        'assets' : path.resolve(__dirname,  '../src/assets' ),        'components' : path.resolve(__dirname,  '../src/components' ),        'scss_vars' : path.resolve(__dirname,  '../src/styles/vars.scss' )      }    },    resolveLoader: {      fallback: [path.join(__dirname,  '../node_modules' )]    },    module: {      loaders: [          {              test: /\.vue$/,  // vue檔案字尾              loader:  'vue'    //使用vue-loader處理          },          {              test: /\.js$/,              loader:  'babel' ,              include: projectRoot,              exclude: /node_modules/          },          {              test: /\.json$/,              loader:  'json'          },          {              test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,              loader:  'url' ,              query: {                limit: 10000,                name: utils.assetsPath( 'img/[name].[hash:7].[ext]' )              }          },          {              test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,              loader:  'url' ,              query: {                limit: 10000,                name: utils.assetsPath( 'fonts/[name].[hash:7].[ext]' )              }          }      ]    },    vue: {     // .vue 檔案配置 loader 及工具 (autoprefixer)      loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),  //// 呼叫cssLoaders方法返回各型別的樣式物件(css: loader)      postcss: [        require( 'autoprefixer' )({          browsers: [ 'last 2 versions' ]        })      ]    } }

  webpack.prod.conf.js 生產環境下的配置檔案

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 var  path = require( 'path' ) var  config = require( '../config' ) var  utils = require( './utils' ) var  webpack = require( 'webpack' ) var  merge = require( 'webpack-merge' ) // 一個可以合併陣列和物件的外掛 var  baseWebpackConfig = require( './webpack.base.conf' ) // 用於從webpack生成的bundle中提取文字到特定檔案中的外掛 // 可以抽取出css,js檔案將其與webpack輸出的bundle分離 var  ExtractTextPlugin = require( 'extract-text-webpack-plugin' )   //如果我們想用webpack打包成一個檔案,css js分離開,需要這個外掛 var  HtmlWebpackPlugin = require( 'html-webpack-plugin' ) // 一個用於生成HTML檔案並自動注入依賴檔案(link/script)的webpack外掛 var  env = config.build.env // 合併基礎的webpack配置 var  webpackConfig = merge(baseWebpackConfig, {      // 配置樣式檔案的處理規則,使用styleLoaders    module: {      loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract:  true  })    },    devtool: config.build.productionSourceMap ?  '#source-map'  false // 開啟source-map,生產環境下推薦使用cheap-source-map或source-map,後者得到的.map檔案體積比較大,但是能夠完全還原以前的js程式碼    output: {      path: config.build.assetsRoot, // 編譯輸出目錄      filename: utils.assetsPath( 'js/[name].[chunkhash].js' ),   // 編譯輸出檔名格式      chunkFilename: utils.assetsPath( 'js/[id].[chunkhash].js' )   // 沒有指定輸出名的檔案輸出的檔名格式    },    vue: {  // vue裡的css也要單獨提取出來      loaders: utils.cssLoaders({  // css載入器,呼叫了utils檔案中的cssLoaders方法,用來返回針對各型別的樣式檔案的處理方式,        sourceMap: config.build.productionSourceMap,        extract:  true      })    },    // 重新配置外掛項    plugins: [      // http://vuejs.github.io/vue-loader/en/workflow/production.html      // 位於開發環境下      new  webpack.DefinePlugin({        'process.env' : env      }),      new  webpack.optimize.UglifyJsPlugin({ // 醜化壓縮程式碼        compress: {          warnings:  false        }      }),      new  webpack.optimize.OccurenceOrderPlugin(),      // extract css into its own file      new  ExtractTextPlugin(utils.assetsPath( 'css/[name].[contenthash].css' )),   // 抽離css檔案      // generate dist index.html with correct asset hash for caching.      // you can customize output by editing /index.html      // see https://github.com/ampedandwired/html-webpack-plugin       // filename 生成網頁的HTML名字,可以使用/來控制檔案檔案的目錄結構,最        // 終生成的路徑是基於webpac配置的output.path的      new  HtmlWebpackPlugin({          // 生成html檔案的名字,路徑和生產環境下的不同,要與修改後的publickPath相結合,否則開啟伺服器後頁面空白        filename: config.build.index,        // 原始檔,路徑相對於本檔案所在的位置        template:  'index.html' ,        inject:  true , // 要把<script>標籤插入到頁面哪個標籤裡(body|true|head|false)        minify: {          removeComments:  true ,          collapseWhitespace:  true ,          removeAttributeQuotes:  true          // more options:          // https://github.com/kangax/html-minifier#options-quick-reference        },        // necessary to consistently work with multiple chunks via CommonsChunkPlugin        chunksSortMode:  'dependency'      }),      // 如果檔案是多入口的檔案,可能存在,重複程式碼,把公共程式碼提取出來,又不會重複下載公共程式碼了      // (多個頁面間會共享此檔案的快取)