All files / src/helpers wrapper.js

96.3% Statements 52/54
89.8% Branches 44/49
85.71% Functions 6/7
96.23% Lines 51/53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                              74x   74x   74x 74x   74x       74x   19x   19x 4x     15x       15x 4x 4x           70x 70x 7x 7x 7x 7x 130x 7x   130x 129x     1x     7x 1x         7x     70x   2x     70x   57x   13x   5x 5x     5x 25x 1x 1x     1x   1x             24x           5x               5x     70x 19x     70x                     1x         70x 98x 98x     70x    
 
import { types as t } from "@babel/core";
import * as th from "./templates";
import * as eh from "./exports";
import * as ast from "./ast";
 
export function wrap(visitor, programNode, opts) {
 
  let {
    defaultExport,
    exportGlobal,
    firstImport,
    injectDynamicImportHelper,
    imports,
    namedExports,
  } = visitor;
 
  let { body } = programNode;
 
  let allExportHelperAdded = false;
  let extendAdded = false;
 
  opts.collapse = !opts.noExportCollapse;
  // opts.extend = !opts.noExportExtend
 
  // Before adding anything, see if the named exports can be collapsed into the default export.
  if (defaultExport && namedExports.length && opts.collapse) {
 
    let { filteredExports, conflictingExports, newDefaultExportIdentifier } = eh.collapseNamedExports(programNode, defaultExport, namedExports, opts);
 
    if (filteredExports.length && !opts.allowUnsafeMixedExports) {
      throw new Error(`Unsafe mixing of conflicting default and named exports. The following named exports are conflicting: (${ast.getPropNames(conflictingExports).join(", ")}).`);
    }
    else {
      namedExports = filteredExports;
    }
 
    // The default export may have changed if the collapse logic needed to assign a prop when the default export was previously anonymous.
    if (newDefaultExportIdentifier) {
      extendAdded = true; // If an anonymous default export needed to be assigned to a a variable, it uses the exports name for convenience.
      defaultExport = newDefaultExportIdentifier;
    }
  }
 
  // If the noWrapBeforeImport opt is set, split any code before the first define and afterwards into separate arrays.
  // This should be done before any interops or other vars are injected.
  const preDefine = [];
  if (opts.noWrapBeforeImport && firstImport) {
    let reachedFirstImport = false;
    const fullBody = body;
    const newBody = [];
    for (const item of fullBody) {
      if (item === firstImport) {
        reachedFirstImport = true;
      }
      if (reachedFirstImport) {
        newBody.push(item);
      }
      else {
        preDefine.push(item);
      }
    }
    if (preDefine.length && !hasUseStrict(programNode)) {
      programNode.directives = [
        t.directive(t.directiveLiteral("use strict")),
        ...(programNode.directives || []),
      ];
    }
    body = newBody;
  }
 
  if (injectDynamicImportHelper) {
    // import() to sap.ui.require() w/ promise and interop
    body.unshift(th.buildDynamicImportHelper());
  }
 
  if (!namedExports.length && defaultExport) {
    // If there's no named exports, return the default export
    body.push(t.returnStatement(defaultExport));
  }
  else if (namedExports.length) {
 
    Eif (!extendAdded) {
      body.push(th.buildDeclareExports()); // i.e. const __exports = {__esModule: true};
    }
 
    for (const namedExport of namedExports) {
      if (namedExport.all) {
        Eif (!allExportHelperAdded) {
          body.push(
            th.buildAllExportHelper()
          );
          allExportHelperAdded = true;
        }
        body.push(
          th.buildAllExport({
            LOCAL: namedExport.value
          })
        );
      }
      else {
        body.push(
          th.buildNamedExport(namedExport)
        );
      }
    }
 
    Iif (defaultExport) {
      body.push(
        th.buildNamedExport({
          key: t.identifier("default"),
          value: defaultExport
        })
      );
    }
    body.push(th.buildReturnExports());
  }
 
  if (imports.some(imp => imp.interop)) {
    body.unshift(th.buildDefaultImportInterop());
  }
 
  programNode.body = [
    ...preDefine,
    generateDefine(
      body,
      imports,
      (exportGlobal || opts.exportAllGlobal)
    )
  ];
}
 
function hasUseStrict(node) {
  return (node.directives || [])
    .some(directive => directive.value.value === "use strict");
}
 
function generateDefine(body, imports, exportGlobal) {
  const defineOpts = {
    SOURCES: t.arrayExpression(imports.map(i => t.stringLiteral(i.src))),
    PARAMS: imports.map(i => t.identifier(i.tmpName)),
    BODY: body
  };
  return exportGlobal ? th.buildDefineGlobal(defineOpts) : th.buildDefine(defineOpts);
}