vue-cli+webpack打包配置


上一篇說的是 webpack打包生成測試和生產版本; 這篇說的這個配置 應該是在打包之前應該配置好的

webpack配置如下:

1. webpack.base.conf.js

入口文件 entry 代碼如下:

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  
}

整體代碼

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

2. 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);
    }
  });
});

2. 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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM