webpack.config.jsjavascript
const cpus = require('os').cpus().length;
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
module.exports = {
module: {
rules: [
// 单进程
//{
// test: /\.tsx?$/,
// 默认状况下,ts-loader 会进行转译和类型检查
// 由于是单进程,因此 webpack 能够收集到错误信息,并经过 dev-server 反馈到浏览器
// 但这也致使了 webpack 构建速度极慢
// use:['ts-loader']
//},
// 多进程
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{loader: 'cache-loader'},
{
loader: 'thread-loader',
options: {
workers: cpus - 1,
},
},
'babel-loader',
{
loader: 'ts-loader',
options: {
// 关闭类型检查,即只进行转译
// 类型检查交给 fork-ts-checker-webpack-plugin 在别的的线程中作
// transpileOnly: true,
// 若是设置了 happyPackMode 为 true
// 会隐式的设置 transpileOnly: true
happyPackMode: true
// 若是是 vue 应用,须要配置下这个
// appendTsSuffixTo: [/\.vue$/]
}
}
]
},
{
test: /\.(js|jsx)$/,
use: ['happypack/loader?id=js'],
exclude: [/node_modules/, /(.|_)min\.js$/],
// include: [
// path.resolve(__dirname, "src")
// ],
}
],
},
plugins: [
// fork 一个进程进行检查
new ForkTsCheckerWebpackPlugin({
// async 为 false,同步的将错误信息反馈给 webpack,若是报错了,webpack 就会编译失败
// async 默认为 true,异步的将错误信息反馈给 webpack,若是报错了,不影响 webpack 的编译
// async: false,
// eslint: false,
checkSyntacticErrors: true
})
]
};
复制代码
tsconfig.jsonhtml
{
"compilerOptions": {
//"module": "commonjs",
"target": "es5",
/* 'react' 模式下,ts 会将 tsx 编译成 jsx 后再将 jsx 编译成 js*/
/* 'preserve' 模式下:TS 会将 tsx 编译成 jsx 后,再也不将 jsx 编译成 js,保留 jsx */
/* 保留 jsx 时,就须要在 ts-loader 前面配置 babel-loader 去处理 jsx */
/* 换句话说:只有想要用 babel-laoder 的时候,才须要设置这个值 */
"jsx": "preserve",
},
}
复制代码
const cpus = require('os').cpus().length;
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
module.exports = {
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
'babel-loader',
{
loader: 'ts-loader',
options: {
// 关闭类型检查,即只进行转译
// 类型检查交给 fork-ts-checker-webpack-plugin 在别的的线程中作
transpileOnly: true,
}
}
]
},
{
test: /\.(js|jsx)$/,
use: ['happypack/loader?id=js'],
exclude: [/node_modules/, /(.|_)min\.js$/],
// include: [
// path.resolve(__dirname, "src")
// ],
}
],
},
plugins: [
// fork 一个进程进行检查
new ForkTsCheckerWebpackPlugin({
// async 为 false,同步的将错误信息反馈给 webpack,若是报错了,webpack 就会编译失败
// async 默认为 true,异步的将错误信息反馈给 webpack,若是报错了,不影响 webpack 的编译
// async: false,
// eslint: false,
checkSyntacticErrors: true
})
]
};
复制代码
tsconfig.jsonvue
{
"compilerOptions": {
//"module": "commonjs",
"target": "es5",
/* 'react' 模式下,ts 会将 tsx 编译成 jsx 后再将 jsx 编译成 js*/
/* 'preserve' 模式下:TS 会将 tsx 编译成 jsx 后,再也不将 jsx 编译成 js,保留 jsx */
/* 保留 jsx 时,就须要在 ts-loader 前面配置 babel-loader 去处理 jsx */
/* 换句话说:只有想要用 babel-laoder 的时候,才须要设置这个值 */
"jsx": "preserve",
},
}
复制代码
webpack.config,jsjava
rules: [
{
test:/\.(tsx?|jsx?)$/,
// 默认会调用 @babel/core
use:'babel-loader'
}
]
复制代码
.babelrcnode
{
"presets": [
"@babel/preset-env"
"@babel/preset-react",
"@babel/preset-typescript"
]
}
复制代码
It's possible to parallelise your builds. Historically this was useful from a performance perspective with webpack 2 / 3. With webpack 4+ there appears to be significantly less benefit and perhaps even cost.
But if that's what you want to do, there's two ways to achieve this: happypack and thread-loader. Both should be used in combination with fork-ts-checker-webpack-plugin for typechecking.)react
package.jsonwebpack
{
"scripts": {
// 再开一个 npm 脚本自动检查类型
"type-check": "tsc --watch",
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@babel/preset-typescript": "^7.3.3",
"typescript": "^3.5.2"
}
}
复制代码
tsconfig.jsongit
{
"compilerOptions": {
// 不生成文件,只作类型检查
"noEmit": true,
},
}
复制代码
有四种语法在 babel 中是没法编译的github
import
/export
),在推荐的 tslint 规则中也建议不要使用 namesapce。namespace Person{
const name = 'abc';
}
复制代码
interface Person {
name: string;
age: number
}
let p1 = {age: 18} as Person;
console.log(p2.name);
let p2 = <Person>{age: 18};
console.log(p3.name);
复制代码
const enum Sex {
man,
woman
}
复制代码
import xxx= require(…)
和 export = xxx
。[译] TypeScript 和 Babel:一场美丽的婚姻web
使用@babel/preset-typescript取代awesome-typescript-loader和ts-loader
https://segmentfault.com/q/1010000019545436