From c2f3c9ce68d00f66369e6a34fb4bbe51cf5052a5 Mon Sep 17 00:00:00 2001 From: Dinesh Nalagatla Date: Sat, 27 Jul 2019 16:47:38 -0700 Subject: [PATCH] Embroider Support: Moved app factory module out of fastboot-app-module * Cleaned up fastboot-app-module. * Added Fastboot-app-factory-plugin to insert app factory module as part of -fastboot.js * This clean up ensures fastboot specific App-factory code is not shipped to browsers. --- index.js | 26 +++++++++++++++---- lib/broccoli/fastboot-app-factory-plugin.js | 24 +++++++++++++++++ lib/utilities/fastboot-app-boot.js | 16 ++++++++++++ ...dule.js => fastboot-app-factory-module.js} | 13 +++------- 4 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 lib/broccoli/fastboot-app-factory-plugin.js create mode 100644 lib/utilities/fastboot-app-boot.js rename lib/utilities/{fastboot-app-module.js => fastboot-app-factory-module.js} (67%) diff --git a/index.js b/index.js index 294cbb4a8..0fe352f5e 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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 @@ -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)) { @@ -189,6 +191,7 @@ module.exports = { let mergedFastBootTree = new MergeTrees(fastbootTrees, { overwrite: true }); + let funneledFastbootTrees = new Funnel(mergedFastBootTree, { destDir: appName }); @@ -196,12 +199,22 @@ module.exports = { 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') }); @@ -361,4 +374,7 @@ module.exports = { return checker.for('ember', 'bower'); }, + _isModuleUnification() { + return (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification(); + } }; diff --git a/lib/broccoli/fastboot-app-factory-plugin.js b/lib/broccoli/fastboot-app-factory-plugin.js new file mode 100644 index 000000000..1a58e85e2 --- /dev/null +++ b/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); + } +}; \ No newline at end of file diff --git a/lib/utilities/fastboot-app-boot.js b/lib/utilities/fastboot-app-boot.js new file mode 100644 index 000000000..1b9c85cea --- /dev/null +++ b/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); +}; diff --git a/lib/utilities/fastboot-app-module.js b/lib/utilities/fastboot-app-factory-module.js similarity index 67% rename from lib/utilities/fastboot-app-module.js rename to lib/utilities/fastboot-app-factory-module.js index a7bbad373..15c5ed9b2 100644 --- a/lib/utilities/fastboot-app-module.js +++ b/lib/utilities/fastboot-app-factory-module.js @@ -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'];", @@ -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); +} \ No newline at end of file