Skip to content

Commit

Permalink
Increment schema to latest 5
Browse files Browse the repository at this point in the history
html-oriented manifest format
-----------------------------

In the new schema format, which is defined in
ember-fastboot/fastboot@3fd5bc9
the manifest is written into HTML and later extracted by fastboot on
server side instead of previously reading from dist/package.json

Note:
The new schema in fastboot does not handle fastboot config
https://github.com/ember-fastboot/ember-cli-fastboot/tree/e4d0b7c7bcdf82def0dc8726835b49d707673f41#providing-additional-config
The support is added in the #854.

Allow to require module path from whitelisted dependency
-------------------------------------------------------

Incrementing schema to 5 also included the changes in schema 4
strictWhitelist
See ember-fastboot/fastboot#200

Revert back to put config in dist/package.json

add data-fastboot-ignore to unexpected files

properly ignore files that should not execute in fastboot
  • Loading branch information
xg-wang committed Aug 22, 2021
1 parent dd38417 commit 92bfac9
Show file tree
Hide file tree
Showing 23 changed files with 744 additions and 84,341 deletions.
99 changes: 77 additions & 22 deletions packages/ember-cli-fastboot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const chalk = require('chalk');

const fastbootAppBoot = require('./lib/utilities/fastboot-app-boot');
const FastBootConfig = require('./lib/broccoli/fastboot-config');
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 @@ -60,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 @@ -109,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 @@ -134,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 @@ -182,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 @@ -195,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 @@ -219,6 +243,7 @@ module.exports = {
return finalFastbootTree;
},

// Note: this hook is ignored when built with embroider
treeForPublic(tree) {
let fastbootTree = this._getFastbootTree();
let trees = [];
Expand All @@ -229,7 +254,8 @@ module.exports = {

let newTree = new MergeTrees(trees);

let 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 @@ -294,18 +320,41 @@ 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,
});
},

/**
* Write fastboot-script tags to the html file
*/
postprocessTree(type, tree) {
this._super(...arguments);
if (type === 'all') {
let fastbootHTMLTree = this._buildHTMLWriter(tree);

// Merge the package.json with the existing tree
return new MergeTrees([tree, fastbootHTMLTree], { overwrite: true });
}

return tree;
},

serverMiddleware(options) {
let emberCliVersion = this._getEmberCliVersion();
let app = options.app;
Expand All @@ -316,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 @@ -384,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 92bfac9

Please sign in to comment.