build.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var fs = require('fs');
  3. var rimraf = require('rimraf');
  4. var acorn = require('acorn');
  5. var walk = require('acorn/dist/walk');
  6. var crypto = require('crypto');
  7. var shasum = crypto.createHash('sha512');
  8. fs.readdirSync(__dirname + '/src').sort().forEach(function (filename) {
  9. shasum.update(fs.readFileSync(__dirname + '/src/' + filename, 'utf8'));
  10. });
  11. const names = {};
  12. const characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  13. let i = characterSet.indexOf(shasum.digest('base64').replace(/[^0-9a-zA-Z]/g, '')[0]);
  14. function getIdFor(name) {
  15. if (names[name]) return names[name];
  16. return names[name] = '_' + characterSet[i++ % characterSet.length]
  17. }
  18. function fixup(src) {
  19. var ast = acorn.parse(src);
  20. src = src.split('');
  21. walk.simple(ast, {
  22. MemberExpression: function (node) {
  23. if (node.computed) return;
  24. if (node.property.type !== 'Identifier') return;
  25. if (node.property.name[0] !== '_') return;
  26. replace(node.property, getIdFor(node.property.name));
  27. }
  28. });
  29. function replace(node, str) {
  30. for (var i = node.start; i < node.end; i++) {
  31. src[i] = '';
  32. }
  33. src[node.start] = str;
  34. }
  35. return src.join('');
  36. }
  37. rimraf.sync(__dirname + '/lib/');
  38. fs.mkdirSync(__dirname + '/lib/');
  39. fs.readdirSync(__dirname + '/src').forEach(function (filename) {
  40. var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
  41. var out = fixup(src);
  42. fs.writeFileSync(__dirname + '/lib/' + filename, out);
  43. });
  44. rimraf.sync(__dirname + '/domains/');
  45. fs.mkdirSync(__dirname + '/domains/');
  46. fs.readdirSync(__dirname + '/src').forEach(function (filename) {
  47. var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
  48. var out = fixup(src);
  49. out = out.replace(/require\(\'asap\/raw\'\)/g, "require('asap')");
  50. fs.writeFileSync(__dirname + '/domains/' + filename, out);
  51. });
  52. rimraf.sync(__dirname + '/setimmediate/');
  53. fs.mkdirSync(__dirname + '/setimmediate/');
  54. fs.readdirSync(__dirname + '/src').forEach(function (filename) {
  55. var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
  56. var out = fixup(src);
  57. out = out.replace(/var asap \= require\(\'([a-z\/]+)\'\);/g, '');
  58. out = out.replace(/asap/g, "setImmediate");
  59. fs.writeFileSync(__dirname + '/setimmediate/' + filename, out);
  60. });