Skip to content

Instantly share code, notes, and snippets.

@MattSurabian
Created May 5, 2016 14:34
Show Gist options
  • Save MattSurabian/dd3d013f786f2ff8c6c7a64e0824cf69 to your computer and use it in GitHub Desktop.
Save MattSurabian/dd3d013f786f2ff8c6c7a64e0824cf69 to your computer and use it in GitHub Desktop.
easy to generate webpack module with custom object body
'use strict';
var NormalModule = require('webpack/lib/NormalModule');
function JsonModule(request, parser, body) {
NormalModule.call(this, request.request, request.request, request.request, [], request.request, parser);
this.body = body;
return this;
}
JsonModule.prototype = Object.create(NormalModule.prototype);
JsonModule.prototype.build = function(options, compilation, resolver, fs, callback) {
var _this = this;
NormalModule.prototype.build.call(this, options, compilation, resolver, {
readFile: function(filepath, callback) {
var buffy = new Buffer(`module.exports = { ${_this.body} };`);
callback(null, buffy);
}
}, callback);
};
JsonModule.prototype.constructor = JsonModule;
module.exports = JsonModule;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment