Skip to content

Commit

Permalink
fix(entity-generator): output type import statements for type only co…
Browse files Browse the repository at this point in the history
…re imports (#5317)

Helps transpilers and bundlers to more efficiently strip out types.

---------

Co-authored-by: Martin Adámek <banan23@gmail.com>
  • Loading branch information
boenrobot and B4nan committed Mar 7, 2024
1 parent dd6daf9 commit bd3f160
Show file tree
Hide file tree
Showing 18 changed files with 758 additions and 748 deletions.
9 changes: 9 additions & 0 deletions packages/entity-generator/src/CoreImportsHelper.ts
@@ -0,0 +1,9 @@
export const POSSIBLE_TYPE_IMPORTS = [
'DefineConfig',
'Hidden',
'Opt',
'Ref',
'EntityRef',
'ScalarRef',
'Rel',
] as const;
3 changes: 2 additions & 1 deletion packages/entity-generator/src/SourceFile.ts
Expand Up @@ -16,6 +16,7 @@ import {
UnknownType,
Utils,
} from '@mikro-orm/core';
import { POSSIBLE_TYPE_IMPORTS } from './CoreImportsHelper';

/**
* @see https://github.com/tc39/proposal-regexp-unicode-property-escapes#other-examples
Expand Down Expand Up @@ -134,7 +135,7 @@ export class SourceFile {
protected generateImports() {
const imports = [];
if (this.coreImports.size > 0) {
imports.push(`import { ${([...this.coreImports].sort().join(', '))} } from '@mikro-orm/core';`);
imports.push(`import { ${([...this.coreImports].sort().map(t => POSSIBLE_TYPE_IMPORTS.includes(t as typeof POSSIBLE_TYPE_IMPORTS[number]) ? `type ${t}` : t).join(', '))} } from '@mikro-orm/core';`);
}
const entityImportExtension = this.options.esmImport ? '.js' : '';
const entityImports = [...this.entityImports].filter(e => e !== this.meta.className);
Expand Down

0 comments on commit bd3f160

Please sign in to comment.