Fix build for Reactotron plugin
Summary: Fixed build for Reactotron plugin. It was broken because one of dependencies has statement "import * as React from 'react'" which transformed wrongly. Reviewed By: passy Differential Revision: D21178313 fbshipit-source-id: d700981a570dc8ded2080910e872b44976b850e8
This commit is contained in:
committed by
Facebook GitHub Bot
parent
2890306ad5
commit
1a692ccf08
@@ -50,6 +50,14 @@ test('transform React identifier to global.React', () => {
|
|||||||
expect(code).toBe('global.React;');
|
expect(code).toBe('global.React;');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('do NOT transform local React namespace import to global.React', () => {
|
||||||
|
const src = `import * as React from 'react';`;
|
||||||
|
const ast = parse(src, {sourceType: 'module'});
|
||||||
|
const transformed = transformFromAstSync(ast, src, babelOptions)!.ast;
|
||||||
|
const {code} = generate(transformed!);
|
||||||
|
expect(code).toMatchInlineSnapshot(`"import * as React from 'react';"`);
|
||||||
|
});
|
||||||
|
|
||||||
test('throw error when requiring outside the plugin', () => {
|
test('throw error when requiring outside the plugin', () => {
|
||||||
const src = 'require("../test.js")';
|
const src = 'require("../test.js")';
|
||||||
const ast = parse(src);
|
const ast = parse(src);
|
||||||
|
|||||||
@@ -31,6 +31,12 @@ function isExcludedPath(path: string) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
function isReactImportIdentifier(path: NodePath<Identifier>) {
|
||||||
|
return (
|
||||||
|
path.parentPath.node.type === 'ImportNamespaceSpecifier' &&
|
||||||
|
path.parentPath.node.local.name === 'React'
|
||||||
|
);
|
||||||
|
}
|
||||||
module.exports = () => ({
|
module.exports = () => ({
|
||||||
visitor: {
|
visitor: {
|
||||||
CallExpression(path: NodePath<CallExpression>, state: any) {
|
CallExpression(path: NodePath<CallExpression>, state: any) {
|
||||||
@@ -80,6 +86,7 @@ module.exports = () => ({
|
|||||||
if (
|
if (
|
||||||
path.node.name === 'React' &&
|
path.node.name === 'React' &&
|
||||||
(path.parentPath.node as any).id !== path.node &&
|
(path.parentPath.node as any).id !== path.node &&
|
||||||
|
!isReactImportIdentifier(path) &&
|
||||||
!isExcludedPath(state.file.opts.filename)
|
!isExcludedPath(state.file.opts.filename)
|
||||||
) {
|
) {
|
||||||
path.replaceWith(identifier('global.React'));
|
path.replaceWith(identifier('global.React'));
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ export default function transform({
|
|||||||
filename: string;
|
filename: string;
|
||||||
options: any;
|
options: any;
|
||||||
src: string;
|
src: string;
|
||||||
presets: any[];
|
presets?: any[];
|
||||||
plugins: any[];
|
plugins?: any[];
|
||||||
}) {
|
}) {
|
||||||
presets = presets ?? [require('@babel/preset-react')];
|
presets = presets ?? [require('@babel/preset-react')];
|
||||||
plugins = plugins ?? [];
|
plugins = plugins ?? [];
|
||||||
|
|||||||
Reference in New Issue
Block a user