How to use the @babel/core.template function in @babel/core

To help you get started, we’ve selected a few @babel/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
export const buildDeclareExports = template(`
  var ${exportName} = {
    __esModule: true
  };
`);

// Uses 'var' since it gets added during wrap
export const buildTempExport = template(`
  var ${exportName} = VALUE;
`);

export const buildExportDefault = template(`
  export default VALUE;
`);

export const buildReturnExports = template(`
  return ${exportName};
`);

export function buildNamedExport(obj) {
  return buildAssign({
    OBJECT: exportsIdentifier,
    NAME: obj.key,
    VALUE: obj.value,
  });
}

export const buildAllExportHelper = template(`
  function extendExports(exports, obj) {
    Object.keys(obj).forEach(function (key) {
      if (key === "default" || key === "__esModule") return;
      Object.defineProperty(exports, key, {
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
`);

export const buildReturn = template(`
  return ID;
`);

export const buildDefaultImportInterop = template(`
  function _interopRequireDefault(obj) { return (obj && obj.__esModule && typeof obj.default !== "undefined") ? obj.default : obj; }
`);

export const buildDefaultImportDeconstructor = template(`
  const LOCAL = _interopRequireDefault(MODULE);
`);

// TODO: inject __extends instead of Object.assign unless useBuiltIns in set
export const buildDynamicImportHelper = template(`
  function __ui5_require_async(path) {
    return new Promise((resolve, reject) => {
      sap.ui.require([path], (module) => {
        if (!module) { 
          return reject("No module returned from " + path); 
        } else if (module.__esModule) { 
          return resolve(module); 
        } else if (module.default) {
          return reject(new Error(path + " module includes a 'default' property but not __esModule. Cannot use as dynamic import"));
        } else {
          module.default = typeof module === "object" ? Object.assign({}, module) : module;
          module.__esModule = true;
          resolve(module);
        }
      })
    })
github babel / babel / packages / babel-plugin-transform-parameters / src / rest.js View on Github external
import { template, types as t } from "@babel/core";

const buildRest = template(`
  for (var LEN = ARGUMENTS.length,
           ARRAY = new Array(ARRAY_LEN),
           KEY = START;
       KEY < LEN;
       KEY++) {
    ARRAY[ARRAY_KEY] = ARGUMENTS[KEY];
  }
`);

const restIndex = template(`
  (INDEX < OFFSET || ARGUMENTS.length <= INDEX) ? undefined : ARGUMENTS[INDEX]
`);

const restIndexImpure = template(`
  REF = INDEX, (REF < OFFSET || ARGUMENTS.length <= REF) ? undefined : ARGUMENTS[REF]
`);

const restLength = template(`
  ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET
`);

function referencesRest(path, state) {
  if (path.node.name === state.name) {
    // Check rest parameter is not shadowed by a binding in another scope.
    return path.scope.bindingIdentifierEquals(state.name, state.outerBinding);
  }
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
const exportName = "__exports";

export const exportsIdentifier = t.identifier(exportName);

export const buildAssign = template(`
  OBJECT.NAME = VALUE;
`);

export const buildDefine = template(`
  sap.ui.define(SOURCES, function (PARAMS) {
    BODY;
  });
`);

export const buildDefineGlobal = template(`
  sap.ui.define(SOURCES, function (PARAMS) {
    BODY;
  }, true);
`);

// Uses 'var' since it gets added during wrap
export const buildDeclareExports = template(`
  var ${exportName} = {
    __esModule: true
  };
`);

// Uses 'var' since it gets added during wrap
export const buildTempExport = template(`
  var ${exportName} = VALUE;
`);
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
return obj[key];
        }
      });
    });
  }
`);

export const buildAllExport = template(`
  extendExports(${exportName}, LOCAL);
`);

export const buildReturn = template(`
  return ID;
`);

export const buildDefaultImportInterop = template(`
  function _interopRequireDefault(obj) { return (obj && obj.__esModule && typeof obj.default !== "undefined") ? obj.default : obj; }
`);

export const buildDefaultImportDeconstructor = template(`
  const LOCAL = _interopRequireDefault(MODULE);
`);

// TODO: inject __extends instead of Object.assign unless useBuiltIns in set
export const buildDynamicImportHelper = template(`
  function __ui5_require_async(path) {
    return new Promise((resolve, reject) => {
      sap.ui.require([path], (module) => {
        if (!module) { 
          return reject("No module returned from " + path); 
        } else if (module.__esModule) { 
          return resolve(module);
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
resolve(module);
        }
      })
    })
  }
`);

export const buildConstDeclaration = template(`
  const NAME = VALUE;
`);

export const buildNamedImportDestructor = template(`
  const LOCAL = MODULE[IMPORTED];
`);

export const buildExtendAssign = template(`
  const NAME = SUPER.extend(FQN, OBJECT);
`);

// This is use when there is not already the function, so always propagate arguments.
export const buildInheritingFunction = template(`
  function NAME() {
    if (typeof SUPER.prototype.NAME === 'function') {
      SUPER.prototype.NAME.apply(this, arguments);
    }
  }
`);

// This is use when there is not already the function, so always propagate arguments.
export const buildInheritingConstructor = template(`
  function constructor() {
    SUPER.prototype.constructor.apply(this, arguments);
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
export const buildDefineGlobal = template(`
  sap.ui.define(SOURCES, function (PARAMS) {
    BODY;
  }, true);
`);

// Uses 'var' since it gets added during wrap
export const buildDeclareExports = template(`
  var ${exportName} = {
    __esModule: true
  };
`);

// Uses 'var' since it gets added during wrap
export const buildTempExport = template(`
  var ${exportName} = VALUE;
`);

export const buildExportDefault = template(`
  export default VALUE;
`);

export const buildReturnExports = template(`
  return ${exportName};
`);

export function buildNamedExport(obj) {
  return buildAssign({
    OBJECT: exportsIdentifier,
    NAME: obj.key,
    VALUE: obj.value,
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
if (key === "default" || key === "__esModule") return;
      Object.defineProperty(exports, key, {
        enumerable: true,
        get: function get() {
          return obj[key];
        }
      });
    });
  }
`);

export const buildAllExport = template(`
  extendExports(${exportName}, LOCAL);
`);

export const buildReturn = template(`
  return ID;
`);

export const buildDefaultImportInterop = template(`
  function _interopRequireDefault(obj) { return (obj && obj.__esModule && typeof obj.default !== "undefined") ? obj.default : obj; }
`);

export const buildDefaultImportDeconstructor = template(`
  const LOCAL = _interopRequireDefault(MODULE);
`);

// TODO: inject __extends instead of Object.assign unless useBuiltIns in set
export const buildDynamicImportHelper = template(`
  function __ui5_require_async(path) {
    return new Promise((resolve, reject) => {
      sap.ui.require([path], (module) => {
github r-murphy / babel-plugin-transform-modules-ui5 / packages / plugin / src / utils / templates.js View on Github external
export default VALUE;
`);

export const buildReturnExports = template(`
  return ${exportName};
`);

export function buildNamedExport(obj) {
  return buildAssign({
    OBJECT: exportsIdentifier,
    NAME: obj.key,
    VALUE: obj.value,
  });
}

export const buildAllExportHelper = template(`
  function extendExports(exports, obj) {
    Object.keys(obj).forEach(function (key) {
      if (key === "default" || key === "__esModule") return;
      Object.defineProperty(exports, key, {
        enumerable: true,
        get: function get() {
          return obj[key];
        }
      });
    });
  }
`);

export const buildAllExport = template(`
  extendExports(${exportName}, LOCAL);
`);
github babel / babel / packages / babel-plugin-transform-modules-systemjs / src / index.js View on Github external
import { declare } from "@babel/helper-plugin-utils";
import hoistVariables from "@babel/helper-hoist-variables";
import { template, types as t } from "@babel/core";
import { getImportSource } from "babel-plugin-dynamic-import-node/utils";

const buildTemplate = template(`
  SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
    "use strict";
    BEFORE_BODY;
    return {
      setters: SETTERS,
      execute: function () {
        BODY;
      }
    };
  });
`);

const buildExportAll = template(`
  for (var KEY in TARGET) {
    if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
  }