1

I'm trying to create a bundle file using SystemJS-builder.

This is my Gulp task:

gulp.task('bundle', function() {

    var builder = new systemjsBuilder('', './app/configs/systemjs.config.js');

    return builder.buildStatic('./app/**/*.js', './production/bundle.js', { 
        minify: true, 
        sourceMaps: true,
        encodeNames: false
    })
    .then(function() {
        console.log('Build complete');
    })
    .catch(function(err) {
        console.log('Build error');
        console.log(err);
    });

});

It creates production/bundle.js fine, and I can see my modules in there. But when I replace the following:

<script>
  System.import('app').catch(function(err){ 
    console.error(err); 
  });
</script>

with:

<script src="production/bundle.js"></script>

inside my "index.html" file, I get:

core.umd.js:5995 EXCEPTION: Uncaught (in promise): Error: No NgModule metadata found for 'HomeModule'.

What am I doing wrong?

I don't want to use Webpack.


Edit:

This is what home.module.ts looks like:

import { NgModule } from '@angular/core';
import { SharedModule } from '../shared/shared.module';
import { HomeComponent } from './components/home/home.component';
import { routing } from './home.routing';

@NgModule({
    imports: [SharedModule, routing],
    providers: [],
    declarations: [HomeComponent]
})

export default class HomeModule {}

Edit 2:

I have now taken a slightly different approach and get a different problem.

After reading this post, this is my updated gulpfile.js:

gulp.task('inline-templates', function () {
    return gulp.src('app/**/*.ts')
    .pipe(inlineNg2Template({ UseRelativePaths: true, indent: 0,  removeLineBreaks: true}))
    .pipe(tsc({
        "target": "ES5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": true,
        "noImplicitAny": false
     }))
    .pipe(gulp.dest('dist/app'));
});

gulp.task('bundle-app', ['inline-templates'], function() {
  var builder = new systemjsBuilder('', 'app/configs/systemjs.config.js');

  return builder
     .bundle('dist/app/**/* - [@angular/**/*.js] - [rxjs/**/*.js]', 'bundles/app.bundle.js', { minify: true})
     .then(function() {
         console.log('Build complete');
      })
     .catch(function(err) {
        console.log('Build error');
        console.log(err);
     });
});

gulp.task('bundle-dependencies', ['inline-templates'], function() {
   var builder = new systemjsBuilder('', 'app/configs/systemjs.config.js');

   return builder
     .bundle('dist/app/**/*.js - [dist/app/**/*.js]', 'bundles/dependencies.bundle.js', { minify: true})
     .then(function() {
        console.log('Build complete');
      })
     .catch(function(err) {
        console.log('Build error');
        console.log(err);
     });
});

gulp.task('production', ['bundle-app', 'bundle-dependencies'], function(){});

This exports my entire app as JavaScript to the "dist" folder, but when bundle-app and bundle-dependencies tries to run I get:

Error on fetch for dist/app/modules/app/app.module at file:///c:/path/to/project/dist/app/modules/app/app.module

From what I can understand, this is because main.js looks like this at the very top:

System.register(['@angular/platform-browser-dynamic', './modules/app/app.module'], function(exports_1, context_1) {

because if I manually edit that file so that it includes ".js" at the end like this:

System.register(['@angular/platform-browser-dynamic', './modules/app/app.module.js'], function(exports_1, context_1) {

The error disappears (for that particular file but continues for the next file).

How can I do this automatically so I don't have to manually edit each single JavaScript file inside the "dist" folder?

Community
  • 1
  • 1
Glenn Utter
  • 2,313
  • 7
  • 32
  • 44

1 Answers1

0

After adding this to my systemjs.config.js the new Gulp setup works:

defaultExtension: 'js',
defaultJSExtensions: true

It now creates two bundles with all code inside it.

Thanks for your replies!

Glenn Utter
  • 2,313
  • 7
  • 32
  • 44
  • Glenn, is your app actually using app.bundle.js for all of the code it uses, or is it still pulling the required bits of code from the dist/app/** folders/files? I am able to get to the point where I have successfully created an app.bundle.js file, but my app's code is still being run from dist/app/**. I can see this by checking the network tab in my dev console and I'm still making way too many requests (a request for every one of my components/pipes/etc). I would appreciate any insight you may have on this one. – jbgarr Nov 09 '16 at 03:17