Skip to content

Commit 42f3789

Browse files
committed
Add new exportType config option
1 parent 4a3eb1e commit 42f3789

12 files changed

Lines changed: 142 additions & 113 deletions

File tree

src/config/Config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FileNameCase, IConfig, StyleFormats } from '@/types';
1+
import { ExportType, FileNameCase, IConfig, StyleFormats } from '@/types';
22
import defaultConfig from './default.json';
33

44
export type ConfigPrefixes = {
@@ -33,6 +33,8 @@ export class Config implements IConfig {
3333

3434
ext: ConfigExt;
3535

36+
exportType: ExportType;
37+
3638
constructor(config: IConfig) {
3739
this.config = Object.assign(defaultConfig, config);
3840

@@ -49,6 +51,7 @@ export class Config implements IConfig {
4951
this.typescript = this.config.typescript;
5052
this.wrapFolder = this.config.wrapFolder;
5153
this.cssModules = this.config.cssModules;
54+
this.exportType = this.config.exportType;
5255
}
5356

5457
private setFilesExtension() {

src/config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"wrapFolder": true,
66
"fileNameCase": "pascal",
77
"path": "src/components",
8-
"cssModules": true
8+
"cssModules": true,
9+
"exportType": "named"
910
}

src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ import { Config } from '@/config/Config';
22
import { getUserConfig } from '@/utils';
33

44
export { Config } from './Config';
5+
export { default as defaultConfig } from './default.json';
6+
57
const userConfig = getUserConfig();
68
export const config = new Config(userConfig);

src/constants/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { StyleFormats } from '@/types';
1+
import { ExportType, StyleFormats } from '@/types';
22
import * as utils from '@/utils';
33

44
// export const ROOT = path.dirname(require.main.filename);
55
export const STYLE_FORMATS: StyleFormats[] = ['CSS', 'SCSS', 'SASS', 'Less', 'Stylus'];
6+
export const EXPORT_TYPES: ExportType[] = ['named', 'default'];
67
export const APP_ROOT = utils.getAppRoot();

src/core/TemplateBase.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import * as t from '@babel/types';
12
import * as c from './temlpates/shared';
23

34
export class TemplateBase {
45
constructor(protected vars) {}
56

6-
protected getReactImportSpecifier() {
7+
protected getReactImportSpecifier(): t.ImportSpecifier[] {
78
return this.vars.hooks.map(hook => c.importSpec(hook));
89
}
910

src/core/temlpates/components/ComponentTemplate.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,24 @@ export class ComponentTemplate extends TemplateBase implements Template {
3333
body.push(t.emptyStatement());
3434
}
3535

36-
body.push(c.component(this.vars.componentName, c.generateHooks(this.vars.hooks)));
37-
body.push(t.emptyStatement());
36+
const component = c.component(this.vars.componentName, c.generateHooks(this.vars.hooks));
37+
38+
if (config.exportType === 'named') {
39+
body.push(t.exportNamedDeclaration(component));
40+
} else {
41+
body.push(component);
42+
}
3843

3944
if (this.hasMod('propTypes')) {
45+
body.push(t.emptyStatement());
4046
body.push(c.propTypes(this.vars.componentName));
4147
}
4248

49+
if (config.exportType === 'default') {
50+
body.push(t.emptyStatement());
51+
body.push(t.exportDefaultDeclaration(t.identifier(this.vars.componentName)));
52+
}
53+
4354
return c.program(body);
4455
}
4556
}

src/core/temlpates/components/ComponentTestTemplate.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import * as c from '../shared';
66

77
export class ComponentTestTemplate extends TemplateBase implements Template {
88
generateAST(): t.File {
9-
const componentPath = `./${this.vars.componentName}.${config.ext.component}`;
9+
const componentPath = `./${this.vars.componentName}`;
1010
const body: t.Statement[] = [];
1111

1212
body.push(c.importDefault('React', 'react'));
1313
body.push(c.importNamed([c.importSpec('render')], '@testing-library/react'));
14-
body.push(c.importNamed([c.importSpec(this.vars.componentName)], componentPath));
14+
15+
if (config.exportType === 'named') {
16+
body.push(c.importNamed([c.importSpec(this.vars.componentName)], componentPath));
17+
} else {
18+
body.push(c.importDefault(this.vars.componentName, componentPath));
19+
}
1520

1621
body.push(t.emptyStatement());
1722

src/core/temlpates/components/HOCTemplate.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as t from '@babel/types';
22
import { TemplateBase } from '@/core/TemplateBase';
33
import { Template } from '@/core/TemplateGenerator';
4+
import { config } from '@/config';
45
import * as c from '../shared';
56

67
export class HOCTemplate extends TemplateBase implements Template {
@@ -15,7 +16,18 @@ export class HOCTemplate extends TemplateBase implements Template {
1516
body.push(t.emptyStatement());
1617
}
1718

18-
body.push(c.hoc(this.vars.componentName, c.generateHooks(this.vars.hooks)));
19+
const hoc = c.hoc(this.vars.componentName, c.generateHooks(this.vars.hooks));
20+
21+
if (config.exportType === 'named') {
22+
body.push(t.exportNamedDeclaration(hoc));
23+
} else {
24+
body.push(hoc);
25+
}
26+
27+
if (config.exportType === 'default') {
28+
body.push(t.emptyStatement());
29+
body.push(t.exportDefaultDeclaration(t.identifier(this.vars.componentName)));
30+
}
1931

2032
return c.program(body);
2133
}

src/core/temlpates/components/HookTemplate.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
import * as t from '@babel/types';
22
import { TemplateBase } from '@/core/TemplateBase';
33
import { Template } from '@/core/TemplateGenerator';
4+
import { config } from '@/config';
45
import * as c from '../shared';
56

67
export class HookTemplate extends TemplateBase implements Template {
78
generateAST(): t.File {
89
const body: t.Statement[] = [];
910

10-
body.push(c.importNamed(this.getReactImportSpecifier(), 'react'));
11-
body.push(t.emptyStatement());
11+
const reactImportSpecifiers = this.getReactImportSpecifier();
12+
13+
if (reactImportSpecifiers.length) {
14+
body.push(c.importNamed(reactImportSpecifiers, 'react'));
15+
body.push(t.emptyStatement());
16+
}
1217

1318
if (this.hasHook('useReducer')) {
1419
body.push(c.useReducerInit());
1520
body.push(t.emptyStatement());
1621
}
1722

18-
body.push(c.hook(this.vars.componentName, c.generateHooks(this.vars.hooks)));
23+
const hook = c.hook(this.vars.componentName, c.generateHooks(this.vars.hooks));
24+
25+
if (config.exportType === 'named') {
26+
body.push(t.exportNamedDeclaration(hook));
27+
} else {
28+
body.push(hook);
29+
}
30+
31+
if (config.exportType === 'default') {
32+
body.push(t.emptyStatement());
33+
body.push(t.exportDefaultDeclaration(t.identifier(this.vars.componentName)));
34+
}
1935

2036
return c.program(body);
2137
}
Lines changed: 67 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import * as t from '@babel/types';
22

33
export const component = (componentName: string, contentStatement: t.Statement[] = []) => {
4-
return t.exportNamedDeclaration(
5-
t.variableDeclaration('const', [
6-
t.variableDeclarator(
7-
t.identifier(componentName),
8-
t.arrowFunctionExpression(
9-
[t.identifier('props')],
10-
t.blockStatement([
11-
...contentStatement,
12-
t.returnStatement(
13-
t.jsxElement(
14-
t.jsxOpeningElement(t.jsxIdentifier('div'), [], false),
15-
t.jsxClosingElement(t.jsxIdentifier('div')),
16-
[],
17-
null
18-
)
19-
),
20-
])
21-
)
22-
),
23-
])
24-
);
4+
return t.variableDeclaration('const', [
5+
t.variableDeclarator(
6+
t.identifier(componentName),
7+
t.arrowFunctionExpression(
8+
[t.identifier('props')],
9+
t.blockStatement([
10+
...contentStatement,
11+
t.returnStatement(
12+
t.jsxElement(
13+
t.jsxOpeningElement(t.jsxIdentifier('div'), [], false),
14+
t.jsxClosingElement(t.jsxIdentifier('div')),
15+
[]
16+
)
17+
),
18+
])
19+
)
20+
),
21+
]);
2522
};
2623

2724
export const test = (componentName: string) => {
@@ -30,94 +27,67 @@ export const test = (componentName: string) => {
3027
t.stringLiteral(componentName),
3128
t.arrowFunctionExpression(
3229
[],
33-
t.blockStatement(
34-
[
35-
t.expressionStatement(
36-
t.callExpression(t.identifier('it'), [
37-
t.stringLiteral('should render without crashing'),
38-
t.arrowFunctionExpression(
39-
[],
40-
t.blockStatement(
41-
[
42-
t.variableDeclaration('const', [
43-
t.variableDeclarator(
44-
t.identifier('component'),
45-
t.callExpression(t.identifier('render'), [
46-
t.jsxElement(
47-
t.jsxOpeningElement(t.jsxIdentifier(componentName), [], true),
48-
null,
49-
[],
50-
false
51-
),
52-
])
30+
t.blockStatement([
31+
t.expressionStatement(
32+
t.callExpression(t.identifier('it'), [
33+
t.stringLiteral('should render without crashing'),
34+
t.arrowFunctionExpression(
35+
[],
36+
t.blockStatement([
37+
t.variableDeclaration('const', [
38+
t.variableDeclarator(
39+
t.identifier('component'),
40+
t.callExpression(t.identifier('render'), [
41+
t.jsxElement(
42+
t.jsxOpeningElement(t.jsxIdentifier(componentName), [], true),
43+
null,
44+
[]
5345
),
54-
]),
55-
],
56-
[]
57-
),
58-
false
59-
),
60-
])
61-
),
62-
],
63-
[]
64-
),
65-
false
46+
])
47+
),
48+
]),
49+
])
50+
),
51+
])
52+
),
53+
])
6654
),
6755
])
6856
);
6957
};
7058

7159
export const hoc = (hocName: string, contentStatement: t.Statement[] = []) => {
72-
return t.exportNamedDeclaration(
73-
t.functionDeclaration(
74-
t.identifier(hocName),
75-
[t.identifier('WrappedComponent')],
76-
t.blockStatement(
77-
[
78-
t.returnStatement(
79-
t.arrowFunctionExpression(
80-
[t.identifier('props')],
81-
t.blockStatement(
82-
[
83-
...contentStatement,
84-
t.returnStatement(
85-
t.jsxElement(
86-
t.jsxOpeningElement(
87-
t.jsxIdentifier('WrappedComponent'),
88-
[t.jsxSpreadAttribute(t.identifier('props'))],
89-
true
90-
),
91-
null,
92-
[],
93-
false
94-
)
95-
),
96-
],
60+
return t.functionDeclaration(
61+
t.identifier(hocName),
62+
[t.identifier('WrappedComponent')],
63+
t.blockStatement([
64+
t.returnStatement(
65+
t.arrowFunctionExpression(
66+
[t.identifier('props')],
67+
t.blockStatement([
68+
...contentStatement,
69+
t.returnStatement(
70+
t.jsxElement(
71+
t.jsxOpeningElement(
72+
t.jsxIdentifier('WrappedComponent'),
73+
[t.jsxSpreadAttribute(t.identifier('props'))],
74+
true
75+
),
76+
null,
9777
[]
98-
),
99-
false
100-
)
101-
),
102-
],
103-
[]
78+
)
79+
),
80+
])
81+
)
10482
),
105-
false,
106-
false
107-
),
108-
[],
109-
null
83+
])
11084
);
11185
};
11286

11387
export const hook = (hookName: string, contentStatement: t.Statement[] = []) => {
114-
return t.exportNamedDeclaration(
115-
t.functionDeclaration(
116-
t.identifier(hookName),
117-
[],
118-
t.blockStatement([...contentStatement, t.returnStatement(t.nullLiteral())], []),
119-
false,
120-
false
121-
)
88+
return t.functionDeclaration(
89+
t.identifier(hookName),
90+
[],
91+
t.blockStatement([...contentStatement, t.returnStatement(t.nullLiteral())])
12292
);
12393
};

0 commit comments

Comments
 (0)