Skip to content

Commit

Permalink
commonjs can import from .js just as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelzor committed May 18, 2023
1 parent ef99728 commit 4366ba7
Show file tree
Hide file tree
Showing 20 changed files with 3,261 additions and 3,387 deletions.
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const config: Config.InitialOptions = {
},
],
},
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
testMatch: ["<rootDir>/tests/**/*.ts"],
testPathIgnorePatterns: [
"<rootDir>/tests/helpers",
Expand Down
2 changes: 0 additions & 2 deletions src/generator/args-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default function generateArgsTypeClassFromArgs(
generateGraphQLScalarsImport(sourceFile);
generateInputsImports(
sourceFile,
dmmfDocument.options,
fields
.map(arg => arg.selectedInputType)
.filter(argInputType => argInputType.location === "inputObjectTypes")
Expand All @@ -43,7 +42,6 @@ export default function generateArgsTypeClassFromArgs(
);
generateEnumsImports(
sourceFile,
dmmfDocument.options,
fields
.map(field => field.selectedInputType)
.filter(argType => argType.location === "enumTypes")
Expand Down
31 changes: 3 additions & 28 deletions src/generator/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ export default async function generateCode(
undefined,
{ overwrite: true },
);
generateEnumsBarrelFile(
enumsBarrelExportSourceFile,
dmmfDocument.options,
emittedEnumNames,
);
generateEnumsBarrelFile(enumsBarrelExportSourceFile, emittedEnumNames);
}

if (dmmfDocument.shouldGenerateBlock("models")) {
Expand All @@ -153,7 +149,6 @@ export default async function generateCode(
);
generateModelsBarrelFile(
modelsBarrelExportSourceFile,
dmmfDocument.options,
dmmfDocument.datamodel.models.map(it => it.typeName),
);
}
Expand Down Expand Up @@ -194,7 +189,6 @@ export default async function generateCode(
);
generateOutputsBarrelFile(
outputsBarrelExportSourceFile,
dmmfDocument.options,
outputTypesToGenerate.map(it => it.typeName),
outputTypesFieldsArgsToGenerate.length > 0,
);
Expand Down Expand Up @@ -224,7 +218,6 @@ export default async function generateCode(
);
generateArgsBarrelFile(
outputsArgsBarrelExportSourceFile,
dmmfDocument.options,
outputTypesFieldsArgsToGenerate.map(it => it.argsTypeName!),
);
}
Expand All @@ -247,7 +240,6 @@ export default async function generateCode(
);
generateInputsBarrelFile(
inputsBarrelExportSourceFile,
dmmfDocument.options,
dmmfDocument.schema.inputTypes.map(it => it.typeName),
);
}
Expand Down Expand Up @@ -278,7 +270,6 @@ export default async function generateCode(
);
generateResolversBarrelFile(
relationResolversBarrelExportSourceFile,
dmmfDocument.options,
dmmfDocument.relationModels.map<GenerateMappingData>(relationModel => ({
resolverName: relationModel.resolverName,
modelName: relationModel.model.typeName,
Expand Down Expand Up @@ -314,11 +305,7 @@ export default async function generateCode(
undefined,
{ overwrite: true },
);
generateArgsBarrelFile(
barrelExportSourceFile,
dmmfDocument.options,
argTypeNames,
);
generateArgsBarrelFile(barrelExportSourceFile, argTypeNames);
}
});

Expand All @@ -341,7 +328,6 @@ export default async function generateCode(
);
generateArgsIndexFile(
relationResolversArgsIndexSourceFile,
dmmfDocument.options,
relationModelsWithArgs.map(
relationModelData => relationModelData.model.typeName,
),
Expand All @@ -359,7 +345,6 @@ export default async function generateCode(
);
generateResolversIndexFile(
relationResolversIndexSourceFile,
dmmfDocument.options,
"relations",
relationModelsWithArgs.length > 0,
);
Expand Down Expand Up @@ -417,7 +402,6 @@ export default async function generateCode(
);
generateResolversBarrelFile(
crudResolversBarrelExportSourceFile,
dmmfDocument.options,
generateMappingData,
);
const crudResolversActionsBarrelExportSourceFile = project.createSourceFile(
Expand All @@ -432,7 +416,6 @@ export default async function generateCode(
);
generateResolversActionsBarrelFile(
crudResolversActionsBarrelExportSourceFile,
dmmfDocument.options,
generateMappingData,
);
const crudResolversIndexSourceFile = project.createSourceFile(
Expand All @@ -445,12 +428,7 @@ export default async function generateCode(
undefined,
{ overwrite: true },
);
generateResolversIndexFile(
crudResolversIndexSourceFile,
dmmfDocument.options,
"crud",
true,
);
generateResolversIndexFile(crudResolversIndexSourceFile, "crud", true);

log("Generating crud resolvers args...");
dmmfDocument.modelMappings.forEach(async mapping => {
Expand Down Expand Up @@ -484,7 +462,6 @@ export default async function generateCode(
);
generateArgsBarrelFile(
barrelExportSourceFile,
dmmfDocument.options,
actionsWithArgs.map(it => it.argsTypeName!),
);
}
Expand All @@ -501,7 +478,6 @@ export default async function generateCode(
);
generateArgsIndexFile(
crudResolversArgsIndexSourceFile,
dmmfDocument.options,
dmmfDocument.modelMappings
.filter(mapping =>
mapping.actions.some(it => it.argsTypeName !== undefined),
Expand Down Expand Up @@ -550,7 +526,6 @@ export default async function generateCode(
);
generateIndexFile(
indexSourceFile,
dmmfDocument.options,
dmmfDocument.relationModels.length > 0,
dmmfDocument.options.blocksToEmit,
);
Expand Down
28 changes: 7 additions & 21 deletions src/generator/generate-enhance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ export function generateEnhanceMap(

if (dmmfDocument.shouldGenerateBlock("crudResolvers")) {
sourceFile.addImportDeclaration({
moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-crud.index${
dmmfDocument.options.emitESM ? ".js" : ""
}`,
moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-crud.index.js`,
namespaceImport: "crudResolvers",
});
sourceFile.addImportDeclaration({
moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/args.index${
dmmfDocument.options.emitESM ? ".js" : ""
}`,
moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/args.index.js`,
namespaceImport: "argsTypes",
});
sourceFile.addVariableStatement({
Expand All @@ -71,9 +67,7 @@ export function generateEnhanceMap(
trailingTrivia: "\r\n",
});
sourceFile.addImportDeclaration({
moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-actions.index${
dmmfDocument.options.emitESM ? ".js" : ""
}`,
moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-actions.index.js`,
namespaceImport: "actionResolvers",
});
sourceFile.addVariableStatement({
Expand Down Expand Up @@ -243,9 +237,7 @@ export function generateEnhanceMap(

if (hasRelations && dmmfDocument.shouldGenerateBlock("relationResolvers")) {
sourceFile.addImportDeclaration({
moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}/resolvers.index${
dmmfDocument.options.emitESM ? ".js" : ""
}`,
moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}/resolvers.index.js`,
namespaceImport: "relationResolvers",
});
sourceFile.addVariableStatement({
Expand Down Expand Up @@ -375,9 +367,7 @@ export function generateEnhanceMap(

if (dmmfDocument.shouldGenerateBlock("models")) {
sourceFile.addImportDeclaration({
moduleSpecifier: `./${modelsFolderName}${
dmmfDocument.options.emitESM ? "/index.js" : ""
}`,
moduleSpecifier: `./${modelsFolderName}/index.js`,
namespaceImport: "models",
});
sourceFile.addVariableStatement({
Expand Down Expand Up @@ -445,9 +435,7 @@ export function generateEnhanceMap(

if (dmmfDocument.shouldGenerateBlock("outputs")) {
sourceFile.addImportDeclaration({
moduleSpecifier: `./${resolversFolderName}/${outputsFolderName}${
dmmfDocument.options.emitESM ? "/index.js" : ""
}`,
moduleSpecifier: `./${resolversFolderName}/${outputsFolderName}/index.js`,
namespaceImport: "outputTypes",
});
sourceFile.addVariableStatement({
Expand Down Expand Up @@ -509,9 +497,7 @@ export function generateEnhanceMap(

if (dmmfDocument.shouldGenerateBlock("inputs")) {
sourceFile.addImportDeclaration({
moduleSpecifier: `./${resolversFolderName}/${inputsFolderName}${
dmmfDocument.options.emitESM ? "/index.js" : ""
}`,
moduleSpecifier: `./${resolversFolderName}/${inputsFolderName}/index.js`,
namespaceImport: "inputTypes",
});
sourceFile.addVariableStatement({
Expand Down
Loading

0 comments on commit 4366ba7

Please sign in to comment.