我正在将使用 Angular 1.x 的 javascript 项目转换为 WebPack 和 TypeScript(使用ts-loader)。我让它大部分工作,但是当 ts-loader 似乎在不直接使用导出时优化我的脚本时,我遇到了麻烦。
这是一个演示问题的示例项目(npm install、webpack,然后加载 index.html 并观察控制台)。
https://github.com/bbottema/webpack-typescript
来自 ClassA 的日志正在显示,但 angular 报告 ClassB 丢失(提供者)。如果您查看 bundle.js,您会发现 ClassB 完全丢失了。不同的是ClassA在导入后直接开始使用,而ClassB在编译时只按类型引用。
这是一个错误,还是有办法强制包含 ClassB?还是我做错了?Angular 2 可能会解决这个问题,但现在这一步太大了。
上述项目中的相关脚本:
包.json
{
"devDependencies": {
"typescript": "^1.7.5",
"ts-loader": "^0.8.1"
},
"dependencies": {
"angular": "1.4.9"
}
}
webpack.config.js
var path = require('path');
module.exports = {
entry: {
app: './src/entry.ts'
},
output: {
filename: './dist/bundle.js'
},
resolve: {
root: [
path.resolve('./src/my_modules'),
path.resolve('node_modules')
],
extensions: ['', '.ts', '.js']
},
module: {
loaders: [{
test: /\.tsx?$/,
loader: 'ts-loader'
}]
}
};
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs"
},
"exclude": [
"node_modules"
]
}
索引.html
<!doctype html>
<html ng-app="myApp">
<body>
<script src="dist/bundle.js"></script>
</body>
</html>
entry.js
declare var require: any;
'use strict';
import ClassA = require('ClassA');
import ClassB = require('ClassB');
var a:ClassA = new ClassA(); // direct use, this works
var angular = require('angular');
angular.module('myApp', []).
// this compiles as it should, but in runtime the provider will not be packaged and angular will throw an error
run(function(myProvider: ClassB) {
}
);
类A.ts
// this line will be logged just fine
console.log('ClassA.ts: if you see this, then ClassA.ts was packaged properly');
class ClassA {
}
export = ClassA;
B类.ts
declare var require: any;
// this line is never logged
console.log('ClassB.ts: if you see this, then ClassB.ts was packaged properly');
class ClassB {
}
var angular = require(angular);
angular.module('myApp').service(new ClassB());
export = ClassB;