Skip to content

Commit

Permalink
fix(cli): only use dynamic imports for ESM projects
Browse files Browse the repository at this point in the history
Closes #3442
  • Loading branch information
B4nan committed Sep 5, 2022
1 parent 5f57ee1 commit b3e43d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/cli/src/CLIHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class CLIHelper {
}

static async getORM(warnWhenNoEntities?: boolean, opts: Partial<Options> = {}): Promise<MikroORM> {
if (!(await ConfigurationLoader.isESM())) {
opts.dynamicImportProvider ??= id => Utils.requireFrom(id, process.cwd());
Utils.setDynamicImportProvider(opts.dynamicImportProvider);
}

const options = await CLIHelper.getConfiguration(warnWhenNoEntities, opts);
options.set('allowGlobalContext', true);
options.set('debug', !!process.env.MIKRO_ORM_VERBOSE);
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/utils/ConfigurationLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class ConfigurationLoader {
this.registerDotenv(options);
const paths = await this.getConfigPaths();
const env = this.loadEnvironmentVars();
const isESM = (await this.getModuleFormatFromPackage()) === 'module';

for (let path of paths) {
path = Utils.absolutePath(path);
Expand All @@ -36,7 +35,7 @@ export class ConfigurationLoader {
tmp = await tmp;
}

const esmConfigOptions = isESM ? { entityGenerator: { esmImport: true } } : {};
const esmConfigOptions = await this.isESM() ? { entityGenerator: { esmImport: true } } : {};

return new Configuration({ ...esmConfigOptions, ...tmp, ...options, ...env }, validate);
}
Expand Down Expand Up @@ -103,9 +102,11 @@ export class ConfigurationLoader {
return paths.filter(p => p.endsWith('.js') || tsNode);
}

static async getModuleFormatFromPackage(): Promise<string> {
static async isESM(): Promise<boolean> {
const config = await ConfigurationLoader.getPackageConfig();
return config?.type ?? '';
const type = config?.type ?? '';

return type === 'module';
}

static async registerTsNode(configPath = 'tsconfig.json'): Promise<boolean> {
Expand Down
8 changes: 3 additions & 5 deletions tests/features/cli/CLIHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,12 @@ Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?
pathExistsMock.mockRestore();
});

test('getModuleFormatFromPackage gets the type from package.json', async () => {
const mikroPackage = await ConfigurationLoader.getModuleFormatFromPackage();
expect(mikroPackage).toEqual('');
test('isESM', async () => {
await expect(ConfigurationLoader.isESM()).resolves.toEqual(false);

const packageSpy = jest.spyOn(ConfigurationLoader, 'getPackageConfig');
packageSpy.mockResolvedValue({ type: 'module' });
const esmModulePackage = await ConfigurationLoader.getModuleFormatFromPackage();
expect(esmModulePackage).toEqual('module');
await expect(ConfigurationLoader.isESM()).resolves.toEqual(true);
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists');
pathExistsMock.mockResolvedValue(true);
const conf = await CLIHelper.getConfiguration();
Expand Down

0 comments on commit b3e43d0

Please sign in to comment.