简体   繁体   中英

Systemjs and tsc setup - *.ts.js 404 (Not Found) Error

I was just trying to create proof-of-concept TS + SystemJS project with minimum possible setup - without "Angular2 starter kit" and such. I've spent an hour googling and tweaking this setup but it still doesn't work.

I have the following setup:

/src/
 - model.ts
 - bootstrap.ts

/dist/

tsconfig.json
system.config.js
index.html

My tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "removeComments": false,
        "outDir": "./dist/"
    },
    "filesGlob": [
        "./src/**/*.ts"
    ],
    "compileOnSave": true
}

My SystemJS config

System.config({
    paths: {
        // paths serve as alias
        'npm:': 'node_modules/'
    },
    map: {
        testapp: './src'
    },
    baseURL: './src',
    packages: {
        testapp: {
            format: 'register',
            defaultExtension: 'js'
        }
    },

});

My index.html

  <script src="/node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="/node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
    System.import('bootstrap.ts')
        .then(function(app) {
            console.log('app bootstrap success!');
        }, console.error.bind(console));
    </script>

In the end I have following error in console:

system.src.js:1051 GET http://0.0.0.0:8080/src/bootstrap.ts.js 404 (Not Found)fetchTextFromURL @ system.src.js:1051....
Error: (SystemJS) XHR error (404 Not Found) loading 

I run tsc -w in one terminal tab and simple https server in the other.

Apparently - SystemJS wants to consume files with .ts.js extension while tsc generates .js and source maps but NOT .ts.js .

Do I miss webpack or gulp to do some part of the job? I thought SystemJS and tsc could do the bundling and transpiling but apparently there's smth missing in the build process. Explanation on the SystemJS setup rather than fixing the bug would be also very helpful.

Setup can be fixed by following:

1) as @Nitzan Tommer mentioned - change .ts to .js in System.import block

index.html: System.import('bootstrap.js')

2) (SystemJS) require is not defined error is fixed by this post - change tsconfig.json to have "system" as modules

{
    "compilerOptions": {
        "module": "system",
        "target": "es5",
//..

Here is the demo repo I've created with this setup: https://github.com/shershen08/systemjs-typescript-simple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM