Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as url from 'node:url';
import type {Project} from 'ts-morph';
import {tsImport} from 'tsx/esm/api';
import type {DrizzleToZeroSchema} from '../relations';
import {tsImportShared} from './ts-import';

export const defaultConfigFilePath = 'drizzle-zero.config.ts';

Expand Down Expand Up @@ -40,7 +40,7 @@ export const getConfigFromFile = async ({

try {
const zeroConfigFilePathUrl = url.pathToFileURL(fullConfigPath).href;
const zeroConfigImport = await tsImport(
const zeroConfigImport = await tsImportShared(
zeroConfigFilePathUrl,
import.meta.url,
);
Expand Down
6 changes: 3 additions & 3 deletions src/cli/drizzle-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as url from 'node:url';
import type {Project} from 'ts-morph';
import {tsImport} from 'tsx/esm/api';
import {drizzleZeroConfig, type DrizzleToZeroSchema} from '../relations';
import {tsImportShared} from './ts-import';
import {ensureSourceFileInProject} from './ts-project';

export const getDefaultConfig = async ({
Expand All @@ -30,7 +30,7 @@ export const getDefaultConfig = async ({
resolvedDrizzleSchemaPath,
).href;

const drizzleSchema = await tsImport(
const drizzleSchema = await tsImportShared(
resolvedDrizzleSchemaPathUrl,
import.meta.url,
);
Expand Down Expand Up @@ -92,7 +92,7 @@ export const getFullDrizzleSchemaFilePath = async ({
await fs.access(fullPath);

const drizzleKitConfigFilePathUrl = url.pathToFileURL(fullPath).href;
const drizzleKitConfigImport = await tsImport(
const drizzleKitConfigImport = await tsImportShared(
drizzleKitConfigFilePathUrl,
import.meta.url,
);
Expand Down
21 changes: 21 additions & 0 deletions src/cli/ts-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {register} from 'tsx/esm/api';

type TsxRegister = ReturnType<typeof register> & {
import: (specifier: string, parentURL: string) => Promise<any>;
};

let tsxRegister: TsxRegister | undefined;

/**
* Import a TypeScript module using a shared tsx register instance.
* Lazily creates a single tsx register on first call, then reuses it
* for all subsequent imports. This avoids the overhead of creating a
* new module register per call (which is what `tsImport()` does internally).
*/
export const tsImportShared = (
specifier: string,
parentURL: string,
): Promise<any> => {
tsxRegister ??= register({namespace: 'drizzle-zero'}) as TsxRegister;
return tsxRegister.import(specifier, parentURL);
};
14 changes: 8 additions & 6 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,13 +1610,15 @@ describe('drizzle-kit functions', () => {

try {
// Setup mocks before importing the function
const mockImport = vi.fn().mockImplementation(path => {
if (path.includes('temp-drizzle-schema.ts')) {
return {users: {}};
}
return {};
});
vi.doMock('tsx/esm/api', () => ({
tsImport: vi.fn().mockImplementation(path => {
if (path.includes('temp-drizzle-schema.ts')) {
return {users: {}};
}
return {};
}),
tsImport: mockImport,
register: vi.fn().mockReturnValue({import: mockImport}),
}));

vi.doMock('../src/relations', () => ({
Expand Down