build.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 ids = [];
  7. var names = {};
  8. function getIdFor(name) {
  9. if (name in names) return names[name];
  10. var id;
  11. do {
  12. id = '_' + Math.floor(Math.random() * 100);
  13. } while (ids.indexOf(id) !== -1)
  14. ids.push(id);
  15. names[name] = id;
  16. return id;
  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 source(node) {
  30. return src.slice(node.start, node.end).join('');
  31. }
  32. function replace(node, str) {
  33. for (var i = node.start; i < node.end; i++) {
  34. src[i] = '';
  35. }
  36. src[node.start] = str;
  37. }
  38. return src.join('');
  39. }
  40. rimraf.sync(__dirname + '/lib/');
  41. fs.mkdirSync(__dirname + '/lib/');
  42. fs.readdirSync(__dirname + '/src').forEach(function (filename) {
  43. var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
  44. var out = fixup(src);
  45. fs.writeFileSync(__dirname + '/lib/' + filename, out);
  46. });
  47. rimraf.sync(__dirname + '/domains/');
  48. fs.mkdirSync(__dirname + '/domains/');
  49. fs.readdirSync(__dirname + '/src').forEach(function (filename) {
  50. var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
  51. var out = fixup(src);
  52. out = out.replace(/require\(\'asap\/raw\'\)/g, "require('asap')");
  53. fs.writeFileSync(__dirname + '/domains/' + filename, out);
  54. });
  55. rimraf.sync(__dirname + '/setimmediate/');
  56. fs.mkdirSync(__dirname + '/setimmediate/');
  57. fs.readdirSync(__dirname + '/src').forEach(function (filename) {
  58. var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
  59. var out = fixup(src);
  60. out = out.replace(/var asap \= require\(\'([a-z\/]+)\'\);/g, '');
  61. out = out.replace(/asap/g, "setImmediate");
  62. fs.writeFileSync(__dirname + '/setimmediate/' + filename, out);
  63. });