~benji/juju-gui/bug-1074336

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node

/**
 * We aggregate and minimize the js sources in order to improve the load speed
 * of the application.
 *
 * We don't want to use the yui combo loader feature because we want to be able
 * to run from only static files and we want to be able to run behind a
 * firewall without access to the internet.
 *
 * The final product will provide three js files: one for the YUI dependencies,
 * one for our custom js code and one for third party js like d3.
 * 
 * Known issues:
 * (1) If we set "bootstrap=false" in the GlobalConfig object, yui disables the
 *     loader object. It means it wont even try to download modules. We cannot
 *     do it because the loader also manages the "use" property which defines
 *     aliases for some of your modules ('juju-views' and 'juju-controllers').
 * (2) During development, we've noticed that some of the yui modules weren't
 *     included in the list of yui files (lang/datatype-date-format_en-US,
 *     parallel, app-transitions-native, gallery-markdown, loader-base). For
 *     some reason, the loader does not resolve the names of the files for
 *     these modules. We need to add them manually in this file.
 */

'use strict';

require('yui').YUI().use(['yui'], function(Y) {
  var merge = require('../lib/merge-files.js'),
      syspath = require('path'),
      paths,
      reqs;

  // First we find all of the paths to our custom Javascript in the app
  // directory.  We need to tell the function to ignore the "assets" directory
  // and the debug version of the modules file. I need to use
  // "syspath.join(process.cwd(), ...)" or else I have... "Error: Cannot find
  // module './app/config.js'" from node's internal module.js file.
  paths = merge.readdir(syspath.join(process.cwd(), './app'),
    [syspath.join(process.cwd(), './app/assets'), './app/modules-debug.js']);

  // Get the name of all the YUI modules that our custom js files use directly.
  reqs = merge.loadRequires(paths);

  // For some reason the loader does not get these requirements.
  // (Known issue #2)
  reqs.push('lang/datatype-date-format_en-US');
  reqs.push('parallel');
  reqs.push('app-transitions-native');
  reqs.push('gallery-markdown');
  reqs.push('loader-base');

  // Get all of the YUI files and their dependencies, and combine them.
  merge.combine(merge.getYUIFiles(reqs),
      './app/assets/javascripts/generated/all-yui.js', true);

  // Combine third party js libraries
  merge.combine([ './app/assets/javascripts/d3.v2.min.js',
      './app/assets/javascripts/reconnecting-websocket.js',
      './app/assets/javascripts/svg-layouts.js' ],
      './app/assets/javascripts/generated/all-third.js', true);

  // Now we only need to generate the file that is used to tell YUI where all
  // the dependencies are.  We either use a debug version or the production
  // version.  The debug version is simply pointers to the individual files.
  // The production version aggregates all of the files together.

  // Creating the combined file for the modules-debug.js and config.js files
  merge.combine([ './app/modules-debug.js', './app/config.js' ],
      './app/assets/javascripts/generated/all-app-debug.js', false);

  // Creating the combined file for all our files.  Note that this includes
  // app/modules.js in the rollup, which is why that file still needs exist.
  merge.combine(paths, './app/assets/javascripts/generated/all-app.js', true);
});