Skip to content

Instantly share code, notes, and snippets.

@StarpTech
Last active February 14, 2017 12:24
Show Gist options
  • Save StarpTech/e77e495b0c21a64f811a9c90f004001a to your computer and use it in GitHub Desktop.
Save StarpTech/e77e495b0c21a64f811a9c90f004001a to your computer and use it in GitHub Desktop.
How to create a bundle writer in LassoJs
// ************************************
// ***** bundle-writer-example.js *****
//*************************************
var myWriter = require('./bundle-writer');
module.exports = function (myLasso, pluginConfig) {
myLasso.config.writer = myWriter(pluginConfig, myLasso.config);
};
// ****************************
// ***** bundle-writer.js *****
//*****************************
function Bundle_getUrl(lassoContext) {
var url = this.url;
if (url) {
return url;
}
var outputFile = this.outputFile;
var urlPrefix = this.urlPrefix;
var outputDir = this.outputDir;
return urlPrefix + outputDir + outputFile;
}
module.exports = function cdnWriter(cdnWriterConfig, lassoConfig) {
// Optional URL prefix to use when generating URLs to the bundled files
var urlPrefix = cdnWriterConfig.urlPrefix;
return {
checkBundleUpToDate: function (bundle, lassoContext, callback) {
// Bundle is not up-to-date and needs to be written
callback(null, false);
},
checkResourceUpToDate: function (path, lassoContext, callback) {
// Resource is not up-to-date and needs to be written
callback(null, false);
},
/**
* This will be called for JS and CSS bundles
*/
writeBundle: function (reader, lassoContext, callback) {
var input = reader.readBundle();
var bundle = lassoContext.bundle;
var contentType = bundle.contentType; // Either 'css' or 'js'
bundle.setWritten(true);
bundle.getUrl = Bundle_getUrl;
bundle.urlPrefix = 'aaa';
bundle.outputDir = '/frfr';
bundle.outputFile = 'fooo.' + contentType;
callback();
},
/**
* This will be called for front-end assets such as images, fonts, etc.
*/
writeResource: function (reader, lassoContext, callback) {
var path = lassoContext.path;
var readableResourceStream = reader.readResource();
callback(null, {
url: '/foobar.png'
})
}
}
}
//**************
// *** Usage ***
//**************
plugins:[{
plugin: require('./bundle-writer-example'),
config: { a: 1 },
enabled: true
}]
@StarpTech
Copy link
Author

Open issues lasso-js/lasso#90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment