How to use the transform function from babel-core

Find comprehensive JavaScript babel-core.transform code examples handpicked from public code repositorys.

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 || {};
fork icon59
star icon958
watch icon40

+ 3 other calls in file

109
110
111
112
113
114
115
116
117
118

``` javascript
var babel = require('babel-core');
require('babel-plugin-streamline');

{code, map} = babel.transform(code, {
        plugins: [streamline],
        // more babel options
        extra: {
                streamline: {
fork icon59
star icon958
watch icon40

217
218
219
220
221
222
223
224
225
226
const babel = require('babel-core')
module.exports = function(source) {
  const babelOptions = {
    presets: ['env'],
  }
  return babel.transform(source, babelOptions).code
}
```

##### vue-loader 源码简析
fork icon50
star icon253
watch icon9

19
20
21
22
23
24
25
26
27
28
const files = await fs.read({ pattern });

return Promise.all(
  files
    .map(({ filename, content }) => {
      const { code, map } = transform(content, {
        ...defaultOptions,
        ...options,
        filename,
      });
fork icon10
star icon59
watch icon43

+ 3 other calls in file

42
43
44
45
46
47
48
49
50
51
.forEach(file => {
  const source = compilation.assets[file].source();
  const minified = source.split(/\r\n|\r|\n/).length < 25;
  const miniSize = minified
    ? false
    : babel.transform(source, { compact: true }).code.length;
  const obj = {
    name: file,
    minified
  };
fork icon108
star icon0
watch icon42

+ 5 other calls in file

18
19
20
21
22
23
24
25
26
27
const { transform } = require('babel-core');

find('src/**/*.js')
  .map(read())
  .map(async file => {
    const { code } = await transform(file.contents);

    file.contents = code;

    return file;
fork icon3
star icon68
watch icon2

+ 3 other calls in file

47
48
49
50
51
52
53
54
55
56
}

// TODO: Use utf8 encoding when reading file?
var code = FS.readFileSync(fullSourcePath, { encoding: 'utf8' })

var result = BABEL.transform(code, options)

var data = !!result ? result.code : null

// Disable strict mode.
fork icon34
star icon51
watch icon11

10
11
12
13
14
15
16
17
18
19
    .replace(/\n+/g, '\n');

it('compiles carmi files to a module that exposes a function', () => {
  const testFile = resolve(__dirname, 'test.carmi.js');
  const original = fs.readFileSync(testFile);
  const {code} = babel.transform(original, {
    plugins: [plugin],
    filename: testFile
  });
  const fnLib = {sum: arr => arr.reduce((a, b) => a + b)};
fork icon17
star icon48
watch icon0

+ 11 other calls in file

79
80
81
82
83
84
85
86
87
88
fs.readFile(fileName, function(err, data) {
  if(err) throw err;
  // convert from a buffer to a string
  var src = data.toString();
  // use our plugin to transform the source
  var out = babel.transform(src, {
    plugins: [moriscript]
  });
  // print the generated code to screen
  console.log(out.code);
fork icon395
star icon47
watch icon2

109
110
111
112
113
114
115
116
117
var bloc = (node, pref = '') => node ? pref + inc() + code(node) + dec() : ''
var list = (node, pref = ',') => node ? inc() + node.map(code).join(pref + eol) + dec() : ''

module.exports = {
  // parse: (js) => babyl.parse(babel.transform(js, { presets: ["es2015"]}).code, { sourceType: 'script', plugins: ['estree']} ),
  parse: (js) => babel.transform(js, { presets: ['es2015'] }).ast,
  // build: (js) => astring.generate(module.exports.parse(js), { generator : Object.assign({}, astring.baseGenerator, brightGen) })
  build: (js) => code(module.exports.parse(js))
}
fork icon2
star icon13
watch icon5

+ 3 other calls in file

140
141
142
143
144
145
146
147
148
    param: `${key} ${type}`
  }
})
const params = _.map(signature, ({ param }) => param).join(',')
const args = _.map(signature, ({ arg }) => arg).join(',')
const es5 = babel.transform(func.toString(), evalOptions)
const code = es5.code.replace(/['"]use strict['"];/, '')

let statement = `(${code})(${args})`
fork icon24
star icon38
watch icon4

+ 3 other calls in file

82
83
84
85
86
87
88
89
90
91
  },
  FunctionDeclaration(path) {
    // do stuff here
  }
}
var result = babel.transform(code, {
  plugins: [
    babelPlugin
  ]
})
fork icon5
star icon13
watch icon2

328
329
330
331
332
333
334
335
336
337
  babelConfig = {},
  noreact
) {
  let codeAst = null;
  try {
    const { ast } = babel.transform(code, Object.assign({}, defaultBabelConfig, babelConfig));
    codeAst = ast;
   //使用babel.transform转化为AST树
  } catch(e) {
    console.error(e);
fork icon3
star icon23
watch icon2

+ 3 other calls in file

95
96
97
98
99
100
101
102
103
104
  throw new Error('You need to pass a callback function.')
}

svgo.optimize(svg, result => {
  const jsx = converter.convert(result.data)
  const { code } = transform(jsx, {
    plugins: [
      'syntax-jsx',
      [
        plugin,
fork icon2
star icon9
watch icon2

69
70
71
72
73
74
75
76
77
78
        }
      }
    }
  }

  let transformed = babel.transform(code, {
    plugins: [ plugin ]
  })
  return transformed.code
}
fork icon1
star icon8
watch icon5

15
16
17
18
19
20
21
22
23
  useBuiltIns: true,
  modules: false
};

const babelOptions = { presets: [[babelPresetEnv, presetOptions]] };
const es5TranspiledContent = babel.transform(
  fileContent,
  babelOptions
).code;
fork icon10
star icon6
watch icon18

+ 3 other calls in file

88
89
90
91
92
93
94
95
96

	return data.replace(/;$/, "")
		.replace(/^(var|let|const|function)/, "");
}

var {code} = transform(js, {
	compact: false
});
const splitted = code.split(/\n(?=const|var|let|function|async)/).filter(e => e);
fork icon0
star icon6
watch icon1

18
19
20
21
22
23
24
25
26
27
let aggregatedMessages = [];
fileTraverser.on('file', (file) => {
  const fileExt = path.extname(file);
  if (fileExt === '.js' || fileExt === '.jsx') {
    const code = fs.readFileSync(file, 'utf8');
    const transformed = babel.transform(code, babelConfig);
    const messages = transformed.metadata['react-intl'].messages;
    aggregatedMessages = [...aggregatedMessages, ...messages];
  }
});
fork icon2
star icon2
watch icon7

+ 3 other calls in file

184
185
186
187
188
189
190
191
192
193
  }

  output.push(`export { default as ${name} } from './${name}';`)
})

const transpiled = babel.transform(output.join('\n'), babelrc)

fs.writeFileSync(
  path.resolve(paths.appBuild, 'remessa-lp-ds.js'),
  transpiled.code
fork icon0
star icon5
watch icon83

+ 3 other calls in file

13
14
15
16
17
18
19
20
21
22
}
else if (fileName.indexOf('node_modules/') >= 0) {
    return (origJs || require.extensions['.js'])(module, fileName);
}
var src = fs.readFileSync(fileName, 'utf8');
output = babel.transform(src, {
    filename: fileName
}).code;

return module._compile(output, fileName);
fork icon0
star icon3
watch icon1

+ 199 other calls in file