Skip to content

Commit

Permalink
fix: ensure dist directories structure for watch
Browse files Browse the repository at this point in the history
  • Loading branch information
torbrenner committed Oct 28, 2024
1 parent 8c1dd83 commit 9dd692c
Showing 1 changed file with 3 additions and 47 deletions.
50 changes: 3 additions & 47 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ export default defineConfig({
root: "./packages/lib/",
build: {
outDir: "../../dist",
emptyOutDir: true,
lib: {
entry: "./index.ts",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formats: bundleComponents ? (["es", "esm", "umd"] as any) : ["es"],
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: {
Expand All @@ -45,7 +44,6 @@ export default defineConfig({
}),
dts({
insertTypesEntry: true,
outDir: "../../dist",
include: ["**/types/*.ts"],
afterBuild: afterBuild,
}),
Expand Down Expand Up @@ -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/");
}
}

/**
Expand All @@ -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
Expand Down

0 comments on commit 9dd692c

Please sign in to comment.