index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. exports.__esModule = true;
  3. var _create = require("babel-runtime/core-js/object/create");
  4. var _create2 = _interopRequireDefault(_create);
  5. exports.default = function (_ref) {
  6. var t = _ref.types;
  7. function isValidRequireCall(path) {
  8. if (!path.isCallExpression()) return false;
  9. if (!path.get("callee").isIdentifier({ name: "require" })) return false;
  10. if (path.scope.getBinding("require")) return false;
  11. var args = path.get("arguments");
  12. if (args.length !== 1) return false;
  13. var arg = args[0];
  14. if (!arg.isStringLiteral()) return false;
  15. return true;
  16. }
  17. var amdVisitor = {
  18. ReferencedIdentifier: function ReferencedIdentifier(_ref2) {
  19. var node = _ref2.node,
  20. scope = _ref2.scope;
  21. if (node.name === "exports" && !scope.getBinding("exports")) {
  22. this.hasExports = true;
  23. }
  24. if (node.name === "module" && !scope.getBinding("module")) {
  25. this.hasModule = true;
  26. }
  27. },
  28. CallExpression: function CallExpression(path) {
  29. if (!isValidRequireCall(path)) return;
  30. this.bareSources.push(path.node.arguments[0]);
  31. path.remove();
  32. },
  33. VariableDeclarator: function VariableDeclarator(path) {
  34. var id = path.get("id");
  35. if (!id.isIdentifier()) return;
  36. var init = path.get("init");
  37. if (!isValidRequireCall(init)) return;
  38. var source = init.node.arguments[0];
  39. this.sourceNames[source.value] = true;
  40. this.sources.push([id.node, source]);
  41. path.remove();
  42. }
  43. };
  44. return {
  45. inherits: require("babel-plugin-transform-es2015-modules-commonjs"),
  46. pre: function pre() {
  47. this.sources = [];
  48. this.sourceNames = (0, _create2.default)(null);
  49. this.bareSources = [];
  50. this.hasExports = false;
  51. this.hasModule = false;
  52. },
  53. visitor: {
  54. Program: {
  55. exit: function exit(path) {
  56. var _this = this;
  57. if (this.ran) return;
  58. this.ran = true;
  59. path.traverse(amdVisitor, this);
  60. var params = this.sources.map(function (source) {
  61. return source[0];
  62. });
  63. var sources = this.sources.map(function (source) {
  64. return source[1];
  65. });
  66. sources = sources.concat(this.bareSources.filter(function (str) {
  67. return !_this.sourceNames[str.value];
  68. }));
  69. var moduleName = this.getModuleName();
  70. if (moduleName) moduleName = t.stringLiteral(moduleName);
  71. if (this.hasExports) {
  72. sources.unshift(t.stringLiteral("exports"));
  73. params.unshift(t.identifier("exports"));
  74. }
  75. if (this.hasModule) {
  76. sources.unshift(t.stringLiteral("module"));
  77. params.unshift(t.identifier("module"));
  78. }
  79. var node = path.node;
  80. var factory = buildFactory({
  81. PARAMS: params,
  82. BODY: node.body
  83. });
  84. factory.expression.body.directives = node.directives;
  85. node.directives = [];
  86. node.body = [buildDefine({
  87. MODULE_NAME: moduleName,
  88. SOURCES: sources,
  89. FACTORY: factory
  90. })];
  91. }
  92. }
  93. }
  94. };
  95. };
  96. var _babelTemplate = require("babel-template");
  97. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  98. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  99. var buildDefine = (0, _babelTemplate2.default)("\n define(MODULE_NAME, [SOURCES], FACTORY);\n");
  100. var buildFactory = (0, _babelTemplate2.default)("\n (function (PARAMS) {\n BODY;\n })\n");
  101. module.exports = exports["default"];