gulp-addModuleExports.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 (window.module && module.exports) {\n' +
  7. ' module.exports = ' + varName + ';\n' +
  8. '};\n';
  9. var extendsAddition =
  10. 'var __extends = (this && this.__extends) || function (d, b) {\n' +
  11. 'for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n' +
  12. 'function __() { this.constructor = d; }\n' +
  13. '__.prototype = b.prototype;\n' +
  14. 'd.prototype = new __();\n' +
  15. '};\n';
  16. if (file.isNull()) {
  17. cb(null, file);
  18. return;
  19. }
  20. if (file.isStream()) {
  21. //streams not supported, no need for now.
  22. return;
  23. }
  24. try {
  25. file.contents = new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportsAddition));
  26. this.push(file);
  27. } catch (err) {
  28. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, {fileName: file.path}));
  29. }
  30. cb();
  31. });
  32. };