How to use the isImport function from @babel/types

Find comprehensive JavaScript @babel/types.isImport code examples handpicked from public code repositorys.

61
62
63
64
65
66
67
68
69
70

traverse(ast, {
  enter: function (path, state) {
    // 动态导入import('xxx')
    if (
      t.isImport(path.node) &&
      path.parentPath &&
      path.parentPath.node &&
      path.parentPath.node.arguments &&
      path.parentPath.node.arguments.length
fork icon1
star icon5
watch icon0

150
151
152
153
154
155
156
157
158
159
160
161
162


const traverse = require("@babel/traverse").default;


const types = require("@babel/types");


const isImport = types.isImport;
/**
 * Transform all the calls to `require()` and `import()` in a file into ID-
 * independent code, and return the list of dependencies. For example, a call
 * like `require('Foo')` could be transformed to `require(_depMap[3], 'Foo')`
fork icon0
star icon0
watch icon1

+ 2 other calls in file

113
114
115
116
117
118
119
120
121
122
123
    .map(identifier => `${identifier} = undefined;`)
    .join('\n');
};


const handleCallExpressionWithRequireOrImport = (path) => {
  if (path.node.callee && types.isImport(path.node.callee)) {
    path.node.callee.type = 'Identifier';
    path.node.callee.name = '__import';
  }
  if (getEnclosingFunction(path)) {
fork icon0
star icon0
watch icon0

+ 21 other calls in file

84
85
86
87
88
89
90
91
92
93
/**
 * @param {T.CallExpression} node
 */
function isDynamicImport(node) {
  return !!(
    T.isImport(node.callee) &&
    node.arguments.length === 1 &&
    T.isStringLiteral(node.arguments[0])
  );
}
fork icon0
star icon0
watch icon0

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)