typescript - 如何根据文件扩展名而不是内容使 SystemJS 转译 'typescript'

标签 typescript systemjs

我在 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/

相关文章:

ecmascript-6 - 在 VueJS + JSPM 中作为注释呈现的组件

javascript - 使用清晰设计系统构建 Angular 2 应用程序

Angular2 - 加载映射模块时出现 SystemJS 404

angular - 自定义实体的 ngbTypeahead

javascript - React Router - 未捕获的语法错误 : Unexpected token < when refreshing an URL with params

javascript - Socket.io 广播到房间不工作

Angular2 延迟加载模块 : how to create a build package with SystemJS Builder?

javascript - Angular 2 typescript 调用函数

javascript - Angular 为 2 的三态复选框

javascript - 如何在没有任何额外依赖的情况下在浏览器中使用 UMD