Skip to content

Commit be983d3

Browse files
committed
fix: Separate ESM and UMD type definitions
1 parent 12f3988 commit be983d3

File tree

8 files changed

+45
-4
lines changed

8 files changed

+45
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
npm-debug.*
33
umd/index.js
4+
umd/types.d.ts

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333
},
3434
"scripts": {
35-
"build": "esm2umd Long index.js > umd/index.js && prettier --write umd/index.js",
35+
"build": "node scripts/build.js",
3636
"lint": "prettier --check .",
3737
"format": "prettier --write .",
3838
"test": "npm run test:unit && npm run test:typescript",
@@ -42,10 +42,11 @@
4242
"files": [
4343
"index.js",
4444
"index.d.ts",
45+
"types.d.ts",
4546
"umd/index.js",
4647
"umd/index.d.ts",
48+
"umd/types.d.ts",
4749
"umd/package.json",
48-
"types.d.ts",
4950
"LICENSE",
5051
"README.md"
5152
],

scripts/build.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
import esm2umd from "esm2umd";
5+
import prettier from "prettier";
6+
7+
const basePath = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
8+
const esmPath = path.join(basePath, "index.js");
9+
const umdPath = path.join(basePath, "umd", "index.js");
10+
11+
const esmSource = fs.readFileSync(esmPath, "utf8");
12+
const umdSource = esm2umd("Long", esmSource);
13+
14+
async function formatWithPrettier(source, filepath) {
15+
const options = await prettier.resolveConfig(filepath);
16+
return await prettier.format(source, { ...options, filepath });
17+
}
18+
19+
const prettierUmdSource = await formatWithPrettier(umdSource, umdPath);
20+
21+
fs.writeFileSync(umdPath, prettierUmdSource);
22+
23+
fs.copyFileSync(
24+
path.join(basePath, "types.d.ts"),
25+
path.join(basePath, "umd", "types.d.ts"),
26+
);

tests/typescript/test-global.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/// <reference path="../../umd/index.d.ts" />
22

33
Long.fromValue(1);
4+
45
var long = new Long(0, 0);
56
long.toNumber();
7+
8+
function test(long: Long) {}

tests/typescript/test-import.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Long from "../../index.js";
22

33
Long.fromValue(1);
4+
45
var long = new Long(0, 0);
56
long.toNumber();
67

8+
function test(long: Long) {}
9+
710
export default Long;

tests/typescript/test-require.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Long = require("../../umd/index.js");
22

33
Long.fromValue(1);
4+
45
var long = new Long(0, 0);
56
long.toNumber();
67

7-
export default Long;
8+
function test(long: Long) {}
9+
10+
export = Long;

types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Common type definitions for both the ESM and UMD variants. The ESM variant
2+
// reexports the Long class as its default export, whereas the UMD variant makes
3+
// the Long class a whole-module export with a global variable fallback.
4+
15
type LongLike =
26
| Long
37
| number

umd/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { Long } from "../types.js";
1+
import { Long } from "./types.js";
22
export = Long;
33
export as namespace Long;

0 commit comments

Comments
 (0)