Skip to content

Commit

Permalink
Merge pull request #60 from farreldarian/fix-type-import
Browse files Browse the repository at this point in the history
feat: verbatimModuleSyntax
  • Loading branch information
fdarian authored Jun 5, 2024
2 parents bfded02 + 2fc24bf commit 30d4283
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
17 changes: 17 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
postgres:
image: postgres:latest
container_name: prisma-generator-drizzle-postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=generator
ports:
- "5432:5432"
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: generator
ports:
- "3306:3306"
32 changes: 26 additions & 6 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,42 @@ export function reduceImports(imports: ImportValue[]) {
...plan.skipped,
...pipe(
plan.toReduce,
reduce(new Map<string, Set<string>>(), (accum, command) => {
if (command.type !== 'namedImport') return accum
reduce(new Map<ModuleKey, Set<string>>(), (accum, import_) => {
if (import_.type !== 'namedImport') return accum

const imports = new Set(accum.get(command.module))
for (const name of command.names) {
const imports = new Set(accum.get(getModuleKey(import_)))
for (const name of import_.names) {
imports.add(name)
}

return accum.set(command.module, imports)
return accum.set(getModuleKey(import_), imports)
}),
(map) => Array.from(map),
map(([path, names]) => namedImport(Array.from(names), path))
map(([key, names]) =>
namedImport(
Array.from(names),
parseModuleKey(key),
isTypeModuleKey(key)
)
)
),
]
}

const TYPE_PREFIX = '_$type_'
type ModuleKey = `_${string}_${string}`
function getModuleKey(import_: ImportValue) {
if (import_.isTypeImport)
return `${TYPE_PREFIX}${import_.module}` as ModuleKey
return import_.module as ModuleKey
}
function isTypeModuleKey(name: ModuleKey) {
return name.includes(TYPE_PREFIX)
}
function parseModuleKey(name: ModuleKey) {
return name.replace(TYPE_PREFIX, '')
}

function writeModules(modules: GeneratedModules) {
const outputPath = getGenerator().output.path

Expand Down
3 changes: 2 additions & 1 deletion packages/generator/src/lib/adapter/fields/createField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ function getCustomType(field: DMMF.Field) {
throw new Error(`Invalid type definition: ${field.documentation}`)

const [module, type] = splits

return {
imports: namedImport([type], module),
imports: namedImport([type], module, true),
code: `.$type<${type}>()`,
}
}
Expand Down
21 changes: 17 additions & 4 deletions packages/generator/src/lib/syntaxes/imports.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import { getModuleResolution } from '~/shared/generator-context'

export function namedImport(names: string[], path: string) {
export function namedImport(
names: string[],
path: string,
isTypeImport = false
) {
return {
type: 'namedImport' as const,
names: names,
module: path,
isTypeImport,
render() {
return `import { ${names.join(', ')} } from '${renderImportPath(path)}';`
// biome-ignore format: off
return `import ${isTypeImport ? 'type ' : ''}{ ${names.join( ', ')} } from '${renderImportPath(path)}';`
},
}
}
export type NamedImport = ReturnType<typeof namedImport>

export function defaultImportValue(name: string, path: string) {
export function defaultImportValue(
name: string,
path: string,
isTypeImport = false
) {
return {
type: 'defaultImport' as const,
name,
module: path,
isTypeImport,
render() {
return `import ${name} from '${renderImportPath(path)}';`
// biome-ignore format: off
return `import ${isTypeImport ? 'type ' : ''}${name} from '${renderImportPath(path)}';`
},
}
}
Expand All @@ -27,6 +39,7 @@ export function wildcardImport(alias: string, path: string) {
return {
type: 'wildcardImport' as const,
module: path,
isTypeImport: false,
render() {
return `import * as ${alias} from '${renderImportPath(path)}';`
},
Expand Down
2 changes: 1 addition & 1 deletion packages/usage/src/lib/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import { schema } from 'prisma/drizzle/schema'
import { object, parse, string, url } from 'valibot'
import { url, object, parse, string } from 'valibot'

const env = parse(
object({
Expand Down

0 comments on commit 30d4283

Please sign in to comment.