Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use esbuild to bundle dependencies and minify source #70

Merged
merged 17 commits into from
Oct 3, 2023
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ This repository provides a self-hosted integration for Smile ID on the Web. It e
- `.gitignore`: Specifies files to ignore in git. These include [./dist](./dist) and [./build](./build).
- `.nvmrc`: Node Version Manager configuration file.
- `.nycrc`: [NYC](https://github.com/istanbuljs/nyc) configuration file.
- `esbuild.js`: [esbuild](https://esbuild.github.io/) configuration file.

## Testing

Expand Down
110 changes: 110 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const fs = require("fs");
const path = require("path");
const esbuild = require("esbuild");
const rimraf = require("rimraf");

/**
* Ensures a directory exists. If not, creates it.
* @param {string} dirPath - The path to the directory.
*/
const ensureDirSync = (dirPath) => {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
};

/**
* Recursively copies files from source to destination with a filter option.
* @param {string} srcDir - The source directory.
* @param {string} destDir - The destination directory.
* @param {Function} filterFn - A function to filter which files to copy.
*/
const copySync = (srcDir, destDir, filterFn) => {
const entries = fs.readdirSync(srcDir, { withFileTypes: true });

for (const entry of entries) {
const srcPath = path.join(srcDir, entry.name);
const destPath = path.join(destDir, entry.name);

if (entry.isDirectory()) {
ensureDirSync(destPath);
copySync(srcPath, destPath, filterFn);
} else if (!filterFn || filterFn(entry.name)) {
fs.copyFileSync(srcPath, destPath);
}
}
};

/**
* Copies specific files from source to destination based on a pattern.
* @param {string} srcDir - The source directory.
* @param {string} pattern - A wildcard pattern to filter files. e.g., '*.html'
* @param {string} destDir - The destination directory.
*/
const copyFiles = (srcDir, pattern, destDir) => {
const files = fs
.readdirSync(srcDir)
.filter((file) => file.match(new RegExp(pattern.replace("*", ".*"))));
files.forEach((file) => {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
fs.copyFileSync(srcPath, destPath);
});
};

/**
* Performs preparatory tasks for production mode.
* Clears the 'dist' directory, ensures it exists,
* and copies necessary files.
*/
const prebuildDist = () => {
rimraf.sync("dist");
ensureDirSync("dist");
copySync("src", "dist", (file) => !file.endsWith(".js"));
};

/**
* Performs preparatory tasks for development mode.
* Clears the 'build' directory, ensures it exists,
* copies necessary files, and copies HTML pages from cypress.
*/
const prebuild = () => {
rimraf.sync("build");
ensureDirSync("build");
copySync("src", "build", (file) => !file.endsWith(".js"));
copyFiles("cypress/pages", "*.html", "build");
};

const files = fs.readdirSync("src/js").filter((file) => file.endsWith(".js"));

if (process.env.NODE_ENV === "development") {
prebuild();
} else {
prebuildDist();
}

const devOptions = {
bundle: true,
};

const prodOptions = {
bundle: true,
minify: true,
};

files.forEach((file) => {
const baseName = path.basename(file, ".js");
if (process.env.NODE_ENV === "development") {
esbuild.build({
...devOptions,
entryPoints: [`src/js/${file}`],
outfile: `build/js/${baseName}.min.js`,
});
} else {
esbuild.build({
...prodOptions,
entryPoints: [`src/js/${file}`],
outfile: `dist/js/${baseName}.min.js`,
});
}
});
13 changes: 0 additions & 13 deletions libs/jszip.min.js

This file was deleted.

9 changes: 0 additions & 9 deletions libs/validate.min.js

This file was deleted.

Loading