Skip to content

Commit

Permalink
Move fastboot specific config to a meta tag; keep manifest in
Browse files Browse the repository at this point in the history
package.json for broccoli-asset-rev support

This commit builds fastboot specific config (available via
FastBoot.config()) to meta tag, which will be extracted by fastboot
when served.

The fastboot config meta has the name ending with
"config/fastboot-environement" to distinguish from regular
"config/environement" meta tags. If the new meta tags are not found,
which happens in embroider builds, we fallback to find regular meta
tags.
  • Loading branch information
xg-wang committed Aug 15, 2021
1 parent 3b74b03 commit f2a4ec1
Show file tree
Hide file tree
Showing 24 changed files with 748 additions and 84,490 deletions.
94 changes: 62 additions & 32 deletions packages/ember-cli-fastboot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const chalk = require('chalk');

const fastbootAppBoot = require('./lib/utilities/fastboot-app-boot');
const FastBootConfig = require('./lib/broccoli/fastboot-config');
const HTMLWriter = require('./lib/broccoli/html-writer');
const BasePageWriter = require('./lib/broccoli/base-page-writer');
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 @@ -61,7 +61,9 @@ module.exports = {
* See: https://ember-cli.com/user-guide/#integration
*/
included(app) {
let assetRev = this.project.addons.find(addon => addon.name === 'broccoli-asset-rev');
let assetRev = this.project.addons.find(
(addon) => addon.name === 'broccoli-asset-rev'
);
if (assetRev && !assetRev.supportsFastboot) {
throw new SilentError(
'This version of ember-cli-fastboot requires a newer version of broccoli-asset-rev'
Expand Down Expand Up @@ -110,7 +112,10 @@ module.exports = {
}

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

// if the fastboot addon is installed, we overwrite the config-module so that the config can be read
Expand All @@ -135,15 +140,19 @@ module.exports = {

// check the ember version and conditionally patch the DOM api
if (this._getEmberVersion().lt('2.10.0-alpha.1')) {
fastbootHtmlBarsTree = this.treeGenerator(path.resolve(__dirname, 'fastboot-app-lt-2-9'));
return tree ? new MergeTrees([tree, fastbootHtmlBarsTree]) : fastbootHtmlBarsTree;
fastbootHtmlBarsTree = this.treeGenerator(
path.resolve(__dirname, 'fastboot-app-lt-2-9')
);
return tree
? new MergeTrees([tree, fastbootHtmlBarsTree])
: fastbootHtmlBarsTree;
}

return tree;
},

_processAddons(addons, fastbootTrees) {
addons.forEach(addon => {
addons.forEach((addon) => {
this._processAddon(addon, fastbootTrees);
});
},
Expand Down Expand Up @@ -183,7 +192,10 @@ module.exports = {
// check the parent containing the fastboot directory
const projectFastbootPath = path.join(this.project.root, 'fastboot');
// ignore the project's fastboot folder if we are an addon, as that is already handled above
if (!this.project.isEmberCLIAddon() && this.existsSync(projectFastbootPath)) {
if (
!this.project.isEmberCLIAddon() &&
this.existsSync(projectFastbootPath)
) {
let fastbootTree = this.treeGenerator(projectFastbootPath);
fastbootTrees.push(fastbootTree);
}
Expand All @@ -196,17 +208,28 @@ module.exports = {
let funneledFastbootTrees = new Funnel(mergedFastBootTree, {
destDir: appName,
});
const processExtraTree = p.preprocessJs(funneledFastbootTrees, '/', this._name, {
registry: this._appRegistry,
});
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));

let newProcessExtraTree = new MergeTrees([processExtraTree, appFactoryModuleTree], {
overwrite: true,
});
let appFactoryModuleTree = writeFile(
'app-factory.js',
fastbootAppFactoryModule(appName)
);

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

function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
Expand All @@ -231,7 +254,8 @@ module.exports = {

let newTree = new MergeTrees(trees);

let fastbootConfigTree = (this._fastbootConfigTree = this._buildFastbootConfigTree(newTree));
let fastbootConfigTree = (this._fastbootConfigTree =
this._buildFastbootConfigTree(newTree));

// Merge the package.json with the existing tree
return new MergeTrees([newTree, fastbootConfigTree], { overwrite: true });
Expand Down Expand Up @@ -296,15 +320,23 @@ module.exports = {

_buildFastbootConfigTree(tree) {
let appConfig = this._getHostAppConfig();
let fastbootAppConfig = appConfig.fastboot;

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

_buildHTMLWriter(tree) {
let appConfig = this._getHostAppConfig();

return new BasePageWriter(tree, {
project: this.project,
appConfig,
appJsPath: this.app.options.outputPaths.app.js,
outputPaths: this.app.options.outputPaths,
});
},

Expand All @@ -314,15 +346,7 @@ module.exports = {
postprocessTree(type, tree) {
this._super(...arguments);
if (type === 'all') {
let { fastbootConfig, appName, manifest } = this._fastbootConfigTree;
let fastbootHTMLTree = new HTMLWriter(tree, {
annotation: 'FastBoot HTML Writer',
fastbootConfig,
appName,
manifest,
appJsPath: this.app.options.outputPaths.app.js,
outputPaths: this.app.options.outputPaths,
});
let fastbootHTMLTree = this._buildHTMLWriter(tree);

// Merge the package.json with the existing tree
return new MergeTrees([tree, fastbootHTMLTree], { overwrite: true });
Expand All @@ -341,8 +365,11 @@ module.exports = {

app.use((req, resp, next) => {
const fastbootQueryParam =
req.query.hasOwnProperty('fastboot') && req.query.fastboot === 'false' ? false : true;
const enableFastBootServe = !process.env.FASTBOOT_DISABLED && fastbootQueryParam;
req.query.hasOwnProperty('fastboot') && req.query.fastboot === 'false'
? false
: true;
const enableFastBootServe =
!process.env.FASTBOOT_DISABLED && fastbootQueryParam;

if (req.serveUrl && enableFastBootServe) {
// if it is a base page request, then have fastboot serve the base page
Expand Down Expand Up @@ -409,7 +436,10 @@ module.exports = {
* TODO Allow add-ons to provide own options and merge them with the application's options.
*/
_fastbootOptionsFor(environment, project) {
const configPath = path.join(path.dirname(project.configPath()), 'fastboot.js');
const configPath = path.join(
path.dirname(project.configPath()),
'fastboot.js'
);

if (fs.existsSync(configPath)) {
return require(configPath)(environment);
Expand Down

0 comments on commit f2a4ec1

Please sign in to comment.