Skip to content

Commit f2061f9

Browse files
Fixes #247 - Allow init function in writer plugins.
1 parent 1a8cb6f commit f2061f9

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

src/writers/Writer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ var reader = require('../reader');
1010
var ok = require('assert').ok;
1111
var equal = require('assert').equal;
1212

13+
async function initWriter (writer, lassoContext) {
14+
if (!writer._initialized && writer.impl.init) {
15+
await writer.impl.init(lassoContext);
16+
writer._initialized = true;
17+
}
18+
}
19+
1320
function Writer(impl) {
1421
Writer.$super.call(this);
1522
this.impl = impl || {};
23+
24+
// Lasso writer `init` function should only be called once. We call it the
25+
// first time that either writing a resource or bundle is attempted.
26+
this._initialized = false;
1627
}
1728

1829
Writer.prototype = {
@@ -193,6 +204,7 @@ Writer.prototype = {
193204
const resourceReader = reader.createResourceReader(path, lassoContext);
194205

195206
try {
207+
await initWriter(this, lassoContext);
196208
const writeResult = await this.impl.writeResource(resourceReader, lassoContext);
197209
return done(writeResult);
198210
} catch (err) {
@@ -245,6 +257,7 @@ Writer.prototype = {
245257
async writeBundles (iteratorFunc, onBundleWrittenCallback, lassoContext) {
246258
ok(lassoContext, 'lassoContext is required');
247259

260+
await initWriter(this, lassoContext);
248261
let promise = Promise.resolve();
249262

250263
iteratorFunc((bundle) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": [
3+
{
4+
"type": "js",
5+
"path": "./foo.js"
6+
}
7+
]
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var a = 5;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "autotest",
3+
"version": "0.0.0"
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = function(lasso, config) {
2+
lasso.config.writer = {
3+
async init (lassoContext) {
4+
module.exports.counter.push('init');
5+
await Promise.resolve();
6+
},
7+
8+
async writeBundle (reader, lassoContext) {
9+
const bundle = lassoContext.bundle;
10+
bundle.url = 'test.com';
11+
module.exports.counter.push('writeBundle');
12+
},
13+
14+
async writeResource (reader, lassoContext) {
15+
module.exports.counter.push('writeResource');
16+
return { url: 'test.com' };
17+
}
18+
};
19+
};
20+
21+
module.exports.counter = [];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var expect = require('chai').expect;
2+
const plugin = require('./plugin');
3+
4+
exports.getLassoConfig = function() {
5+
return {
6+
fingerprintsEnabled: false,
7+
bundlingEnabled: true,
8+
plugins: [plugin]
9+
};
10+
};
11+
12+
exports.getLassoOptions = function() {
13+
return {
14+
dependencies: [
15+
'./browser.json'
16+
]
17+
};
18+
};
19+
20+
exports.check = function(lassoPageResult, writerTracker) {
21+
expect(plugin.counter).to.deep.equal(['init', 'writeBundle']);
22+
expect(lassoPageResult.getJavaScriptFiles().length).to.equal(0);
23+
expect(lassoPageResult.getJavaScriptUrls()).to.deep.equal(['test.com']);
24+
};

0 commit comments

Comments
 (0)