build.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. });