vue-cli+webpack打包配置

vue-cli+webpack打包配置css

一: 目錄結構:html

├── README.md
├── build
│   ├── build.js
│   ├── check-versions.js
│   ├── utils.js
│   ├── vue-loader.conf.js
│   ├── webpack.base.conf.js
│   ├── webpack.dev.conf.js
│   └── webpack.prod.conf.js
├── config
│   ├── dev.env.js
│   ├── index.js 
│   ├── test.env.js
│   └── prod.env.js
│  
├── index.html
├── package.json
├── src
│   ├── App.vue
│   ├── assets
│   │   └── logo.png
│   ├── components
│   │   └── HelloWorld.vue
│   │── router
│   │    └── index.js
│   └── main.js
├── static
├── .babelrc
├── .editorconfig
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .postcssrc.js

二:指令分析:vue

2-1 先看 package.json 裏面的scripts的字段node

"scripts": {
  "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
  "start": "npm run dev",
  "unit": "jest --config test/unit/jest.conf.js --coverage",
  "test": "npm run unit",
  "lint": "eslint --ext .js,.vue src test/unit/specs",
  "build": "node build/build.js"
}

運行 npm run dev 後,會執行開發環境打包,就會執行 build文件夾下的 webpack.dev.conf.js代碼,運行 npm run build後,
會進行正式環境打包, 執行build/build.js文件代碼。咱們首先來看下 webpack的配置。webpack

三:webpack配置git

3-1 webpack.base.conf.jsgithub

入口文件 entry 代碼以下:web

entry: {
  app: './src/main.js'
}

輸出文件 output 代碼以下:正則表達式

output: {
  path: config.build.assetsRoot,  // 導出目錄的絕對路徑 在項目的根目錄下 會新建dist文件夾
  filename: '[name].js',  // 導出文件的文件名
  publicPath: process.env.NODE_ENV === 'production'
    ? config.build.assetsPublicPath
    : config.dev.assetsPublicPath  
}

如上代碼 config; 在頁面上引入代碼: const config = require('../config'); 咱們能夠打開config下的index.js查看下代碼就能夠明白
了,若是是正式環境 publicPath = config.build.assetsPublicPath, 若是是開發環境 publicPath = config.dev.assetsPublicPath;publicPath 是虛擬目錄,自動指向path編譯的目錄。好比在正式環境打包會生成vue-cli

dist
  static
    css
    js
    index.html

生成如上的目錄,那麼設置 publicPath 爲 './', 那麼在index.html引入的路徑就變爲 ./css/xx.css, js路徑變爲 ./js/xx.js
, 直接打開index.html就能夠訪問到 css 和 對應的js的。
若是是開發環境,設置 publicPath爲 '/',那麼在開發環境 訪問頁面; 好比 http://localhost:8080; 那麼js路徑就是
http://localhost:8080/app.js了;

文件解析 resolve (主要設置模塊如何被解析)

// 設置模塊如何被解析
resolve: {
  // 自動解析肯定的擴展名,導入模塊時不帶擴展名
  extensions: ['.js', '.vue', '.json'],

  // 建立import 或 require的別名
  /*
   好比以下文件
   src
     components
       a.vue
     router
       home
         index.vue
    在index.vue裏面,正常引用A組件;以下:
    import A from '../../components/a.vue';
    若是設置了 alias後,那麼引用的地方能夠以下這樣了
    import A from '@/components/a.vue';
    注意:這裏的 @ 起到了 resolve('src')路徑的做用了。
  */
  alias: {
    'vue$': 'vue/dist/vue.esm.js',
    '@': resolve('src')
  }
} 

模塊解析module (處理項目不一樣類型的模塊)

module: {
  rules: [
    // 在開發環境下 對於以.js或.vue後綴結尾的文件(在src目錄下或test目錄下的文件),使用eslint進行文件語法檢測。
    ...(config.dev.useEslint ? [createLintingRule()] : []),  
    {
      test: /\.vue$/,  // vue 文件後綴的
      loader: 'vue-loader', // 使用vue-loader處理
      options: vueLoaderConfig // options是對vue-loader作的額外選項配置 文件配置在 ./vue-loader.conf 內能夠查看代碼
    },
    {
      test: /\.js$/, // js文件後綴的
      loader: 'babel-loader', // 使用babel-loader處理
      include: [resolve('src'), resolve('test')] // 包含src和test的文件夾
    },
    {
      test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, // 處理圖片後綴
      loader: 'url-loader',  // 使用url-loader處理
      options: {
        limit: 10000,  // 圖片小於10000字節時以base64的方式引用
        name: utils.assetsPath('img/[name].[hash:7].[ext]')  // 文件名爲name.7位hash的值.擴展名
      }
    },
    {
      test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,  // 音頻文件後綴
      loader: 'url-loader',
      options: {
        limit: 10000, // 小於10000字節時的時候處理
        name: utils.assetsPath('media/[name].[hash:7].[ext]') // 文件名爲name.7位hash的值.擴展名
      }
    },
    {
      test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, // 字體文件
      loader: 'url-loader',
      options: {
        limit: 10000, // 字體文件小於10000字節的時候處理
        name: utils.assetsPath('fonts/[name].[hash:7].[ext]') // 文件名爲name.7位hash的值.擴展名
      }
    }
  ]
}

webpack.base.conf.js代碼以下:

'use strict';
const path = require('path');
const utils = require('./utils');
const config = require('../config');
const vueLoaderConfig = require('./vue-loader.conf');

function resolve (dir) {
  return path.join(__dirname, '..', dir);
}
/*
 對於以.js或.vue後綴結尾的文件(在src目錄下或test目錄下的文件),使用eslint進行文件語法檢測。
*/
const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
});

module.exports = {
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot, // 導出目錄的絕對路徑
    filename: '[name].js', // 導出文件的文件名
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  // 設置模塊如何被解析
  resolve: {
    // 自動解析肯定的擴展名,導入模塊時不帶擴展名
    extensions: ['.js', '.vue', '.json'],

    // 建立import 或 require的別名
    /*
     好比以下文件
     src
       components
         a.vue
       router
         home
           index.vue
      在index.vue裏面,正常引用A組件;以下:
      import A from '../../components/a.vue';
      若是設置了 alias後,那麼引用的地方能夠以下這樣了
      import A from '@/components/a.vue';
      注意:這裏的 @ 起到了 resolve('src')路徑的做用了。
    */
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src')
    }
  },
  module: {
    rules: [
      // 在開發環境下 對於以.js或.vue後綴結尾的文件(在src目錄下或test目錄下的文件),使用eslint進行文件語法檢測。
      ...(config.dev.useEslint ? [createLintingRule()] : []),
      {
        test: /\.vue$/,  // vue 文件後綴的
        loader: 'vue-loader', // 使用vue-loader處理
        options: vueLoaderConfig // options是對vue-loader作的額外選項配置 文件配置在 ./vue-loader.conf 內能夠查看代碼
      },
      {
        test: /\.js$/, // js文件後綴的
        loader: 'babel-loader', // 使用babel-loader處理
        include: [resolve('src'), resolve('test')] // 包含src和test的文件夾
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, // 處理圖片後綴
        loader: 'url-loader',  // 使用url-loader處理
        options: {
          limit: 10000,  // 圖片小於10000字節時以base64的方式引用
          name: utils.assetsPath('img/[name].[hash:7].[ext]')  // 文件名爲name.7位hash的值.擴展名
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,  // 音頻文件後綴
        loader: 'url-loader',
        options: {
          limit: 10000, // 小於10000字節時的時候處理
          name: utils.assetsPath('media/[name].[hash:7].[ext]') // 文件名爲name.7位hash的值.擴展名
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, // 字體文件
        loader: 'url-loader',
        options: {
          limit: 10000, // 字體文件小於10000字節的時候處理
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]') // 文件名爲name.7位hash的值.擴展名
        }
      }
    ]
  },
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
};

對webpack.base.conf中的 const vueLoaderConfig = require('./vue-loader.conf');
vue-loader.conf.js 代碼以下:

'use strict';
const utils = require('./utils');
const config = require('../config');
// 判斷是不是生產環境
const isProduction = process.env.NODE_ENV === 'production';
const sourceMapEnabled = isProduction
  ? config.build.productionSourceMap
  : config.dev.cssSourceMap;

module.exports = {
  // 處理 .vue文件中的樣式
  loaders: utils.cssLoaders({
    // 是否打開 source-map
    sourceMap: sourceMapEnabled,
    // 是否提取樣式到單獨的文件
    extract: isProduction
  }),
  cssSourceMap: sourceMapEnabled,
  cacheBusting: config.dev.cacheBusting,
  transformToRequire: {
    video: ['src', 'poster'],
    source: 'src',
    img: 'src',
    image: 'xlink:href'
  }
};

3-2 webpack.dev.conf.js

開發環境下的 webpack配置,經過merge方法合併 webpack.base.conf.js 基礎配置。
一些代碼以下:

'use strict';
const utils = require('./utils');
const webpack = require('webpack');
const config = require('../config');

// webpack-merge是一個能夠合併數組和對象的插件
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
module.exports = merge(baseWebpackConfig, {})

模塊配置

module: {
  // 經過傳入一些配置來獲取rules配置,此處傳入了sourceMap: false,表示不生成sourceMap
  rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
}

在utils.styleLoaders中的配置以下

/*
 生成style-loader的配置
 style-loader文檔:https://github.com/webpack/style-loader
 @method styleLoaders
 @param {Object} options生成的配置
 @return {Array} style-loader的配置
*/
exports.styleLoaders = function (options) {
  const output = []; // 定義返回的數組,數組中保存的是針對各種型的樣式文件的處理方式
  const loaders = exports.cssLoaders(options); // 調用cssLoaders方法返回各種型的樣式對象(css: loader)

  for (const extension in loaders) { // 循環遍歷loaders
    const loader = loaders[extension]; // 根據遍歷得到的key(extension)來獲得value(loader)
    output.push({
      test: new RegExp('\\.' + extension + '$'), // 處理的文件類型
      use: loader  // 用loader來處理,loader來自loaders[extension]
    });
  }
  return output;
};

上面的代碼中調用了exports.cssLoaders(options),用來返回針對各種型的樣式文件的處理方式
以下代碼:

/*
 * 生成處理css的loaders配置
 * @method cssLoaders
 * @param {Object} options 生成的配置
 options = {
   // 是否開啓 sourceMap
   sourceMap: true,
   // 是否提取css
   extract: true
 }
 @return {Object} 處理css的loaders的配置對象
*/
exports.cssLoaders = function (options) {
  options = options || {};

  const cssLoader = {
    loader: 'css-loader',
    options: { // options是loader的選項配置
      // 根據參數是否生成sourceMap文件 生成環境下壓縮文件
      sourceMap: options.sourceMap
    }
  };

  const postcssLoader = {
    loader: 'postcss-loader',
    options: {
      sourceMap: options.sourceMap
    }
  };

  // generate loader string to be used with extract text plugin
  /*
   生成ExtractTextPlugin對象或loader字符串
   @method generateLoaders
   @param {Array} loader 名稱數組
   @return {String | Object} ExtractTextPlugin對象或loader字符串
   */
  function generateLoaders (loader, loaderOptions) { // 生成loader
    // 默認是css-loader
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader];

    if (loader) { // 若是參數loader存在
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, { // 將loaderOptions和sourceMap組成一個對象
          sourceMap: options.sourceMap
        })
      });
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    // 當extract爲true時,提取css,生成環境中,默認爲true
    if (options.extract) {  // 若是傳入的options存在extract且爲true
      return ExtractTextPlugin.extract({  // ExtractTextPlugin分離js中引入的css文件
        use: loaders, // 處理的loader
        fallback: 'vue-style-loader' // 沒有被提取分離時使用的loader
      });
    } else {
      return ['vue-style-loader'].concat(loaders);
    }
  }

  // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  // 返回css類型對應的loader組成的對象 generateLoaders()來生成loader
  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass'),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  };
};

在styleLoaders函數內 執行 const loaders = exports.cssLoaders(options); 調用cssLoaders方法返回各種型的樣式對象(css: loader);因此最終loaders 會返回以下對象:

loaders = {
  css: generateLoaders(),
  postcss: generateLoaders(),
  less: generateLoaders('less'),
  sass: generateLoaders('sass', { indentedSyntax: true }),
  scss: generateLoaders('sass'),
  stylus: generateLoaders('stylus'),
  styl: generateLoaders('stylus')
};

執行generateLoaders()函數後,又會返回代碼中的對象;若是是正式環境的話,css會分離;所以會返回以下的js對象:

return ExtractTextPlugin.extract({  // ExtractTextPlugin分離js中引入的css文件
  use: loaders, // 處理的loader
  fallback: 'vue-style-loader' // 沒有被提取分離時使用的loader
});

若是是開發環境下的話;會返回:

return ['vue-style-loader'].concat(loaders);

最後 在 styleLoaders函數中;會進行loaders循環;以下:

for (const extension in loaders) { // 循環遍歷loaders
  const loader = loaders[extension]; // 根據遍歷得到的key(extension)來獲得value(loader)
  output.push({
    test: new RegExp('\\.' + extension + '$'), // 處理的文件類型
    use: loader  // 用loader來處理,loader來自loaders[extension]
  });
}

最後返回 output, 返回的數組,數組中保存的是針對各種型的樣式文件的處理方式。

插件配置以下代碼:

plugins: [
  new webpack.DefinePlugin({ // 編譯時配置的全局變量
    'process.env': require('../config/dev.env') // 當前環境爲開發環境
  }),
  // 開啓webpack熱更新功能
  new webpack.HotModuleReplacementPlugin(),

  new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.

  // webpack編譯過程當中出錯的時候跳過報錯階段,不會阻塞編譯,在編譯結束後報錯
  new webpack.NoEmitOnErrorsPlugin(),
  // https://github.com/ampedandwired/html-webpack-plugin
  // 自動將依賴注入html模板,並輸出最終的html文件到目標文件夾
  new HtmlWebpackPlugin({
    filename: 'index.html',  // 生成的文件名
    template: 'index.html',  // 模板
    inject: true
  })
]

下面是 webpack.dev.conf.js 全部的代碼:

'use strict';
const utils = require('./utils');
const webpack = require('webpack');
const config = require('../config');

// webpack-merge是一個能夠合併數組和對象的插件
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');

// html-webpack-plugin用於將webpack編譯打包後的產品文件注入到html模板中
// 即自動在index.html裏面加上<link>和<script>標籤引用webpack打包後的文件
const HtmlWebpackPlugin = require('html-webpack-plugin');

// friendly-errors-webpack-plugin用於更友好地輸出webpack的警告、錯誤等信息
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const portfinder = require('portfinder');

const HOST = process.env.HOST;
const PORT = process.env.PORT && Number(process.env.PORT);

// 開發環境下的webpack配置,經過merge方法合併webpack.base.conf.js基礎配置
const devWebpackConfig = merge(baseWebpackConfig, {
  module: {
    // 經過傳入一些配置來獲取rules配置,此處傳入了sourceMap: false,表示不生成sourceMap
    rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  },
  // cheap-module-eval-source-map is faster for development
  // 使用這種source-map更快
  devtool: config.dev.devtool,

  // these devServer options should be customized in /config/index.js
  devServer: {
    clientLogLevel: 'warning',
    historyApiFallback: true,
    hot: true,
    compress: true,
    host: HOST || config.dev.host,
    port: PORT || config.dev.port,
    open: config.dev.autoOpenBrowser,
    overlay: config.dev.errorOverlay
      ? { warnings: false, errors: true }
      : false,
    publicPath: config.dev.assetsPublicPath,
    proxy: config.dev.proxyTable,
    quiet: true, // necessary for FriendlyErrorsPlugin
    watchOptions: {
      poll: config.dev.poll
    }
  },
  plugins: [
    new webpack.DefinePlugin({ // 編譯時配置的全局變量
      'process.env': require('../config/dev.env') // 當前環境爲開發環境
    }),
    // 開啓webpack熱更新功能
    new webpack.HotModuleReplacementPlugin(),

    new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.

    // webpack編譯過程當中出錯的時候跳過報錯階段,不會阻塞編譯,在編譯結束後報錯
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    // 自動將依賴注入html模板,並輸出最終的html文件到目標文件夾
    new HtmlWebpackPlugin({
      filename: 'index.html',  // 生成的文件名
      template: 'index.html',  // 模板
      inject: true
    })
  ]
});

module.exports = new Promise((resolve, reject) => {
  portfinder.basePort = process.env.PORT || config.dev.port;
  portfinder.getPort((err, port) => {
    if (err) {
      reject(err);
    } else {
      // publish the new Port, necessary for e2e tests
      process.env.PORT = port;
      // add port to devServer config
      devWebpackConfig.devServer.port = port;

      // Add FriendlyErrorsPlugin
      devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({ // 友好的錯誤提示
        compilationSuccessInfo: {
          messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`]
        },
        onErrors: config.dev.notifyOnErrors
        ? utils.createNotifierCallback()
        : undefined
      }));

      resolve(devWebpackConfig);
    }
  });
});

3-3 webpack.prod.conf.js

生產環境下的webpack配置,經過merge方法合併webpack.base.conf.js基礎配置
以下代碼:

const path = require('path');
const utils = require('./utils');
const webpack = require('webpack');
// 配置文件
const config = require('../config');
// webpack 配置合併插件
const merge = require('webpack-merge');
// webpack 基本配置
const baseWebpackConfig = require('./webpack.base.conf');

const webpackConfig = merge(baseWebpackConfig, {});

module的處理,主要是針對css的處理, 一樣的此處調用了 utils.styleLoaders; 

module: {
  // styleLoaders
  rules: utils.styleLoaders({
    sourceMap: config.build.productionSourceMap,
    extract: true,
    usePostCSS: true
  })
}

輸出文件output

output: {
  // 編譯輸出的靜態資源根路徑 建立dist文件夾
  path: config.build.assetsRoot,

  // 編譯輸出的文件名
  filename: utils.assetsPath('js/[name].[chunkhash].js'),

  // 沒有指定輸出名的文件輸出的文件名 或能夠理解爲 非入口文件的文件名,而又須要被打包出來的文件命名配置,如按需加載的模塊
  chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
}

webpack.prod.conf.js 全部代碼以下:

'use strict';
// node自帶的文件路徑工具
const path = require('path');

const utils = require('./utils');
const webpack = require('webpack');

// 配置文件
const config = require('../config');

// webpack 配置合併插件
const merge = require('webpack-merge');

// webpack 基本配置
const baseWebpackConfig = require('./webpack.base.conf');

// webpack 複製文件和文件夾的插件
// https://github.com/kevlened/copy-webpack-plugin
const CopyWebpackPlugin = require('copy-webpack-plugin');

// 自動生成 html 而且注入到 .html 文件中的插件
// https://github.com/ampedandwired/html-webpack-plugin
const HtmlWebpackPlugin = require('html-webpack-plugin');

// 提取css的插件
// https://github.com/webpack-contrib/extract-text-webpack-plugin
const ExtractTextPlugin = require('extract-text-webpack-plugin');

// webpack 優化壓縮和優化 css 的插件
// https://github.com/NMFR/optimize-css-assets-webpack-plugin
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');

// js壓縮插件
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

// 若是當前環境爲測試環境,則使用測試環境
// 不然,使用生產環境
const env = process.env.NODE_ENV === 'testing'
  ? require('../config/test.env')
  : require('../config/prod.env');

const webpackConfig = merge(baseWebpackConfig, {
  module: {
    // styleLoaders
    rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: true,
      usePostCSS: true
    })
  },
  // 是否開啓 sourceMap
  devtool: config.build.productionSourceMap ? config.build.devtool : false,
  output: {
    // 編譯輸出的靜態資源根路徑 建立dist文件夾
    path: config.build.assetsRoot,

    // 編譯輸出的文件名
    filename: utils.assetsPath('js/[name].[chunkhash].js'),

    // 沒有指定輸出名的文件輸出的文件名 或能夠理解爲 非入口文件的文件名,而又須要被打包出來的文件命名配置,如按需加載的模塊
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  },
  plugins: [
    // 配置全局環境爲生產環境
    // http://vuejs.github.io/vue-loader/en/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': env
    }),
    // js文件壓縮插件
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: { // 壓縮配置
          warnings: false // 不顯示警告
        }
      },
      sourceMap: config.build.productionSourceMap, // 生成sourceMap文件
      parallel: true
    }),
    // extract css into its own file
    // 將js中引入的css分離的插件
    new ExtractTextPlugin({
      filename: utils.assetsPath('css/[name].[contenthash].css'), // 分離出的css文件名
      // set the following option to `true` if you want to extract CSS from
      // codesplit chunks into this main css file as well.
      // This will result in *all* of your app's CSS being loaded upfront.
      allChunks: false
    }),
    // 壓縮提取出的css,並解決ExtractTextPlugin分離出的js重複問題(多個文件引入同一css文件)
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      cssProcessorOptions: config.build.productionSourceMap
        ? { safe: true, map: { inline: false } }
        : { safe: true }
    }),
    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // 將 index.html 做爲入口,注入 html 代碼後生成 index.html文件 引入css文件和js文件
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: process.env.NODE_ENV === 'testing'
        ? 'index.html'
        : config.build.index,  // 生成的html的文件名
      template: 'index.html',  // 依據的模板
      inject: true,  // 注入的js文件將會被放在body標籤中,當值爲'head'時,將被放在head標籤中
      minify: { // 壓縮配置
        removeComments: true,  // 刪除html中的註釋代碼
        collapseWhitespace: true, // 刪除html中的空白符
        removeAttributeQuotes: true // 刪除html元素中屬性的引號

        // 更多選項 https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      // 必須經過 CommonsChunkPlugin一致地處理多個 chunks
      chunksSortMode: 'dependency'  // 按dependency的順序引入
    }),
    // keep module.id stable when vender modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),
    // split vendor js into its own file
    // 分割公共 js 到獨立的文件vendor中
    // https://webpack.js.org/guides/code-splitting-libraries/#commonschunkplugin
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor', // 文件名
      minChunks (module) { // 聲明公共的模塊來自node_modules文件夾
        // any required modules inside node_modules are extracted to vendor
        // node_modules中的任何所需模塊都提取到vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        );
      }
    }),
    /* 
     上面雖然已經分離了第三方庫,每次修改編譯都會改變vendor的hash值,致使瀏覽器緩存失效。
     緣由是vendor包含了webpack在打包過程當中會產生一些運行時代碼,運行時代碼中實際上保存了打包後的文件名。
     當修改業務代碼時,業務代碼的js文件的hash值必然會改變。一旦改變必然
     會致使vendor變化。vendor變化會致使其hash值變化。
    */
    // 下面主要是將運行時代碼提取到單獨的manifest文件中,防止其影響vendor.js
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      minChunks: Infinity
    }),
    // This instance extracts shared chunks from code splitted chunks and bundles them
    // in a separate chunk, similar to the vendor chunk
    // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
    new webpack.optimize.CommonsChunkPlugin({
      name: 'app',
      async: 'vendor-async',
      children: true,
      minChunks: 3
    }),

    // 複製靜態資源,將static文件內的內容複製到指定文件夾
    // https://github.com/kevlened/copy-webpack-plugin
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.build.assetsSubDirectory,
        ignore: ['.*']
      }
    ])
  ]
});

// 配置文件開啓了gzip壓縮
if (config.build.productionGzip) {
  // 引入壓縮文件的組件,該插件會對生成的文件進行壓縮,生成一個.gz文件
  // https://github.com/webpack-contrib/compression-webpack-plugin
  const CompressionWebpackPlugin = require('compression-webpack-plugin');

  // 向webpackconfig.plugins中加入下方的插件
  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]', // 目標文件名
      algorithm: 'gzip', // 使用gzip壓縮
      test: new RegExp(  // 知足正則表達式的文件會被壓縮
        '\\.(' +
        config.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240, // 資源文件大於10240B=10kB時會被壓縮
      minRatio: 0.8  // 最小壓縮比達到0.8時纔會被壓縮
    })
  );
}
// 開啓包分析的狀況時, 給 webpack plugins添加 webpack-bundle-analyzer 插件
if (config.build.bundleAnalyzerReport) {
  const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  webpackConfig.plugins.push(new BundleAnalyzerPlugin());
}
module.exports = webpackConfig;

github上的項目

 注意: 上面的配置代碼都是從vue-cli上下載下來的,每一個配置的含義都是從網上資料總結出來的,爲了之後項目的須要直接能夠拿來使用,且記錄下配置的含義;

相關文章
相關標籤/搜索