Skip to content

Commit 6d8dbab

Browse files
author
xiongjie
committed
feat: prefer use import * as for extend
we detect ts file content,and generate import * as xx from "file" when module just has export.
1 parent f396e5f commit 6d8dbab

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/generators/extend.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { declMapping } from '../config';
66
import { GeneratorResult, TsGenConfig, TsHelperConfig } from '..';
77

88
const debug = debuglog('egg-ts-helper#generators_extend');
9+
const hasExportWithoutDefault = (filePath: string) => {
10+
const result = utils.findExportNode(fs.readFileSync(filePath, 'utf-8'));
11+
return !result.exportDefaultNode;
12+
};
913

1014
export default function ExtendGenerator(config: TsGenConfig, baseConfig: TsHelperConfig) {
1115
const fileList = config.file ? [ config.file ] : config.fileList;
@@ -41,7 +45,9 @@ export default function ExtendGenerator(config: TsGenConfig, baseConfig: TsHelpe
4145

4246
// get import info
4347
const moduleName = `Extend${interfaceEnvironment}${interfaceName}`;
44-
const importContext = utils.getImportStr(config.dtsDir, f, moduleName);
48+
49+
const useImportStar = hasExportWithoutDefault(f);
50+
const importContext = utils.getImportStr(config.dtsDir, f, moduleName, useImportStar);
4551
tsList.push({
4652
dist,
4753
content:
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
let helper;
1+
export function isCool() {
2+
console.info('is Cool');
3+
}
24

3-
helper = {
4-
isCool() {
5-
console.info('is Cool');
6-
},
7-
8-
isNotCool() {
9-
console.info('is not Cool');
10-
},
11-
};
12-
13-
export default helper;
5+
export function isNotCool() {
6+
console.info('is not Cool');
7+
}

test/generators/extend.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('generators/extend.test.ts', () => {
4949
it('should works without error with helper', () => {
5050
const result = triggerGenerator('extend', appDir, 'helper.ts');
5151
const item = result[0];
52-
assert(item.content!.includes('../../../app/extend/helper'));
52+
assert(item.content!.includes('import * as ExtendIHelper from \'../../../app/extend/helper\''));
5353
assert(item.content!.includes('type ExtendIHelperType = typeof ExtendIHelper'));
5454
assert(item.content!.includes('interface IHelper extends ExtendIHelperType { }'));
5555
});
@@ -58,7 +58,7 @@ describe('generators/extend.test.ts', () => {
5858
const result = triggerGenerator('extend', appDir, 'application.unittest.ts');
5959
const item = result[0];
6060
assert(item.dist === path.resolve(appDir, './typings/app/extend/application.unittest.d.ts'));
61-
assert(item.content!.includes('../../../app/extend/application.unittest'));
61+
assert(item.content!.includes('import ExtendUnittestApplication from \'../../../app/extend/application.unittest\''));
6262
assert(item.content!.includes('type ExtendUnittestApplicationType = typeof ExtendUnittestApplication;'));
6363
assert(item.content!.includes('interface Application extends ExtendUnittestApplicationType { }'));
6464
});

0 commit comments

Comments
 (0)