diff --git a/src/generators/extend.ts b/src/generators/extend.ts index 884a526..d94fff6 100644 --- a/src/generators/extend.ts +++ b/src/generators/extend.ts @@ -6,6 +6,10 @@ import { declMapping } from '../config'; import { GeneratorResult, TsGenConfig, TsHelperConfig } from '..'; const debug = debuglog('egg-ts-helper#generators_extend'); +const hasExportWithoutDefault = (filePath: string) => { + const result = utils.findExportNode(fs.readFileSync(filePath, 'utf-8')); + return !result.exportDefaultNode; +}; export default function ExtendGenerator(config: TsGenConfig, baseConfig: TsHelperConfig) { const fileList = config.file ? [ config.file ] : config.fileList; @@ -41,7 +45,9 @@ export default function ExtendGenerator(config: TsGenConfig, baseConfig: TsHelpe // get import info const moduleName = `Extend${interfaceEnvironment}${interfaceName}`; - const importContext = utils.getImportStr(config.dtsDir, f, moduleName); + + const useImportStar = hasExportWithoutDefault(f); + const importContext = utils.getImportStr(config.dtsDir, f, moduleName, useImportStar); tsList.push({ dist, content: diff --git a/test/fixtures/app/app/extend/helper.ts b/test/fixtures/app/app/extend/helper.ts index f1159be..31c3840 100644 --- a/test/fixtures/app/app/extend/helper.ts +++ b/test/fixtures/app/app/extend/helper.ts @@ -1,13 +1,7 @@ -let helper; +export function isCool() { + console.info('is Cool'); +} -helper = { - isCool() { - console.info('is Cool'); - }, - - isNotCool() { - console.info('is not Cool'); - }, -}; - -export default helper; +export function isNotCool() { + console.info('is not Cool'); +} diff --git a/test/generators/extend.test.ts b/test/generators/extend.test.ts index ce3722d..b6cf355 100644 --- a/test/generators/extend.test.ts +++ b/test/generators/extend.test.ts @@ -49,7 +49,7 @@ describe('generators/extend.test.ts', () => { it('should works without error with helper', () => { const result = triggerGenerator('extend', appDir, 'helper.ts'); const item = result[0]; - assert(item.content!.includes('../../../app/extend/helper')); + assert(item.content!.includes('import * as ExtendIHelper from \'../../../app/extend/helper\'')); assert(item.content!.includes('type ExtendIHelperType = typeof ExtendIHelper')); assert(item.content!.includes('interface IHelper extends ExtendIHelperType { }')); }); @@ -58,7 +58,7 @@ describe('generators/extend.test.ts', () => { const result = triggerGenerator('extend', appDir, 'application.unittest.ts'); const item = result[0]; assert(item.dist === path.resolve(appDir, './typings/app/extend/application.unittest.d.ts')); - assert(item.content!.includes('../../../app/extend/application.unittest')); + assert(item.content!.includes('import ExtendUnittestApplication from \'../../../app/extend/application.unittest\'')); assert(item.content!.includes('type ExtendUnittestApplicationType = typeof ExtendUnittestApplication;')); assert(item.content!.includes('interface Application extends ExtendUnittestApplicationType { }')); });