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 12f3988 commit be983d3
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 4 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 @@ -32,7 +32,7 @@
}
},
"scripts": {
"build": "esm2umd Long 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 @@ -42,10 +42,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("Long", 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"),
);
3 changes: 3 additions & 0 deletions tests/typescript/test-global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// <reference path="../../umd/index.d.ts" />

Long.fromValue(1);

var long = new Long(0, 0);
long.toNumber();

function test(long: Long) {}
3 changes: 3 additions & 0 deletions tests/typescript/test-import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Long from "../../index.js";

Long.fromValue(1);

var long = new Long(0, 0);
long.toNumber();

function test(long: Long) {}

export default Long;
5 changes: 4 additions & 1 deletion tests/typescript/test-require.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Long = require("../../umd/index.js");

Long.fromValue(1);

var long = new Long(0, 0);
long.toNumber();

export default Long;
function test(long: Long) {}

export = Long;
4 changes: 4 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Common type definitions for both the ESM and UMD variants. The ESM variant
// reexports the Long class as its default export, whereas the UMD variant makes
// the Long class a whole-module export with a global variable fallback.

type LongLike =
| Long
| number
Expand Down
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 { Long } from "../types.js";
import { Long } from "./types.js";
export = Long;
export as namespace Long;

0 comments on commit be983d3

Please sign in to comment.