Skip to content

Commit 7a581e7

Browse files
committed
feat(core): add new config option "newJsx"
add possibility to remove react import in favor of new jsx transform
1 parent e78fc27 commit 7a581e7

8 files changed

Lines changed: 35 additions & 13 deletions

File tree

src/config/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class Config implements IConfig {
2626

2727
path: string;
2828

29+
newJsx: boolean;
30+
2931
jsxExt: boolean;
3032

3133
typescript: boolean;
@@ -57,6 +59,7 @@ export class Config implements IConfig {
5759
// Boolean
5860

5961
this.jsxExt = config.jsxExt ?? this.jsxExt;
62+
this.newJsx = config.newJsx ?? this.newJsx;
6063
this.typescript = config.typescript ?? this.typescript;
6164
this.wrapFolder = config.wrapFolder ?? this.wrapFolder;
6265
this.cssModules = config.cssModules ?? this.cssModules;

src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const DEFAULT_CONFIG: IConfig = {
88
styles: 'scss',
99
typescript: false,
1010
jsxExt: true,
11+
newJsx: false,
1112
wrapFolder: true,
1213
fileNameCase: 'pascal',
1314
path: 'src/components',

src/core/TemplateBase.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as t from '@babel/types';
22
import { ReactHook, Variables } from '@/types';
3+
import { config } from '@/config';
34
import * as c from './temlpates/shared';
45

56
/* istanbul ignore next */
@@ -10,7 +11,19 @@ export class TemplateBase {
1011
this.vars = vars;
1112
}
1213

13-
protected getReactImportSpecifier(): t.ImportSpecifier[] {
14+
protected getReactImport(): t.ImportDeclaration | null {
15+
const reactImportSpecifiers = this.getReactImportSpecifiers();
16+
17+
if (config.newJsx) {
18+
if (reactImportSpecifiers.length) {
19+
return c.importNamed(reactImportSpecifiers, 'react');
20+
}
21+
return null;
22+
}
23+
return c.importDefault('React', 'react', reactImportSpecifiers);
24+
}
25+
26+
protected getReactImportSpecifiers(): t.ImportSpecifier[] {
1427
return this.vars.hooks.map(hook => c.importSpec(hook));
1528
}
1629

src/core/temlpates/components/ComponentTemplate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ export class ComponentTemplate
4848
generateAST() {
4949
const body: t.Statement[] = [];
5050

51-
body.push(
52-
c.importDefault('React', 'react', this.getReactImportSpecifier())
53-
);
51+
const reactImport = this.getReactImport();
52+
if (reactImport) {
53+
body.push(reactImport);
54+
}
5455

5556
if (this.hasMod('propTypes')) {
5657
body.push(c.importDefault('PropTypes', 'prop-types'));

src/core/temlpates/components/ComponentTestTemplate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export class ComponentTestTemplate
1616
const componentPath = `./${this.vars.componentName}`;
1717
const body: t.Statement[] = [];
1818

19-
body.push(c.importDefault('React', 'react'));
19+
const reactImport = this.getReactImport();
20+
if (reactImport) {
21+
body.push(reactImport);
22+
}
2023
body.push(
2124
c.importNamed([c.importSpec('render')], '@testing-library/react')
2225
);

src/core/temlpates/components/HOCTemplate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ export class HOCTemplate extends TemplateBase implements Template {
1313
generateAST(): t.File {
1414
const body: t.Statement[] = [];
1515

16-
body.push(
17-
c.importDefault('React', 'react', this.getReactImportSpecifier())
18-
);
16+
const reactImport = this.getReactImport();
17+
if (reactImport) {
18+
body.push(reactImport);
19+
}
1920
body.push(t.emptyStatement());
2021

2122
if (this.hasHook('useReducer')) {

src/core/temlpates/components/HookTemplate.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ export class HookTemplate extends TemplateBase implements Template {
1313
generateAST(): t.File {
1414
const body: t.Statement[] = [];
1515

16-
const reactImportSpecifiers = this.getReactImportSpecifier();
17-
18-
if (reactImportSpecifiers.length) {
19-
body.push(c.importNamed(reactImportSpecifiers, 'react'));
20-
body.push(t.emptyStatement());
16+
const reactImport = this.getReactImport();
17+
if (reactImport) {
18+
body.push(reactImport);
2119
}
20+
body.push(t.emptyStatement());
2221

2322
if (this.hasHook('useReducer')) {
2423
body.push(c.useReducerInit());

src/types/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type ReactHooks = ReactHook[];
1414
export interface IConfig {
1515
styles?: StyleFormats;
1616
typescript?: boolean;
17+
newJsx?: boolean;
1718
jsxExt?: boolean;
1819
fileNameCase?: FileNameCase;
1920
path: string;

0 commit comments

Comments
 (0)