Skip to content

Commit 835fcbd

Browse files
committed
Increase version and added fix for no default import
1 parent 591ce25 commit 835fcbd

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

packages/babel-plugin-optimize-react/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# babel-plugin-optimize-react-hooks
1+
# babel-plugin-optimize-react
22

33
This Babel 7 plugin optimizes React hooks by transforming common patterns into more effecient output when using with tools such as [Create React App](https://github.com/facebook/create-react-app). For example, with this plugin the following output is optimized as shown:
44

packages/babel-plugin-optimize-react/__tests__/__snapshots__/hooks-test.js.snap

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,111 @@ const {
100100
useState: foo
101101
} = React;"
102102
`;
103+
104+
exports[`React hook transforms should support destructuring hooks from imports #2 1`] = `
105+
"import React from \\"react\\";
106+
const __reactCreateElement__ = React.createElement;
107+
const {
108+
useState
109+
} = React;
110+
export function MyComponent() {
111+
const _ref_0 = useState(0);
112+
113+
const setCounter = _ref_0[1];
114+
const counter = _ref_0[0];
115+
return __reactCreateElement__(\\"div\\", null, counter);
116+
}"
117+
`;
118+
119+
exports[`React hook transforms should support destructuring hooks from imports #3 1`] = `
120+
"import React from \\"react\\";
121+
const __reactCreateElement__ = React.createElement;
122+
const useState = React.useState;
123+
export function MyComponent() {
124+
const _ref_0 = useState(0);
125+
126+
const setCounter = _ref_0[1];
127+
const counter = _ref_0[0];
128+
return __reactCreateElement__(\\"div\\", null, counter);
129+
}"
130+
`;
131+
132+
exports[`React hook transforms should support destructuring hooks from imports #4 1`] = `
133+
"import React from \\"react\\";
134+
const __reactCreateElement__ = React.createElement;
135+
const foo = React.useState;
136+
export function MyComponent() {
137+
const _ref_0 = foo(0);
138+
139+
const setCounter = _ref_0[1];
140+
const counter = _ref_0[0];
141+
return __reactCreateElement__(\\"div\\", null, counter);
142+
}"
143+
`;
144+
145+
exports[`React hook transforms should support destructuring hooks from imports #5 1`] = `
146+
"import React from \\"react\\";
147+
const __reactCreateElement__ = React.createElement;
148+
const {
149+
useState: foo
150+
} = React;
151+
export function MyComponent() {
152+
const _ref_0 = foo(0);
153+
154+
const setCounter = _ref_0[1];
155+
const counter = _ref_0[0];
156+
return __reactCreateElement__(\\"div\\", null, counter);
157+
}"
158+
`;
159+
160+
exports[`React hook transforms should support destructuring hooks from imports 1`] = `
161+
"import React from \\"react\\";
162+
const __reactCreateElement__ = React.createElement;
163+
const {
164+
useState
165+
} = React;
166+
export function MyComponent() {
167+
const _ref_0 = useState(0);
168+
169+
const setCounter = _ref_0[1];
170+
const counter = _ref_0[0];
171+
return __reactCreateElement__(\\"div\\", null, counter);
172+
}"
173+
`;
174+
175+
exports[`React hook transforms should support destructuring hooks from require calls 1`] = `
176+
"const React = require(\\"react\\");
177+
178+
const __reactCreateElement__ = React.createElement;
179+
const {
180+
useState
181+
} = React;
182+
export function MyComponent() {
183+
const _ref_0 = useState(0);
184+
185+
const setCounter = _ref_0[1];
186+
const counter = _ref_0[0];
187+
return __reactCreateElement__(\\"div\\", null, counter);
188+
}"
189+
`;
190+
191+
exports[`React hook transforms should support transform hook imports 1`] = `
192+
"import React from \\"react\\";
193+
const {
194+
useState
195+
} = React;"
196+
`;
197+
198+
exports[`React hook transforms should support transform hook imports with aliasing 1`] = `
199+
"import React from \\"react\\";
200+
const {
201+
useState: foo
202+
} = React;"
203+
`;
204+
205+
exports[`React hook transforms should support transform hook imports with no default 1`] = `
206+
"import React from \\"react\\";
207+
const {
208+
useState
209+
} = React;"
210+
`;

packages/babel-plugin-optimize-react/__tests__/hooks-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,12 @@ describe('React hook transforms', () => {
113113
const output = transform(test);
114114
expect(output).toMatchSnapshot();
115115
});
116+
117+
it('should support transform hook imports with no default', () => {
118+
const test = `
119+
import {useState} from "react";
120+
`;
121+
const output = transform(test);
122+
expect(output).toMatchSnapshot();
123+
});
116124
});

packages/babel-plugin-optimize-react/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function(babel) {
2222
const hooks = [];
2323
if (t.isStringLiteral(node.source) && node.source.value === 'react') {
2424
const specifiers = path.get('specifiers');
25+
let hasDefaultSpecifier = false;
2526

2627
for (let specifier of specifiers) {
2728
if (t.isImportSpecifier(specifier)) {
@@ -37,8 +38,16 @@ module.exports = function(babel) {
3738
specifier.remove();
3839
}
3940
}
41+
} else if (t.isImportDefaultSpecifier(specifier)) {
42+
hasDefaultSpecifier = true;
4043
}
4144
}
45+
// If there is no default specifier for React, add one
46+
if (!hasDefaultSpecifier && specifiers.length > 0) {
47+
const defaultSpecifierNode = t.importDefaultSpecifier(t.identifier("React"));
48+
49+
path.pushContainer('specifiers', defaultSpecifierNode);
50+
}
4251
}
4352
return hooks;
4453
}

packages/babel-plugin-optimize-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-optimize-react",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Babel plugin for optimizing common React patterns",
55
"repository": "facebookincubator/create-react-app",
66
"license": "MIT",

0 commit comments

Comments
 (0)