index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (_ref) {
  4. var t = _ref.types;
  5. function isValidDefine(path) {
  6. if (!path.isExpressionStatement()) return;
  7. var expr = path.get("expression");
  8. if (!expr.isCallExpression()) return false;
  9. if (!expr.get("callee").isIdentifier({ name: "define" })) return false;
  10. var args = expr.get("arguments");
  11. if (args.length === 3 && !args.shift().isStringLiteral()) return false;
  12. if (args.length !== 2) return false;
  13. if (!args.shift().isArrayExpression()) return false;
  14. if (!args.shift().isFunctionExpression()) return false;
  15. return true;
  16. }
  17. return {
  18. inherits: require("babel-plugin-transform-es2015-modules-amd"),
  19. visitor: {
  20. Program: {
  21. exit: function exit(path, state) {
  22. var last = path.get("body").pop();
  23. if (!isValidDefine(last)) return;
  24. var call = last.node.expression;
  25. var args = call.arguments;
  26. var moduleName = args.length === 3 ? args.shift() : null;
  27. var amdArgs = call.arguments[0];
  28. var func = call.arguments[1];
  29. var browserGlobals = state.opts.globals || {};
  30. var commonArgs = amdArgs.elements.map(function (arg) {
  31. if (arg.value === "module" || arg.value === "exports") {
  32. return t.identifier(arg.value);
  33. } else {
  34. return t.callExpression(t.identifier("require"), [arg]);
  35. }
  36. });
  37. var browserArgs = amdArgs.elements.map(function (arg) {
  38. if (arg.value === "module") {
  39. return t.identifier("mod");
  40. } else if (arg.value === "exports") {
  41. return t.memberExpression(t.identifier("mod"), t.identifier("exports"));
  42. } else {
  43. var memberExpression = void 0;
  44. if (state.opts.exactGlobals) {
  45. var globalRef = browserGlobals[arg.value];
  46. if (globalRef) {
  47. memberExpression = globalRef.split(".").reduce(function (accum, curr) {
  48. return t.memberExpression(accum, t.identifier(curr));
  49. }, t.identifier("global"));
  50. } else {
  51. memberExpression = t.memberExpression(t.identifier("global"), t.identifier(t.toIdentifier(arg.value)));
  52. }
  53. } else {
  54. var requireName = (0, _path.basename)(arg.value, (0, _path.extname)(arg.value));
  55. var globalName = browserGlobals[requireName] || requireName;
  56. memberExpression = t.memberExpression(t.identifier("global"), t.identifier(t.toIdentifier(globalName)));
  57. }
  58. return memberExpression;
  59. }
  60. });
  61. var moduleNameOrBasename = moduleName ? moduleName.value : this.file.opts.basename;
  62. var globalToAssign = t.memberExpression(t.identifier("global"), t.identifier(t.toIdentifier(moduleNameOrBasename)));
  63. var prerequisiteAssignments = null;
  64. if (state.opts.exactGlobals) {
  65. var globalName = browserGlobals[moduleNameOrBasename];
  66. if (globalName) {
  67. prerequisiteAssignments = [];
  68. var members = globalName.split(".");
  69. globalToAssign = members.slice(1).reduce(function (accum, curr) {
  70. prerequisiteAssignments.push(buildPrerequisiteAssignment({ GLOBAL_REFERENCE: accum }));
  71. return t.memberExpression(accum, t.identifier(curr));
  72. }, t.memberExpression(t.identifier("global"), t.identifier(members[0])));
  73. }
  74. }
  75. var globalExport = buildGlobalExport({
  76. BROWSER_ARGUMENTS: browserArgs,
  77. PREREQUISITE_ASSIGNMENTS: prerequisiteAssignments,
  78. GLOBAL_TO_ASSIGN: globalToAssign
  79. });
  80. last.replaceWith(buildWrapper({
  81. MODULE_NAME: moduleName,
  82. AMD_ARGUMENTS: amdArgs,
  83. COMMON_ARGUMENTS: commonArgs,
  84. GLOBAL_EXPORT: globalExport,
  85. FUNC: func
  86. }));
  87. }
  88. }
  89. }
  90. };
  91. };
  92. var _path = require("path");
  93. var _babelTemplate = require("babel-template");
  94. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  95. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  96. var buildPrerequisiteAssignment = (0, _babelTemplate2.default)("\n GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}\n");
  97. var buildGlobalExport = (0, _babelTemplate2.default)("\n var mod = { exports: {} };\n factory(BROWSER_ARGUMENTS);\n PREREQUISITE_ASSIGNMENTS\n GLOBAL_TO_ASSIGN = mod.exports;\n");
  98. var buildWrapper = (0, _babelTemplate2.default)("\n (function (global, factory) {\n if (typeof define === \"function\" && define.amd) {\n define(MODULE_NAME, AMD_ARGUMENTS, factory);\n } else if (typeof exports !== \"undefined\") {\n factory(COMMON_ARGUMENTS);\n } else {\n GLOBAL_EXPORT\n }\n })(this, FUNC);\n");
  99. module.exports = exports["default"];