Search code examples
typescriptsystemjs

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


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 ?


Solution

  • 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