All files / dist/helpers wrapper.js

87.67% Statements 64/73
82.46% Branches 47/57
87.5% Functions 7/8
98.41% Lines 62/63
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    1x     1x   1x   1x   1x   1x   1x   1x   1x   1x   4x                     68x   68x   68x 68x   68x       68x   19x   19x 4x   15x       15x 4x 4x           64x 64x 5x 5x 5x 5x 110x 5x   110x 109x   1x     5x 1x   5x     64x   2x     64x   55x 9x   4x 4x     4x 21x 1x 1x 1x   1x       20x       4x           4x     64x 16x     64x       1x       64x 76x 76x     64x  
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.wrap = wrap;
 
var _babelTypes = require('babel-types');
 
var t = _interopRequireWildcard(_babelTypes);
 
var _templates = require('./templates');
 
var th = _interopRequireWildcard(_templates);
 
var _exports = require('./exports');
 
var eh = _interopRequireWildcard(_exports);
 
var _ast = require('./ast');
 
var ast = _interopRequireWildcard(_ast);
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
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: 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);
}