How to use @babel/core

Comprehensive @babel/core code examples:

How to use @babel/core.memberExpression:

250
251
252
253
254
255
256
257
258
259
                return obj.self;
            }

            return t.VariableDeclarator(
                t.identifier(obj.local),
                !ret.ref ? callExpression : t.memberExpression(callExpression, t.identifier(ret.ref)),
            );
        }),
    );
}

How to use @babel/core.callExpression:

237
238
239
240
241
242
243
244
245
246

path.replaceWithMultiple(
    list.map(function (obj) {
        var ret = moduleMapper(obj.imported, moduleName);

        var callExpression = t.callExpression(
            node.init.callee,
            [t.stringLiteral(typeof ret === 'string' ? ret : ret ? ret.moduleName : moduleName)].concat(
                restArguments,
            ),

How to use @babel/core.VariableDeclarator:

248
249
250
251
252
253
254
255
256
257

    if (!ret) {
        return obj.self;
    }

    return t.VariableDeclarator(
        t.identifier(obj.local),
        !ret.ref ? callExpression : t.memberExpression(callExpression, t.identifier(ret.ref)),
    );
}),

How to use @babel/core.OptionManager:

480
481
482
483
484
485
486
487
488
489
490
491
  }
  return false;
}


function compile(filename, callback) {
  const opts = new babel.OptionManager().init({ filename, ignore, only, configFile, rootMode });


  // If opts is not present, the file is ignored, either by explicit input into
  // babel-watch or by `.babelignore`.
  if (!opts) {

How to use @babel/core.Identifier:

110
111
112
113
114
115
116
117
118
119
      path.replaceWith(
        t.StringLiteral(replace[name])
      );
    } else {
      path.replaceWith(
        t.Identifier(replace[name].toString())
      );
    }
  }
});

How to use @babel/core.StringLiteral:

106
107
108
109
110
111
112
113
114
115
Identifier(path) {
  Object.keys(replace).forEach(name => {
    if (path.node.name === name) {
      if (identifierToStringLiteral.includes(name)) {
        path.replaceWith(
          t.StringLiteral(replace[name])
        );
      } else {
        path.replaceWith(
          t.Identifier(replace[name].toString())

How to use @babel/core.config:

80
81
82
83
84
85
86
87
88
89
}

const partialConfig = babel.loadPartialConfig({
    /*
     * There are two types of babel configuration:
     * - project-wide configuration in babel.config.* files
     * - file-relative configuration in .babelrc.* files
     *   or package.json files with a "babel" key
     *
     * To detect the file-relative configuration we need

How to use @babel/core.loadOptionsAsync:

29
30
31
32
33
34
35
36
37
38
39
  console.log(ast)
}


// try with loading config file
{
  const options = await loadOptionsAsync({
    root: urlToFileSystemPath(directoryUrl),
  })
  const ast = parser.parse(fileText, {
    ...options,

How to use @babel/core.importDefaultSpecifier:

85
86
87
88
89
90
91
92
93
94
/** The domains for each type of input specifier. */
const domains = {
    [$.import]: {
        declaration: types.importDeclaration,
        defaultSpecifier: (local) =>
            types.importDefaultSpecifier(types.identifier(local)),
        namespaceSpecifier: (local) =>
            types.importNamespaceSpecifier(types.identifier(local)),
        namedSpecifier: (local, exported) =>
            types.importSpecifier(

How to use @babel/core.importSpecifier:

89
90
91
92
93
94
95
96
97
98
    defaultSpecifier: (local) =>
        types.importDefaultSpecifier(types.identifier(local)),
    namespaceSpecifier: (local) =>
        types.importNamespaceSpecifier(types.identifier(local)),
    namedSpecifier: (local, exported) =>
        types.importSpecifier(
            types.identifier(local),
            types.identifier(exported)
        )
},

How to use @babel/core.importNamespaceSpecifier:

87
88
89
90
91
92
93
94
95
96
[$.import]: {
    declaration: types.importDeclaration,
    defaultSpecifier: (local) =>
        types.importDefaultSpecifier(types.identifier(local)),
    namespaceSpecifier: (local) =>
        types.importNamespaceSpecifier(types.identifier(local)),
    namedSpecifier: (local, exported) =>
        types.importSpecifier(
            types.identifier(local),
            types.identifier(exported)

How to use @babel/core.isMemberExpression:

141
142
143
144
145
146
147
148
149
150
// case 3: (row) => String(row.id);
if (t.isCallExpression(expression.body)) {
  if (
    expression.body.callee.name === 'String' &&
    expression.body.arguments.length === 1 &&
    t.isMemberExpression(expression.body.arguments[0])
  ) {
    const { object, property } = expression.body.arguments[0];

    if (object.name === rowName && t.isIdentifier(property)) {

How to use @babel/core.isJSXAttribute:

64
65
66
67
68
69
70
71
72
73
  return;
}

const rk = node.attributes.find(attr => {
  return (
    t.isJSXAttribute(attr) &&
    t.isJSXIdentifier(attr.name) &&
    attr.name.name === 'rowKey'
  );
});

How to use @babel/core.exportSpecifier:

102
103
104
105
106
107
108
109
110
111
    defaultSpecifier: (local) =>
        types.exportDefaultSpecifier(types.identifier(local)),
    namespaceSpecifier: (local, exported) =>
        types.exportNamespaceSpecifier(types.identifier(exported)),
    namedSpecifier: (local, exported) =>
        types.exportSpecifier(
            types.identifier(local),
            types.identifier(exported)
        )
}

How to use @babel/core.exportNamespaceSpecifier:

100
101
102
103
104
105
106
107
108
109
declaration: (specifiers, source) =>
    types.exportNamedDeclaration(null, specifiers, source),
defaultSpecifier: (local) =>
    types.exportDefaultSpecifier(types.identifier(local)),
namespaceSpecifier: (local, exported) =>
    types.exportNamespaceSpecifier(types.identifier(exported)),
namedSpecifier: (local, exported) =>
    types.exportSpecifier(
        types.identifier(local),
        types.identifier(exported)

How to use @babel/core.exportNamedDeclaration:

96
97
98
99
100
101
102
103
104
105
            types.identifier(exported)
        )
},
[$.export]: {
    declaration: (specifiers, source) =>
        types.exportNamedDeclaration(null, specifiers, source),
    defaultSpecifier: (local) =>
        types.exportDefaultSpecifier(types.identifier(local)),
    namespaceSpecifier: (local, exported) =>
        types.exportNamespaceSpecifier(types.identifier(exported)),

How to use @babel/core.exportDefaultSpecifier:

98
99
100
101
102
103
104
105
106
107
},
[$.export]: {
    declaration: (specifiers, source) =>
        types.exportNamedDeclaration(null, specifiers, source),
    defaultSpecifier: (local) =>
        types.exportDefaultSpecifier(types.identifier(local)),
    namespaceSpecifier: (local, exported) =>
        types.exportNamespaceSpecifier(types.identifier(exported)),
    namedSpecifier: (local, exported) =>
        types.exportSpecifier(

How to use @babel/core.ImportDefaultSpecifier:

90
91
92
93
94
95
96
97
98
99

return t.importDeclaration(
    // [t.importSpecifier(t.identifier(local), t.identifier(imported))],
    [
        !ret.ref || ret.ref === 'default'
            ? t.ImportDefaultSpecifier(t.identifier(local))
            : t.ImportSpecifier(t.identifier(local), t.identifier(ret.ref)),
    ],
    t.stringLiteral(typeof ret === 'string' ? ret : ret.moduleName),
);

How to use @babel/core.ImportSpecifier:

91
92
93
94
95
96
97
98
99
100
    return t.importDeclaration(
        // [t.importSpecifier(t.identifier(local), t.identifier(imported))],
        [
            !ret.ref || ret.ref === 'default'
                ? t.ImportDefaultSpecifier(t.identifier(local))
                : t.ImportSpecifier(t.identifier(local), t.identifier(ret.ref)),
        ],
        t.stringLiteral(typeof ret === 'string' ? ret : ret.moduleName),
    );
});

How to use @babel/core.isVariableDeclaration:

149
150
151
152
153
154
155
156
157
158
var localModule = localModules[path.node.name];

if (localModule) {
    if (grantParent) {
        if (t.isImportDeclaration(grantParent.node)
            || t.isVariableDeclaration(grantParent.node)
            || t.isVariableDeclarator(grantParent.node)
        ) {
            matchedNode = path.parentPath.node;
        } else if (

How to use @babel/core.DEFAULT_EXTENSIONS:

125
126
127
128
129
130
131
132
133
134
const only = program.only;
const ignore = program.ignore;
const configFile = program.configFile ? path.resolve(cwd, program.configFile) : undefined;
const rootMode = program.rootMode;
// We always transpile the default babel extensions. The option only adds more.
const transpileExtensions = babel.DEFAULT_EXTENSIONS.concat(program.extensions.map((ext) => ext.trim()));
const debug = Boolean(program.debug || program.debugBrk || program.inspect || program.inspectBrk)
const restartTimeout = Number.isFinite(program.restartTimeout) ? program.restartTimeout : 2000;

const mainModule = program.args[0];

How to use @babel/core.isJSXIdentifier:

37
38
39
40
41
42
43
44
45
46
  }
},

JSXOpeningElement(node) {
  if (
    t.isJSXIdentifier(node.name) === false ||
    node.name.name !== 'DataTable'
  ) {
    return;
  }

How to use @babel/core.importDeclaration:

86
87
88
89
90
91
92
93
94
95
if (!ret) {
    ignoreMap[local] = true;
    return t.importDeclaration([selfNode], t.stringLiteral(moduleName));
}

return t.importDeclaration(
    // [t.importSpecifier(t.identifier(local), t.identifier(imported))],
    [
        !ret.ref || ret.ref === 'default'
            ? t.ImportDefaultSpecifier(t.identifier(local))

How to use @babel/core.isVariableDeclarator:

150
151
152
153
154
155
156
157
158
159

if (localModule) {
    if (grantParent) {
        if (t.isImportDeclaration(grantParent.node)
            || t.isVariableDeclaration(grantParent.node)
            || t.isVariableDeclarator(grantParent.node)
        ) {
            matchedNode = path.parentPath.node;
        } else if (
            t.isObjectPattern(grantParent.node)

How to use @babel/core.isObjectPattern:

154
155
156
157
158
159
160
161
162
163
    || t.isVariableDeclaration(grantParent.node)
    || t.isVariableDeclarator(grantParent.node)
) {
    matchedNode = path.parentPath.node;
} else if (
    t.isObjectPattern(grantParent.node)
    && t.isVariableDeclarator(grantParent.parentPath.node)
) {
    matchedNode = grantParent.parentPath.node;
}

How to use @babel/core.transformFromAst:

40
41
42
43
44
45
46
47
48
49
        //保存所依赖的模块
        dependencies[node.source.value] = newFile
    }
})
//通过@babel/core和@babel/preset-env进行代码的转换
const {code} = babel.transformFromAst(ast, null, {
    presets: ["@babel/preset-env"]
})
return{
    filename,//该文件名

How to use @babel/core.loadOptions:

38
39
40
41
42
43
44
45
46
47
48
  }
  return ''
}


function getBabelConfig () {
  const { plugins = [] } = babel.loadOptions()
  const partialConfig = babel.loadPartialConfig()
  const targets = getTargetByConfig(partialConfig) || browserslist.loadConfig({ path: process.cwd() })
  const { list = [] } = coreJsCompat({ targets })
  return {

How to use @babel/core.stringLiteral:

135
136
137
138
139
140
141
142
143
144

switch (specifierType) {
    case $.default:
        return domain.declaration(
            [domain.defaultSpecifier(localName, exportedName)],
            types.stringLiteral(path)
        );

    case $.namespace:
        return domain.declaration(

How to use @babel/core.loadPartialConfigAsync:

43
44
45
46
47
48
49
50
51
52
let partialConfig;
if (path.basename(configFile) === "package.json") {
	// for the package.json, we need to extract the babel config
	partialConfig = require(configFile);
	if (partialConfig.babel) {
		partialConfig = await babel.loadPartialConfigAsync(
			Object.assign({ filename: "src/dummy.js" }, partialConfig.babel)
		);
	} else {
		partialConfig = configFile = undefined;