Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ export default {
name: "clear-double-boot",

initialize: function(instance) {
var originalDidCreateRootView = instance.didCreateRootView;
if (typeof FastBoot === 'undefined') {
var originalDidCreateRootView = instance.didCreateRootView;

instance.didCreateRootView = function() {
let elements = document.querySelectorAll(instance.rootElement + ' .ember-view');
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
element.parentNode.removeChild(element);
}
instance.didCreateRootView = function() {
let elements = document.querySelectorAll(instance.rootElement + ' .ember-view');
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
element.parentNode.removeChild(element);
}

originalDidCreateRootView.apply(instance, arguments);
};
originalDidCreateRootView.apply(instance, arguments);
};
}
}
}
177 changes: 116 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,112 @@
'use strict';

var path = require('path');
var fs = require('fs-extra');
var existsSync = require('exists-sync');

var EventEmitter = require('events').EventEmitter;
var mergeTrees = require('broccoli-merge-trees');
var VersionChecker = require('ember-cli-version-checker');

var patchEmberApp = require('./lib/ext/patch-ember-app');
var fastbootAppModule = require('./lib/utilities/fastboot-app-module');

var filterInitializers = require('fastboot-filter-initializers');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dependency needed still?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this was the dirty ember-app private patching that we was causing issues and perf problems.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually referring to fastboot-filter-initializers :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah no i need to remove that.

var FastBootBuild = require('./lib/broccoli/fastboot-build');
var FastBootConfig = require('./lib/broccoli/fastboot-config');

var FASTBOOT_DIR = 'fastboot/app';

/*
* Main entrypoint for the Ember CLI addon.
*/

module.exports = {
name: 'ember-cli-fastboot',

init() {
this._super.init && this._super.init.apply(this, arguments);

this.emitter = new EventEmitter();
},

includedCommands: function() {
return {
'fastboot': require('./lib/commands/fastboot')(this),

/* fastboot:build is deprecated and will be removed in a future version */
'fastboot:build': require('./lib/commands/fastboot-build')
};
},

// TODO remove this after serve issues are fixed correctly
init() {
this._super.init && this._super.init.apply(this, arguments);

this.emitter = new EventEmitter();
},

// TODO remove this after serve issues are fixed correctly
on: function() {
this.emitter.on.apply(this.emitter, arguments);
},

// TODO remove this after serve issues are fixed correctly
emit: function() {
this.emitter.emit.apply(this.emitter, arguments);
},

/**
* Called at the start of the build process to let the addon know it will be
* used. At this point, we can rely on the EMBER_CLI_FASTBOOT environment
* variable being set.
*
* Once we've determined which mode we're in (browser build or FastBoot build),
* we mixin additional Ember addon hooks appropriate to the current build target.
*/
// TODO remove this after serve issues are fixed correctly
postBuild: function() {
this.emit('postBuild');
},

included: function(app) {
patchEmberApp(app);
// set autoRun to false since we will conditionally include creating app when app files
// is eval'd in app-boot
app.options.autoRun = false;
// move fastboot initializers out of app namespace
// TODO remove this before Fastboot 1.0 GAs
this.moveFastbootInitializers();
},

config: function() {
if (this.app && this.app.options.__is_building_fastboot__) {
return { APP: { autoboot: false } };
}
// Function to move fastboot specific initializers and instance initializers from app/ to fastboot/app
// Should we move fastboot/[initializers-*]/browser too? It may need a code change of no-op the initializer in fastboot environment
// should we just throw a deprecation warning for fastboot/[initializers-*]/browser files instead?
moveFastbootInitializers: function() {
var deprecate = this.project.ui.writeDeprecateLine.bind(this.project.ui);
var nodeModulesPath = this.project.nodeModulesPath;
var fastbootInitializerTypes = [ 'initializers', 'instance-initializers'];
this.project.addons.forEach(function(addon) {
var currentAddonPath = path.join(nodeModulesPath, addon.name);

// check to see if it is a fastboot complaint addon
var isFastbootAddon = fastbootInitializerTypes.some(function(fastbootInitializerType) {
var fastbootPath = path.join(currentAddonPath, 'app', fastbootInitializerType, 'fastboot');

return existsSync(fastbootPath);
});

if (isFastbootAddon) {
deprecate('Having fastboot specific code in app directory of ' + addon.name + ' is deprecated. Please move it to fastboot/app directory.');
var fastbootDirPath = path.join(currentAddonPath, FASTBOOT_DIR);
// check if fastboot/app exists
if(!existsSync(fastbootDirPath)) {
fs.mkdirsSync(fastbootDirPath);
}

// copy over app/initializers/fastboot and app/instance/initializers/fastboot
fastbootInitializerTypes.forEach(function(fastbootInitializerType) {
var srcFastbootPath = path.join(currentAddonPath, 'app', fastbootInitializerType, 'fastboot');

if (existsSync(srcFastbootPath)) {
var destFastbootPath = path.join(fastbootDirPath, fastbootInitializerType);
if (!existsSync(destFastbootPath)) {
fs.mkdirSync(destFastbootPath);
}

// fastboot initializer type exists so we need to move this fastboot/app
var fastbootFiles = fs.readdirSync(srcFastbootPath);
fastbootFiles.forEach(function(fastbootFile) {
var srcPath = path.join(srcFastbootPath, fastbootFile);
var destPath = path.join(destFastbootPath, fastbootFile);
fs.copySync(srcPath, destPath);

// delete the original path files so that there are no two initializers with the same name
fs.unlinkSync(srcPath);
});
};
});
}
});
},

/**
Expand All @@ -76,68 +125,74 @@ module.exports = {
}

if (type === 'app-boot') {
return fastbootAppModule(config.modulePrefix);
return fastbootAppModule(config.modulePrefix, JSON.stringify(config.APP || {}));
}

if (type === 'config-module' && this.app.options.__is_building_fastboot__) {
var linesToRemove = contents.length;
while(linesToRemove) {
// Clear out the default config from ember-cli
contents.pop();
linesToRemove--;
}

return 'return FastBoot.config();';
// if the fastboot addon is installed, we overwrite the config-module so that the config can be read
// from meta tag for browser build and from Fastboot config for fastboot target
if (type === 'config-module') {
var emberCliPath = path.join(this.app.project.nodeModulesPath, 'ember-cli');
contents.splice(0, contents.length);
contents.push('if (typeof FastBoot !== \'undefined\') {');
contents.push('return FastBoot.config();');
contents.push('} else {');
contents.push('var prefix = \'' + config.modulePrefix + '\';');
contents.push(fs.readFileSync(path.join(emberCliPath, 'lib/broccoli/app-config-from-meta.js')));
contents.push('}');
return;
}
},

treeForApp: function(defaultTree) {
var trees = [defaultTree];
// todo replace this with tree for fastboot app
treeForFastbootApp: function(tree) {
var trees = [];
if (tree) {
trees = [tree];
}

if (this._getEmberVersion().lt('2.10.0-alpha.1')) {
trees.push(this.treeGenerator(path.resolve(this.root, 'app-lt-2-9')));
}

return mergeTrees(trees, { overwrite: true });
},

/**
* Filters out initializers and instance initializers that should only run in
* browser mode.
*/
preconcatTree: function(tree) {
return filterInitializers(tree, this.app.name);
return mergeTrees(trees.filter(Boolean), { overwrite: true });
},

/**
* After the entire Broccoli tree has been built for the `dist` directory,
* adds the `fastboot-config.json` file to the root.
* adds the `package.json` file to the root.
*/
postprocessTree: function(type, tree) {
if (type === 'all') {
var fastbootTree = this.buildFastBootTree();
var fastbootConfigTree = this.buildFastbootConfigTree(tree);

// Merge the package.json with the existing tree
return mergeTrees([tree, fastbootTree], {overwrite: true});
return mergeTrees([tree, fastbootConfigTree], {overwrite: true});
}

return tree;
},

buildFastBootTree: function() {
var fastbootBuild = new FastBootBuild({
ui: this.ui,
buildFastbootConfigTree : function(tree) {
var env = this.app.env;
var config = this.project.config(env);
var fastbootConfig = config.fastboot;
if (config.hasOwnProperty('APP')) {
config['APP']['autoboot'] = false;
} else {
config['APP'] = {
'autoboot': false
};
}

return new FastBootConfig(tree, {
assetMapPath: this.assetMapPath,
project: this.project,
app: this.app,
parent: this.parent
name: this.app.name,
outputPaths: this.app.options.outputPaths,
ui: this.ui,
fastbootAppConfig: fastbootConfig,
appConfig: config
});

return fastbootBuild.toTree();
},

postBuild: function() {
this.emit('postBuild');
},

_getEmberVersion: function() {
Expand All @@ -150,6 +205,6 @@ module.exports = {
}

return checker.for('ember-source', 'npm');
},
}

};
Loading