Skip to content

Commit

Permalink
Fix .vite-envs.json generation
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Feb 19, 2024
1 parent ec631f7 commit 4376519
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function viteEnvs() {
| undefined;
}
| undefined = undefined;
let htmlPre: string | undefined = undefined;

const plugin = {
"name": "vite-envs",
Expand Down Expand Up @@ -170,33 +171,8 @@ export function viteEnvs() {
const { baseBuildTimeEnv, env, envLocal, buildInfos, acceptedEnvVarNames } =
resultOfConfigResolved;

create_vite_envs_meta_file: {
if (buildInfos === undefined) {
break create_vite_envs_meta_file;
}

const { assetsUrlPath, distDirPath } = buildInfos;

const viteEnvsMeta: ViteEnvsMeta = {
"version": JSON.parse(
fs
.readFileSync(pathJoin(getThisCodebaseRootDirPath(), "package.json"))
.toString("utf8")
).version,
assetsUrlPath,
"htmlPre": html,
env,
baseBuildTimeEnv
};

if (!fs.existsSync(distDirPath)) {
fs.mkdirSync(distDirPath, { "recursive": true });
}

fs.writeFileSync(
pathJoin(distDirPath, viteEnvsMetaFileBasename),
JSON.stringify(viteEnvsMeta, undefined, 4)
);
if (buildInfos !== undefined) {
htmlPre = html;
}

const mergedEnv = {
Expand Down Expand Up @@ -226,6 +202,42 @@ export function viteEnvs() {

return $.html();
}
},
"closeBundle": async () => {
assert(resultOfConfigResolved !== undefined);

const { baseBuildTimeEnv, env, buildInfos } = resultOfConfigResolved;

if (buildInfos === undefined) {
return;
}

assert(htmlPre !== undefined);

const { assetsUrlPath, distDirPath } = buildInfos;

const viteEnvsMeta: ViteEnvsMeta = {
"version": JSON.parse(
fs
.readFileSync(pathJoin(getThisCodebaseRootDirPath(), "package.json"))
.toString("utf8")
).version,
assetsUrlPath,
env,
baseBuildTimeEnv,
htmlPre
};

if (!fs.existsSync(distDirPath)) {
fs.mkdirSync(distDirPath, { "recursive": true });
}

console.log(pathJoin(distDirPath, viteEnvsMetaFileBasename));

fs.writeFileSync(
pathJoin(distDirPath, viteEnvsMetaFileBasename),
Buffer.from(JSON.stringify(viteEnvsMeta, null, 4), "utf8")
);
}
} satisfies Plugin;

Expand Down

0 comments on commit 4376519

Please sign in to comment.