ts-loader使用happypack后打包更慢了

ts-loader使用happypack后打包更慢了
代码如下

const path = require('path');
const fs = require('fs');
const lessToJs = require('less-vars-to-js');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const env = process.argv.slice(-1)[0];

const devUrlLoader = 'url-loader?limit=8192&name=[hash:8].[name].[ext]';
const prodUrlLoader = 'url-loader?limit=8192&name=[hash:8].[name].[ext]&outputPath=assets/images/&publicPath=assets/images';

// 获取自己定义的要覆盖antd默认样式的文件
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, '../src/assets/style/theme.less'), 'utf8'));

module.exports = {
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            // loader: 'happypack/loader?id=js',
            loader: 'babel-loader',
          },
          {
            // loader: 'happypack/loader?id=ts',  // 用这个打包速度慢了很多
            loader: 'ts-loader',
          },
        ],
        include: [
          path.join(__dirname, '../src'),
        ],
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'happypack/loader?id=js',
            // loader: 'babel-loader',
          },
        ],
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: [{
            loader: 'css-loader',
          }],
          fallback: 'style-loader',
        }),
      },
      {
        test: /\.sass$/,
        use: ['style-loader', 'css-loader', 'sass-loader?outputStyle=expanded&indentedSyntax'],
      }, {
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader?outputStyle=expanded'],
      },
      {
        test: /\.less$/,
        exclude: /node_modules/,
        use: ExtractTextPlugin.extract({
          use: [{
            loader: 'happypack/loader?id=less_src', // 用这个打包速度慢了很多
          }],
          fallback: 'style-loader',
        }),
      },
      {
        test: /\.less$/,
        exclude: /src/,
        use: ExtractTextPlugin.extract({
          use: [{
            loader: 'happypack/loader?id=less_node_modules', // 用这个打包速度慢了很多
          }],
          fallback: 'style-loader',
        }),
      },
      {
        test: /\.(png|jpe?g|gif|woff|woff2|ttf|eot)$/,
        loader: env === 'development' ? devUrlLoader : prodUrlLoader,
      },
      {
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
      },
    ],
  },
  plugins: [

  ],
  resolve: {
    modules: [
      path.resolve(__dirname, '../src'),
      'node_modules',
    ],
    extensions: ['.js', '.json', '.jsx', '.ts', '.tsx'],
    alias: {
      src: path.resolve(__dirname, '../src/'),
    },
  },
};


const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');
const lessToJs = require('less-vars-to-js');
const fs = require('fs');

// happypack 加速打包
const HappyPack = require('happypack');
const os = require('os');

const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const commonConfig = require('./webpack.base.js');

// 获取自己定义的要覆盖antd默认样式的文件
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, '../src/assets/style/theme.less'), 'utf8'));

module.exports = function (env) {
  return merge(commonConfig, {
    cache: true,
    devtool: 'cheap-module-eval-source-map',
    entry: {
      bundle: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://localhost:8000',
        'webpack/hot/only-dev-server',
        './src/index.tsx',
      ],
      vendor: ['react', 'react-dom'],
      lib: ['antd'],
    },
    output: {
      path: path.join(__dirname, '/../dist/assets'),
      filename: '[name].js',
      publicPath: '/assets/',
      sourceMapFilename: '[name].map',
    },
    devServer: {
      historyApiFallback: true,
      noInfo: false,
      hot: true,
      open: true,
      stats: 'minimal',
      contentBase: './src/',
      publicPath: '/assets/',
      compress: true,
      port: 8000,
      proxy: {
        '/api/*': {
          target: 'http://localhost:9090',
          secure: false,
          changeOrigin: true,
        },
      },
    },
    optimization: {
      runtimeChunk: {
        name: 'manifest',
      },
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new ExtractTextPlugin({
        filename: 'style.css',
        disable: false,
        allChunks: true,
      }),
      new HappyPack({
        id: 'ts',
        threadPool: happyThreadPool,
        loaders: [
          {
            path: 'ts-loader',
            query: { happyPackMode: true, transpileOnly: true },
          },
        ],
      }),
      new ForkTsCheckerWebpackPlugin({ checkSyntacticErrors: true }),
      new HappyPack({
        id: 'js',
        loaders: ['babel-loader'],
        threadPool: happyThreadPool,
      }),
      new HappyPack({
        id: 'less_src',
        loaders: [{
          loader: 'css-loader',
          options: {
            importLoaders: 1,
            modules: true,
            namedExport: true,
            camelCase: true,
            localIdentName: '[path][name]__[local]--[hash:base64:5]',
          },
        }, {
          loader: 'postcss-loader',
        },
        {
          loader: 'less-loader',
          options: {
            javascriptEnabled: true,
            modifyVars: themeVariables,
          },
        }],
        threadPool: happyThreadPool,
      }),
      new HappyPack({
        id: 'less_node_modules',
        loaders: [{
          loader: 'css-loader',
          options: {
            importLoaders: 1,
          },
        }, {
          loader: 'postcss-loader',
        },
        {
          loader: 'less-loader',
          options: {
            javascriptEnabled: true,
            modifyVars: themeVariables,
          },
        }],
        threadPool: happyThreadPool,
      }),
    ],
  });
};

      

不明白是为什么,打包反而更慢了,难倒我配置错了?

这是我的项目,完整的代码在这里
https://github.com/wanliyunya...

2018/8/9
不仅仅ts慢了,打包less样式也变慢了。我觉得是我的配置问题呢!!!大神们帮我看看呗

回复
阅读 10.4k
6 个回答

很可能不是配置的问题,几个项目用下来,只要使用了happypack速度都会变慢,构建速度优化效果最佳的始终是基础代码剥离

是首次慢还是每次更改文件都慢?happypack提速的原理之一是缓存,但是这个是建立在你的依赖关系比较清晰的情况下,依赖清晰的意思就是,该用的时候 import,不该用的时候不 import,如果依赖关系特别复杂,可能影响缓存命中率。

虽然说现在我也不折腾 webpack 了,不过从 1.0 用到 3.0 总体的感觉是这样的,就是如果要提高构建速度,那么使用 webpack 官方提供的一些最佳实践其实已经足够了,比如 CommonChunk、resolve、alias等,除了配置本身,和你书写代码的方式也很有关系,良好的代码组织方式也有利于构建工具进行自动优化。

在这个基础上,使用 happypack 这种插件也只是锦上添花的而已,它本身并不会瞬间将你的开发体验提升一个量级。

希望有所帮助。

用awesome-typescript-loader替换ts-loader能提高打包速度。

ts-loader options里面加上transpileOnly: true;
然后用fork-ts-checker-webpack-plugin另起线程处理tslint校验;
速度提升非常明显。

新手上路,请多包涵

是你的配置写错了 ,你在一个 tsx 处理器里给了两个异步的loader;处理一个tsx 文件先用ts-loader 转换成jsx 再用babel-loader 转换成 js; 这应该是一个完整的组合功能 这个组合才是一个多线程能保证完整执行的最小单元
const happyTsx =new happyPack({

id:"happy-tsx"
loaders:["babel-loader","ts-loader"]

})

logo
Microsoft
子站问答
访问
宣传栏