【问题标题】:How to make SystemJS transpile 'typescript' based on file extension not contents如何根据文件扩展名而不是内容使 SystemJS 转译“打字稿”
【发布时间】:2015-10-09 07:26:37
【问题描述】:

我在 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 * as ts from 'typescript'; // or any other package

let a = [1, 2, 3];
let b = [1, 2, 3];

看起来 SystemJS 通过“内容”识别打字稿文件 - 这是正确的吗?如果是,如何强制它转译每个 .ts 或 src/ 文件?

【问题讨论】:

    标签: typescript systemjs


    【解决方案1】:

    正如您所怀疑的,systemjs 猜测您在文件中使用的语法。您可以通过添加来帮助 systemjs

    // maybe you need to use " format:'register' " instead
    System.config({
      meta: {
        '*.ts': {
          format: 'es6'
        }
      }
    });
    

    更多信息module-formats

    【讨论】:

    • 谢谢。我知道该选项,并且 t 对于外部库很有用,但是为应用程序中的每个文件/类(> 100)编写这个没有意义(使动态加载的整个想法无关紧要)。特别是当我知道在 src/dir 中只有 .ts 文件产生 es6 模块时。我想在包级别而不是模块上的东西 - System.config({ meta: { './src': { format: 'es6' } } });或扩展级别配置 - System.config({ meta: { '.ts': { format: 'es6' } } });
    • 您可以根据需要使用匹配的语法。 './src/*.ts': { 格式:'es6 }
    • 我没有注意到 - 在检查它作为答案时可能会误点击。对不起(如果那是我)
    【解决方案2】:

    我编写了一个模块来帮助使用 SystemJS 处理多个文件扩展名: https://www.npmjs.com/package/one-plugin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2013-10-07
      • 2011-08-19
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多