From 264f44c8ec850f45aaeb2ca59e4d48eac37b1150 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 22 Aug 2023 12:45:40 -0700 Subject: [PATCH] fix(createAspNetCoreHmrPlugin) #289: copy public folder on startup so that static assets are not missing. --- src/coalesce-vue/src/build.ts | 38 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/coalesce-vue/src/build.ts b/src/coalesce-vue/src/build.ts index 749d9dee6..ca63e00d0 100644 --- a/src/coalesce-vue/src/build.ts +++ b/src/coalesce-vue/src/build.ts @@ -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"; @@ -414,7 +414,16 @@ 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", @@ -422,13 +431,7 @@ async function writeHtml(server: ViteDevServer) { "/" ); - 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) { @@ -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 @@ -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; @@ -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) {