Skip to content

Replace Prettier with Biome as the code formatter to improve performance during the code generation phase #1145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/twelve-plants-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"swagger-typescript-api": patch
---

Replace Prettier with Biome as the code formatter to improve performance during the code generation phase.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ generateApi({
extractRequestBody: false,
extractEnums: false,
unwrapResponseData: false,
prettier: {
// By default prettier config is load from your project
printWidth: 120,
tabWidth: 2,
trailingComma: "all",
parser: "typescript",
},
defaultResponseType: "void",
singleHttpClient: true,
cleanOutput: false,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
"test": "vitest run"
},
"dependencies": {
"@biomejs/js-api": "^0.7.1",
"@biomejs/wasm-nodejs": "^1.9.4",
"@types/swagger-schema-official": "^2.0.25",
"c12": "^3.0.2",
"citty": "^0.1.6",
"consola": "^3.4.2",
"cosmiconfig": "^9.0.0",
"eta": "^2.2.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"nanoid": "^5.1.5",
"prettier": "~3.5.3",
"swagger-schema-official": "2.0.0-bab6bed",
"swagger2openapi": "^7.0.8",
"typescript": "~5.8.2"
Expand Down
26 changes: 16 additions & 10 deletions src/code-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as prettier from "prettier";
import * as path from "node:path";
import { Biome, Distribution } from "@biomejs/js-api";
import * as nanoid from "nanoid";
import * as typescript from "typescript";
import type { CodeGenConfig } from "./configuration.js";

Expand Down Expand Up @@ -34,23 +36,27 @@ export class CodeFormatter {
return content;
};

prettierFormat = async (content: string) => {
const formatted = await prettier.format(
content,
this.config.prettierOptions,
);
return formatted;
format = async (content: string) => {
const biome = await Biome.create({ distribution: Distribution.NODE });
biome.applyConfiguration({
files: { maxSize: Number.MAX_SAFE_INTEGER },
formatter: { indentStyle: "space" },
});
const formatted = biome.formatContent(content, {
filePath: path.format({ name: nanoid.nanoid(), ext: "ts" }),
});
return formatted.content;
};

formatCode = async (
code: string,
{ removeUnusedImports = true, prettierFormat = true } = {},
{ removeUnusedImports = true, format = true } = {},
) => {
if (removeUnusedImports) {
code = this.removeUnusedImports(code);
}
if (prettierFormat) {
code = await this.prettierFormat(code);
if (format) {
code = await this.format(code);
}
return code;
};
Expand Down
24 changes: 0 additions & 24 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as cosmiconfig from "cosmiconfig";
import lodash from "lodash";
import type { OpenAPI } from "openapi-types";
import * as typescript from "typescript";
Expand Down Expand Up @@ -87,7 +86,6 @@ export class CodeGenConfig {
outOfModuleApi: "Common",
};
routeNameDuplicatesMap = new Map();
prettierOptions = { ...CONSTANTS.PRETTIER_OPTIONS };
hooks: Hooks = {
onPreBuildRoutePath: (_routePath: unknown) => void 0,
onBuildRoutePath: (_routeData: unknown) => void 0,
Expand Down Expand Up @@ -390,7 +388,6 @@ export class CodeGenConfig {
templateExtensions = [".eta", ".ejs"];

constructor({
prettierOptions = getDefaultPrettierOptions(),
codeGenConstructs,
primitiveTypeConstructs,
constants,
Expand All @@ -405,10 +402,6 @@ export class CodeGenConfig {

this.update({
...otherConfig,
prettierOptions:
prettierOptions === undefined
? getDefaultPrettierOptions()
: prettierOptions,
hooks: lodash.merge(this.hooks, hooks || {}),
constants: {
...CONSTANTS,
Expand All @@ -430,20 +423,3 @@ export class CodeGenConfig {
objectAssign(this, update);
};
}

const getDefaultPrettierOptions = () => {
const prettier = cosmiconfig
.cosmiconfigSync("prettier", {
searchStrategy: "global",
})
.search();

if (prettier) {
return {
...prettier.config,
parser: "typescript",
};
}

return { ...CONSTANTS.PRETTIER_OPTIONS };
};
7 changes: 0 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ export const HTTP_CLIENT = {
AXIOS: "axios",
} as const;

export const PRETTIER_OPTIONS = {
printWidth: 120,
tabWidth: 2,
trailingComma: "all",
parser: "typescript",
} as const;

export const PROJECT_VERSION = packageJson.version;

export const RESERVED_BODY_ARG_NAMES = ["data", "body", "reqBody"];
Expand Down
Loading