TypeScript loader for Webpack. This project was started as a fork of https://github.com/andreypopp/typescript-loader. Thanks @andreypopp for the great project.
The main goal of this loader is to support the watch mode and webpack-dev-server with incremental compilation. Also there are a lot of problems in other TypeScript loaders that were fixed here.
npm install awesome-typescript-loader --save-dev
- Add
.ts
as a resolvable extension. - Configure all files with a
.ts
extension to be handled byawesome-typescript-loader
.
webpack.config.js
module.exports = {
// Currently we need to add '.ts' to resolve.extensions array.
resolve: {
extensions: ['', '.ts', '.webpack.js', '.web.js', '.js']
},
// Source maps support (or 'inline-source-map' also works)
devtool: 'source-map',
// Add loader for .ts files.
module: {
loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript-loader'
}
]
}
};
After that, you would be able to build TypeScript files with webpack.
- target = 'es5'
You can use tsconfig.json file to configure your compiler and loader:
{
"compilerOptions": {
"noImplicitAny": true,
"removeComments": true,
},
"awesomeTypescriptLoaderOptions": {
/* ... */
}
}
Allows use of TypeScript compilers other than the official one. Should be
set to the NPM name of the compiler, e.g. ntypescript or path to a tsc
file.
Note that the compiler must be installed in your project. You can also use
nightly versions.
Specify whether or not the loader emits webpacks's require type. You might use emitRequireType=false
with Node, because it already has own require type in node.d.ts
file.
Allows use of library other than the target
's default one. Useful when you want to use ES6 library with ES5 target. Also you might use library=es6
with Node.
Allows to use several TypeScript compilers with different settings in one app. Just override instanceName
to initialize another instance.
Collect files dependency graph and re-emit all dependent files along with changed file.
Specify path to a TS config file. Useful when you have multiple config files. This setting is useless inside TS config file.
Use this setting to force loader to use webpack's way to load files. Useful only with ts-jsx-loader. Builds may become slower.
Array of paths to .d.ts files that must be included in program. Useful with rewriteImports
.
Use this setting to disable type checking if you want.
Do type checking in a separate process, so webpack don't need to wait. Significantly improves development workflow with tools like react-hot-loader.
Works only with ForkCheckerPlugin
:
var ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
plugins: [
new ForkCheckerPlugin(),
]
Less logging from the checker.
Invoke Babel to traspile files. Useful with ES6 target.
Use pre-compiled files if any. Files must be named as {filename}.js
and {filename}.map
.
Use internal file cache. Useful with Babel, when processing takes a long time to complete. Improves warm-up time.
Directory when cache is stored.
Invoke glob resolver using 'filesGlob' and 'exclude' sections of tsconfig
.
You can pass compiler options inside loader query string or in tsconfig file.
This loader has support of both --watch
and webpack-dev-server
modes. It handles file dependencies
using internal webpack dependency markers. When you change a file, the loader recompiles all the dependencies.
The most natural way to structure your code with TypeScript and webpack is to use external modules, and these work as you would expect.
npm install --save react
import * as React from 'react';
This project doesn't aim to support internal modules, because it's hard to resolve dependencies for the watch mode if you use such modules. Of course, you can still use them without watch, but this function is unstable.
All declaration files should be resolvable from the entry file.
The easiest way to do this is to create a references.d.ts
file which contains
references to all of your declaration files. Then reference
references.d.ts
from your entry file.