/* eslint max-len: 0 */

import { basename, extname } from "path";
import template from "babel-template";

let buildPrerequisiteAssignment = template(`
  GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}
`);

let buildGlobalExport = template(`
  var mod = { exports: {} };
  factory(BROWSER_ARGUMENTS);
  PREREQUISITE_ASSIGNMENTS
  GLOBAL_TO_ASSIGN = mod.exports;
`);

let buildWrapper = template(`
  (function (global, factory) {
    if (typeof define === "function" && define.amd) {
      define(MODULE_NAME, AMD_ARGUMENTS, factory);
    } else if (typeof exports !== "undefined") {
      factory(COMMON_ARGUMENTS);
    } else {
      GLOBAL_EXPORT
    }
  })(this, FUNC);
`);

export default function ({ types: t }) {
  function isValidDefine(path) {
    if (!path.isExpressionStatement()) return;

    let expr = path.get("expression");
    if (!expr.isCallExpression()) return false;
    if (!expr.get("callee").isIdentifier({ name: "define" })) return false;

    let args = expr.get("arguments");
    if (args.length === 3 && !args.shift().isStringLiteral()) return false;
    if (args.length !== 2) return false;
    if (!args.shift().isArrayExpression()) return false;
    if (!args.shift().isFunctionExpression()) return false;

    return true;
  }

  return {
    inherits: require("babel-plugin-transform-es2015-modules-amd"),

    visitor: {
      Program: {
        exit(path, state) {
          let last = path.get("body").pop();
          if (!isValidDefine(last)) return;

          let call = last.node.expression;
          let args = call.arguments;

          let moduleName = args.length === 3 ? args.shift() : null;
          let amdArgs = call.arguments[0];
          let func = call.arguments[1];
          let browserGlobals = state.opts.globals || {};

          let commonArgs = amdArgs.elements.map((arg) => {
            if (arg.value === "module" || arg.value === "exports") {
              return t.identifier(arg.value);
            } else {
              return t.callExpression(t.identifier("require"), [arg]);
            }
          });

          let browserArgs = amdArgs.elements.map((arg) => {
            if (arg.value === "module") {
              return t.identifier("mod");
            } else if (arg.value === "exports") {
              return t.memberExpression(t.identifier("mod"), t.identifier("exports"));
            } else {
              let memberExpression;

              if (state.opts.exactGlobals) {
                let globalRef = browserGlobals[arg.value];
                if (globalRef) {
                  if (globalRef.indexOf(".") > -1) {
                    memberExpression = globalRef.split(".").reduce(
                      (accum, curr) => t.memberExpression(accum, t.identifier(curr)), t.identifier("global")
                    );
                  } else {
                    memberExpression = t.memberExpression(t.identifier("global"), t.identifier(globalRef));
                  }
                } else {
                  memberExpression = t.memberExpression(
                    t.identifier("global"), t.identifier(t.toIdentifier(arg.value))
                  );
                }
              } else {
                let requireName = basename(arg.value, extname(arg.value));
                let globalName = browserGlobals[requireName] || requireName;
                memberExpression = t.memberExpression(
                  t.identifier("global"), t.identifier(t.toIdentifier(globalName))
                );
              }

              return memberExpression;
            }
          });

          let moduleNameOrBasename = moduleName ? moduleName.value : this.file.opts.basename;
          let globalToAssign = t.memberExpression(
            t.identifier("global"), t.identifier(t.toIdentifier(moduleNameOrBasename))
          );
          let prerequisiteAssignments = null;

          if (state.opts.exactGlobals) {
            let globalName = browserGlobals[moduleNameOrBasename];

            if (globalName) {
              if (globalName.indexOf(".") > -1) {
                prerequisiteAssignments = [];

                let members = globalName.split(".");
                let namespacedProperties = members.slice(1).reduce((accum, curr, index) => {
                  let prerequisiteAssignment = buildPrerequisiteAssignment({ GLOBAL_REFERENCE: accum[index] });
                  prerequisiteAssignments.push(prerequisiteAssignment);
                  accum.push(t.memberExpression(accum[index], t.identifier(curr)));
                  return accum;
                }, [t.memberExpression(t.identifier("global"), t.identifier(members[0]))]);

                globalToAssign = namespacedProperties[namespacedProperties.length -1];
              } else {
                globalToAssign = t.memberExpression(t.identifier("global"), t.identifier(globalName));
              }
            }
          }

          let globalExport = buildGlobalExport({
            BROWSER_ARGUMENTS: browserArgs,
            PREREQUISITE_ASSIGNMENTS: prerequisiteAssignments,
            GLOBAL_TO_ASSIGN: globalToAssign
          });

          last.replaceWith(buildWrapper({
            MODULE_NAME: moduleName,
            AMD_ARGUMENTS: amdArgs,
            COMMON_ARGUMENTS: commonArgs,
            GLOBAL_EXPORT: globalExport,
            FUNC: func
          }));
        }
      }
    }
  };
}

@@ -2541,63 +2541,8 @@
 ) %7B%0A
-                  if (globalRef.indexOf(%22.%22) %3E -1) %7B%0A  
     
@@ -2603,18 +2603,16 @@
 reduce(%0A
-  
         
@@ -2732,167 +2732,10 @@
     
-  );%0A                  %7D else %7B%0A                    memberExpression = t.memberExpression(t.identifier(%22global%22), t.identifier(globalRef));%0A                  %7D
+);
 %0A   
@@ -3788,60 +3788,8 @@
 ) %7B%0A
-              if (globalName.indexOf(%22.%22) %3E -1) %7B%0A  
     
@@ -3835,34 +3835,32 @@
 %0A%0A              
- 
- 
 let members = gl
@@ -3876,26 +3876,24 @@
 split(%22.%22);%0A
-  
             
@@ -3979,34 +3979,32 @@
                 
- 
- 
 let prerequisite
@@ -4081,18 +4081,16 @@
 ex%5D %7D);%0A
-  
         
@@ -4167,18 +4167,16 @@
         
-  
 accum.pu
@@ -4245,26 +4245,24 @@
             
-  
 return accum
@@ -4263,18 +4263,16 @@
  accum;%0A
-  
         
@@ -4360,34 +4360,32 @@
 %0A%0A              
-  
 globalToAssign =
@@ -4443,150 +4443,8 @@
 1%5D;%0A
-              %7D else %7B%0A                globalToAssign = t.memberExpression(t.identifier(%22global%22), t.identifier(globalName));%0A              %7D%0A
     

Remove unnecessary checking for "." since the code works in either case