File tree Expand file tree Collapse file tree 4 files changed +34
-11
lines changed
Expand file tree Collapse file tree 4 files changed +34
-11
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ import * as fs from 'node:fs/promises';
22import * as path from 'node:path' ;
33import * as url from 'node:url' ;
44import type { Project } from 'ts-morph' ;
5- import { tsImport } from 'tsx/esm/api' ;
65import type { DrizzleToZeroSchema } from '../relations' ;
6+ import { tsImportShared } from './ts-import' ;
77
88export const defaultConfigFilePath = 'drizzle-zero.config.ts' ;
99
@@ -40,7 +40,7 @@ export const getConfigFromFile = async ({
4040
4141 try {
4242 const zeroConfigFilePathUrl = url . pathToFileURL ( fullConfigPath ) . href ;
43- const zeroConfigImport = await tsImport (
43+ const zeroConfigImport = await tsImportShared (
4444 zeroConfigFilePathUrl ,
4545 import . meta. url ,
4646 ) ;
Original file line number Diff line number Diff line change @@ -3,8 +3,8 @@ import * as fs from 'node:fs/promises';
33import * as path from 'node:path' ;
44import * as url from 'node:url' ;
55import type { Project } from 'ts-morph' ;
6- import { tsImport } from 'tsx/esm/api' ;
76import { drizzleZeroConfig , type DrizzleToZeroSchema } from '../relations' ;
7+ import { tsImportShared } from './ts-import' ;
88import { ensureSourceFileInProject } from './ts-project' ;
99
1010export const getDefaultConfig = async ( {
@@ -30,7 +30,7 @@ export const getDefaultConfig = async ({
3030 resolvedDrizzleSchemaPath ,
3131 ) . href ;
3232
33- const drizzleSchema = await tsImport (
33+ const drizzleSchema = await tsImportShared (
3434 resolvedDrizzleSchemaPathUrl ,
3535 import . meta. url ,
3636 ) ;
@@ -92,7 +92,7 @@ export const getFullDrizzleSchemaFilePath = async ({
9292 await fs . access ( fullPath ) ;
9393
9494 const drizzleKitConfigFilePathUrl = url . pathToFileURL ( fullPath ) . href ;
95- const drizzleKitConfigImport = await tsImport (
95+ const drizzleKitConfigImport = await tsImportShared (
9696 drizzleKitConfigFilePathUrl ,
9797 import . meta. url ,
9898 ) ;
Original file line number Diff line number Diff line change 1+ import { register } from 'tsx/esm/api' ;
2+
3+ type TsxRegister = ReturnType < typeof register > & {
4+ import : ( specifier : string , parentURL : string ) => Promise < any > ;
5+ } ;
6+
7+ let tsxRegister : TsxRegister | undefined ;
8+
9+ /**
10+ * Import a TypeScript module using a shared tsx register instance.
11+ * Lazily creates a single tsx register on first call, then reuses it
12+ * for all subsequent imports. This avoids the overhead of creating a
13+ * new module register per call (which is what `tsImport()` does internally).
14+ */
15+ export const tsImportShared = (
16+ specifier : string ,
17+ parentURL : string ,
18+ ) : Promise < any > => {
19+ tsxRegister ??= register ( { namespace : 'drizzle-zero' } ) as TsxRegister ;
20+ return tsxRegister . import ( specifier , parentURL ) ;
21+ } ;
Original file line number Diff line number Diff line change @@ -1610,13 +1610,15 @@ describe('drizzle-kit functions', () => {
16101610
16111611 try {
16121612 // Setup mocks before importing the function
1613+ const mockImport = vi . fn ( ) . mockImplementation ( path => {
1614+ if ( path . includes ( 'temp-drizzle-schema.ts' ) ) {
1615+ return { users : { } } ;
1616+ }
1617+ return { } ;
1618+ } ) ;
16131619 vi . doMock ( 'tsx/esm/api' , ( ) => ( {
1614- tsImport : vi . fn ( ) . mockImplementation ( path => {
1615- if ( path . includes ( 'temp-drizzle-schema.ts' ) ) {
1616- return { users : { } } ;
1617- }
1618- return { } ;
1619- } ) ,
1620+ tsImport : mockImport ,
1621+ register : vi . fn ( ) . mockReturnValue ( { import : mockImport } ) ,
16201622 } ) ) ;
16211623
16221624 vi . doMock ( '../src/relations' , ( ) => ( {
You can’t perform that action at this time.
0 commit comments