Skip to content

Commit

Permalink
Merge pull request #28 from trentmwillis/inserter-refactor
Browse files Browse the repository at this point in the history
Refactor meta replacement to allow for customizability
  • Loading branch information
trentmwillis authored Oct 25, 2016
2 parents 89c7492 + 2cdc29f commit e00e969
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
15 changes: 12 additions & 3 deletions lib/asset-manifest-inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var BroccoliCachingWriter = require('broccoli-caching-writer');
var path = require('path');
var fs = require('fs-extra');
var existsSync = require('exists-sync');
var metaReplacer = require('./meta-handler').replacer;
var meta = require('./meta-handler');

/**
* A Broccoli Plugin to modify index.html to include the asset manifest.
Expand All @@ -22,6 +22,13 @@ function AssetManifestInserter(inputNodes, options) {
options.indexName = options.indexName || 'index.html';
options.cacheInclude = [options.indexName, 'tests/index.html', 'asset-manifest.json'];

// Defines a transform for the contents that will be placed in the meta tag.
this.transformer = options.transformer || meta.transformer;

// Defines how to insert the meta information into the files.
// Currently not customizable.
this.replacer = meta.replacer;

BroccoliCachingWriter.call(this, inputNodes, {
annotation: options.annotation
});
Expand All @@ -43,15 +50,17 @@ AssetManifestInserter.prototype.build = function() {
manifest = { bundles: {} };
}

var transformedManifest = this.transformer(manifest);

if (existsSync(indexFilePath)) {
var indexFile = fs.readFileSync(indexFilePath, { encoding: 'utf8' });
var index = metaReplacer(indexFile, manifest);
var index = this.replacer(indexFile, transformedManifest);
fs.writeFileSync(path.join(this.outputPath, this.options.indexName), index);
}

if (existsSync(testIndexFilePath)) {
var testIndexFile = fs.readFileSync(testIndexFilePath, { encoding: 'utf8' });
var testIndex = metaReplacer(testIndexFile, manifest);
var testIndex = this.replacer(testIndexFile, transformedManifest);
fs.mkdirSync(path.join(this.outputPath, 'tests'));
fs.writeFileSync(path.join(this.outputPath, 'tests', 'index.html'), testIndex);
}
Expand Down
10 changes: 6 additions & 4 deletions lib/meta-handler.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
/**
* Replace the manifest meta tag with an updated version of the manifest.
* We do this in both the app's and test's index.html file.
*
* @private
*/
var regex = /<meta name="([^"]*\/config\/asset-manifest)" content=[^>]*>/;

function escaper(sourceJSON) {
function transformer(sourceJSON) {
return escape(JSON.stringify(sourceJSON));
}

function replacer(fileContents, manifest) {
function replacer(fileContents, metaContents) {
if (!regex.test(fileContents)) {
return fileContents;
}

var metaName = regex.exec(fileContents)[1];
var metaString = '<meta name="' + metaName + '" content="' + escaper(manifest) + '" />';
var metaString = '<meta name="' + metaName + '" content="' + metaContents + '" />';

return fileContents.replace(regex, metaString);
}

module.exports = { escaper, replacer };
module.exports = { transformer, replacer };
19 changes: 18 additions & 1 deletion node-tests/asset-manifest-inserter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,29 @@ describe('asset-manifest-inserter', function() {
var testIndex = fs.readFileSync(testIndexFilePath, { encoding: 'utf8' });
var assetManifest = fs.readJsonSync(manifestFilePath);

var needle = metaHandler.escaper(assetManifest);
var needle = metaHandler.transformer(assetManifest);

assert.notEqual(index.indexOf(needle), -1);
assert.notEqual(testIndex.indexOf(needle), -1);
})
);

it('uses a custom transformer to modify the manifest', build(function(results) {
var output = results.directory;
var indexFilePath = path.join(output, 'index.html');
var testIndexFilePath = path.join(output, 'tests', 'index.html');
var manifestFilePath = path.join(inputTrees[0], 'asset-manifest.json')

var index = fs.readFileSync(indexFilePath, { encoding: 'utf8' });
var testIndex = fs.readFileSync(testIndexFilePath, { encoding: 'utf8' });
var assetManifest = fs.readJsonSync(manifestFilePath);

var needle = 'herp-de-derp';

assert.notEqual(index.indexOf(needle), -1);
assert.notEqual(testIndex.indexOf(needle), -1);
}, { transformer: function() { return 'herp-de-derp'; } })
);

});
});
2 changes: 1 addition & 1 deletion node-tests/manifest-generator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('manifest-generator', function() {

assert.deepEqual(manifest, expectedManifest, 'generated manifest equals the expected manifest');

var escapedManifest = metaHandler.escaper(manifest);
var escapedManifest = metaHandler.transformer(manifest);
var indexFile = fs.readFileSync(path.join(output, indexPath));

assert.notEqual(indexFile.indexOf(escapedManifest), -1, 'index contains the escaped manifest');
Expand Down
8 changes: 4 additions & 4 deletions node-tests/meta-handler-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var assert = require('assert');
var metaHandler = require('../lib/meta-handler');

function testStringGenerator(sourceJSON) {
return '<meta name="testing/config/asset-manifest" content="'+ metaHandler.escaper(sourceJSON) +'" />';
return '<meta name="testing/config/asset-manifest" content="'+ metaHandler.transformer(sourceJSON) +'" />';
}

describe('meta-handler', function() {
Expand All @@ -18,7 +18,7 @@ describe('meta-handler', function() {
var before = testStringGenerator({ foo: '>' });
var after = testStringGenerator(replacement);

var result = metaHandler.replacer(before, replacement);
var result = metaHandler.replacer(before, metaHandler.transformer(replacement));
assert.equal(result, after);
});

Expand All @@ -35,7 +35,7 @@ describe('meta-handler', function() {
before[1]
];

var result = metaHandler.replacer(before.join(''), replacement);
var result = metaHandler.replacer(before.join(''), metaHandler.transformer(replacement));
assert.equal(result, after.join(''));
});

Expand All @@ -54,7 +54,7 @@ describe('meta-handler', function() {
before[2]
];

var result = metaHandler.replacer(before.join('\r\n'), replacement);
var result = metaHandler.replacer(before.join('\r\n'), metaHandler.transformer(replacement));
assert.equal(result, after.join('\r\n'));
});
});
Expand Down

0 comments on commit e00e969

Please sign in to comment.