Search code examples
javascriptaureliasystemjs

Aurelia and System.js production build


I am maintaining an existing Aurelia project that apparently wasn't created with Aurelia CLI.

I need to create a production build but am not being able with the current configuration. The development build works just fine, but, as expected, downloads a lot of code to user machine.

After running gulp prod (gulpfile listed below), I get two JS files: app-build-{revnumber}.js and vendor-build-{revnumber}.js, but Aurelia keeps trying to load the main.js file.

Error loading main.js

I tried building the main.js together (commented code in gulpfile.js), but had no success - only vendor bundle is loaded:

Only vendor bundle is loaded

Here are my config files:

config.js

System.config({
  baseURL: "/www",
  defaultJSExtensions: true,
  transpiler: "babel",
  babelOptions: {
    "stage": 0,
    "optional": [
      "runtime",
      "optimisation.modules.system"
    ]
  },
  paths: {
    "*": "src/*",
    "github:*": "jspm_packages/github/*",
    "npm:*": "jspm_packages/npm/*"
  },
  map: { /* mappings */ }
});

gulpfile.js

var gulp = require('gulp');
var bundler = require('aurelia-bundler');
var image = image = require('gulp-image');
var replace = require('gulp-replace');
var gulpsync = require('gulp-sync')(gulp);

var config = {
  force: true,
  baseURL: '.',
  configPath: './config.js',
  bundles: {
    // "dist/main": {
    //   includes: [
    //     '[main.js]'
    //   ],
    //   options: {
    //     inject: true,
    //     minify: false,
    //     rev: false
    //   }
    // },
    "dist/app-build": {
      includes: [
        '[**/*.js]',
        '**/*.html!text',
        '**/*.css!text'
      ],
      options: {
        inject: true,
        minify: true,
        rev: true
      }
    },
    "dist/vendor-build": {
      includes: [ /* all external modules */ ],
      options: {
        inject: true,
        minify: true,
        rev: true
      }
    }
  }
};

gulp.task("bundle", function () {
  return bundler.bundle(config)
    .then(function () {
      gulp.src('config.js')
        .pipe(replace('dist/', ''))
        .pipe(replace('src', 'dist'))
        .pipe(gulp.dest(''));
    });
});

gulp.task("unbundle",
  function () {
    return bundler.unbundle(config)
      .then(function () {
        gulp.src('config.js')
          .pipe(replace('dist', 'src'))
          .pipe(gulp.dest(''));
      });
  });

gulp.task("image-bundle",
  function () {
    gulp.src('./src/media/*')
      .pipe(image())
      .pipe(gulp.dest('./dist/media'));
  });

gulp.task("files-bundle", function () {
  return gulp
    .src('./src/style/material.woff2')
    .pipe(gulp.dest('./dist/style'));
});

gulp.task('prod', gulpsync.sync(['unbundle', 'bundle', 'image-bundle', 'files-bundle']));

gulp.task('dev', gulpsync.sync(['unbundle']));

index.html

<!DOCTYPE html>
<html>
<head>...</head>
<body aurelia-app="main">
    <script src="www/jspm_packages/system.js"></script>
    <script src="www/config.js"></script>
    <script>
        System.import('aurelia-bootstrapper');
    </script>
</body>
</html>

Solution

  • After a lot of little adjustments, I solved the main.js issue.

    It seems like System.js looks for the dependency in the bundle and, if not found, it hits the network. Here's what I did to fix my bundle:

    1. Consolidated dist/ folder as the bundle source

      In config.js, set the path of * to dist/* and don't modify it in any Gulp task. from src to dist

    2. Before the bundle task runs, I copy all contents from src/ to dist/
    3. In the dist/app-build bundle, I added the option depCache: true. It does not work when false (gives the error of main.js not found), but I don't really know why.

    The added/modified Gulp tasks:

    // deletes all files in the output path
    gulp.task('clean', ['unbundle'], function () {
      return gulp.src(['dist/'])
        .pipe(vinylPaths(del));
    });
    
    gulp.task('copy', function () {
      return gulp.src(['src/**/*'])
        .pipe(gulpCopy('dist/', { prefix: 1 }));
    });
    
    gulp.task("bundle", sync(['clean', 'copy']), function () {
      return bundler.bundle(config);
    });
    
    gulp.task("unbundle", function () {
      return bundler.unbundle(config);
    });