Skip to content

Commit

Permalink
Embroider Support: Moved app factory module out of fastboot-app-module
Browse files Browse the repository at this point in the history
* Cleaned up fastboot-app-module.
* Added Fastboot-app-factory-plugin to insert app factory module as part of <app>-fastboot.js
* This clean up ensures fastboot specific App-factory code is not shipped to browsers.
  • Loading branch information
dnalagatla committed Jul 27, 2019
1 parent 538adf8 commit a0f09f3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
21 changes: 17 additions & 4 deletions index.js
Expand Up @@ -9,8 +9,9 @@ const FastBootExpressMiddleware = require('fastboot-express-middleware');
const FastBoot = require('fastboot');
const chalk = require('chalk');

const fastbootAppModule = require('./lib/utilities/fastboot-app-module');
const fastbootAppBoot = require('./lib/utilities/fastboot-app-boot');
const FastBootConfig = require('./lib/broccoli/fastboot-config');
const FastBootAppFactory = require('./lib/broccoli/fastboot-app-factory-plugin');
const migrateInitializers = require('./lib/build-utilities/migrate-initializers');
const SilentError = require('silent-error');

Expand Down Expand Up @@ -108,7 +109,7 @@ module.exports = {

if (type === 'app-boot') {
const isModuleUnification = (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();
return fastbootAppModule(config.modulePrefix, JSON.stringify(config.APP || {}), isModuleUnification);
return fastbootAppBoot(config.modulePrefix, JSON.stringify(config.APP || {}), isModuleUnification);
}

// if the fastboot addon is installed, we overwrite the config-module so that the config can be read
Expand Down Expand Up @@ -174,10 +175,11 @@ module.exports = {
*/
_getFastbootTree() {
const appName = this._name;
const isModuleUnification = (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();

let fastbootTrees = [];
this._processAddons(this.project.addons, fastbootTrees);

this._processAddons(this.project.addons, fastbootTrees);
// check the parent containing the fastboot directory
const projectFastbootPath = path.join(this.project.root, 'fastboot');
if (this.existsSync(projectFastbootPath)) {
Expand All @@ -189,19 +191,30 @@ module.exports = {
let mergedFastBootTree = new MergeTrees(fastbootTrees, {
overwrite: true
});

let funneledFastbootTrees = new Funnel(mergedFastBootTree, {
destDir: appName
});
const processExtraTree = p.preprocessJs(funneledFastbootTrees, '/', this._name, {
registry: this._appRegistry
});

// FastBoot app factory module
let appFactoryModuleTree = new FastBootAppFactory(processExtraTree, {
appName,
isModuleUnification
});

let newProcessExtraTree = new MergeTrees([processExtraTree, appFactoryModuleTree], {
overwrite: true
});

function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}

let appFilePath = stripLeadingSlash(this.app.options.outputPaths.app.js);
let finalFastbootTree = new Concat(processExtraTree, {
let finalFastbootTree = new Concat(newProcessExtraTree, {
outputFile: appFilePath.replace(/\.js$/, '-fastboot.js')
});

Expand Down
24 changes: 24 additions & 0 deletions lib/broccoli/fastboot-app-factory-plugin.js
@@ -0,0 +1,24 @@
/* eslint-env node */
'use strict';

const path = require('path');
const fs = require('fs');
const Plugin = require('broccoli-plugin');
const fastbootAppFactoryModule = require('../utilities/fastboot-app-factory-module');

module.exports = class FastBootAppFactory extends Plugin {
constructor(inputNode, options) {
super([inputNode], {
annotation: 'Generate: FastBoot app-factory module',
persistentOutput: true
});
this.appName = options.appName;
this.isModuleUnification = options.isModuleUnification;
}

build() {
let outputPath = path.join(this.outputPath, 'app-factory.js');
let content = fastbootAppFactoryModule(this.appName, this.isModuleUnification);
fs.writeFileSync(outputPath, content);
}
};
16 changes: 16 additions & 0 deletions lib/utilities/fastboot-app-boot.js
@@ -0,0 +1,16 @@
'use strict'

// Added as app boot code to app.js that allows booting of the application
// in browser. This code is injected during app-boot type of contentFor hook for ember-cli.
module.exports = function fastbootAppBoot(prefix, configAppAsString, isModuleUnification) {
var appSuffix = isModuleUnification ? "src/main" : "app";
return [
"",
"if (typeof FastBoot === 'undefined') {",
" if (!runningTests) {",
" require('{{MODULE_PREFIX}}/" + appSuffix + "')['default'].create({{CONFIG_APP}});",
" }",
"}",
""
].join("\n").replace(/\{\{MODULE_PREFIX\}\}/g, prefix).replace(/\{\{CONFIG_APP\}\}/g, configAppAsString);
};
Expand Up @@ -7,16 +7,9 @@
// The module defined here is prefixed with a `~` to make it less
// likely to collide with user code, since it is not possible to
// define a module with a name like this in the file system.
module.exports = function fastbootAppModule(prefix, configAppAsString, isModuleUnification) {
module.exports = function fastBootAppFactoryModule(prefix, isModuleUnification) {
var appSuffix = isModuleUnification ? "src/main" : "app";
return [
"",
"if (typeof FastBoot === 'undefined') {",
" if (!runningTests) {",
" require('{{MODULE_PREFIX}}/" + appSuffix + "')['default'].create({{CONFIG_APP}});",
" }",
"}",
"",
"define('~fastboot/app-factory', ['{{MODULE_PREFIX}}/" + appSuffix + "', '{{MODULE_PREFIX}}/config/environment'], function(App, config) {",
" App = App['default'];",
" config = config['default'];",
Expand All @@ -28,5 +21,5 @@ module.exports = function fastbootAppModule(prefix, configAppAsString, isModuleU
" };",
"});",
""
].join("\n").replace(/\{\{MODULE_PREFIX\}\}/g, prefix).replace(/\{\{CONFIG_APP\}\}/g, configAppAsString);
};
].join("\n").replace(/\{\{MODULE_PREFIX\}\}/g, prefix);
}

0 comments on commit a0f09f3

Please sign in to comment.