Skip to content

Commit

Permalink
fix: Separate ESM and UMD type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Feb 17, 2025
1 parent 2a9bea9 commit e7055ca
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
npm-debug.*
umd/index.js
umd/types.d.ts
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"license": "BSD-3-Clause",
"scripts": {
"build": "esm2umd bcrypt index.js > umd/index.js && prettier --write umd/index.js",
"build": "node scripts/build.js",
"lint": "prettier --check .",
"format": "prettier --write .",
"test": "npm run test:unit && npm run test:typescript",
Expand All @@ -56,10 +56,11 @@
"files": [
"index.js",
"index.d.ts",
"types.d.ts",
"umd/index.js",
"umd/index.d.ts",
"umd/types.d.ts",
"umd/package.json",
"types.d.ts",
"LICENSE",
"README.md"
],
Expand Down
26 changes: 26 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import esm2umd from "esm2umd";
import prettier from "prettier";

const basePath = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const esmPath = path.join(basePath, "index.js");
const umdPath = path.join(basePath, "umd", "index.js");

const esmSource = fs.readFileSync(esmPath, "utf8");
const umdSource = esm2umd("bcrypt", esmSource);

async function formatWithPrettier(source, filepath) {
const options = await prettier.resolveConfig(filepath);
return await prettier.format(source, { ...options, filepath });
}

const prettierUmdSource = await formatWithPrettier(umdSource, umdPath);

fs.writeFileSync(umdPath, prettierUmdSource);

fs.copyFileSync(
path.join(basePath, "types.d.ts"),
path.join(basePath, "umd", "types.d.ts"),
);
2 changes: 1 addition & 1 deletion umd/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as bcrypt from "../types.js";
import * as bcrypt from "./types.js";
export = bcrypt;
export as namespace bcrypt;

0 comments on commit e7055ca

Please sign in to comment.