How to make SystemJS transpile 'typescript' based on file extension not contents

廉价感情. 提交于 2019-12-10 17:27:57

问题


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];

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.

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];

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 ?


回答1:


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'
    }
  }
});

more info module-formats




回答2:


I wrote a module to help out with handling multiple file extensions with SystemJS: https://www.npmjs.com/package/one-plugin



来源:https://stackoverflow.com/questions/33032390/how-to-make-systemjs-transpile-typescript-based-on-file-extension-not-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!