我在 index.html 中有这个 SystemJS 配置:
<body>
<script src="node_modules/systemjs/dist/system.js"></script>
<script>
System.config({
defaultJSExtensions: true,
transpiler: 'typescript',
map: {
typescript: 'node_modules/typescript/lib/typescript.js'
},
packages: {
"ts": {
"defaultExtension": "ts"
}
},
});
System.import('ts/main');
</script>
</body>
main.ts:
let a = [1, 2, 3];
let b = [1, 2, 3];
我得到:Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
。看起来文件没有被 SystemJS 转译。
当我在第一行添加 import 语句时,它完美地工作:
import * as ts from 'typescript'; // or any other package
let a = [1, 2, 3];
let b = [1, 2, 3];
看起来 SystemJS 通过“内容”识别 typescript 文件 - 这是正确的吗?如果是,如何强制它转译每个 .ts 或 src/ 文件?
最佳答案
如您所料,systemjs 正在猜测您在文件中使用的语法。您可以通过添加
来帮助 systemjs// maybe you need to use " format:'register' " instead
System.config({
meta: {
'*.ts': {
format: 'es6'
}
}
});
更多信息 module-formats
关于typescript - 如何根据文件扩展名而不是内容使 SystemJS 转译 'typescript',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33032390/