Skip to content

Commit

Permalink
fix(createAspNetCoreHmrPlugin) #289: copy public folder on startup so…
Browse files Browse the repository at this point in the history
… that static assets are not missing.
  • Loading branch information
ascott18 committed Aug 22, 2023
1 parent 09491c0 commit 264f44c
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/coalesce-vue/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MagicString from "magic-string";

import path from "node:path";
import os from "node:os";
import { existsSync } from "node:fs";
import fs from "node:fs";
import { readFile, writeFile, mkdir } from "node:fs/promises";
import { spawn } from "node:child_process";
import { TLSSocket } from "node:tls";
Expand Down Expand Up @@ -414,21 +414,24 @@ function getHtmlTargetDir(config: ResolvedConfig) {
}
async function writeHtml(server: ViteDevServer) {
const htmlSourceFileName = server.config.root + "/index.html";
if (existsSync(htmlSourceFileName)) {
const targetDir = getHtmlTargetDir(server.config);

fs.mkdirSync(targetDir, { recursive: true });

// Copy static public assets too
if (server.config.publicDir) {
copyDir(server.config.publicDir, targetDir);
}

if (fs.existsSync(htmlSourceFileName)) {
let outputHtml = await readFile(htmlSourceFileName, "utf-8");
outputHtml = await server.transformIndexHtml(
"/index.html",
outputHtml,
"/"
);

const targetDir = getHtmlTargetDir(server.config);

try {
if (!existsSync(targetDir)) {
await mkdir(targetDir);
}

await writeFile(path.join(targetDir, "index.html"), outputHtml, "utf-8");
server.config.logger.info(` Coalesce: Wrote index.html to ${targetDir}`);
} catch (e) {
Expand All @@ -439,6 +442,19 @@ async function writeHtml(server: ViteDevServer) {
}
}

function copyDir(srcDir: string, destDir: string) {
for (const file of fs.readdirSync(srcDir)) {
const srcFile = path.resolve(srcDir, file);
const destFile = path.resolve(destDir, file);
const stat = fs.statSync(srcFile);
if (stat.isDirectory()) {
copyDir(srcFile, destFile);
} else {
fs.copyFileSync(srcFile, destFile);
}
}
}

/** Create a plugin that will apply transforms to work around
* https://github.com/rollup/rollup/issues/4637, eliminating
* errors in production caused by class names getting mangled by Rollup
Expand All @@ -454,7 +470,9 @@ export function createClassNameFixerPlugin() {
// Auto set esbuild to keep names as well,
// which is also an important part of making vue-class-component work.
config.esbuild ??= {};
config.esbuild.keepNames ??= true;
if (config.esbuild) {
config.esbuild.keepNames ??= true;
}
},
configResolved(config) {
command = config.command;
Expand Down Expand Up @@ -521,7 +539,7 @@ export async function getCertPaths(certName?: string) {
const certFilePath = path.join(baseFolder, `${certificateName}.pem`);
const keyFilePath = path.join(baseFolder, `${certificateName}.key`);

let valid = existsSync(certFilePath) && existsSync(keyFilePath);
let valid = fs.existsSync(certFilePath) && fs.existsSync(keyFilePath);
let certContent;

if (valid) {
Expand Down

0 comments on commit 264f44c

Please sign in to comment.