let babelTransfrom = (plain) => { try { let matchCommonJS = (opt) => opt === 'transform-es2015-modules-commonjs'; let strict = false; if(global.Mancy.session.mode === 'Strict') { strict = true; if(_.findIndex(ReplConstants.BABEL_OPTIONS.plugins, matchCommonJS) === -1) { ReplConstants.BABEL_OPTIONS.plugins.push('transform-es2015-modules-commonjs'); } } else { _.remove(ReplConstants.BABEL_OPTIONS.plugins, matchCommonJS); } let code = babel .transform(plain, ReplConstants.BABEL_OPTIONS) .code; // transform imports added by babel scripts into require if(!strict && code.indexOf('import') !== -1) { code = code.replace(importMatcher, importToRequire); } return strict ? code.substring(USE_STRICT_LENGTH - 1) : code; } catch(e) { return e; } }
const output = transform(file.data, opts);
sourceMapTarget: file.relative }); var res = babel.transform(file.contents.toString(), fileOpts); if (file.sourceMap && res.map) { applySourceMap(file, res.map);
it('transforms "react-native-config"" to "react-native-config-node"', function () { const code = 'import abc from "react-native-config";' const expected = 'import abc from "react-native-config-node";' const actual = babel.transform(code, { plugins: [pluginPath] }).code assert(actual === expected) })
function testPlugin(code) { const result = transform(code, { presets: [["env", {"targets": {"node": "8.0"}}]], plugins: [ require.resolve('../../src/babel'), 'babel-plugin-transform-es2015-modules-commonjs', ], }) return result.code }
// transpile es6 syntax const transpileCode = source => { return transform(source, { presets: ['es2015', 'env', 'react'], plugins: ['transform-object-rest-spread'] }).code }
process(src, filename) { if (babel.util.canCompile(filename)) { return babel.transform(src, { filename, presets: [jestPreset], retainLines: true, }).code; } return src; }
const extractFromFile = async (fileName) => { try { const code = await readFile(fileName); // Use babel plugin to extract instances where react-intl is used const { metadata: result } = await transform(code, { presets, plugins }); // object-shorthand for (const message of result['react-intl'].messages) { for (const locale of locales) { const oldLocaleMapping = oldLocaleMappings[locale][message.id]; // Merge old translations into the babel extracted instances where react-intl is used const newMsg = ( locale === DEFAULT_LOCALE) ? message.defaultMessage : ''; localeMappings[locale][message.id] = (oldLocaleMapping) ? oldLocaleMapping : newMsg; } } } catch (error) { process.stderr.write(`Error transforming file: ${fileName}\n${error}`); } }
function buildContent(content, filename, destination, babelOptions = {}) { babelOptions.filename = filename; const result = transform(content, babelOptions); outputFileSync(destination, result.code, {encoding: 'utf8'}); }
it('does not transform "React-native-config"', function () { const code = 'import abc from "React-native-config";' const expected = 'import abc from "React-native-config";' const actual = babel.transform(code, { plugins: [pluginPath] }).code assert(actual === expected) })
it('does not transform "react-native-config-node"', function () { const code = 'import abc from "react-native-config-node";' const expected = 'import abc from "react-native-config-node";' const actual = babel.transform(code, { plugins: [pluginPath] }).code assert(actual === expected) })
const extractFromFile = async (fileName) => { try { const code = await readFile(fileName); // Use babel plugin to extract instances where react-intl is used const { metadata: result } = await transform(code, { presets, plugins }); // object-shorthand for (const message of result['react-intl'].messages) { for (const locale of locales) { const oldLocaleMapping = oldLocaleMappings[locale][message.id]; // Merge old translations into the babel extracted instances where react-intl is used const newMsg = ( locale === DEFAULT_LOCALE) ? message.defaultMessage : ''; localeMappings[locale][message.id] = (oldLocaleMapping) ? oldLocaleMapping : newMsg; } } } catch (error) { process.stderr.write(`Error transforming file: ${fileName}\n${error}`); } }
const extractFromFile = async (fileName) => { try { const code = await readFile(fileName); // Use babel plugin to extract instances where react-intl is used const { metadata: result } = await transform(code, { presets, plugins }); // object-shorthand for (const message of result['react-intl'].messages) { for (const locale of locales) { const oldLocaleMapping = oldLocaleMappings[locale][message.id]; // Merge old translations into the babel extracted instances where react-intl is used const newMsg = ( locale === DEFAULT_LOCALE) ? message.defaultMessage : ''; localeMappings[locale][message.id] = (oldLocaleMapping) ? oldLocaleMapping : newMsg; } } } catch (error) { process.stderr.write(`Error transforming file: ${fileName}\n${error}`); } }
const extractFromFile = async (fileName) => { try { const code = await readFile(fileName); // Use babel plugin to extract instances where react-intl is used const { metadata: result } = await transform(code, { presets, plugins }); // object-shorthand for (const message of result['react-intl'].messages) { for (const locale of locales) { const oldLocaleMapping = oldLocaleMappings[locale][message.id]; // Merge old translations into the babel extracted instances where react-intl is used const newMsg = ( locale === DEFAULT_LOCALE) ? message.defaultMessage : ''; localeMappings[locale][message.id] = (oldLocaleMapping) ? oldLocaleMapping : newMsg; } } } catch (error) { process.stderr.write(`Error transforming file: ${fileName}\n${error}`); } }
const extractFromFile = async (fileName) => { try { const code = await readFile(fileName); // Use babel plugin to extract instances where react-intl is used const { metadata: result } = await transform(code, { presets, plugins }); // object-shorthand for (const message of result['react-intl'].messages) { for (const locale of locales) { const oldLocaleMapping = oldLocaleMappings[locale][message.id]; // Merge old translations into the babel extracted instances where react-intl is used const newMsg = ( locale === DEFAULT_LOCALE) ? message.defaultMessage : ''; localeMappings[locale][message.id] = (oldLocaleMapping) ? oldLocaleMapping : newMsg; } } } catch (error) { process.stderr.write(`Error transforming file: ${fileName}\n${error}`); } }