How to use the importSpecifier function from babel-types

Find comprehensive JavaScript babel-types.importSpecifier code examples handpicked from public code repositorys.

73
74
75
76
77
78
79
80
81
82
// 将import Vue from 'vue' 替换为 import {Vue, createMP} from 'vue'
ImportDeclaration: function (path) {
  if (path.node.specifiers && path.node.source && path.node.source.value === 'vue') {
    const specifiersValue = path.node.specifiers[0].local.name
    path.node.specifiers = [
      t.importSpecifier(t.identifier(specifiersValue), t.identifier(specifiersValue)),
      t.importSpecifier(t.identifier('createMP'), t.identifier('createMP'))
    ]
  }
},
fork icon49
star icon47
watch icon12

+ 87 other calls in file

7
8
9
10
11
12
13
14
15
16

function getImport(path) {
  return path.node.specifiers.reduce((result, specifier) => {
    if (specifier.imported) {
      result.push(
        babelTypes.importSpecifier(
          babelTypes.identifier(specifier.local.name),
          babelTypes.identifier(specifier.imported.name),
        ),
      );
fork icon15
star icon76
watch icon11

+ 5 other calls in file

51
52
53
54
55
56
57
58
59
60
};

const makeImportStatement = nodeKeys => {
  return Object.assign(
    t.importDeclaration(
      nodeKeys.map(nodeKey => t.importSpecifier(t.identifier(nodeKey), t.identifier(nodeKey))),
      t.stringLiteral('graphql/language/ast')
    ),
    { importKind: 'type' }
  );
fork icon5
star icon52
watch icon3

+ 9 other calls in file

50
51
52
53
54
55
56
57
58
59
traverse(AST, {
        ImportDeclaration: {
                enter(path) {
                        if (path.get('source').node.value === 'enzyme') {
                                const imports = path.get('specifiers');
                                const shallowSpec = t.importSpecifier(
                                        t.identifier('shallow'),
                                        t.identifier('shallow')
                                );
                                imports[0].insertAfter(shallowSpec);
fork icon2
star icon5
watch icon3

+ 3 other calls in file

43
44
45
46
47
48
49
50
51
52
if (isDefault) {
  specifier = types.importDefaultSpecifier(localIdentifier);
} else if (isNamespace) {
  specifier = types.importNamespaceSpecifier(localIdentifier);
} else {
  specifier = types.importSpecifier(
    localIdentifier,
    typeof identifier === 'string' ? localIdentifier : types.identifier(identifier[1])
  );
}
fork icon0
star icon2
watch icon1

+ 7 other calls in file

202
203
204
205
206
207
208
209
210
211
const stylePath = path.resolve(path.dirname(filePath), value)
if (styleFiles.indexOf(stylePath) < 0) {
  styleFiles.push(stylePath)
}
const importSpecifiers = _.uniqBy(stylesImportedArr, 'originName')
  .map(item => t.importSpecifier(t.identifier(item.styleName), t.identifier(item.originName)))
astPath.replaceWith(t.importDeclaration(
  importSpecifiers,
  t.stringLiteral(`${path.dirname(value)}/${basename}_styles`))
)
fork icon0
star icon0
watch icon4

+ 11 other calls in file

58
59
60
61
62
63
64
65
66
67
 * import type {NAMES[0], NAMES[1], ...} from 'MODULE';
 */
function importTypes(names: Array<string>, module: string) {
  const importDeclaration = t.importDeclaration(
    names.map(name =>
      t.importSpecifier(t.identifier(name), t.identifier(name)),
    ),
    t.stringLiteral(module),
  );
  importDeclaration.importKind = 'type';
fork icon0
star icon0
watch icon2

+ 3 other calls in file

24
25
26
27
28
29
30
31
32

if (file.usingGraphQL) {
  const graphqlImportDeclaration = t.importDeclaration(
    [
      t.importSpecifier(t.identifier('graphql'), t.identifier('graphql')),
      t.importSpecifier(t.identifier('compose'), t.identifier('compose')),
    ],
    t.stringLiteral('react-apollo')
  );
fork icon0
star icon0
watch icon4

+ 27 other calls in file

Other functions in babel-types

Sorted by popularity

function icon

babel-types.identifier is the most popular function in babel-types (4076 examples)