How to use @babel/helper-plugin-utils - 10 common examples

To help you get started, we’ve selected a few @babel/helper-plugin-utils 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 c-zhuo / easycanvas / src / babel-plugin.js View on Github external
import { declare } from '@babel/helper-plugin-utils';
import jsx from '@babel/plugin-syntax-jsx';
import helper from '@babel/helper-builder-react-jsx';
import * as t from "@babel/types";

const plugin = declare((api, options) => {
    api.assertVersion(7);

    const THROW_IF_NAMESPACE =
        options.throwIfNamespace === undefined ? true : !!options.throwIfNamespace;

    const PRAGMA_DEFAULT = options.pragma || "Easycanvas.createElement";
    const PRAGMA_FRAG_DEFAULT = options.pragmaFrag || "window.Easycanvas ? window.Easycanvas.View : View";

    const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
    const JSX_FRAG_ANNOTATION_REGEX = /\*?\s*@jsxFrag\s+([^\s]+)/;

    // returns a closure that returns an identifier or memberExpression node
    // based on the given id
    const createIdentifierParser = (id) => () => {
        return id
            .split('.')
github blocks / blocks / packages / mdx / serializer / src / parse-jsx.js View on Github external
constructor() {
    const result = {
      props: {}
    }
    this.result = result

    this.plugin = declare(babel => {
      babel.assertVersion(7)

      const visitProps = {
        JSXAttribute(path) {
          const key = path.node.name.name
          // only handles string/static props
          const value = path.node.value.value
          result.props[key] = value
        }
      }

      return {
        visitor: {
          JSXOpeningElement(path) {
            // only parse root-level element
            if (result.type) return
github ChristopherBiscardi / gatsby-mdx / packages / babel-plugin-pluck-exports / index.js View on Github external
constructor() {
    const exports = [];
    this.state = { exports: exports };
    this.plugin = declare(api => {
      api.assertVersion(7);

      return {
        visitor: {
          ExportNamedDeclaration(path) {
            const name = path.get("declaration.declarations.0").node.id.name;

            // TODO: make this check an option to the babel plugin if we need to
            // hoist any other exports.
            if (name === "pageQuery") {
              const pageQueryExport = path.hub.file.code.slice(
                path.node.start,
                path.node.end
              );
              exports.push(pageQueryExport);
              path.remove();
github swashata / wp-webpack-script / packages / babel-preset-base / src / index.ts View on Github external
import { declare } from '@babel/helper-plugin-utils';
import { preset, PresetOptions } from './preset';
/**
 * Export a function to declare this preset.
 *
 * It takes options from userland and passes to @babel/preset-env
 */
module.exports = declare((api, opts) => {
	// Check if version is 7
	api.assertVersion(7);

	// Give back the babel config based on options
	return preset(opts as PresetOptions);
});
github mdx-js / mdx / packages / remark-mdx / extract-imports-and-exports.js View on Github external
constructor() {
    const nodes = []
    this.state = {nodes}

    this.plugin = declare(api => {
      api.assertVersion(7)

      return {
        visitor: {
          ExportDefaultDeclaration(path) {
            const {start} = path.node
            nodes.push({type: 'export', start, default: true})
          },
          ExportNamedDeclaration(path) {
            const {start} = path.node
            nodes.push({type: 'export', start})
          },
          ImportDeclaration(path) {
            const {start} = path.node

            // Imports that are used in exports can end up as
github auth0 / cosmos / core / babel-preset / index.js View on Github external
/*
  This file contains the babel plugins that
  are recommended for usage with cosmos
*/

const { declare } = require('@babel/helper-plugin-utils')

module.exports = declare(api => {
  api.assertVersion(7)

  return {
    presets: [
      [require('@babel/preset-env'), { modules: false }],
      require('@babel/preset-typescript'),
      require('@babel/preset-react')
    ],
    plugins: [
      require('@babel/plugin-proposal-class-properties'),
      require('@babel/plugin-proposal-object-rest-spread')
    ]
  }
})
github ChristopherBiscardi / gatsby-mdx / packages / gatsby-mdx / utils / babel-plugin-gather-exports / index.js View on Github external
constructor() {
    const exports = {};
    this.state = { exports: exports };
    this.plugin = declare(api => {
      api.assertVersion(7);

      return {
        visitor: {
          ExportNamedDeclaration(path) {
            const declaration = path.node.declaration;

            if (
              declaration &&
              declaration.type === "VariableDeclaration" &&
              declaration.kind === "const"
            ) {
              declaration.declarations.forEach(declarator => {
                try {
                  exports[declarator.id.name] = JSON5.parse(
                    generate(declarator.init).code
github airbnb / babel-plugin-inline-react-svg / src / index.js View on Github external
import { extname, dirname, parse as parseFilename } from 'path';
import { readFileSync } from 'fs';
import { parse } from '@babel/parser';
import { declare } from '@babel/helper-plugin-utils';
import resolve from 'resolve';

import optimize from './optimize';
import escapeBraces from './escapeBraces';
import transformSvg from './transformSvg';
import fileExistsWithCaseSync from './fileExistsWithCaseSync';

let ignoreRegex;

export default declare(({
  assertVersion,
  template,
  traverse,
  types: t,
}) => {
  assertVersion(7);

  const buildSvg = ({
    IS_EXPORT,
    EXPORT_FILENAME,
    SVG_NAME,
    SVG_CODE,
    SVG_DEFAULT_PROPS_CODE,
  }) => {
    const namedTemplate = `
      var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
github ikatun / nnode / src / babel-plugin-transform-typescript / babel-plugin-transform-typescript.ts View on Github external
case "TSQualifiedName":
    case "TSExpressionWithTypeArguments":
    case "TSTypeQuery":
      return true;
    default:
      return false;
  }
}

interface State {
  programPath: any;
}

const PARSED_PARAMS = new WeakSet();

export default declare((api, { jsxPragma = "React" }) => {
  api.assertVersion(7);

  const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;

  return {
    name: "transform-typescript",
    inherits: syntaxTypeScript,

    visitor: {
      //"Pattern" alias doesn't include Identifier or RestElement.
      Pattern: visitPattern,
      Identifier: visitPattern,
      RestElement: visitPattern,

      Program(path, state: State) {
        state.programPath = path;
github babel / babel / packages / babel-plugin-transform-template-literals / src / index.js View on Github external
import { declare } from "@babel/helper-plugin-utils";
import { template, types as t } from "@babel/core";

export default declare((api, options) => {
  api.assertVersion(7);
  const { loose } = options;

  let helperName = "taggedTemplateLiteral";
  if (loose) helperName += "Loose";

  /**
   * This function groups the objects into multiple calls to `.concat()` in
   * order to preserve execution order of the primitive conversion, e.g.
   *
   *   "".concat(obj.foo, "foo", obj2.foo, "foo2")
   *
   * would evaluate both member expressions _first_ then, `concat` will
   * convert each one to a primitive, whereas
   *
   *   "".concat(obj.foo, "foo").concat(obj2.foo, "foo2")

@babel/helper-plugin-utils

General utilities for plugins to use

MIT
Latest version published 2 months ago

Package Health Score

95 / 100
Full package analysis

Popular @babel/helper-plugin-utils functions

Similar packages