How to use the transform function from babel-core
Find comprehensive JavaScript babel-core.transform code examples handpicked from public code repositorys.
GitHub: Sage/streamlinejs
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 || {};
59
958
40
+ 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: {
59
958
40
GitHub: fxxqq/fe-blog
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 源码简析
50
253
9
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, });
10
59
43
+ 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 };
108
0
42
+ 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;
3
68
2
+ 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.
34
51
11
GitHub: wix-incubator/carmi
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)};
17
48
0
+ 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);
395
47
2
GitHub: mattaylor/rokuJs
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)) }
2
13
5
+ 3 other calls in file
GitHub: langateam/node-plv8
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})`
24
38
4
+ 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 ] })
5
13
2
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);
3
23
2
+ 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,
2
9
2
GitHub: nomic-io/modbox
69 70 71 72 73 74 75 76 77 78
} } } } let transformed = babel.transform(code, { plugins: [ plugin ] }) return transformed.code }
1
8
5
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;
10
6
18
+ 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);
0
6
1
GitHub: IDAGIO/intl-utils
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]; } });
2
2
7
+ 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
0
5
83
+ 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);
0
3
1
+ 199 other calls in file
babel-core.transform is the most popular function in babel-core (393 examples)