Skip to content

Commit

Permalink
fix(core): compile with module: 'Node16' to have real dynamic impor…
Browse files Browse the repository at this point in the history
…ts (#3439)
  • Loading branch information
B4nan committed Aug 31, 2022
1 parent 4870d58 commit 50347ef
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/cli/src/commands/GenerateEntitiesCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ArgumentsCamelCase, Argv, CommandModule } from 'yargs';
import { Utils } from '@mikro-orm/core';
import type { EntityManager } from '@mikro-orm/knex';
import { CLIHelper } from '../CLIHelper';

Expand Down Expand Up @@ -45,7 +46,7 @@ export class GenerateEntitiesCommand<U extends Options = Options> implements Com
}

const orm = await CLIHelper.getORM(false);
const { EntityGenerator } = await import('@mikro-orm/entity-generator');
const { EntityGenerator } = await Utils.dynamicImport('@mikro-orm/entity-generator');
const generator = new EntityGenerator(orm.em as EntityManager);
const dump = await generator.generate({ save: args.save, baseDir: args.path, schema: args.schema });

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/ConfigurationLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ConfigurationLoader {
if (await pathExists(`${basePath}/package.json`)) {
/* istanbul ignore next */
try {
return await import(`${basePath}/package.json`);
return await Utils.dynamicImport(`${basePath}/package.json`);
} catch {
return {};
}
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,12 @@ export class Utils {
}
}

if (id.endsWith('.ts')) {
id.replace(/\.ts$/, '.js');
}

if (process.env.TS_JEST) {
return import(id);
return require(id);
}

/* istanbul ignore next */
const ret = await Function(`return import('${id}')`)();
const ret = await import(id);
return ret.default ?? ret;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/migrations/src/Migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class Migrator implements IMigrator {
return DatabaseSchema.create(this.driver.getConnection(), this.driver.getPlatform(), this.config);
}

const data = await import(this.snapshotPath);
const data = await Utils.dynamicImport(this.snapshotPath);
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
const { tables, namespaces, ...rest } = data;
const tableInstances = tables.map((tbl: Dictionary) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/issues/GH1664.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/core';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql/src';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { mockLogger } from '../helpers';

@Entity()
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "Node16",
"target": "es2020",
"lib": ["es2020"],
"moduleResolution": "node",
"moduleResolution": "Node16",
"declaration": true,
"sourceMap": false,
"strict": true,
Expand Down

0 comments on commit 50347ef

Please sign in to comment.