【问题标题】:TypeScript compilation extremely slow > 12sTypeScript 编译极慢 > 12s
【发布时间】:2016-08-30 14:49:00
【问题描述】:

只是把它放在那里看看其他人是否有这个问题......

我已经使用 webpack 作为我的构建工具构建了一个带有 typescript 的 angular 2 应用程序,一切运行良好,但是我注意到 typescript 编译超级超级慢,我现在只有 12 秒......,而且它很漂亮很明显,这都是由于打字稿编译过程造成的......

我使用 ts-loader 或 awesome-typescript-loader 得到了类似的结果,如果我注释掉这个加载器,我的构建时间会下降到大约 1 秒....

经过一些研究,编译打字稿时“慢”时间似乎是“正常的”,但 12 秒是正常的吗?

旧帖提示可能是节点版本冲突,我目前运行的是v4.4.2...

下面是我的 webpack 代码,以防有人发现那里有问题。Uglify 部分中的注释代码是由于角 2 侧的一些“错误”造成的...

const path = require('path');
const merge = require('webpack-merge');
const webpack = require('webpack');

const NpmInstallPlugin = require('npm-install-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TARGET = process.env.npm_lifecycle_event;

const PATHS = {
  app: path.join(__dirname, 'app'),
  dist: path.join(__dirname, 'dist')
};

const common = {
  entry: {
    vendor: ['./app/vendor.ts'],
    main: ['./app/boot.component.ts']
  },
  output: {
    filename: '[name].[hash].bundle.js',
    path: PATHS.dist
  },
  resolve: {
    extensions: ['', '.js', '.ts']
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Qarrot Performance',
      template: 'index.template.ejs',
      commonStyles: [
        '/public/styles/vendor/normalize.css',
        '/public/styles/main.css'
      ],
      commonScripts: [],
      baseHref: '/',
      unsupportedBrowser: true,
      mobile: true,
      appMountId: 'app'
    }),
  ],
  module: {
    loaders: [
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        loaders: ['ts-loader']
      },
      {
        test: /\.scss$/,
        loader: 'raw-loader!sass-loader'
      },
      {
        test: /\.html$/,
        loader: "html"
      }
    ]
  }
}

// Dev Settings
if(TARGET === 'start' || !TARGET) {
  module.exports = merge(common, {
    devtool: 'eval-source-map',
    devServer: {
      contentBase: PATHS.build,
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
      stats: 'errors-only',
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new NpmInstallPlugin({
        save: true
      })
    ]
  });
}

// Prod Settings
if(TARGET === 'build') {
  module.exports = merge(common, {
    devtool: 'cheap-module-source-map',
    plugins: [
      // new webpack.optimize.UglifyJsPlugin({
      //   beautify: false,
      //   mangle: false,
      //   compress : { screw_ie8 : true },
      //   comments: false
      // }),
      new webpack.optimize.DedupePlugin(),
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"production"'
      }),
      new CopyWebpackPlugin([
            { from: 'public', to: 'public' }
      ]),
      new webpack.optimize.CommonsChunkPlugin({
        names: ['vendor', 'manifest']
      }),
    ]
  });
}

我也在 Mac 上,运行 Angular 2 beta.15 和 webpack 版本 1.12,下面是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "compileOnSave": false,
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

【问题讨论】:

    标签: performance typescript angular webpack


    【解决方案1】:

    我会坚持使用awesome-typescript-loader。它有一些您可以启用的性能选项:缓存选项和仅转译选项:

    "awesomeTypescriptLoaderOptions": {
      "useCache": true,
      "transpileOnly": true
    }
    

    这两种方法都显着缩短了编译时间。

    【讨论】:

      【解决方案2】:

      请分享你tsconfig.json。很可能您将noEmitOnError 设置为true,这意味着编译器在任何发射之前强制对整个代码库进行类型检查

      请将其设置为 false。

      【讨论】:

      • 嘿@basart 谢谢你的建议,我已经用我的 tsconfig 更新了我的问题......我没有设置那个选项,但是当我添加它时它并没有太大的影响。 ..
      猜你喜欢
      • 2014-04-25
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多