我有以下依赖项:
"devDependencies": {
"@types/node": "^4.0.27-alpha",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"ts-loader": "^0.8.2",
"typescript": "^2.0.0",
"webpack": "^1.13.1"
}
.babelrc:
{
"presets": [
"es2015",
"stage-0"
]
}
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": false,
"outDir": "built"
},
"exclude": [
"node_modules"
]
}
webpack.config.js:
module.exports = {
entry: ['babel-polyfill', './src/'],
output: {
path: __dirname,
filename: './built/bundle.js',
},
resolve: {
modulesDirectories: ['node_modules'],
extensions: ['', '.js', '.ts'],
},
module: {
loaders: [{
test: /\.tsx?$/, loaders: ['ts-loader', 'babel-loader'], exclude: /node_modules/
}],
}
};
/src/index.ts:
async function foo() {
const value = await bar();
console.log(value);
}
function bar() {
return new Promise((resolve, reject) => {
return resolve(4);
});
}
(async function run() {
await foo();
}());
使用这个设置可以正常工作,我可以构建和运行它(记录了4个正确)。但是我总是在Webpack上遇到一些错误:
ERROR in ./src/index.ts
(4,32): error TS2304: Cannot find name 'regeneratorRuntime'.
ERROR in ./src/index.ts
(6,12): error TS2304: Cannot find name 'regeneratorRuntime'.
ERROR in ./src/index.ts
(31,451): error TS2346: Supplied parameters do not match any signature of call target.
ERROR in ./src/index.ts
(40,33): error TS2304: Cannot find name 'regeneratorRuntime'.
ERROR in ./src/index.ts
(41,12): error TS2304: Cannot find name 'regeneratorRuntime'.
看起来与babel-polyfill
有关。我错过了什么吗?
ts-loader
和使用babel-loader
与Babel 7的区别之一是,在转译过程中,ts-loader
会进行类型检查,而babel-loader
则不会。在本回答中链接的有关使用Babel 7的TypeScript的说明建议单独调用tsc
进行类型检查。 - Ben Regenspants-loader
。这就是重点。无论是Babel本身还是tsc本身都将达到一种状态,不需要插件即可实现相同的结果。建议人们依赖社区维护的插件通常并不具备未来的可靠性。FYI:我不使用ts-loader
,因为我不需要其中的功能。我追求稳定性。 - VimNing