How to use babel-core

Comprehensive babel-core code examples:

How to use babel-core.transformFileAsync:

135
136
137
138
139
140
141
142
143
144
145
    })
  })
}


function compileJsAndReplace(filePath){
   babel.transformFileAsync(filePath, {
      babelrc: false,
      presets: [
        ['env', {
          'modules': 'umd',

How to use babel-core.Pipeline:

How to use babel-core.types:

58
59
60
61
62
63
64
65
66
67
```
此时,我们为插件创建一个文件,内容为导出一个默认的函数:
```js
// moriscript.js
module.exports = function(babel) {
  var t = babel.types;
  return {
    visitor: {
    }
  };

How to use babel-core.transformFile:

2
3
4
5
6
7
8
9
10
11
12
const babel = require('babel-core');


module.exports = (filePath, callback) => {
  // Transform the file.
  // Check process.env.NODE_ENV to see if we should create sourcemaps.
  babel.transformFile(
    filePath,
    {
      sourceMaps: process.env.NODE_ENV === 'development' ? 'inline' : false,
      comments: false,

How to use babel-core.buildExternalHelpers:

208
209
210
211
212
213
214
215
216
217
  });
}


function buildCdnAngular(dirname) {
  var s = babelCore.buildExternalHelpers(angularBabelHelpers, 'global');

  return makeTask('build-cdn-angular: ' + dirname, function() {
    return gulp.src('./build-targets/cdn-angular.js')
      .pipe(plugins.browserify({

How to use babel-core.transformFileSync:

11
12
13
14
15
16
17
18
19
20
if (os.platform() === 'win32') moduleName = moduleName.replace(/\\/gm, '/');
let content = `define("${moduleName}", `;
content += `function(require, module, exports, window,document,frames,self,location,navigator,localStorage,history,Caches,screen,alert,confirm,prompt,fetch,XMLHttpRequest,WebSocket,webkit,WeixinJSCore,Reporter,print,URL,DOMParser,upload,preview,build,showDecryptedInfo,syncMessage,checkProxy,showSystemInfo,openVendor,openToolsLog,showRequestInfo,help,showDebugInfoTable,closeDebug,showDebugInfo,__global,WeixinJSBridge){\n`;

if (projectConfig.setting.es6 && fs.statSync(sourceFile).size / 1024 < 500) {
  content += babel.transformFileSync(sourceFile, {presets: ['babel-preset-env', 'babel-preset-stage-0'].map(require.resolve)}).code;
} else {
  content += fs.readFileSync(path.resolve(sourceFile), {encoding: 'utf8'});
}
content += '\n});';

How to use babel-core.OptionManager:

97
98
99
100
101
102
103
104
105
106
107
108
109
110
}


function compile(filename) {
  var result = void 0;


  var opts = new _babelCore.OptionManager().init((0, _extend2.default)({ sourceRoot: _path2.default.dirname(filename) }, (0, _cloneDeep2.default)(transformOpts), { filename: filename }));


  var cacheKey = (0, _stringify2.default)(opts) + ":" + babel.version;


  var env = process.env.BABEL_ENV || process.env.NODE_ENV;

How to use babel-core.transformFromAst:

18
19
20
21
22
23
24
25
26
27
    dependencies.push(node.source.value);
  },
});

const id = ID++;
const {code} = transformFromAst(ast, null, {
  presets: ['env'],
});

const customCode = loader(filename, code)

How to use babel-core.util:

133
134
135
136
137
138
139
140
141
142
143
144


function shouldIgnore(filename) {
  if (!ignore && !only) {
    return getRelativePath(filename).split(_path2.default.sep).indexOf("node_modules") >= 0;
  } else {
    return _babelCore.util.shouldIgnore(filename, ignore || [], only);
  }
}


function loader(m, filename) {

How to use babel-core.transform:

27
28
29
30
31
32
33
34
35
36
        vars += "var " + decl[2] + ";";
        if (decl[1] === "function") cmd = decl[2] + "=function " + decl[2] + decl[3];
        else cmd = decl[2] + decl[3];
}
var babelOptions = util.babelOptions(options);
var source = vars + babel.transform("(function(_) {" + cmd + "})(__callback);", babelOptions).code;

context.__filename = filename;
// cannot assign context.__ directly in callback - need to investigate why
context.__private = context.__private || {};