transform.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var Transform = require('stream').Transform;
  2. var collector = require('./service-collector');
  3. var license = require('./browser-builder').license;
  4. module.exports = function(file) {
  5. var stream = new Transform();
  6. var didDefineServices = !!process.env.AWS_SERVICES;
  7. var isEntryPoint = !!file.match(/[\/\\]lib[\/\\]browser\.js$/);
  8. stream._transform = function(data, encoding, callback) {
  9. callback(null, data);
  10. };
  11. if (isEntryPoint) {
  12. if (didDefineServices) {
  13. // We need to strip out the default requires statement
  14. stream._transform = function(data, encoding, callback) {
  15. var code = data.toString();
  16. code = code.trim();
  17. var lines = code.split('\n');
  18. lines = lines.filter(function(line) {
  19. return !line.match(/^require\(.+browser_default['"]\);$/);
  20. });
  21. code = lines.join('\n');
  22. data = Buffer.from(code);
  23. callback(null, data);
  24. };
  25. var src = collector(process.env.AWS_SERVICES);
  26. stream._flush = function(callback) {
  27. stream.push(src);
  28. callback();
  29. };
  30. }
  31. stream.push(license);
  32. }
  33. return stream;
  34. };