node.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var fs = require("fs");
  2. exports.FILES = [
  3. require.resolve("../lib/utils.js"),
  4. require.resolve("../lib/ast.js"),
  5. require.resolve("../lib/transform.js"),
  6. require.resolve("../lib/parse.js"),
  7. require.resolve("../lib/scope.js"),
  8. require.resolve("../lib/compress.js"),
  9. require.resolve("../lib/output.js"),
  10. require.resolve("../lib/sourcemap.js"),
  11. require.resolve("../lib/mozilla-ast.js"),
  12. require.resolve("../lib/propmangle.js"),
  13. require.resolve("../lib/minify.js"),
  14. require.resolve("./exports.js"),
  15. ];
  16. new Function("exports", function() {
  17. var code = exports.FILES.map(function(file) {
  18. return fs.readFileSync(file, "utf8");
  19. });
  20. code.push("exports.describe_ast = " + describe_ast.toString());
  21. return code.join("\n\n");
  22. }())(exports);
  23. function to_comment(value) {
  24. if (typeof value != "string") value = JSON.stringify(value, function(key, value) {
  25. return typeof value == "function" ? "<[ " + value + " ]>" : value;
  26. }, 2);
  27. return "// " + value.replace(/\n/g, "\n// ");
  28. }
  29. if (+process.env["UGLIFY_BUG_REPORT"]) exports.minify = function(files, options) {
  30. if (typeof options == "undefined") options = "<<undefined>>";
  31. var code = [
  32. "// UGLIFY_BUG_REPORT",
  33. to_comment(options),
  34. ];
  35. if (typeof files == "string") {
  36. code.push("");
  37. code.push("//-------------------------------------------------------------")
  38. code.push("// INPUT CODE", files);
  39. } else for (var name in files) {
  40. code.push("");
  41. code.push("//-------------------------------------------------------------")
  42. code.push(to_comment(name), files[name]);
  43. }
  44. if (options.sourceMap && options.sourceMap.url) {
  45. code.push("");
  46. code.push("//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9");
  47. }
  48. var result = { code: code.join("\n") };
  49. if (options.sourceMap) result.map = '{"version":3,"sources":[],"names":[],"mappings":""}';
  50. return result;
  51. };
  52. function describe_ast() {
  53. var out = OutputStream({ beautify: true });
  54. doitem(AST_Node);
  55. return out.get() + "\n";
  56. function doitem(ctor) {
  57. out.print("AST_" + ctor.TYPE);
  58. var props = ctor.SELF_PROPS.filter(function(prop) {
  59. return !/^\$/.test(prop);
  60. });
  61. if (props.length > 0) {
  62. out.space();
  63. out.with_parens(function() {
  64. props.forEach(function(prop, i) {
  65. if (i) out.space();
  66. out.print(prop);
  67. });
  68. });
  69. }
  70. if (ctor.documentation) {
  71. out.space();
  72. out.print_string(ctor.documentation);
  73. }
  74. if (ctor.SUBCLASSES.length > 0) {
  75. out.space();
  76. out.with_block(function() {
  77. ctor.SUBCLASSES.sort(function(a, b) {
  78. return a.TYPE < b.TYPE ? -1 : 1;
  79. }).forEach(function(ctor, i) {
  80. out.indent();
  81. doitem(ctor);
  82. out.newline();
  83. });
  84. });
  85. }
  86. }
  87. }
  88. function infer_options(options) {
  89. var result = exports.minify("", options);
  90. return result.error && result.error.defs;
  91. }
  92. exports.default_options = function() {
  93. var defs = infer_options({ 0: 0 });
  94. Object.keys(defs).forEach(function(component) {
  95. var options = {};
  96. options[component] = { 0: 0 };
  97. if (options = infer_options(options)) {
  98. defs[component] = options;
  99. }
  100. });
  101. return defs;
  102. };