簡體   English   中英

優化 webpack 構建時間

[英]Optimizing webpack build time

保存文件中的更改后,我在編譯項目時遇到問題; 文件的編譯時間需要 2 分鍾或更長時間。

為了解決這個問題,我采取了以下步驟:

1.在 babel-loader 中,屬性 cacheDirectory 的選項對象中的文檔設置為 true,cacheComprassion 為 false 2. 在 ts-loader 中,屬性 transpileOnly 和 happyPackMode 屬性的選項對象中的文檔中設置為 true 3.dev-禁用工具以獲得更好的性能 4. ForkTsCheckerWebpackPlugin 連接到打字稿中的檢查類型 5. 代碼拆分定制

網絡包配置文件

const path = require('path');
const ESLintPlugin = require('eslint-webpack-plugin');
const webpack = require('webpack');
// eslint-disable-next-line import/no-extraneous-dependencies
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
  entry: [
    path.resolve(__dirname, 'src/index.tsx'),
  ],
  mode: 'development',
  module: {
    rules: [
      { parser: { requireEnsure: false } },
      {
        test: /\.(ts|tsx)$/,
        exclude: /(node_modules)/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              cacheCompression: false,
            },
          },
          {
            loader: 'ts-loader', options: {
              transpileOnly: true,
              happyPackMode: true
            },
          },
        ],
      },
      {
        test: /\.scss$/,
        use: [
          {
            // creates style nodes from JS strings
            loader: 'style-loader',
          },
          {
            // translates CSS into CommonJS
            loader: 'css-loader',
          },
          {
            // compiles Sass to CSS
            loader: 'sass-loader',
          },
          {
            loader: 'postcss-loader',
            options: {
              ident: 'postcss',
              plugins: [
                // eslint-disable-next-line global-require
                require('autoprefixer'),
              ],
            },
          },
        ],
      },
      {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'less-loader'],
        }),
      },
      {
        test: /\.css$/,
        use: [
          {
            // creates style nodes from JS strings
            loader: 'style-loader',
          },
          {
            // translates CSS into CommonJS
            loader: 'css-loader',
          },
        ],
      },
      {
        test: /\.svg/,
        use: {
          loader: 'svg-url-loader',
        },
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        use: [
          {
            loader: 'file-loader',
          },
        ],
      },
    ],
  },
  // devtool: 'source-map',
  optimization: {
    runtimeChunk: {
      name: 'app',
    },
    splitChunks: {
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: 'all',
        },
      },
    },
    minimize: true,
  },
  resolve: { extensions: ['*', '.js', '.jsx', '.ts', '.tsx', 'scss'], modules: ['node_modules'] },
  output: {
    // path: `${__dirname}/dist`,
    // filename: 'app.js',
    publicPath: 'http://localhost:3000/hmr/',
    chunkFilename: '[id].js?v=[chunkhash]',
  },
  devServer: {
    stats: {
      // assets: true,
      // cachedAssets: true,
      // performance: true,
      // entrypoints: true,
      // chunkGroups: true,
      // chunks: true,
      // chunkModules: true,
      // show modules contained in each chunk
      // chunkOrigins: true,
      // show the origin of a chunk (why was this chunk created)
      // chunkRelations: true,
      // show relations to other chunks (parents, children, sibilings)
      // dependentModules: true,
    },
    disableHostCheck: true,
    host: '127.0.0.1',
    port: 3000,
    publicPath: '/hmr/',
    filename: 'app.js',
    // hot: true,
    // hotOnly: true,
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new ForkTsCheckerWebpackPlugin({
      typescript: {
        diagnosticOptions: {
          semantic: true,
          syntactic: true,
        },
      },
      async: true,
    }),
    new ESLintPlugin({
      eslintPath: 'eslint',
      fix: true,
      quiet: true,
      extensions: ['ts', 'tsx'],
    }),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  ],
};

請告訴我如何在保存文件后減少項目中文件的編譯時間?

解決了這個問題。 問題出在 eslint-loader-webpack 插件中。 如果你安裝了舊版本的 eslint-loader,那么一切正常。

如何解決eslint-loader-webpack插件慢的問題?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM