Skip to content

Commit

Permalink
Configure codegen configs to skip duplicate codegen when file already…
Browse files Browse the repository at this point in the history
… exists (#197)

* Configure codegen configs to skip duplicate codegen when file already exists

* Add optional env-oriented file to improve performance
  • Loading branch information
mhevyk authored Sep 16, 2024
1 parent 894a6ef commit 12195d6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
14 changes: 14 additions & 0 deletions checkExistingCodegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from "fs";
import path from "path";

export default function checkExistingCodegen(isProduction: boolean) {
const generatesFile = isProduction
? "dist/temp_api/index.tsx"
: "src/api/generated/index.tsx";
const generatesFilePath = path.resolve(__dirname, generatesFile);

return {
codegenFileExists: fs.existsSync(generatesFilePath),
generatesFile,
};
}
22 changes: 17 additions & 5 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@ import fs from "fs";
import mri from "mri";
import path from "path";

import checkExistingCodegen from "./checkExistingCodegen";

const parsedArgv = mri(process.argv);

const mode = parsedArgv.mode ?? process.env.NODE_ENV ?? "development";
const mode = parsedArgv.mode ?? process.env.NODE_ENV;
const isProduction = mode === "production";

const generatesFile = isProduction
? "dist/temp_api/index.tsx"
: "src/api/generated/index.tsx";
const { generatesFile, codegenFileExists } = checkExistingCodegen(isProduction);

const isViteCommand = process.env.npm_lifecycle_script.includes("vite");

// if command source is not vite and codegen file exists, then we should skip codegen step because vite uses codegen plugin. That means process.exit(1) will terminate later vite command, but it is already handled inside vite.config.ts
if (codegenFileExists && !isViteCommand) {
process.exit(0);
}

const envFiles = [".env"];

if (mode) {
envFiles.push(`.env.${mode}`);
}

const envFiles = [".env", `.env.${mode}`];
for (const envFile of envFiles) {
const envPath = path.resolve(__dirname, envFile);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
"include": ["vite.config.ts", "checkExistingCodegen.ts", "codegen.ts"]
}
9 changes: 8 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ import codegen from "vite-plugin-graphql-codegen";
import string from "vite-plugin-string";
import svgr from "vite-plugin-svgr";

import checkExistingCodegen from "./checkExistingCodegen";

const BUILD_DIR = "dist";

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd());
const isProductionMode = mode === "production";
const isBuildInNotProductionMode = command === "build" && !isProductionMode;

const buildAliases = isProductionMode && {
"@/api/generated": path.resolve(__dirname, `${BUILD_DIR}/temp_api`),
};

const { codegenFileExists } = checkExistingCodegen(isProductionMode);

return {
build: {
sourcemap: command === "build" && !isProductionMode,
sourcemap: isBuildInNotProductionMode,
outDir: BUILD_DIR,
rollupOptions: {
output: {
Expand Down Expand Up @@ -53,7 +58,9 @@ export default defineConfig(({ mode, command }) => {
string(),
svgr({ include: "**/*.svg" }),
codegen({
runOnStart: !codegenFileExists,
throwOnStart: true,
runOnBuild: isBuildInNotProductionMode,
configOverrideOnBuild: {
hooks: {
afterAllFileWrite: () => {
Expand Down

0 comments on commit 12195d6

Please sign in to comment.