How to use @babel/helper-module-imports - 10 common examples

To help you get started, we’ve selected a few @babel/helper-module-imports 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 alan-ai / alan-sdk-reactnative / testtools / node_modules / @babel / helper-builder-react-jsx-experimental / src / index.js View on Github external
function addAutoImports(path, state) {
    const imports = getImportNames(path, state);
    if (isModule(path)) {
      // import {jsx} from "react";
      // import {createElement} from "react";
      const importMap = {};

      imports.forEach(importName => {
        if (!importMap[importName]) {
          importMap[importName] = addNamed(
            path,
            importName,
            getSource(state.source, importName),
            {
              importedInterop: "uncompiled",
              ensureLiveReference: true,
            },
          ).name;
        }
github alan-ai / alan-sdk-reactnative / testtools / node_modules / @babel / helper-builder-react-jsx-experimental / src / index.js View on Github external
imports.forEach(importName => {
        const source = getSource(state.source, importName);
        if (!importMap[importName]) {
          if (!sourceMap[source]) {
            // var _react = require("react")
            sourceMap[source] = addNamespace(path, source, {
              importedInterop: "uncompiled",
              ensureLiveReference: true,
            }).name;
          }

          importMap[importName] = sourceMap[source];
        }
      });
      return importMap;
github Jordaneisenburger / fallback-studio / src / pwa-studio / packages / pwa-buildpack / src / magento-layout-loader / babel-plugin-magento-layout.js View on Github external
insertElementAdjacentToChild(position) {
        const { containerPath, currentOperation, onWarning } = this;
        const { targetChild, componentPath } = currentOperation;
        const targetChildPath = this.findContainerChildByName(targetChild);
        if (!targetChildPath) {
            onWarning(
                `Attempted to inject a PWA Studio extension, but specified targetChild was not found\n` +
                    `operation: ${currentOperation.operation}\n` +
                    `targetContainer: ${currentOperation.targetContainer}\n` +
                    `targetChild: ${currentOperation.targetChild}`
            );
            return;
        }
        const componentIdent = addDefault(targetChildPath, componentPath, {
            nameHint: 'Extension'
        }).name;
        // TODO: extensionNode needs to be wrapped in a new ContainerChild,
        // and an error boundary
        const extensionNode = this.buildCreateElementCall(componentIdent);
        this.insertAdjacentArgumentsNode(
            position,
            containerPath,
            targetChildPath.node,
            extensionNode
        );
    }
github ant-design / babel-plugin-import / src / Plugin.js View on Github external
importMethod(methodName, file, pluginState) {
    if (!pluginState.selectedMethods[methodName]) {
      const libraryDirectory = this.libraryDirectory;
      const style = this.style;
      const transformedMethodName = this.camel2UnderlineComponentName  // eslint-disable-line
        ? transCamel(methodName, '_')
        : this.camel2DashComponentName
          ? transCamel(methodName, '-')
          : methodName;
      const path = winPath(
        this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
      );
      pluginState.selectedMethods[methodName] = this.transformToDefaultImport  // eslint-disable-line
        ? addDefault(file.path, path, { nameHint: methodName })
        : addNamed(file.path, methodName, path);
      if (this.customStyleName) {
        const stylePath = winPath(this.customStyleName(transformedMethodName));
        addSideEffect(file.path, `${stylePath}`);
      } else if (this.styleLibraryDirectory) {
        const stylePath = winPath(
          join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
        );
        addSideEffect(file.path, `${stylePath}`);
      } else if (style === true) {
        addSideEffect(file.path, `${path}/style`);
      } else if (style === 'css') {
        addSideEffect(file.path, `${path}/style/css`);
      } else if (typeof style === 'function') {
        const stylePath = style(path, file);
        if (stylePath) {
github indico / js-flask-urls / packages / flask-urls.macro / src / flask-urls.macro.js View on Github external
});
      }

      replacement = buildFuncMock({
        FUNC: builderFuncId,
        ENDPOINT: t.stringLiteral(endpoint),
      });
    } else {
      const data = config.urlMap[endpoint];
      if (!data) {
        throw new MacroError('flask-url.macro must reference a valid flask endpoint');
      }

      // generate import
      if (!builderFuncId) {
        builderFuncId = addDefaultImport(state.file.path, config.builder, {
          nameHint: 'buildFlaskURL',
        });
      }

      replacement = buildFunc({
        FUNC: builderFuncId,
        RULE: t.valueToNode(data),
        BASE: t.stringLiteral(config.basePath),
      });
    }

    // replace the tagged template expression with the builder function
    parentPath.replaceWith(replacement);
  });
};
github indico / js-flask-urls / packages / flask-urls.macro / src / flask-urls.macro.js View on Github external
const quasi = parentPath.node.quasi;
    if (quasi.expressions.length) {
      throw new MacroError('flask-url.macro cannot contain expressions');
    }

    const endpoint = quasi.quasis[0].value.cooked;

    if (builderFuncId) {
      builderFuncId = t.cloneDeep(builderFuncId);
    }

    let replacement;
    if (config.mock) {
      if (!builderFuncId) {
        builderFuncId = addNamedImport(state.file.path, 'mockFlaskURL', config.builder, {
          nameHint: 'mockFlaskURL',
        });
      }

      replacement = buildFuncMock({
        FUNC: builderFuncId,
        ENDPOINT: t.stringLiteral(endpoint),
      });
    } else {
      const data = config.urlMap[endpoint];
      if (!data) {
        throw new MacroError('flask-url.macro must reference a valid flask endpoint');
      }

      // generate import
      if (!builderFuncId) {
github CraigCav / css-zero / src / macro / index.js View on Github external
// remove variable declarations of css``;
    cssRefs.forEach(ref => {
      ref.parentPath.parentPath.remove();
    });

    // if no styles have been used, there's no more work to do
    if (!Object.keys(styleSheet.usage).length) return;

    const filename = state.file.opts.filename;

    // choose a file to save the styles
    const outputFilename = relative(process.cwd(), filename.replace(/\.[^.]+$/, '.zero.css'));

    // include this file as an import to the referenced module, so that css-loader can pick it up at bundle-time
    addSideEffect(stylesRefs[0], './' + basename(outputFilename));

    // combine all the used styles
    const cssText = styleSheet.toString();

    // Read the file first to compare the content
    // Write the new content only if it's changed
    // This will prevent unnecessary reloads
    let currentCssText;

    try {
      currentCssText = readFileSync(outputFilename, 'utf-8');
    } catch (e) {
      // Ignore error
    }

    // if the files hasn't changed, nothing more to do
github ant-design / babel-plugin-import / src / Plugin.js View on Github external
? transCamel(methodName, '-')
          : methodName;
      const path = winPath(
        this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
      );
      pluginState.selectedMethods[methodName] = this.transformToDefaultImport  // eslint-disable-line
        ? addDefault(file.path, path, { nameHint: methodName })
        : addNamed(file.path, methodName, path);
      if (this.customStyleName) {
        const stylePath = winPath(this.customStyleName(transformedMethodName));
        addSideEffect(file.path, `${stylePath}`);
      } else if (this.styleLibraryDirectory) {
        const stylePath = winPath(
          join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
        );
        addSideEffect(file.path, `${stylePath}`);
      } else if (style === true) {
        addSideEffect(file.path, `${path}/style`);
      } else if (style === 'css') {
        addSideEffect(file.path, `${path}/style/css`);
      } else if (typeof style === 'function') {
        const stylePath = style(path, file);
        if (stylePath) {
          addSideEffect(file.path, stylePath);
        }
      }
    }
    return Object.assign({}, pluginState.selectedMethods[methodName]);
  }
github ant-design / babel-plugin-import / src / Plugin.js View on Github external
const libraryDirectory = this.libraryDirectory;
      const style = this.style;
      const transformedMethodName = this.camel2UnderlineComponentName  // eslint-disable-line
        ? transCamel(methodName, '_')
        : this.camel2DashComponentName
          ? transCamel(methodName, '-')
          : methodName;
      const path = winPath(
        this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
      );
      pluginState.selectedMethods[methodName] = this.transformToDefaultImport  // eslint-disable-line
        ? addDefault(file.path, path, { nameHint: methodName })
        : addNamed(file.path, methodName, path);
      if (this.customStyleName) {
        const stylePath = winPath(this.customStyleName(transformedMethodName));
        addSideEffect(file.path, `${stylePath}`);
      } else if (this.styleLibraryDirectory) {
        const stylePath = winPath(
          join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
        );
        addSideEffect(file.path, `${stylePath}`);
      } else if (style === true) {
        addSideEffect(file.path, `${path}/style`);
      } else if (style === 'css') {
        addSideEffect(file.path, `${path}/style/css`);
      } else if (typeof style === 'function') {
        const stylePath = style(path, file);
        if (stylePath) {
          addSideEffect(file.path, stylePath);
        }
      }
    }
github ark120202 / babel-lua / packages / babel-plugin-lua-runtime / src / index.js View on Github external
this.addDefaultImport = (source, nameHint, blockHoist) => {
        // If something on the page adds a helper when the file is an ES6
        // file, we can't reused the cached helper name after things have been
        // transformed because it has almost certainly been renamed.
        const cacheKey = isModule(file.path);
        const key = `${source}:${nameHint}:${cacheKey || ''}`;

        let cached = cache.get(key);
        if (cached) {
          cached = t.cloneDeep(cached);
        } else {
          cached = addDefault(file.path, source, {
            importedInterop: 'uncompiled',
            nameHint,
            blockHoist,
          });

          cache.set(key, cached);
        }
        cached[Symbol.for('helper')] = true;

@babel/helper-module-imports

Babel helper functions for inserting module loads

MIT
Latest version published 2 months ago

Package Health Score

95 / 100
Full package analysis

Similar packages