简体   繁体   English

将 TerserWebpackPlugin webpack 插件的源 map 选项设置为 true 会显着增加 webpack 构建时间

[英]Setting the TerserWebpackPlugin webpack plugin's source map option to true significantly increases webpack build time

I'm setting source-maps to be on production.我正在将源地图设置为投入生产。 I'm using TerserWebpackPlugin to minify my js(which appears to be the default one according to the webpack docs).我正在使用 TerserWebpackPlugin 来缩小我的 js(根据 webpack 文档,这似乎是默认的)。 This plugin has a config option for sourceMap , which from the docs it appears you have to enable for best practices(but I'm not 100% sure although it does work without it).这个插件有一个sourceMap的配置选项,从文档看来,你必须启用最佳实践(但我不能 100% 确定,尽管没有它它也能工作)。 The thing is, when it's set to true the option adds an extra 12ish minutes to build time(from around 1:15 mins to 13ish mins).问题是,当它设置为 true 时,该选项会额外增加 12 分钟的构建时间(从大约 1:15 分钟到 13 分钟左右)。 An extra 12 mins of build time being added feels a bit much so I'm guessing when sourceMap: true it parses the source maps however source maps are only downloaded once a user opens their dev tools so I'm wondering if this is even needed.添加额外 12 分钟的构建时间感觉有点多,所以我猜什么时候sourceMap: true它解析源映射但是只有在用户打开他们的开发工具后才会下载源映射,所以我想知道这是否甚至需要.

My question is, is this config required?我的问题是,这个配置是必需的吗? And if so, is possible to speed up the build process any?如果是这样,是否有可能加快构建过程?

Here's my configs by the way:顺便说一下,这是我的配置:

webpack.common.js webpack.common.js

const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const WEBPACK_OUTPUT_PATH = path.resolve(`${__dirname}/webpack_output`);
module.exports = {
  entry: { ... },
  output: {
    path: WEBPACK_OUTPUT_PATH,
    filename: '[name]_bundle.js',
  },
  module: { ... },
  plugins: [
    new CleanWebpackPlugin([WEBPACK_OUTPUT_PATH]),
    new webpack.DefinePlugin({
      'global.BUILD_NUMBER': Date.now(),
    }),
  ],
  resolve: {
    alias: {
      ...
    },
    extensions: ['.js', '.jsx', '.json', '.scss', 'css'],
  },
  watchOptions: {
    poll: true,
    ignored: /node_modules/,
  },
};

webpack.prod.js webpack.prod.js

const webpack = require('webpack');
const merge = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const common = require('./webpack.common.js');
module.exports = merge(common, {
  // NOTE: There are internal webpack plugins that are used when production mode is enabled so they
  // are not defined below. Read more about them here: https://webpack.js.org/plugins/internal-plugins/
  mode: 'production',
  devtool: 'source-map', 
  performance: {
    hints: 'warning',
  },
  output: {
    pathinfo: false,
  },
  optimization: {
    namedModules: false,
    namedChunks: false,
    nodeEnv: 'production', 
    flagIncludedChunks: true,
    occurrenceOrder: true,
    concatenateModules: true,
    splitChunks: {
      hidePathInfo: true,
      minSize: 30000,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
    },
    noEmitOnErrors: true,
    checkWasmTypes: true,
    minimize: true,
  },
  plugins: [
    new TerserPlugin({
      sourceMap: true,
    }),
    new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') }),
    new webpack.optimize.ModuleConcatenationPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
  ],
});

You have several options here, depending on what you need.根据您的需要,您有多种选择。 First one, put parallel: true so it's:第一个,把parallel: true所以它是:

new TerserPlugin({
  sourceMap: true,
  parallel: true
})

https://webpack.js.org/plugins/terser-webpack-plugin/#parallel https://webpack.js.org/plugins/terser-webpack-plugin/#parallel

Other option is not to provide sourceMap in production mode.其他选项是在生产模式下不提供 sourceMap。 You can conditionally set sourceMap: true for more advanced solutions, such as using getIfUtils for webpack config.您可以有条件地设置sourceMap: true以获得更高级的解决方案,例如将getIfUtils 用于 webpack 配置。

so your TerserPlugin config would be:所以你的 TerserPlugin 配置将是:

new TerserPlugin({
  sourceMap: ifProduction(false, true), // if prod, disable it, otherwise enable
  parallel: true
})

But let's get back to question.但让我们回到问题上来。 parallel: true show improve the build for the start and production mode is by default heavier task to perform than 'development` mode build. parallel: true显示改进启动和production模式的构建默认情况下执行比“开发”模式构建更重的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM