如何根据文件扩展名而不是内容使 SystemJS 转译“打字稿" [英] How to make SystemJS transpile 'typescript' based on file extension not contents
问题描述
我在 index.html 中有这个 SystemJS 配置:
I have this SystemJS config in index.html:
<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];
我得到:未捕获的语法错误:在严格模式之外尚不支持块范围的声明(let、const、function、class)
.看起来文件没有被 SystemJS 转译.
I get: Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
. It looks like file is not transpiled by SystemJS.
当我在第一行添加 import 语句时,它工作得很好:
When I add import statement in first line it works perfectly:
import * as ts from 'typescript'; // or any other package
let a = [1, 2, 3];
let b = [1, 2, 3];
看起来 SystemJS 通过内容"识别打字稿文件 - 这是正确的吗?如果是,如何强制它转译每个 .ts 或 src/ 文件?
It looks like SystemJS recognizes typescript file by "contents" - is this correct? If yes, how to force it to transpile every .ts or src/ file ?
推荐答案
正如你所怀疑的,systemjs 猜测你在你的文件中使用了哪种语法.您可以通过添加
As you suspected, the systemjs guessing which syntax you are using in your file. You can help the systemjs by adding
// maybe you need to use " format:'register' " instead
System.config({
meta: {
'*.ts': {
format: 'es6'
}
}
});
更多信息模块格式
这篇关于如何根据文件扩展名而不是内容使 SystemJS 转译“打字稿"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!