diff --git a/index.js b/index.js index 520748c..0ead079 100644 --- a/index.js +++ b/index.js @@ -37,5 +37,10 @@ module.exports = { this.app = app; this.initializeOptions(); }, - treeFor: function() {} -} + treeFor: function() {}, + + // ember-cli-fastboot uses the presence of this flag to give a + // helpful error if you're using an older version of this addon that + // doesn't know how to rewrite the fastboot manifest. + supportsFastboot: true +}; diff --git a/lib/asset-rev.js b/lib/asset-rev.js index b2ab203..24135e2 100644 --- a/lib/asset-rev.js +++ b/lib/asset-rev.js @@ -1,6 +1,7 @@ var defaults = require('./default-options'); var Fingerprint = require('./fingerprint'); var UseRev = require('broccoli-asset-rewrite'); +var FastbootManifestRewrite = require('./fastboot-manifest-rewrite'); function AssetRev(inputTree, options) { if (!(this instanceof AssetRev)) { @@ -37,7 +38,7 @@ function AssetRev(inputTree, options) { var fingerprintTree2 = Fingerprint(assetRewrite, this); var assetRewrite2 = UseRev(fingerprintTree2, this); - return assetRewrite2; + return new FastbootManifestRewrite(assetRewrite2, this.assetMap); } module.exports = AssetRev; diff --git a/lib/fastboot-manifest-rewrite.js b/lib/fastboot-manifest-rewrite.js new file mode 100644 index 0000000..16dc1f8 --- /dev/null +++ b/lib/fastboot-manifest-rewrite.js @@ -0,0 +1,42 @@ +/* jshint node: true */ +"use strict"; + +var Filter = require('broccoli-persistent-filter'); +var fs = require('fs'); + +module.exports = class FastbootManifestRewrite extends Filter { + constructor(inputNode, assetMap) { + super(inputNode, { annotation: 'broccoli-asset-rev/fastboot-manifest-rewrite'}); + this.assetMap = assetMap; + this.extensions = ['json']; + } + + getDestFilePath(relativePath) { + if (relativePath === 'package.json') { + return relativePath; + } + } + + processString(contents, relativePath) { + var pkg = JSON.parse(contents); + if (pkg.fastboot && pkg.fastboot.manifest) { + var manifest = pkg.fastboot.manifest; + this._process(manifest, 'appFiles', true); + this._process(manifest, 'vendorFiles', true); + this._process(manifest, 'htmlFile', false); + return JSON.stringify(pkg); + } + return contents; + } + + _process(manifest, key, asArray) { + if (manifest[key]) { + if (asArray) { + var assetMap = this.assetMap; + manifest[key] = manifest[key].map(function(filename){ return assetMap[filename] || filename; }); + } else { + manifest[key] = this.assetMap[manifest[key]] || manifest[key]; + } + } + } +}; diff --git a/package.json b/package.json index bd3aba0..75cad4f 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "dependencies": { "broccoli-asset-rewrite": "^1.1.0", "broccoli-filter": "^1.2.2", + "broccoli-persistent-filter": "^1.4.3", "json-stable-stringify": "^1.0.0", "minimatch": "^3.0.4", "rsvp": "^3.0.6" diff --git a/tests/filter-tests.js b/tests/filter-tests.js index 79dd442..4fa5571 100644 --- a/tests/filter-tests.js +++ b/tests/filter-tests.js @@ -416,4 +416,19 @@ describe('broccoli-asset-rev', function() { fs.statSync.restore() }); }); + + it('rewrites the fastboot manifest', function(){ + var sourcePath = 'tests/fixtures/fastboot'; + + var node = AssetRev(sourcePath + '/input', { + extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map'], + replaceExtensions: ['html', 'js', 'css'] + }); + + builder = new broccoli.Builder(node); + return builder.build().then(function(graph) { + confirmOutput(graph.directory, sourcePath + '/output'); + }); + }); + }); diff --git a/tests/fixtures/fastboot/input/assets/application.js b/tests/fixtures/fastboot/input/assets/application.js new file mode 100644 index 0000000..0ba8818 --- /dev/null +++ b/tests/fixtures/fastboot/input/assets/application.js @@ -0,0 +1,5 @@ +const translations = { + en: 'translations/en.json' +}; + +console.log('Hello World'); diff --git a/tests/fixtures/fastboot/input/images/icons/favicon.png b/tests/fixtures/fastboot/input/images/icons/favicon.png new file mode 100644 index 0000000..3db835b Binary files /dev/null and b/tests/fixtures/fastboot/input/images/icons/favicon.png differ diff --git a/tests/fixtures/fastboot/input/images/sample.png b/tests/fixtures/fastboot/input/images/sample.png new file mode 100644 index 0000000..cdfaf5e Binary files /dev/null and b/tests/fixtures/fastboot/input/images/sample.png differ diff --git a/tests/fixtures/fastboot/input/index.html b/tests/fixtures/fastboot/input/index.html new file mode 100644 index 0000000..8713e94 --- /dev/null +++ b/tests/fixtures/fastboot/input/index.html @@ -0,0 +1,15 @@ + + +
+ + +