Re-use babel transformations
Summary: SORRY FOR BIG DIFF, but it's really hard to split it as all these changes are cross-dependent and should be made at once: 1. Moved transformations to separate package "flipper-babel-transformer" and linked it using yarn workspaces to "static" and "pkg" packages where they are re-used. Removed double copies of transformations we had before int these two packages. 2. Converted transformations to typescript 3. Refactored transformations to avoid relying on file system paths for customisation (FB stubs and Electron stubs for headless build) 4. As babel transformations must be built before other builds - enabled incremental build for them and changed scripts to invoke the transformations build before other build scripts 5. As we need to deploy all the dependencies including the fresh "flipper-babel-transformer" as a part of "static" - implemented script which copies package with all the dependencies taking in account yarn workspaces (hoisting and symlinks) Reviewed By: passy, mweststrate Differential Revision: D20690662 fbshipit-source-id: 38a275b60d3c91e01ec21d1dbd72d03c05cfac0b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
07a6a3b87d
commit
c1bb656a0d
@@ -12,17 +12,6 @@
|
||||
},
|
||||
"bugs": "https://github.com/facebook/flipper/issues",
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.9.0",
|
||||
"@babel/generator": "^7.9.3",
|
||||
"@babel/parser": "^7.9.3",
|
||||
"@babel/plugin-proposal-class-properties": "^7.8.3",
|
||||
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
|
||||
"@babel/plugin-proposal-object-rest-spread": "^7.9.0",
|
||||
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
|
||||
"@babel/plugin-transform-flow-strip-types": "^7.9.0",
|
||||
"@babel/plugin-transform-modules-commonjs": "^7.9.0",
|
||||
"@babel/plugin-transform-typescript": "^7.9.0",
|
||||
"@babel/preset-react": "^7.9.1",
|
||||
"@oclif/command": "^1",
|
||||
"@oclif/config": "^1",
|
||||
"@oclif/plugin-help": "^2",
|
||||
@@ -31,24 +20,26 @@
|
||||
"@types/node": "^13.7.5",
|
||||
"cli-ux": "^5.4.5",
|
||||
"fs-extra": "^8.1.0",
|
||||
"metro": "^0.58.0",
|
||||
"flipper-babel-transformer": "0.2.0",
|
||||
"inquirer": "^7.0.5",
|
||||
"metro": "^0.58.0",
|
||||
"tslib": "^1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@oclif/dev-cli": "^1",
|
||||
"@types/jest": "^24.0.21",
|
||||
"@types/jest": "25.1.4",
|
||||
"globby": "^10",
|
||||
"jest": "^24.9.0",
|
||||
"jest": "^25.1.0",
|
||||
"prettier": "^2.0.0",
|
||||
"ts-jest": "^24.1.0",
|
||||
"ts-jest": "^25.2.1",
|
||||
"ts-node": "^8",
|
||||
"typescript": "^3.7.2"
|
||||
},
|
||||
"scripts": {
|
||||
"prebuild": "cd ../babel-transformer && yarn build",
|
||||
"build": "tsc -b",
|
||||
"postpack": "rm -f oclif.manifest.json",
|
||||
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
|
||||
"prepack": "rm -rf lib && yarn build && oclif-dev manifest && oclif-dev readme",
|
||||
"prepare": "yarn run build",
|
||||
"prepublishOnly": "yarn test && yarn run lint",
|
||||
"preversion": "yarn run lint",
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const {parse} = require('@babel/parser');
|
||||
const {transformFromAstSync} = require('@babel/core');
|
||||
const {default: generate} = require('@babel/generator');
|
||||
|
||||
const flipperRequires = require('../flipper-requires');
|
||||
|
||||
const babelOptions = {
|
||||
ast: true,
|
||||
plugins: [flipperRequires],
|
||||
filename: 'index.js',
|
||||
};
|
||||
|
||||
test('transform react requires to global object', () => {
|
||||
const src = 'require("react")';
|
||||
const ast = parse(src);
|
||||
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
|
||||
const {code} = generate(transformed);
|
||||
expect(code).toBe('global.React;');
|
||||
});
|
||||
|
||||
test('transform react-dom requires to global object', () => {
|
||||
const src = 'require("react-dom")';
|
||||
const ast = parse(src);
|
||||
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
|
||||
const {code} = generate(transformed);
|
||||
expect(code).toBe('global.ReactDOM;');
|
||||
});
|
||||
|
||||
test('transform flipper requires to global object', () => {
|
||||
const src = 'require("flipper")';
|
||||
const ast = parse(src);
|
||||
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
|
||||
const {code} = generate(transformed);
|
||||
expect(code).toBe('global.Flipper;');
|
||||
});
|
||||
|
||||
test('transform React identifier to global.React', () => {
|
||||
const src = 'React;';
|
||||
const ast = parse(src);
|
||||
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
|
||||
const {code} = generate(transformed);
|
||||
expect(code).toBe('global.React;');
|
||||
});
|
||||
|
||||
test.skip('throw error when requiring outside the plugin', () => {
|
||||
const src = 'require("../test.js")';
|
||||
const ast = parse(src);
|
||||
expect(() => {
|
||||
transformFromAstSync(ast, src, babelOptions);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('allow requiring from parent folder as long as we stay in plugin folder', () => {
|
||||
const src = 'require("../test.js")';
|
||||
const ast = parse(src);
|
||||
const transformed = transformFromAstSync(ast, src, {
|
||||
...babelOptions,
|
||||
root: '/path/to/plugin',
|
||||
filename: '/path/to/plugin/subfolder/index.js',
|
||||
}).ast;
|
||||
const {code} = generate(transformed);
|
||||
expect(code).toBe('require("../test.js");');
|
||||
});
|
||||
@@ -1,38 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
function isDynamicRequire(node) {
|
||||
return (
|
||||
node.type === 'CallExpression' &&
|
||||
node.callee.type === 'Identifier' &&
|
||||
node.callee.name === 'require' &&
|
||||
(node.arguments.length !== 1 || node.arguments[0].type !== 'StringLiteral')
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = function (babel, state) {
|
||||
const t = babel.types;
|
||||
|
||||
return {
|
||||
name: 'replace-dynamic-requires',
|
||||
visitor: {
|
||||
CallExpression(path) {
|
||||
if (!isDynamicRequire(path.node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
path.replaceWith(
|
||||
t.identifier(
|
||||
'triggerDynamicRequireError_' + state.file.opts.filename,
|
||||
),
|
||||
);
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,94 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const BUILTINS = [
|
||||
'electron',
|
||||
'buffer',
|
||||
'child_process',
|
||||
'crypto',
|
||||
'dgram',
|
||||
'dns',
|
||||
'fs',
|
||||
'http',
|
||||
'https',
|
||||
'net',
|
||||
'os',
|
||||
'readline',
|
||||
'stream',
|
||||
'string_decoder',
|
||||
'tls',
|
||||
'tty',
|
||||
'zlib',
|
||||
'constants',
|
||||
'events',
|
||||
'url',
|
||||
'assert',
|
||||
'util',
|
||||
'path',
|
||||
'perf_hooks',
|
||||
'punycode',
|
||||
'querystring',
|
||||
'cluster',
|
||||
'console',
|
||||
'module',
|
||||
'process',
|
||||
'vm',
|
||||
'domain',
|
||||
'v8',
|
||||
'repl',
|
||||
'timers',
|
||||
'node-fetch',
|
||||
];
|
||||
|
||||
const IGNORED_MODULES = [
|
||||
'bufferutil',
|
||||
'utf-8-validate',
|
||||
'spawn-sync',
|
||||
'./src/logcat',
|
||||
'./src/monkey',
|
||||
'./src/adb',
|
||||
];
|
||||
|
||||
function isRequire(node) {
|
||||
return (
|
||||
node.type === 'CallExpression' &&
|
||||
node.callee.type === 'Identifier' &&
|
||||
node.callee.name === 'require' &&
|
||||
node.arguments.length === 1 &&
|
||||
node.arguments[0].type === 'StringLiteral'
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = function (babel) {
|
||||
const t = babel.types;
|
||||
|
||||
return {
|
||||
name: 'infinity-import-react',
|
||||
visitor: {
|
||||
CallExpression(path) {
|
||||
if (!isRequire(path.node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const source = path.node.arguments[0].value;
|
||||
|
||||
if (
|
||||
BUILTINS.includes(source) ||
|
||||
BUILTINS.some((moduleName) => source.startsWith(`${moduleName}/`))
|
||||
) {
|
||||
path.node.callee.name = 'electronRequire';
|
||||
}
|
||||
|
||||
if (IGNORED_MODULES.includes(source)) {
|
||||
path.replaceWith(t.identifier('triggerReferenceError'));
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,35 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const babylon = require('@babel/parser');
|
||||
const fs = require('fs');
|
||||
|
||||
const electronStubs = babylon.parseExpression(
|
||||
fs.readFileSync('static/electron-stubs.notjs').toString(),
|
||||
);
|
||||
|
||||
module.exports = function (babel) {
|
||||
return {
|
||||
name: 'replace-electron-requires-with-stubs',
|
||||
visitor: {
|
||||
CallExpression(path) {
|
||||
if (
|
||||
path.node.type === 'CallExpression' &&
|
||||
path.node.callee.type === 'Identifier' &&
|
||||
path.node.callee.name === 'require' &&
|
||||
path.node.arguments.length > 0
|
||||
) {
|
||||
if (path.node.arguments[0].value === 'electron') {
|
||||
path.replaceWith(electronStubs);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,51 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const replaceFBStubs = fs.existsSync(
|
||||
path.join(__dirname, '..', '..', 'app', 'src', 'fb'),
|
||||
);
|
||||
const isFBFile = (filePath) => filePath.includes(`${path.sep}fb${path.sep}`);
|
||||
|
||||
const requireFromFolder = (folder, path) =>
|
||||
new RegExp(folder + '/[A-Za-z0-9.-_]+(.js)?$', 'g').test(path);
|
||||
|
||||
module.exports = function (babel) {
|
||||
return {
|
||||
name: 'replace-dynamic-requires',
|
||||
visitor: {
|
||||
CallExpression(path, state) {
|
||||
if (
|
||||
replaceFBStubs &&
|
||||
path.node.type === 'CallExpression' &&
|
||||
path.node.callee.type === 'Identifier' &&
|
||||
path.node.callee.name === 'require' &&
|
||||
path.node.arguments.length > 0
|
||||
) {
|
||||
if (
|
||||
requireFromFolder('fb', path.node.arguments[0].value) &&
|
||||
!isFBFile(state.file.opts.filename)
|
||||
) {
|
||||
throw new Error(
|
||||
'For files which are not under fb/ do not require directly from fb/, but rather from fb-stubs/ to not break flow-typing and make sure stubs are up-to-date.',
|
||||
);
|
||||
} else if (
|
||||
requireFromFolder('fb-stubs', path.node.arguments[0].value)
|
||||
) {
|
||||
path.node.arguments[0].value = path.node.arguments[0].value.replace(
|
||||
'/fb-stubs/',
|
||||
'/fb/',
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,81 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const {resolve, dirname} = require('path');
|
||||
|
||||
// do not apply this transform for these paths
|
||||
const EXCLUDE_PATHS = [
|
||||
'/node_modules/react-devtools-core/',
|
||||
'relay-devtools/DevtoolsUI',
|
||||
];
|
||||
|
||||
function isExcludedPath(path) {
|
||||
for (const epath of EXCLUDE_PATHS) {
|
||||
if (path.indexOf(epath) > -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} // $FlowFixMe
|
||||
module.exports = ({types: t}) => ({
|
||||
visitor: {
|
||||
// $FlowFixMe
|
||||
CallExpression(path, state) {
|
||||
if (isExcludedPath(state.file.opts.filename)) {
|
||||
return;
|
||||
}
|
||||
const node = path.node;
|
||||
const args = node.arguments || [];
|
||||
|
||||
if (
|
||||
node.callee.name === 'require' &&
|
||||
args.length === 1 &&
|
||||
t.isStringLiteral(args[0])
|
||||
) {
|
||||
if (args[0].value === 'flipper') {
|
||||
path.replaceWith(t.identifier('global.Flipper'));
|
||||
} else if (args[0].value === 'react') {
|
||||
path.replaceWith(t.identifier('global.React'));
|
||||
} else if (args[0].value === 'react-dom') {
|
||||
path.replaceWith(t.identifier('global.ReactDOM'));
|
||||
} else if (args[0].value === 'adbkit') {
|
||||
path.replaceWith(t.identifier('global.adbkit'));
|
||||
} else if (
|
||||
// require a file not a pacakge
|
||||
args[0].value.indexOf('/') > -1 &&
|
||||
// in the plugin itself and not inside one of its dependencies
|
||||
state.file.opts.filename.indexOf('node_modules') === -1 &&
|
||||
// the resolved path for this file is outside the plugins root
|
||||
!resolve(dirname(state.file.opts.filename), args[0].value).startsWith(
|
||||
state.file.opts.root,
|
||||
) &&
|
||||
!resolve(dirname(state.file.opts.filename), args[0].value).indexOf(
|
||||
'/static/',
|
||||
) < 0
|
||||
) {
|
||||
throw new Error(
|
||||
`Plugins cannot require files from outside their folder. Attempted to require ${resolve(
|
||||
dirname(state.file.opts.filename),
|
||||
args[0].value,
|
||||
)} which isn't inside ${state.file.opts.root}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
Identifier(path, state) {
|
||||
if (
|
||||
path.node.name === 'React' &&
|
||||
path.parentPath.node.id !== path.node &&
|
||||
!isExcludedPath(state.file.opts.filename)
|
||||
) {
|
||||
path.replaceWith(t.identifier('global.React'));
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -1,124 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const generate = require('@babel/generator').default;
|
||||
const babylon = require('@babel/parser');
|
||||
const babel = require('@babel/core');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
function transform({filename, options, src}) {
|
||||
const isTypeScript = filename.endsWith('.tsx') || filename.endsWith('.ts');
|
||||
const presets = [require('@babel/preset-react')];
|
||||
|
||||
const ast = babylon.parse(src, {
|
||||
filename,
|
||||
plugins: isTypeScript
|
||||
? [
|
||||
'jsx',
|
||||
'typescript',
|
||||
'classProperties',
|
||||
'optionalChaining',
|
||||
'nullishCoalescingOperator',
|
||||
]
|
||||
: [
|
||||
'jsx',
|
||||
['flow', {all: true}],
|
||||
'classProperties',
|
||||
'objectRestSpread',
|
||||
'optionalChaining',
|
||||
'nullishCoalescingOperator',
|
||||
],
|
||||
sourceType: 'module',
|
||||
});
|
||||
|
||||
// run babel
|
||||
const plugins = [];
|
||||
|
||||
if (!isTypeScript) {
|
||||
plugins.push(
|
||||
require('@babel/plugin-transform-modules-commonjs'),
|
||||
require('@babel/plugin-proposal-object-rest-spread'),
|
||||
require('@babel/plugin-proposal-class-properties'),
|
||||
require('@babel/plugin-transform-flow-strip-types'),
|
||||
require('@babel/plugin-proposal-optional-chaining'),
|
||||
require('@babel/plugin-proposal-nullish-coalescing-operator'),
|
||||
require('./dynamic-requires.js'),
|
||||
);
|
||||
} else {
|
||||
plugins.push(
|
||||
require('@babel/plugin-transform-typescript'),
|
||||
require('@babel/plugin-proposal-class-properties'),
|
||||
require('@babel/plugin-transform-modules-commonjs'),
|
||||
require('@babel/plugin-proposal-optional-chaining'),
|
||||
require('@babel/plugin-proposal-nullish-coalescing-operator'),
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
fs.existsSync(
|
||||
path.resolve(path.dirname(path.dirname(__dirname)), 'app', 'src', 'fb'),
|
||||
)
|
||||
) {
|
||||
plugins.push(require('./fb-stubs.js'));
|
||||
}
|
||||
if (!options.isTestRunner) {
|
||||
// Replacing require statements with electronRequire to prevent metro from
|
||||
// resolving them. electronRequire are resolved during runtime by electron.
|
||||
// As the tests are not bundled by metro and run in @jest-runner/electron,
|
||||
// electron imports are working out of the box.
|
||||
plugins.push(require('./electron-requires'));
|
||||
}
|
||||
plugins.push(require('./flipper-requires.js'));
|
||||
const transformed = babel.transformFromAst(ast, src, {
|
||||
ast: true,
|
||||
babelrc: !filename.includes('node_modules'),
|
||||
code: false,
|
||||
comments: false,
|
||||
compact: false,
|
||||
root: options.projectRoot,
|
||||
filename,
|
||||
plugins,
|
||||
presets,
|
||||
sourceMaps: true,
|
||||
retainLines: !!options.isTestRunner,
|
||||
});
|
||||
const result = generate(
|
||||
transformed.ast,
|
||||
{
|
||||
filename,
|
||||
sourceFileName: filename,
|
||||
sourceMaps: true,
|
||||
retainLines: !!options.isTestRunner,
|
||||
},
|
||||
src,
|
||||
);
|
||||
return {
|
||||
ast: transformed.ast,
|
||||
code: result.code,
|
||||
filename,
|
||||
map: result.map,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
transform,
|
||||
// Disable caching of babel transforms all together. We haven't found a good
|
||||
// way to cache our transforms, as they rely on side effects like env vars or
|
||||
// the existence of folders in the file system.
|
||||
getCacheKey: () => Math.random().toString(36),
|
||||
process(src, filename, config, options) {
|
||||
return transform({
|
||||
src,
|
||||
filename,
|
||||
config,
|
||||
options: {...options, isTestRunner: true},
|
||||
});
|
||||
},
|
||||
};
|
||||
@@ -52,12 +52,7 @@ export default async function runBuild(
|
||||
createModuleIdFactory,
|
||||
},
|
||||
transformer: {
|
||||
babelTransformerPath: path.resolve(
|
||||
__dirname,
|
||||
'..',
|
||||
'transforms',
|
||||
'index.js',
|
||||
),
|
||||
babelTransformerPath: require.resolve('flipper-babel-transformer'),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user