Skip to content

Commit

Permalink
Initial PR to add support for fastboot (#140)
Browse files Browse the repository at this point in the history
* Initial PR to add support for fastboot

* Use this._findHost to find parent app
  • Loading branch information
jasonmit committed Jun 1, 2017
1 parent 97dbc90 commit 493b5ab
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 41 deletions.
70 changes: 29 additions & 41 deletions index.js
@@ -1,56 +1,53 @@
/* globals require, module, process, __dirname */
'use strict';

const mergeTrees = require('broccoli-merge-trees');
const rename = require('broccoli-stew').rename;
const defaults = require('lodash.defaults');
const funnel = require('broccoli-funnel');
const existsSync = require('exists-sync');
const stew = require('broccoli-stew');
const chalk = require('chalk');
const path = require('path');

// Checks to see whether this build is targeting FastBoot. Note that we cannot
// check this at boot time--the environment variable is only set once the build
// has started, which happens after this file is evaluated.
function isFastBoot() {
const rename = stew.rename;
const map = stew.map;

function isLegacyFastboot() {
return process.env.EMBER_CLI_FASTBOOT === 'true';
}

module.exports = {
name: 'moment',

included(app) {
included() {
this._super.included.apply(this, arguments);

// see: https://github.com/ember-cli/ember-cli/issues/3718
while (typeof app.import !== 'function' && app.app) {
app = app.app;
}

this.app = app;
let app = this._findHost();
this.momentOptions = this.getConfig();
this.fastbootTarget = 'fastboot-moment.js';

if (this.momentOptions.includeTimezone) {
this.fastbootTarget = 'fastboot-moment-timezone.js'
}

if (isFastBoot()) {
this.importFastBootDependencies(app);
if (isLegacyFastboot()) {
this.importLegacyFastBootDependencies(app);
} else {
this.importBrowserDependencies(app);
}

return app;
},

importFastBootDependencies(app) {
if (arguments.length < 1) {
throw new Error('Application instance must be passed to import');
}
updateFastBootManifest(manifest) {
manifest.vendorFiles.push('moment/' + this.fastbootTarget);

return manifest;
},

importLegacyFastBootDependencies(app) {
app.import(this.treePaths.vendor + '/fastboot-moment.js');
},

importBrowserDependencies(app) {
if (arguments.length < 1) {
throw new Error('Application instance must be passed to import');
}

let vendor = this.treePaths.vendor;
let options = this.momentOptions;

Expand Down Expand Up @@ -130,7 +127,7 @@ module.exports = {
treeForPublic() {
let publicTree = this._super.treeForPublic.apply(this, arguments);

if (isFastBoot()) {
if (isLegacyFastboot()) {
return publicTree;
}

Expand All @@ -154,31 +151,22 @@ module.exports = {
},

treeForVendor(vendorTree) {
if (isFastBoot()) {
return this.treeForNodeVendor(vendorTree);
if (isLegacyFastboot()) {
return this.legacyTreeForFastBootVendor(vendorTree);
}

return this.treeForBrowserVendor(vendorTree);
},

treeForNodeVendor(vendorTree) {
legacyTreeForFastBootVendor(vendorTree) {
let trees = [];
let options = this.momentOptions;

if (vendorTree) {
trees.push(vendorTree);
}

let fileName;
if (options.includeTimezone) {
// includes all of moment.js
fileName = 'fastboot-moment-timezone.js';
} else {
fileName = 'fastboot-moment.js';
}

let tree = funnel(path.join(__dirname, './assets'), {
files: [fileName]
let tree = funnel(path.join(__dirname, './public'), {
files: [this.fastbootTarget]
});

tree = rename(tree, () => 'fastboot-moment.js');
Expand Down Expand Up @@ -260,6 +248,6 @@ module.exports = {
);
}

return mergeTrees(trees);
return map(mergeTrees(trees), (content) => `if (typeof FastBoot === 'undefined') { ${content} }`);
}
};
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
"broccoli-stew": "^1.4.0",
"chalk": "^1.1.3",
"ember-cli-babel": "^5.1.7",
"ember-cli-import-polyfill": "^0.2.0",
"exists-sync": "^0.0.4",
"lodash.defaults": "^4.2.0",
"moment": "^2.18.1",
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -1832,6 +1832,10 @@ ember-cli-htmlbars@^1.0.0, ember-cli-htmlbars@^1.1.1:
json-stable-stringify "^1.0.0"
strip-bom "^2.0.0"

ember-cli-import-polyfill@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/ember-cli-import-polyfill/-/ember-cli-import-polyfill-0.2.0.tgz#c1a08a8affb45c97b675926272fe78cf4ca166f2"

ember-cli-inject-live-reload@^1.4.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/ember-cli-inject-live-reload/-/ember-cli-inject-live-reload-1.6.1.tgz#82b8f5be454815a75e7f6d42c9ce0bc883a914a3"
Expand Down

0 comments on commit 493b5ab

Please sign in to comment.