我希望使用 webpack 来转换index.ts
,但它也会在没有依赖关系的情况下检查another.ts
。为什么?
演示:
$ node_modules/webpack/bin/webpack.js
[...]
ERROR in [at-loader] ./another.ts:1:3
TS1005: ';' expected.
ERROR in [at-loader] ./another.ts:1:6
TS1005: ';' expected.
ERROR in [at-loader] ./another.ts:1:33
TS1002: Unterminated string literal.
ERROR in [at-loader] ./another.ts:1:1
TS2304: Cannot find name 'I'.
ERROR in [at-loader] ./another.ts:1:3
TS2304: Cannot find name 'don'.
完整演示:
$ cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
$ sudo yum install epel-release
$ sudo yum install nodejs
$ node -v
v6.10.3
$ npm init
$ npm install --save-dev typescript ts-loader webpack
$ node_modules/typescript/bin/tsc -v
Version 2.3.4
$ node_modules/typescript/bin/tsc --init
$ vim index.ts
index.ts
:
class Person {
public name: string;
constructor(name:string) {
this.name = name;
}
}
let p:Person = new Person("Joe");
console.log(p.name);
Transpilation有效:
$ node_modules/typescript/bin/tsc index.ts
执行工作:
$ node index.js
Joe
Webpack 配置:
$ node_modules/webpack/bin/webpack.js -v
3.0.0
$ vim webpack.config.js
webpack.config.js
:
module.exports = {
entry: './index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
}
]
},
output: {
filename: 'bundle.js',
path: __dirname
}
};
Transpilation有效:
$ node_modules/webpack/bin/webpack.js
执行工作:
$ node bundle.js
Joe
另一个文件创建:
$ vim another.ts
another.ts
:
I don't want to check this file.
重新翻译:
$ node_modules/webpack/bin/webpack.js
错误!
ERROR in [at-loader] ./another.ts:1:3
TS1005: ';' expected.
ERROR in [at-loader] ./another.ts:1:6
TS1005: ';' expected.
ERROR in [at-loader] ./another.ts:1:33
TS1002: Unterminated string literal.
ERROR in [at-loader] ./another.ts:1:1
TS2304: Cannot find name 'I'.
ERROR in [at-loader] ./another.ts:1:3
TS2304: Cannot find name 'don'.
another.ts
已被检查,但它不在webpack.config.js
中!为什么!
我尝试使用awesome-typescript-loader
代替ts-loader
,同样的问题。