Skip to content

Instantly share code, notes, and snippets.

@tadatuta
Created December 15, 2015 19:45
Show Gist options
  • Save tadatuta/8532c874acb499391948 to your computer and use it in GitHub Desktop.
Save tadatuta/8532c874acb499391948 to your computer and use it in GitHub Desktop.
ENB version of webpack define plugin
var vow = require('vow'),
enb = require('enb'),
buildFlow = enb.buildFlow || require('enb/lib/build-flow'),
vfs = enb.asyncFS || require('enb/lib/fs/async-fs'),
utils = require('enb-source-map/lib/utils'),
File = require('enb-source-map/lib/file');
module.exports = buildFlow.create()
.name('define')
.target('target', '?.js')
.useFileList(['vanilla.js', 'js', 'browser.js'])
.defineOption('variables', {})
.defineOption('sourcemap', false)
.builder(function (sourceFiles) {
return this._readSourceFiles(sourceFiles)
.then(function(sources) {
var file = new File(this.node.resolvePath(this._target), { sourceMap: this._sourcemap }),
variables = this._variables;
sources.forEach(function(source) {
var contents = source.contents.replace(/%%%(.*?)%%%/g, function(match, varName) {
if (typeof variables[varName] === 'undefined') throw new Error('There is no value for', varName, 'placeholder');
return variables[varName];
});
file.writeFileContent(source.path, contents);
});
console.log('this._sourcemap', this._sourcemap);
return this._sourcemap ?
utils.joinContentAndSourceMap(file.getContent(), file.getSourceMap()) :
file.render();
}, this);
})
.methods({
/**
* Reads source js files.
*
* @protected
* @param {FileList} files
* @returns {FileData[]}
*/
_readSourceFiles: function (files) {
var node = this.node;
return vow.all(files.map(function (file) {
return vfs.read(file.fullname, 'utf8')
.then(function (contents) {
return {
path: file.fullname,
relPath: node.relativePath(file.fullname),
contents: contents
};
});
}));
}
})
.createTech();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment