gulp-addModuleExports.js 734 B

12345678910111213141516171819202122232425262728293031
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. module.exports = function (varName) {
  4. return through.obj(function (file, enc, cb) {
  5. var moduleExportsAddition =
  6. '\nif (module && module.exports) {\n' +
  7. ' module.exports = ' + varName + ';\n' +
  8. '};\n';
  9. if (file.isNull()) {
  10. cb(null, file);
  11. return;
  12. }
  13. if (file.isStream()) {
  14. //streams not supported, no need for now.
  15. return;
  16. }
  17. try {
  18. file.contents = new Buffer(String(file.contents).concat(moduleExportsAddition));
  19. this.push(file);
  20. } catch (err) {
  21. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, {fileName: file.path}));
  22. }
  23. cb();
  24. });
  25. };