How to use babel-plugin-macros - 10 common examples

To help you get started, weโ€™ve selected a few babel-plugin-macros 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 kentcdodds / babel-plugin-codegen / src / macro.js View on Github external
// const printAST = require('ast-pretty-print')
const {createMacro} = require('babel-plugin-macros')
const getReplacers = require('./replace')

module.exports = createMacro(codegenMacros)

function codegenMacros({references, state, babel}) {
  const {asIdentifier} = getReplacers(babel)
  const fileOpts = state.file.opts
  references.default.forEach(referencePath => {
    if (asIdentifier(referencePath, fileOpts) === false) {
      throw referencePath.buildCodeFrameError(
        'codegen macro must be used as a tagged template literal, function, jsx, or .require call',
        Error,
      )
    }
  })
}
github kentcdodds / import-all.macro / src / macro.js View on Github external
const path = require('path')
// const printAST = require('ast-pretty-print')
const {createMacro} = require('babel-plugin-macros')
const glob = require('glob')

module.exports = createMacro(prevalMacros)

function prevalMacros({references, state, babel}) {
  references.default.forEach(referencePath => {
    if (referencePath.parentPath.type === 'CallExpression') {
      asyncVersion({referencePath, state, babel})
    } else if (
      referencePath.parentPath.type === 'MemberExpression' &&
      referencePath.parentPath.node.property.name === 'sync'
    ) {
      syncVersion({referencePath, state, babel})
    } else if (
      referencePath.parentPath.type === 'MemberExpression' &&
      referencePath.parentPath.node.property.name === 'deferred'
    ) {
      deferredVersion({referencePath, state, babel})
    } else {
github jaredLunde / react-broker / src / macro / index.js View on Github external
import path from 'path'
import {createMacro} from 'babel-plugin-macros'

const pkgName = 'react-broker'
export default createMacro(evaluateMacros)

// cycles through each call to the macro and determines an output for each
function evaluateMacros({references, state, babel}) {
  references.default.forEach(referencePath =>
    makeLegacyEnsure({
      state,
      babel,
      referencePath,
    })
  )
}

// if Broker is defined in the scope then it will use that Broker, otherwise
// it requires the module.
function makeLegacyEnsure({state, babel, referencePath}) {
  const brokerTemplate = babel.template.smart(
github dsherret / ts-nameof / packages / ts-nameof.macro / src / index.js View on Github external
function getPath() {
            const parentPath = path.parentPath; // identifier;
            if (parentPath.type === "CallExpression")
                return parentPath;
            const grandParentPath = parentPath.parentPath;
            if (parentPath.type === "MemberExpression" && grandParentPath.type === "CallExpression")
                return grandParentPath;
            throw new MacroError("[ts-nameof]: Could not find a call expression at path: " + grandParentPath.getSource());
        }
    });
github commercetools / ui-kit / babel.config.js View on Github external
// behavior for any plugins that require one.
          useBuiltIns: true,
        },
      ],
      [
        '@emotion/babel-preset-css-prop',
        {
          sourceMap: isEnvDevelopment,
          autoLabel: !isEnvProduction,
        },
      ],
    ].filter(Boolean),
    plugins: [
      // Experimental macros support. Will be documented after it's had some time
      // in the wild.
      require('babel-plugin-macros').default,
      // export { default } from './foo'
      require('@babel/plugin-proposal-export-default-from').default,
      // export * from './foo'
      require('@babel/plugin-proposal-export-namespace-from').default,
      // Necessary to include regardless of the environment because
      // in practice some other transforms (such as object-rest-spread)
      // don't work without it: https://github.com/babel/babel/issues/7215
      require('@babel/plugin-transform-destructuring').default,
      // class { handleClick = () => { } }
      // Enable loose mode to use assignment instead of defineProperty
      // See discussion in https://github.com/facebook/create-react-app/issues/4263
      [
        require('@babel/plugin-proposal-class-properties').default,
        {
          loose: true,
        },
github fkling / astexplorer / website / src / parsers / js / transformers / babel-plugin-macros / index.js View on Github external
import compileModule from '../../../utils/compileModule';
import pkg from 'babel-plugin-macros/package';

const ID = 'babel-plugin-macros';
export default {
  id: ID,
  displayName: ID,
  version: pkg.version,
  homepage: pkg.homepage,

  defaultParserID: 'babylon7',

  loadTransformer(callback) {
    require([
      '../../../transpilers/babel',
      'babel7',
      'recast',
      'babel-plugin-macros',
    ], (transpile, babel, recast, macro) => callback({ transpile: transpile.default, babel, recast, macro}));
  },

  transform({ transpile, babel, recast, macro}, transformCode, code) {
    transformCode = transpile(transformCode);
    let transform = compileModule( // eslint-disable-line no-shadow
      transformCode,
github fkling / astexplorer / website / src / parsers / js / transformers / babel-plugin-macros / index.js View on Github external
import compileModule from '../../../utils/compileModule';
import pkg from 'babel-plugin-macros/package';

const ID = 'babel-plugin-macros';
export default {
  id: ID,
  displayName: ID,
  version: pkg.version,
  homepage: pkg.homepage,

  defaultParserID: 'babylon7',

  loadTransformer(callback) {
    require([
      '../../../transpilers/babel',
      'babel7',
      'recast',
      'babel-plugin-macros',
    ], (transpile, babel, recast, macro) => callback({ transpile: transpile.default, babel, recast, macro}));
  },

  transform({ transpile, babel, recast, macro}, transformCode, code) {
    transformCode = transpile(transformCode);
    let transform = compileModule( // eslint-disable-line no-shadow
github bohdanbirdie / react-css-modules.macro / src / macro.js View on Github external
stylesArgument,
          marcoConfig.warning
            ? t.booleanLiteral(true)
            : t.booleanLiteral(false),
        ]),
      ),
    ]);

    firstImportDeclarationNode.insertBefore(helperImportDeclaration);
    firstNonImportDeclarationNode.insertBefore(bindedStylesDeclaration);

    programPath.traverse(visitor(t, getStyleNameIdentifier, marcoConfig));
  });
};

export default createMacro(myMacro, { configName: "reactCssModulesMacro" });
github theKashey / react-imported-component / src / macro.ts View on Github external
throw new Error(
    'you have used `react-imported-component/macro` without `babel-plugin-macro` or `react-hot-loader/babel` installed'
  );
};

const lazy: typeof import('./HOC').lazy = neverCallMe;
const imported: typeof import('./HOC').default = neverCallMe;
const importedModule: typeof import('./Module').importedModule = neverCallMe;
const useImported: typeof import('./useImported').useImported = neverCallMe;

const ImportedModule: typeof import('./Module').ImportedModule = neverCallMe;
const ImportedComponent: typeof import('./Component').ImportedComponent = neverCallMe;

export { lazy, imported, importedModule, ImportedModule, ImportedComponent, useImported, assignImportedComponents };

export default createMacro(macro);
github evenchange4 / svgr.macro / src / macro.js View on Github external
const arrowFunctionExpression =
          ast.program.body[1].declarations[0].init;
        return t.objectProperty(
          t.stringLiteral(componentName),
          arrowFunctionExpression,
        );
      });

      referencePath.parentPath.replaceWith(
        t.objectExpression(objectProperties),
      );
    }
  });
}

export default createMacro(svgrMacro);

babel-plugin-macros

Allows you to build compile-time libraries

MIT
Latest version published 3 years ago

Package Health Score

76 / 100
Full package analysis