首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化webpack构建时间

优化webpack构建时间
EN

Stack Overflow用户
提问于 2020-11-09 04:09:21
回答 1查看 704关注 0票数 0

在保存文件中的更改后,我在编译项目时遇到了问题;文件的编译时间需要2分钟或更长时间。

为了解决这个问题,我采取了以下步骤:

1.在babel-loader中,将文档中option对象中的属性cacheDirectory设置为true,将cacheComprassion设置为false 2.在ts-loader中,将文档中option对象中的transpileOnly和happyPackMode属性设置为true。3.禁用set tool以获得更好的性能4. ForkTsCheckerWebpackPlugin连接到typescript中的检查类型5.自定义代码拆分

Webpack配置文件

代码语言:javascript
复制
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$/),
  ],
};

请告诉我如何在保存文件后减少项目中文件的编译时间?

EN

回答 1

Stack Overflow用户

发布于 2020-11-10 03:08:05

解决了问题。问题出现在eslint-loader-webpack插件中。如果您安装了旧版本的eslint-loader,那么一切工作正常。

如何解决eslint-loader-webpack插件运行缓慢的问题?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64742540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档