Skip to content

Commit

Permalink
Fixes #247 - Allow init function in writer plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkelleher committed Oct 31, 2017
1 parent 1a8cb6f commit f2061f9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/writers/Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ var reader = require('../reader');
var ok = require('assert').ok;
var equal = require('assert').equal;

async function initWriter (writer, lassoContext) {
if (!writer._initialized && writer.impl.init) {
await writer.impl.init(lassoContext);
writer._initialized = true;
}
}

function Writer(impl) {
Writer.$super.call(this);
this.impl = impl || {};

// Lasso writer `init` function should only be called once. We call it the
// first time that either writing a resource or bundle is attempted.
this._initialized = false;
}

Writer.prototype = {
Expand Down Expand Up @@ -193,6 +204,7 @@ Writer.prototype = {
const resourceReader = reader.createResourceReader(path, lassoContext);

try {
await initWriter(this, lassoContext);
const writeResult = await this.impl.writeResource(resourceReader, lassoContext);
return done(writeResult);
} catch (err) {
Expand Down Expand Up @@ -245,6 +257,7 @@ Writer.prototype = {
async writeBundles (iteratorFunc, onBundleWrittenCallback, lassoContext) {
ok(lassoContext, 'lassoContext is required');

await initWriter(this, lassoContext);
let promise = Promise.resolve();

iteratorFunc((bundle) => {
Expand Down
8 changes: 8 additions & 0 deletions test/autotests/plugins/write-bundle/browser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": [
{
"type": "js",
"path": "./foo.js"
}
]
}
1 change: 1 addition & 0 deletions test/autotests/plugins/write-bundle/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a = 5;
4 changes: 4 additions & 0 deletions test/autotests/plugins/write-bundle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "autotest",
"version": "0.0.0"
}
21 changes: 21 additions & 0 deletions test/autotests/plugins/write-bundle/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = function(lasso, config) {
lasso.config.writer = {
async init (lassoContext) {
module.exports.counter.push('init');
await Promise.resolve();
},

async writeBundle (reader, lassoContext) {
const bundle = lassoContext.bundle;
bundle.url = 'test.com';
module.exports.counter.push('writeBundle');
},

async writeResource (reader, lassoContext) {
module.exports.counter.push('writeResource');
return { url: 'test.com' };
}
};
};

module.exports.counter = [];
24 changes: 24 additions & 0 deletions test/autotests/plugins/write-bundle/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var expect = require('chai').expect;
const plugin = require('./plugin');

exports.getLassoConfig = function() {
return {
fingerprintsEnabled: false,
bundlingEnabled: true,
plugins: [plugin]
};
};

exports.getLassoOptions = function() {
return {
dependencies: [
'./browser.json'
]
};
};

exports.check = function(lassoPageResult, writerTracker) {
expect(plugin.counter).to.deep.equal(['init', 'writeBundle']);
expect(lassoPageResult.getJavaScriptFiles().length).to.equal(0);
expect(lassoPageResult.getJavaScriptUrls()).to.deep.equal(['test.com']);
};

0 comments on commit f2061f9

Please sign in to comment.