Skip to content

Commit

Permalink
Disentangle from broccoli-asset-rev
Browse files Browse the repository at this point in the history
ember-cli-fastboot has a lot of intimate knowledge of broccoli-asset-rev, and it's forced to always run after broccoli-asset-rev.

This increases complexity and reduces flexibility, resulting in problems like ef4/prember#2, in which we want to use the output of ember-cli-fastboot to render some content HTML which will then need to run through broccoli-asset-rev.

Rewriting assets is broccoli-asset-rev's responsibility, so we can just teach it how to properly rewrite a fastboot-capable ember app. I've done that work [here](https://github.com/ef4/broccoli-asset-rev#838f1b7186fde566d3e8010196c1f974423a4946).

This PR is the corresponding change in ember-cli-fastboot, which mostly just rips out unneeded code.

This is not an upgrade that can be done gracefully, however, since addons cannot dynamically change their `before` and `after` declarations, and my entire motiviation in doing this is to get rid of `after: ['broccoli-asset-rev']`. So this PR creates a hard requirement that the user has a correspondingly new broccoli-asset-rev. I added a clear error message so that it should be only a small stumbling block.

This PR will not pass the test suite, because ember-cli-addon-tests is apparently incapable of being told what dependencies to use. Perhaps it would be best to simply wait to see if we can get the newer broccoli-asset-rev released and upgraded in the default app blueprint before merging this.
  • Loading branch information
ef4 committed Feb 12, 2018
1 parent c3c4942 commit 5fae5bd
Show file tree
Hide file tree
Showing 4 changed files with 687 additions and 1,088 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const chalk = require('chalk');
const fastbootAppModule = require('./lib/utilities/fastboot-app-module');
const FastBootConfig = require('./lib/broccoli/fastboot-config');
const migrateInitializers = require('./lib/build-utilities/migrate-initializers');
const SilentError = require('silent-error');

const Concat = require('broccoli-concat');
const Funnel = require('broccoli-funnel');
Expand Down Expand Up @@ -48,16 +49,16 @@ module.exports = {
* See: https://ember-cli.com/user-guide/#integration
*/
included(app) {

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");
}

// set autoRun to false since we will conditionally include creating app when app files
// is eval'd in app-boot
app.options.autoRun = false;

if (app.options.fingerprint) {
// set generateAssetMap to be true so that manifest files can be correctly written
// in package.json
app.options.fingerprint.generateAssetMap = true;
}

// get the app registry object and app name so that we can build the fastboot
// tree
this._appRegistry = app.registry;
Expand Down Expand Up @@ -283,7 +284,6 @@ module.exports = {
let fastbootAppConfig = appConfig.fastboot;

return new FastBootConfig(tree, {
assetMapPath: this.assetMapPath,
project: this.project,
name: this.app.name,
outputPaths: this.app.options.outputPaths,
Expand Down
48 changes: 2 additions & 46 deletions lib/broccoli/fastboot-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env node */
'use strict';

const fs = require('fs');
Expand All @@ -7,7 +8,6 @@ const merge = require('ember-cli-lodash-subset').merge;
const md5Hex = require('md5-hex');
const path = require('path');
const Plugin = require('broccoli-plugin');
const SilentError = require('silent-error');

const stringify = require('json-stable-stringify');

Expand All @@ -23,7 +23,6 @@ module.exports = class FastBootConfig extends Plugin {
this.project = options.project;

this.name = options.name;
this.assetMapEnabled = options.assetMapEnabled;
this.ui = options.ui;
this.fastbootAppConfig = options.fastbootAppConfig;
this.outputPaths = options.outputPaths;
Expand All @@ -39,22 +38,6 @@ module.exports = class FastBootConfig extends Plugin {
this.htmlFile = 'index.html';
}

let defaultAssetMapPath = 'assets/assetMap.json';
let assetRev = this.project.addons.find(addon => addon.name === 'broccoli-asset-rev');

if (assetRev && assetRev.options) {
this.assetMapEnabled = !!(assetRev.options.enabled && assetRev.options.generateAssetMap);

if (assetRev.options.assetMapPath) {
this.assetMapPath = assetRev.options.assetMapPath;
}

if (assetRev.options.fingerprintAssetMap) {
defaultAssetMapPath = 'assets/assetMap-*.json'
}
}

this.assetMapPath = this.assetMapPath || options.assetMapPath || defaultAssetMapPath;
}


Expand Down Expand Up @@ -145,19 +128,6 @@ module.exports = class FastBootConfig extends Plugin {
this.moduleWhitelist = uniq(moduleWhitelist);
}

readAssetManifest() {
let assetMapPath = path.join(this.inputPaths[0], this.assetMapPath);

try {
let assetMap = JSON.parse(fs.readFileSync(assetMapPath));
return assetMap;
} catch (e) {
if (this.assetMapEnabled) {
throw new SilentError("assetMap.json not found at: %s. Make sure `generateAssetMap` is set to true", assetMapPath);
}
}
}

updateFastBootManifest(manifest) {
this.project.addons.forEach(addon =>{
if (addon.updateFastBootManifest) {
Expand Down Expand Up @@ -187,21 +157,7 @@ module.exports = class FastBootConfig extends Plugin {
htmlFile: this.htmlFile
};

manifest = this.updateFastBootManifest(manifest);

let rewrittenAssets = this.readAssetManifest();

if (rewrittenAssets) {
// update the vendor file with the fingerprinted file
let rewrittenVendorFiles = manifest['vendorFiles'].map(file => rewrittenAssets.assets[file] || file);
manifest['vendorFiles'] = rewrittenVendorFiles;

// update the app files array with fingerprinted files
let rewrittenAppFiles = manifest['appFiles'].map(file => rewrittenAssets.assets[file] || file);
manifest['appFiles'] = rewrittenAppFiles;
}

this.manifest = manifest;
this.manifest = this.updateFastBootManifest(manifest);
}

buildHostWhitelist() {
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@
},
"ember-addon": {
"configPath": "tests/dummy/config",
"after": [
"broccoli-asset-rev"
],
"before": [
"broccoli-serve-files"
]
Expand Down
Loading

0 comments on commit 5fae5bd

Please sign in to comment.