webpack打包vue-cli項目之webpack.base.conf.js文件分析

開場

當我們使用vue-cli創建項目的時候,build文件夾的配置文件裏面除了擁有編譯相關的dev文件和打包相關的prod文件以外,還有另外一個使用很頻繁的base文件。這個base裏使用了一些編譯和打包時候都會用到的信息,所以被單獨提取出來。因此我們在學習webpack打包過程的時候,也要知道這個文件裏的對象含義。

上代碼

'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)
}

module.exports = {
  // 基礎上下文
  context: path.resolve(__dirname, '../'),
  // webpack的入口文件,
  //  打包時,生成entry chunk。如果entry是字符串或者數組,則只會生成一個chunk。如果是對象,則會生成多個chunk。
  entry: {
    app: './src/main.js'
  },
  /**
   * The top-level output key contains set of options instructing webpack on how and where it should output your bundles,
   * assets and anything else you bundle or load with webpack.
   *
   * */
  output: {
    path: config.build.assetsRoot,
    /**
     * when creating multiple bundles via more than one entry point, code splitting, or various plugins,
     * you should use one of the following substitutions to give each bundle a unique name
     *
     * */
     // 輸出文件的名字,與prod整合時,會被替換成prod裏的命名風格
    filename: '[name].js',
    // 根據環境變量選擇路徑
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },

  /**
   * 當webpack試圖去加載模塊的時候,它默認是查找以 .js 結尾的文件的,
   * 它並不知道 .vue 結尾的文件是什麼鬼玩意兒,
   * 所以我們要在配置文件中告訴webpack,
   * 遇到 .vue 結尾的也要去加載,
   * 添加 resolve 配置項,如下:
   */
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    // 設置別名 
    alias: {
      // 簡化導入代碼
      'vue$': 'vue/dist/vue.esm.js',
      // 一件回到src文件夾,不用一直回到上一級
      '@': resolve('src'),
    }
  },
  module: {
    rules: [
      {
        // vue-loader vue文件轉js文件
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        // babel-loader ES6轉ES5, JSX轉JS
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        // 資源加載器,下同
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  //  不知道幹嘛的 估計用處不大
  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'
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章