From 9dd692cbf27c9e92545d31ae841afd1139a14ba8 Mon Sep 17 00:00:00 2001 From: Torben Brenner Date: Mon, 28 Oct 2024 16:22:30 +0100 Subject: [PATCH] fix: ensure dist directories structure for watch --- vite.config.ts | 50 +++----------------------------------------------- 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index b8fd4374..cc75d417 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -13,7 +13,6 @@ export default defineConfig({ root: "./packages/lib/", build: { outDir: "../../dist", - emptyOutDir: true, lib: { entry: "./index.ts", // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -21,9 +20,9 @@ export default defineConfig({ name: pkg.name.replace(/-./g, (char) => char[1].toUpperCase()), fileName: (format) => ({ - es: `${pkg.name}.js`, - esm: `${pkg.name}.min.js`, - umd: `${pkg.name}.umd.js`, + es: `${pkg.name.replace("@samply/", "")}.js`, + esm: `${pkg.name.replace("@samply/", "")}.min.js`, + umd: `${pkg.name.replace("@samply/", "")}.umd.js`, })[format], }, rollupOptions: { @@ -45,7 +44,6 @@ export default defineConfig({ }), dts({ insertTypesEntry: true, - outDir: "../../dist", include: ["**/types/*.ts"], afterBuild: afterBuild, }), @@ -81,14 +79,6 @@ function minifyEs(): PluginOption { */ function afterBuild(): void { concatenateDeclarationFiles("./dist/src/types/"); - - /** - * Building somehow adds another @samply folder to the dist folder so this workaround is needed - * to move the files to the root of the dist folder and delete the unnecessary folder - */ - if (fs.existsSync("./dist/@samply/")) { - restructureDirectory("./dist/@samply/"); - } } /** @@ -107,40 +97,6 @@ function concatenateDeclarationFiles(folderPath: string): void { fs.rmSync("./dist/src", { recursive: true, force: true }); } -/** - * Restructure the directory to match the npm package structure - * this removes the @samply folder and moves the files to the root of the dist folder - * @param path the path where the files are located - */ -function restructureDirectory(path: string): void { - moveFile(`${path}lens.js`, "./dist/lens.js"); - moveFile(`${path}lens.min.js`, "./dist/lens.min.js"); - moveFile(`${path}lens.umd.js`, "./dist/lens.umd.js"); -} - -/** - * Moves a file from the oldFile to the target - * Removes the @samply folder if it is empty - * @param oldFile the path of the old file - * @param target the path of the target file - */ -function moveFile(oldFile: string, target: string): void { - let attempts = 0; - const interval = setInterval(() => { - attempts++; - if (fs.readFileSync(oldFile, "utf-8").length > 0) { - fs.renameSync(oldFile, target); - clearInterval(interval); - } else if (attempts > 10) { - clearInterval(interval); - throw new Error("File not found"); - } - if (fs.readdirSync("./dist/@samply/").length === 0) { - fs.rmSync("./dist/@samply/", { recursive: true, force: true }); - } - }, 1000); -} - /** * removes all import statements from a file * @param filePath the path of the file