4

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 ?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

thx. i am aware of that option and t's useful for external libs but writing this for every file/class (>100) in an app does not makes sense (makes whole idea of dynamic loading irrelevant). specially when I know that in src/ dir are only .ts files producing es6 modules. I would like sth on package level not module like - System.config({ meta: { './src': { format: 'es6' } } }); OR extension level config - System.config({ meta: { '.ts': { format: 'es6' } } });
you can use matching syntax if you need. './src/*.ts': { format: 'es6 }
I did not notice that - probably cliked by mistake while checking it as an answer. Sorry for that (IF that was me)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.