1
votes

My project folder structure is the same as quick-start ToH https://angular.io/docs/ts/latest/tutorial/toh-pt1.html

So to separate .ts file and .js file, I added to the tsconfig.json with "outDir": "dist". tsc then compiled all the .js and .map files into the dist folder. So far so good. dist folder is parallel to app folder.

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "outDir": "dist",
    "suppressImplicitAnyIndexErrors":true
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
} 

Next I changed the system.import in index.html

<script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      },
      services: {defaultExtension: 'js'}
    }
  });
  //System.import('app/main')
  System.import('./dist/app/main')
        .then(null, console.error.bind(console));
</script>

Now after npm start, the browser gave me

 system.src.js:1154 GET http://localhost:3000/dist/app/main 404 (Not Found)

How can I let system import function find the main.js?

The folder structure looks like this

root
  |--app            // all .ts files are here
  |--dist
       |--app       // all .js and .map files are here
1
Didn't you move the index.html into the dist? Correctly, it should be thereDinistro
I did. it works from within dist folder (and I think that is the purpose of dist folder for production). but then I lost the ability to debug in typescript. But thanks for mentioning.Shawn

1 Answers

3
votes

I would leverage a map block if you want to keep the main HTML file into the root folder:

<script>
  System.config({
    map: {
      app: 'dist/app' // <-----
    },
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      },
      services: {defaultExtension: 'js'}
    }
  });
  System.import('app/main')
        .then(null, console.error.bind(console));
</script>