Add babel bridge to switch easily between 6 and 7

Summary:
This bridge pulls in the various babel things and exports them again.

In a future update this bridge will also pull in babel 7 versions of the same thing and an env var will determine which sources to use so you can switch between using babel 6 or 7 through ENV.

Reviewed By: davidaurelio

Differential Revision: D6844862

fbshipit-source-id: 610a60eaf7bf368eddcd015e37f228651e2fd78b
This commit is contained in:
Peter van der Zee 2018-01-30 07:30:29 -08:00 committed by Facebook Github Bot
parent f9dc29afbc
commit 448c85014c
22 changed files with 95 additions and 40 deletions

View File

@ -12,7 +12,7 @@
'use strict';
const babelGenerate = require('babel-generator').default;
const {babelGenerate} = require('../../babel-bridge');
const {
generateAssetCodeFileAst,
generateRemoteAssetCodeFileAst,

View File

@ -12,8 +12,8 @@
'use strict';
const babel = require('babel-core');
const babylon = require('babylon');
const {babelCore: babel} = require('../babel-bridge');
const {babylon} = require('../babel-bridge');
import type {AssetDataWithoutFiles} from '../Assets';
import type {ModuleTransportLike} from '../shared/types.flow';

View File

@ -12,9 +12,10 @@
*/
'use strict';
const babel = require('babel-core');
const constantFolding = require('../constant-folding');
const {babelCore: babel} = require('../../../babel-bridge');
function parse(code) {
return babel.transform(code, {code: false, babelrc: false, compact: true});
}

View File

@ -13,7 +13,9 @@
const inline = require('../inline');
const {transform, transformFromAst} = require('babel-core');
const {
babelCore: {transform, transformFromAst},
} = require('../../../babel-bridge');
const babelOptions = {
babelrc: false,

View File

@ -12,7 +12,7 @@
'use strict';
const babel = require('babel-core');
const {babelCore: babel} = require('../../babel-bridge');
import type {Ast, BabelSourceMap} from 'babel-core';

View File

@ -15,24 +15,24 @@
const JsFileWrapping = require('../../ModuleGraph/worker/JsFileWrapping');
const assetTransformer = require('../../assetTransformer');
const babylon = require('babylon');
const collectDependencies = require('../../ModuleGraph/worker/collectDependencies');
const constantFolding = require('./constant-folding');
const generate = require('babel-generator').default;
const inline = require('./inline');
const minify = require('./minify');
const optimizeDependencies = require('../../ModuleGraph/worker/optimizeDependencies');
const path = require('path');
const {babylon} = require('../../babel-bridge');
const {babelGenerate: generate} = require('../../babel-bridge');
const {toSegmentTuple} = require('metro-source-map');
import type {LogEntry} from 'metro-core/src/Logger';
import type {BabelSourceMap} from 'babel-core';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
import type {DynamicRequiresBehavior} from '../../ModuleGraph/worker/collectDependencies';
import type {LocalPath} from '../../node-haste/lib/toLocalPath';
import type {ResultWithMap} from './minify';
import type {Ast, Plugins as BabelPlugins} from 'babel-core';
import type {DynamicRequiresBehavior} from '../../ModuleGraph/worker/collectDependencies';
import type {BabelSourceMap} from 'babel-core';
import type {LogEntry} from 'metro-core/src/Logger';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
export type TransformedCode = {
code: string,

View File

@ -12,7 +12,7 @@
'use strict';
const babel = require('babel-core');
const {babelCore: babel} = require('../../babel-bridge');
const t = babel.types;
const importMap = new Map([['ReactNative', 'react-native']]);

View File

@ -12,10 +12,11 @@
'use strict';
const babel = require('babel-core');
const inlinePlatform = require('./inline-platform');
const invariant = require('fbjs/lib/invariant');
const {babelCore: babel} = require('../../babel-bridge');
import type {Ast} from 'babel-core';
import type {BabelSourceMap} from 'babel-core';
const t = babel.types;

View File

@ -10,9 +10,10 @@
*/
'use strict';
const generate = require('babel-generator').default;
const stub = require('sinon/lib/sinon/stub');
const {babelGenerate: generate} = require('../babel-bridge');
exports.fn = () => {
const s = stub();
const f = jest.fn(s);

View File

@ -14,7 +14,7 @@
/* eslint-disable no-unclear-flowtypes */
const babel = require('babel-core');
const {babelCore: babel} = require('../../babel-bridge');
const MODULE_FACTORY_PARAMETERS = ['global', 'require', 'module', 'exports'];
const POLYFILL_FACTORY_PARAMETERS = ['global'];

View File

@ -13,9 +13,9 @@
'use strict';
const babylon = require('babylon');
const collectDependencies = require('../collectDependencies');
const {babylon} = require('../../../babel-bridge');
const {codeFromAst, comparableCode} = require('../../test-helpers');
const {any} = expect;

View File

@ -13,8 +13,9 @@
'use strict';
const babylon = require('babylon');
const optimizeDependencies = require('../optimizeDependencies');
const {babylon} = require('../../../babel-bridge');
const {InvalidRequireCallError} = optimizeDependencies;
const {codeFromAst, comparableCode} = require('../../test-helpers');

View File

@ -15,17 +15,19 @@
const invariant = require('fbjs/lib/invariant');
const nullthrows = require('fbjs/lib/nullthrows');
const t = require('babel-types');
const transformModule = require('../transform-module');
const {babelTypes: types} = require('../../../babel-bridge');
const {babylon: {parse}} = require('../../../babel-bridge');
const {babelGenerate: generate} = require('../../../babel-bridge');
const {babelTraverse: traverse} = require('../../../babel-bridge');
const {fn} = require('../../test-helpers');
const {parse} = require('babylon');
const {SourceMapConsumer} = require('source-map');
const generate = require('babel-generator').default;
const {traverse} = require('babel-core');
import type {TransformVariants} from '../../types.flow';
const t = types;
jest.mock('image-size', () => buffer => {
return JSON.parse(buffer.toString('utf8')).__size;
});

View File

@ -12,11 +12,12 @@
'use strict';
const babelTemplate = require('babel-template');
const nullthrows = require('fbjs/lib/nullthrows');
const {traverse, types} = require('babel-core');
const prettyPrint = require('babel-generator').default;
const {babelTemplate} = require('../../babel-bridge');
const {babelTraverse: traverse} = require('../../babel-bridge');
const {babelTypes: types} = require('../../babel-bridge');
const {babelGenerate: generate} = require('../../babel-bridge');
import type {TransformResultDependency} from '../types.flow';
import type {Ast} from 'babel-core';
@ -189,7 +190,7 @@ const makeAsyncRequire = babelTemplate(
);
function invalidRequireOf(type, node) {
const str = prettyPrint(node).code;
const str = generate(node).code;
return new InvalidRequireCallError(type, str, node.loc.start);
}

View File

@ -14,7 +14,7 @@
/* eslint-disable no-unclear-flowtypes */
const babelGenerate = require('babel-generator').default;
const {babelGenerate} = require('../../babel-bridge');
function generate(
ast: Object,

View File

@ -12,7 +12,7 @@
'use strict';
const babel = require('babel-core');
const {babelCore: babel} = require('../../babel-bridge');
const constantFolding = require('../../JSTransformer/worker/constant-folding')
.plugin;
const generate = require('./generate');

View File

@ -12,8 +12,8 @@
'use strict';
const {traverse} = require('babel-core');
const prettyPrint = require('babel-generator').default;
const {babelTraverse: traverse} = require('../../babel-bridge');
const {babelGenerate} = require('../../babel-bridge');
import type {TransformResultDependency} from '../types.flow';
import type {Ast} from 'babel-core';
@ -67,7 +67,7 @@ function processRequireCall(node) {
throw new InvalidRequireCallError(
'Post-transform calls to require() expect 2 arguments, the first ' +
'of which has the shape `_dependencyMapName[123]`, ' +
`but this was found: \`${prettyPrint(node).code}\``,
`but this was found: \`${babelGenerate(node).code}\``,
);
}
node.arguments = [node.arguments[0]];

View File

@ -0,0 +1,45 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+javascript_foundation
* @flow (won't like this)
* @format
*/
'use strict';
// This is a temporary migration bridge to switch between babel 6 and 7
const babelCore6 = require('babel-core');
const babelGenerate6 = require('babel-generator').default;
const babelTemplate6 = require('babel-template');
const babelTraverse6 = require('babel-core').traverse;
const babelTypes6 = require('babel-core').types;
const babylon6 = require('babylon');
const externalHelpersPlugin6 = require('babel-plugin-external-helpers');
const inlineRequiresPlugin6 = require('babel-preset-fbjs/plugins/inline-requires');
const makeHMRConfig6 = require('babel-preset-react-native/configs/hmr');
const resolvePlugins6 = require('babel-preset-react-native/lib/resolvePlugins');
module.exports = {
// TODO: `babelGenerate: process.env.BABEL_VER === 7 ? babelGenerate7 : babelGenerate6,` etc
version: process.env.BABEL_VER === '7' ? 7 : 6,
babelCore: babelCore6,
babelGenerate: babelGenerate6,
babelTemplate: babelTemplate6,
babelTraverse: babelTraverse6,
babelTypes: babelTypes6,
babylon: babylon6,
externalHelpersPlugin: externalHelpersPlugin6,
inlineRequiresPlugin: inlineRequiresPlugin6,
makeHMRConfig: makeHMRConfig6,
resolvePlugins: resolvePlugins6,
};

View File

@ -11,7 +11,7 @@
*/
'use strict';
const babel = require('babel-core');
const {babelCore: babel} = require('./babel-bridge');
import type {TransformOptions} from './JSTransformer/worker';
import type {Plugins as BabelPlugins} from 'babel-core';

View File

@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`getTransformCacheKeyFn Should return always the same key for the same params 1`] = `"1b825aa92d266cc01c946596e777463bd7e68c82"`;
exports[`getTransformCacheKeyFn Should return always the same key for the same params 1`] = `"dbf93a9e80558a6a2a7d3deb2f79c523dca3c7ce"`;

View File

@ -14,7 +14,7 @@
'use strict';
const babel = require('babel-core');
const {babelCore: babel} = require('../../../babel-bridge');
const babelConfig = require('../../../babelRegisterOnly').config;
const fs = require('fs');

View File

@ -13,18 +13,19 @@
*/
'use strict';
const babel = require('babel-core');
const crypto = require('crypto');
const externalHelpersPlugin = require('babel-plugin-external-helpers');
const fs = require('fs');
const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires');
const json5 = require('json5');
const makeHMRConfig = require('babel-preset-react-native/configs/hmr');
const path = require('path');
const resolvePlugins = require('babel-preset-react-native/lib/resolvePlugins');
import type {Plugins as BabelPlugins} from 'babel-core';
const {babelCore: babel} = require('./babel-bridge');
const {externalHelpersPlugin} = require('./babel-bridge');
const {inlineRequiresPlugin} = require('./babel-bridge');
const {makeHMRConfig} = require('./babel-bridge');
const {resolvePlugins} = require('./babel-bridge');
import type {Transformer, TransformOptions} from './JSTransformer/worker';
import type {Plugins as BabelPlugins} from 'babel-core';
const cacheKeyParts = [
fs.readFileSync(__filename),