包详细信息

awesome-typescript-loader

s-panferov404.6kMIT5.2.1

Awesome TS loader for webpack

webpack, loader, webpack-loader, typescript

自述文件

TypeScript loader for Webpack

Join the chat at https://gitter.im/s-panferov/awesome-typescript-loader Build Status

See also:

  1. tygen — TypeScript documentation generator.

Installation

npm install awesome-typescript-loader --save-dev

Performance issues

Please note that ATL works the same way as a TypeScript compiler as much as possible. So please be careful with your files/exclude/include sections.

ADVICE: Turn on useCache option.

ADVICE: Typically you want your files section to include only entry points.

ADVICE: The loader works faster if you use isolatedModules or forceIsolatedModules options.

ADVICE: The loader works faster if you will use reportFiles to restrict checking scope.

ADVICE: Use the loader together with hard-source-webpack-plugin

The world is changing, other solutions are evolving and ATL may work slower for some workloads. Feel free to try ts-loader with HappyPack or thread-loader and hard-source-webpack-plugin.

Differences between ts-loader

awesome-typescript-loader loader was created mostly to speed-up compilation in my own projects. Some of them are quite big and I wanted to have full control on how my files are compiled. There are two major points:

1) atl has first-class integration with Babel and enables caching possibilities. This can be useful for those who use Typescript with Babel. When useBabel and useCache flags are enabled, typescript's emit will be transpiled with Babel and cached. So next time if source file (+environment) has the same checksum we can totally skip typescript's and babel's transpiling. This significantly reduces build time in this scenario.

2) atl is able to fork type-checker and emitter to a separate process, which also speeds-up some development scenarios (e.g. react with react-hot-loader) So your webpack compilation will end earlier and you can explore compiled version in your browser while your files are typechecked.

Configuration

  1. Add .ts as a resolvable extension.
  2. Configure all files with a .ts extension to be handled by awesome-typescript-loader.

webpack.config.js

// `CheckerPlugin` is optional. Use it if you want async error reporting.
// We need this plugin to detect a `--watch` mode. It may be removed later
// after https://github.com/webpack/webpack/issues/3460 will be resolved.
const { CheckerPlugin } = require('awesome-typescript-loader')

module.exports = {

  // Currently we need to add '.ts' to the resolve.extensions array.
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.jsx']
  },

  // Source maps support ('inline-source-map' also works)
  devtool: 'source-map',

  // Add the loader for .ts files.
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'awesome-typescript-loader'
      }
    ]
  },
  plugins: [
      new CheckerPlugin()
  ]
};

After that, you will be able to build TypeScript files with webpack.

NodeJS versions

The loader supports NodeJS 4 and newer.

tsconfig.json

You can use the tsconfig.json file to configure your compiler and loader:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "removeComments": true
    },
    "awesomeTypescriptLoaderOptions": {
        /* ... */
    }
}

Supported TypeScript

awesome-typescript-loader@2.x aims to support only typescript@2.x and webpack@2x, if you need old compilers please use 1.x or 0.x versions.

Advanced path resolution in TypeScript 2.0

If you want to use new paths and baseUrl feature of TS 2.0 please include TsConfigPathsPlugin. This feature is available only for webpack@2.1.

const { TsConfigPathsPlugin } = require('awesome-typescript-loader');

resolve: {
    plugins: [
        new TsConfigPathsPlugin(/* { configFileName, compiler } */)
    ]
}

Loader options

silent (boolean) (default=false)

No logging from the checker. Please note that this option disables async error reporting because this option bans console.log() usage.

compiler (string) (default='typescript')

Allows use of TypeScript compilers other than the official one. Must be set to the NPM name of the compiler, e.g. ntypescript or the path to a package folder. Note that the compiler must be installed in your project. You can also use nightly versions.

useTranspileModule (boolean) (default=false)*

Use fast transpileModule emit mode. Disables automatically when you set compilerOption declaration: true (reference).

instance (string) (default='at-loader')

Allows the use of several TypeScript compilers with different settings in one app. Override instance to initialize another instance.

configFileName (string) (default='tsconfig.json')

Specifies the path to a TS config file. This is useful when you have multiple config files. This setting is useless inside a TS config file.

transpileOnly (boolean)

Use this setting to disable type checking, enabling this will nullify the CheckerPlugin usage in your webpack configuration.

errorsAsWarnings (boolean)

Emit all typescript errors as warnings.

forceIsolatedModules (boolean)

Use this setting to disable dependent module recompilation.

ignoreDiagnostics (number[]) (default=[])

You can squelch certain TypeScript errors by specifying an array of diagnostic codes to ignore. For example, you can transpile stage 1 properties from *.js using "ignoreDiagnostics": [8014].

useBabel (boolean) (default=false)

Invoke Babel to transpile files. Useful with ES6 target. Please see useCache option which can improve warm-up time.

If you're using babelOptions, anything in .babelrc will take precedence. This breaks expected usage for scenarios where you need two sets of Babel configs (example: one for Webpack, one for your build tools).

You may want to "babelrc": false to disable .babelrc if you don't want it:

{
    "useBabel": true,
    "babelOptions": {
        "babelrc": false, /* Important line */
        "presets": [
            ["@babel/preset-env", { "targets": "last 2 versions, ie 11", "modules": false }]
        ]
    },
    "babelCore": "@babel/core", // needed for Babel v7
}

babelCore (string) (default=undefined)

Override the path used to find babel-core. Useful if node_modules is installed in a non-standard place or webpack is being invoked from a directory not at the root of the project.

For Babel 7, this should be set to "@babel/core".

babelOptions (object) (default=null)

Use this option to pass some options to Babel (e.g. presets). Please note that .babelrc file is more universal way to do this.

useCache (boolean) (default=false)

Use internal file cache. This is useful with Babel, when processing takes a long time to complete. Improves warm-up time.

usePrecompiledFiles (boolean) (default=false)

Use pre-compiled files if any. Files must be named as {filename}.js and {filename}.map.

cacheDirectory (string) (default='.awcache')

Directory where cache is stored.

reportFiles (string[])

Specify globs to report file diagnostics. ALL OTHER ERRORS WILL NOT BE REPORTED. Example:

reportFiles: [
    "src/**/*.{ts,tsx}"
]

getCustomTransformers (string | ((program: ts.Program) => ts.CustomTransformers | undefined)) (default=undefined)

Provide custom transformers, TypeScript 2.4.1+. Example:

const styledComponentsTransformer = require('typescript-plugin-styled-components').default;
const keysTransformer = require('ts-transformer-keys/transformer').default;

// ...
rules: [
    {
        test: /\.tsx?$/,
        loader: 'awesome-typescript-loader',
        options: {
            // ... other loader's options
            getCustomTransformers: program => ({
                before: [
                    styledComponentsTransformer(),
                    keysTransformer(program)
                ]
            })
        }
    }
]

Compiler options

You can pass compiler options inside the loader query string or in a TS config file.

更新日志

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.2.1 (2018-09-06)

5.2.0 (2018-06-21)

Features

  • pass ts.Program as an argument for getCustomTransformers (#594) (c0d10bf)

5.1.1 (2018-06-19)

Bug Fixes

  • dependencies: make webpack-log a dependency (#593) (247730d)

5.1.0 (2018-06-10)

Bug Fixes

  • pass Unix-style path to config parser (#581) (c006193)

Features

5.0.0 (2018-04-06)

5.0.0-1 (2018-03-07)

5.0.0-0 (2018-03-06)

Features

4.0.0 (2018-03-06)

Features

4.0.0-1 (2018-03-02)

4.0.0-0 (2018-02-24)

3.5.0 (2018-02-24)

Features

  • allow setting getCustomTransformers as a path to a module (#531) (ad7cfad)

3.4.1 (2017-12-01)

Bug Fixes

  • use proper compiler variable (e30023d)

3.4.0 (2017-11-18)

3.4.0-0 (2017-11-17)

3.3.0 (2017-11-01)

Bug Fixes

  • performance issue on case-insensitive file systems #507 (fc04d29)
  • use enhancedResolve to resolve babel-core (9f7e978)

Features

  • add errorsAsWarnings option (bc9b544)
  • add forceIsolatedModules (7f74bba)

3.2.3 (2017-08-14)

Bug Fixes

  • checker: pass through execArgv to checker child process (#479) (5c7b0de)
  • delay sending diagnotics request (90022ae)

3.2.2 (2017-07-26)

Bug Fixes

  • dependencies: upgrading node dependencies (#472) (9473d2c)

3.2.1 (2017-06-28)

Bug Fixes

  • speedup caching by pre-hashing cacheIdentifier (589683f)

3.2.0 (2017-06-28)

Bug Fixes

  • paths-plugin: properly skip .d.ts files (f14eaab)
  • don't kill the loader when compilation.bail = true (#448) (3d8f91a)
  • environment cache invalidation (#449) (c073af1)

3.2.0-rc.0 (2017-06-16)

Features

  • case errors, silent, diagnostics, paths-plugin fixes (#446) (a15de0a)

3.1.3 (2017-04-28)

3.1.2 (2017-03-07)

Bug Fixes

  • properly handle declarationDir (0742e1a)

3.1.1 (2017-03-07)

Bug Fixes

  • proper check for fileName (399167f)

3.1.0 (2017-03-07)

Bug Fixes

  • always output unix-like path (905e14b)

Features

3.0.8 (2017-02-26)

Bug Fixes

  • don't add undefined to files (70d9f40)

3.0.7 (2017-02-22)

3.0.6 (2017-02-22)

Bug Fixes

3.0.5 (2017-02-22)

3.0.4 (2017-02-16)

Bug Fixes

3.0.4-rc.2 (2017-02-11)

3.0.4-rc.1 (2017-02-11)

Bug Fixes

  • try to fix some places of potential performance degradation, refs #366 (9c31177)

3.0.4-rc.0 (2017-02-09)

Bug Fixes

  • try to fix some places of potential performance degradation, refs #366 (441ef99)

3.0.3 (2017-02-06)

Bug Fixes

  • allow to pass context to PathPlugin, basic tests (207b164)

3.0.2 (2017-02-05)

3.0.1 (2017-02-05)

3.0.0 (2017-02-05)

Bug Fixes

  • refactor tests, add angular-webpack-starter test, also fixes #286 (41df56b)

Features

  • get options from this.options (f43a215)

3.0.0-beta.20 (2017-02-05)

Bug Fixes

3.0.0-beta.19 (2017-02-04)

Bug Fixes

  • undefined error on result.deps (ef56f36)

Features

3.0.0-beta.18 (2017-01-10)

Bug Fixes

  • correcting references to typescriptService.d.ts (8b5413d)
  • forgot to remove stale reference from tests (aef976f)
  • typo in package.json file (760e26b)

3.0.0-beta.17 (2016-12-14)

Bug Fixes

3.0.0-beta.16 (2016-12-14)

Bug Fixes

2.2.4 (2016-09-07)

Bug Fixes

2.2.3 (2016-09-06)

Bug Fixes

  • don't make configFilePath absolute twice, fixes #220 (cab47b5), closes #220

2.2.2 (2016-09-06)

2.2.1 (2016-08-15)

Bug Fixes

  • don't concat paths that already absolute (069e493)

2.2.0 (2016-08-15)

Bug Fixes

  • proper initial fileNames injection, refs #205 (71d01b1)

Features

2.1.1 (2016-07-25)

Bug Fixes

  • resolve dependencies for types entries (b70a81c)

2.0.1 (2016-07-08)

Bug Fixes

  • guard for 1.8.10 (1d1867b)
  • minor paths-plugin fix (230780a)
  • node 0.12.x (ec07d17)
  • process watch files with toUnix (79b72f1)
  • proper module resolution for .d.ts files (36b412c)
  • rename ts -> tsImpl, refs #156 (d8f24a1)
  • search all nodes for modules (can be optimized) (b3a9218)
  • watch files on Windows (normalize) (75b41d7)

Features

  • exclude test (3d24ccd)
  • fixed module resolution; add lib option support (e5ff563)
  • initial impl of PathsPlugin, refs #156 (6401a94)
  • more options, better error messages from ts (e4498df)
  • no more async, simplify module resolution (87f096e)
  • resolve type reference directives (799b17c)
  • rework path normalization for new TS (f6dcc22)
  • support types preventive resolution (6916c8f)
  • use TS's preProcessFile to extract deps, refs #168 (96fba97)

2.0.0 (2016-07-07)

Bug Fixes

  • guard for 1.8.10 (1d1867b)
  • minor paths-plugin fix (230780a)
  • process watch files with toUnix (79b72f1)
  • proper module resolution for .d.ts files (36b412c)
  • rename ts -> tsImpl, refs #156 (d8f24a1)
  • search all nodes for modules (can be optimized) (b3a9218)
  • watch files on Windows (normalize) (75b41d7)

Features

  • fixed module resolution; add lib option support (e5ff563)
  • initial impl of PathsPlugin, refs #156 (6401a94)
  • more options, better error messages from ts (e4498df)
  • no more async, simplify module resolution (87f096e)
  • resolve type reference directives (799b17c)
  • rework path normalization for new TS (f6dcc22)
  • support types preventive resolution (6916c8f)
  • use TS's preProcessFile to extract deps, refs #168 (96fba97)

2.0.0-rc.18 (2016-06-29)

Bug Fixes

2.0.0-rc.17 (2016-06-27)

2.0.0-rc.16 (2016-06-25)

Bug Fixes

2.0.0-rc.15 (2016-06-25)

Features

2.0.0-rc.14 (2016-06-25)

Features

  • support types preventive resolution (a74441f)

2.0.0-rc.13 (2016-06-24)

Bug Fixes

  • watch files on Windows (normalize) (3e8f324)

2.0.0-rc.12 (2016-06-23)

Bug Fixes

  • process watch files with toUnix (045ffd8)

2.0.0-rc.11 (2016-06-23)

Features

  • rework path normalization for new TS (76ada5d)

2.0.0-rc.10 (2016-06-21)

Bug Fixes

2.0.0-rc.9 (2016-06-21)

2.0.0-rc.8 (2016-06-21)

Features

  • resolve type reference directives (27b4f9f)

2.0.0-rc.7 (2016-06-21)

Features

  • use TS's preProcessFile to extract deps, refs #168 (0a064e7)

2.0.0-rc.6 (2016-06-20)

2.0.0-rc.5 (2016-06-20)

Bug Fixes

  • search all nodes for modules (can be optimized) (dacee2d)

2.0.0-rc.4 (2016-06-20)

Bug Fixes

  • proper module resolution for .d.ts files (9752d72)

2.0.0-rc.3 (2016-06-20)

2.0.0-rc.2 (2016-06-20)

2.0.0-rc.1 (2016-06-20)

2.0.0-rc.0 (2016-06-20)

Features

  • fixed module resolution; add lib option support (6fe51a9)
  • no more async, simplify module resolution (a8e867e)

1.1.1 (2016-06-16)

Bug Fixes

1.1.0 (2016-06-16)

Features

1.0.0 (2016-06-10)

Features

0.19.1 (2016-06-05)

Features

  • *: check if module is internal before processing it (c2e2cc29)

0.19.0 (2016-06-05)

0.18.0 (2016-05-13)

Bug Fixes

  • *:
    • babel polyfill require guard, refs #121, #133 (e71f1555)
    • node6 compat, refs #134 (4b66ab78)
  • cache: deprecation warning (c56fe459)

0.18.0-rc.0 (2016-04-24)

Features

  • *: speed-up compilation of dynamic requires + total speedup (d3d740b5)

0.17.0 (2016-04-24)

Bug Fixes

Features

0.17.0-rc.7 (2016-04-21)

Features

  • *: update compilation process, fix sourceMap bug, refs #125 (73f575d0)

0.17.0-rc.6 (2016-04-15)

Features

  • *: add option to disable .d.ts files typechecks (519c5543)

0.17.0-rc.5 (2016-04-04)

Bug Fixes

  • *: bind resolver to original context for webpack2 (f5105f6b)

0.17.0-rc.4 (2016-03-30)

Bug Fixes

  • *: patch resolver for webpack2 (60ca7777)

0.17.0-rc.3 (2016-03-29)

Bug Fixes

Features

  • *:
    • impl declaration option basic support (0169df57)
    • add babel options, write some tests on babel (12c708cc)

0.17.0-rc.2 (2016-03-23)

Features

  • *: update resolver to work with webpack2, refs #109 (680d036a)

0.17.0-rc.1 (2016-03-22)

Features

0.17.0-rc.0 (2016-03-21)

Features

0.16.2 (2016-03-19)

Bug Fixes

  • *:
    • don't show exclude warning when only files present, (cf19faec, closes #97)
    • ignore out option. (53276b03, closes #96)

0.16.1 (2016-03-19)

Bug Fixes

0.16.0 (2016-03-19)

Features

  • *:
    • resolve tsconfig with different name (ebe24d98)
    • resolve tsconfig with different name (0de96388)

0.16.0-rc.0 (2016-03-04)

Bug Fixes

  • instance: fix error message about babel (bd1e0b08)

Features

  • *:
    • allowJs works (wip) (d7a8a641)
    • xit salsa test for now (6c973b6a)
    • add travis to project (0c6309a2)
    • loader plugins (e.g. docscript) (76d0a548)
    • remove bluebird, format filename for sourcemaps (1dacfa85)

0.15.10 (2016-01-26)

Bug Fixes

  • *:
    • exclude package.json files from program (d8546dc1)
    • fix program update issue (a606ed89)
  • checker-runtime: fix SyncResolver type (b55af480)
  • deps:
    • don't resolve symlinks to all .d.ts files (a23d4bf4)
    • fix path respolution for angular-style file names (b43e2d8f)
  • host:
    • add ad-hoc files lookup to support typings scenario (b768c243)
    • import path (478553a7)
  • index: use instance.options to invoke externals (1715bee8)

Features

  • *:
    • use only .d.ts files from tsconfig.json (baa4d4e9)
    • debounce running checker (938fc968)
    • add glob warning (0cb62506)
    • add sourceRoot support (bddf79cb)
    • use another preset for babel to work on prev node versions (5c7972d4)
    • return webpack pre-resolution (19c559a9)
    • refactor tsconfig parser (b2f414f1)
    • async refactor (9831bdcc)
  • checker-runtime: checker now can resolve imports too (99220c1f)
  • tests: create a basic test for the loader (9aed17f4)

0.15.9 (2015-12-03)

Bug Fixes

  • deps: don't resolve symlinks to all .d.ts files (a23d4bf4)

0.15.8 (2015-12-03)

Bug Fixes

  • index: use instance.options to invoke externals (1715bee8)

0.15.7 (2015-12-03)

0.15.6 (2015-12-02)

Bug Fixes

  • *: exclude package.json files from program (d8546dc1)

0.15.5 (2015-12-02)

Bug Fixes

  • host: add ad-hoc files lookup to support typings scenario (b768c243)

0.15.4 (2015-11-26)

Features

0.15.3 (2015-11-24)

Bug Fixes

  • checker-runtime: fix SyncResolver type (b55af480)

Features

  • checker-runtime: checker now can resolve imports too (99220c1f)

0.15.2 (2015-11-24)

Bug Fixes

  • deps: fix path respolution for angular-style file names (b43e2d8f)

Features

0.15.1 (2015-11-20)

Bug Fixes

  • *: fix program update issue (a606ed89)

0.15.0 (2015-11-19)

0.15.0-rc.2 (2015-11-18)

Features

  • *: use another preset for babel to work on prev node versions (5c7972d4)

0.15.0-rc.1 (2015-11-16)

0.15.0-rc.0 (2015-11-13)

Bug Fixes

Features

0.14.1 (2015-10-31)

Bug Fixes

  • *:
    • correctly process jsx option (f7ae91c4)
    • fix unrecoverable error during watch (6f7ad270)
    • watch tsx files too (cc494a3b)
  • index:
    • correct work with a promise chain (c20d684c)
    • use try-catch instead of if when resolving lib files (946fc850)
    • don't call the callback twice (3abf8e48)

Features

0.14.0 (2015-09-17)

Features

0.14.0-rc.1 (2015-09-15)

Features

0.14.0-rc.0 (2015-09-10)

Features

  • *: checker in sep. process now runs only with ForkCheckerPlugin (85e9e1bc)

0.13.1 (2015-09-03)

Features

  • *: pass resolution info into the checker (266ec78e)

0.12.0 (2015-08-14)

Features

  • *: read .d.ts files from tsconfig.json (d4025d30)

0.12.0-rc.2 (2015-08-12)

Features

0.12.0-rc.1 (2015-08-10)

Features

0.12.0-rc.0 (2015-08-10)

Features

  • *: learn loader to pick-up precompiled files if any (71935896)

0.11.3 (2015-08-06)

Bug Fixes

  • index: correct work with a promise chain (c20d684c)

0.11.2 (2015-08-04)

Bug Fixes

  • index: use try-catch instead of if when resolving lib files (946fc850)

0.11.1 (2015-08-02)

0.11.0 (2015-08-02)

Features

  • *: support the new libs path (c224f05d)

0.11.0-rc.0 (2015-07-31)

Features

  • *: externals is an array now (69cadb52)

0.9.1 (2015-07-24)

Features

  • checker: fix compiler reference (8dcad5d2)

0.9.0 (2015-07-24)

Bug Fixes

  • index: don't call the callback twice (3abf8e48)

Features

  • *: use a compiler from a client app (cae4d00f)

0.8.0 (2015-07-23)

Features

  • *: add alias example (ca86fd71)
  • runtime: add require signature for AMD (970ff875)

0.7.1 (2015-07-18)

Bug Fixes

  • *: fix unrecoverable error during watch (6f7ad270)

0.7.0 (2015-07-18)

0.5.4 (2015-06-09)

0.5.3 (2015-06-05)

Features

0.5.2 (2015-05-30)

Features

0.5.1 (2015-05-30)

Features

  • helpers: add +1 to line because they are zero-based in TS (76efd423)

0.5.0 (2015-05-30)

Features

  • *: play well with ExtractTextPlugin (33807f1b)

0.4.0 (2015-05-30)

Features

  • *:
    • add tsconfig support and return ts-jsx-loader back (c03c73e1)
    • refactor and impl separate file analyzer (8964d3d7)

0.3.1 (2015-05-11)

0.3.0 (2015-05-11)

Features

  • *: use new just-emit workflow; this must be much faster too (9d3a9961)