Skip to content

Commit

Permalink
feat(src): 💡 create ci4 vite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-gujjjar committed Jan 7, 2024
1 parent cd80f98 commit 2e7e6bc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import path from "path";
import {loadEnv, type ResolvedConfig} from "vite";
import {readFileSync} from "fs";
import {fileURLToPath} from "url";

import type {Ci4} from "./types/plugin";
import {highlightVersion} from "./utils/decorate";
import {frameworkVersion, pluginVersion} from "./utils/version";

const ci4: Ci4 = (_config: string | string[]) => {
let config: ResolvedConfig;

return {
name: "ci4",
enforce: "post",
config: (userConfig) => {
return userConfig;
},
configResolved: (resolveConfig) => {
config = resolveConfig;
},
configureServer: (server) => {
const envDir = config.envDir || process.cwd();
const appUrl =
loadEnv(config.mode, envDir, "app.baseURL")["app.baseURL"] ?? "undefined";

console.log(loadEnv(config.mode, envDir, "app.baseURL")["app.baseURL"]);

server.httpServer?.once("listening", () => {
setTimeout(() => {
server.config.logger.info("");

frameworkVersion()
.then((version) => {
server.config.logger.info(highlightVersion("CodeIgniter", version));

return pluginVersion();
})
.then((version) => {
server.config.logger.info(highlightVersion("Plugin ", version));

return version;
});
}, 100);
});

return () =>
server.middlewares.use((req, res, next) => {
if (req.url === "/index.html") {
res.statusCode = 404;

res.end(
readFileSync(path.join(dirname(), "index.html"))
.toString()
.replace(/{{ APP_URL }}/g, appUrl)
);
}

next();
});
},
};
};

function dirname(): string {
return fileURLToPath(new URL(".", import.meta.url));
}

export default ci4;
7 changes: 7 additions & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type {ConfigEnv, Plugin, UserConfig} from "vite";

interface Ci4Plugin extends Plugin {
config: (config: UserConfig, env: ConfigEnv) => UserConfig;
}

export declare type Ci4 = (_config: string | string[]) => Ci4Plugin;

0 comments on commit 2e7e6bc

Please sign in to comment.