Skip to content

Commit

Permalink
Embroider Support: Moved app factory module out of fastboot-app-… (#714)
Browse files Browse the repository at this point in the history
Embroider Support: Moved app factory module out of fastboot-app-module
  • Loading branch information
rwjblue authored Aug 12, 2019
2 parents 85a2aad + 617a17c commit 2ad9389
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
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 fastbootAppFactoryModule = require('./lib/utilities/fastboot-app-factory-module');
const migrateInitializers = require('./lib/build-utilities/migrate-initializers');
const SilentError = require('silent-error');

Expand Down Expand Up @@ -107,8 +108,8 @@ 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);
const isModuleUnification = this._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 = this._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,28 @@ 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
const writeFile = require('broccoli-file-creator');
let appFactoryModuleTree = writeFile("app-factory.js", fastbootAppFactoryModule(appName, this._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 Expand Up @@ -362,4 +373,8 @@ module.exports = {

return checker.for('ember', 'bower');
},

_isModuleUnification() {
return (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();
}
};
16 changes: 16 additions & 0 deletions lib/utilities/fastboot-app-boot.js
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
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);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"release-it": "^12.0.1",
"release-it-lerna-changelog": "^1.0.2",
"request": "^2.88.0",
"rsvp": "^4.8.3"
"rsvp": "^4.8.3",
"broccoli-file-creator": "^1.1.1"
},
"engines": {
"node": "6.* || 8.* || >= 10.*"
Expand Down

0 comments on commit 2ad9389

Please sign in to comment.