0

My app is structure like so:

app
- assets
- src
-- autogenerated
-- components
---- app
---- tabs

The "autogenerated" folder contains the compiled javascript files generated from typescript using the same directory structure from the components folder. I use gulp-tsc to create it. (I'll eventually rename this to "build")

app, and tabs are components... app being the top level component.

In app.ts (in app/components/app) is:

import {Component, bootstrap} from 'angular2/angular2';
import {Tabs} from '../Tabs/tabs';
@Component({
    selector: 'my-app',
    templateUrl: 'src/components/app/app.html',
    directives: [Tabs]
})
class AppComponent { }
bootstrap(AppComponent);

When this compiles, the generated js file has this:

var tabs_1 = require('../Tabs/tabs');

which does not resolve and results in a 404 since it has no .js extension when being loaded by the browser.

Edit... just realized that "require" is from nodejs, not systemjs... still not sure how to proceed.

** Edit 2 ** The gulp task:

gulp.task('compile', function() {
  // compile sass to css
  gulp.src('./app/assets/sass/*.scss')
    .pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
    .pipe(gulp.dest('./app/assets/css'));

  // compile typescript
  // this ignores tsconfig.json, but ideally configuration in both places should match
  gulp.src(['./app/src/**/*.ts'])
    .pipe(typescript({  target: "ES5",
                        experimentalDecorators: true,
                        sourceMap: true,
                        emitDecoratorMetadata: true }))
    .pipe(gulp.dest('./app/src/autogenerated/'));
});

enter image description here

The gulp task shouldn't be using the tsconfig.json... I added it to remove the error in visual studio code.... but here's whats in the tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  }
}

index.html code:

<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <script src="../../node_modules/systemjs/dist/system.src.js"></script>
    <script src="../../node_modules/angular2/bundles/angular2.dev.js"></script>
<script>
  System.config({
    packages: {'app': {
        defaultExtension: 'js'}
      }
  });
  System.import('../src/components/app/app.js');
</script>
  </head>
  <body>
    <my-app>Loading... please wait</my-app>
  </body>
</html>
2
  • Can you show more configuration ? Gulp file for example ? Commented Nov 10, 2015 at 19:25
  • I've added the gulp task. It uses the gulp-tsc package. Commented Nov 10, 2015 at 19:31

2 Answers 2

1
  1. Use tsconfig.json, it's THE best way to configure your TypeScript compiler, and it is maintained by the TypeScript team. No need for some gulp tasks to understand what is typescript. Just run 'tsc' in the command line (Let gulp run 'exec').

  2. If you compile your modules into commonjs, (That's totally valid) you should have some way to load commonjs modules in the browser. And SystemJS (Same as angular2 is using) is currently the best module loader as of today. So, just create a SystemJs config file for your app and import the app.

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

3 Comments

I think my brain is getting fried with all the different module loaders, etc. (jspm, commonjs, systemjs...). I'll go with the recommendation to just use the typescript compiler instead of some gulp package. Just to be clear... I don't think I created any modules. I just used npm to install node modules. Typescript compiles import statements to require statements, which I can't get to load into the browser. An example would be great... I can't find any good one on the internets. I've added my systemjs code from index.html to my question
I see now that you have the systemjs as your module loader. what's the problem now? (It should work as far as I can see)
When viewing the angular2 app in Electron, it try's to load /Tabs/tabs instead of /Tabs/tabs.js. Without the js extension it doesn't resolve.
1

Visual Studio with TypeScript installed ignores your tsconfig file.

In you project properties under TypeScript Build set your Module system to "System"

You will have to manually change your project file by adding these lines under PropertyGroup:

<TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>
<TypeScriptModuleResolution>node</TypeScriptModuleResolution>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.