diff --git a/.gitignore b/.gitignore index d838984..7928a65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ npm-debug.* umd/index.js +umd/types.d.ts diff --git a/package.json b/package.json index 5e65c85..b09677b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" ], diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 0000000..8e5769d --- /dev/null +++ b/scripts/build.js @@ -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"), +); diff --git a/tests/typescript/test-global.ts b/tests/typescript/test-global.ts index dd3b0a8..838df6b 100644 --- a/tests/typescript/test-global.ts +++ b/tests/typescript/test-global.ts @@ -1,5 +1,8 @@ /// Long.fromValue(1); + var long = new Long(0, 0); long.toNumber(); + +function test(long: Long) {} diff --git a/tests/typescript/test-import.ts b/tests/typescript/test-import.ts index 1e2472a..dc3b440 100644 --- a/tests/typescript/test-import.ts +++ b/tests/typescript/test-import.ts @@ -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; diff --git a/tests/typescript/test-require.ts b/tests/typescript/test-require.ts index 82c2aeb..af0bd6c 100644 --- a/tests/typescript/test-require.ts +++ b/tests/typescript/test-require.ts @@ -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; diff --git a/types.d.ts b/types.d.ts index c8eaf14..7693ca4 100644 --- a/types.d.ts +++ b/types.d.ts @@ -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 diff --git a/umd/index.d.ts b/umd/index.d.ts index 50f20bc..5036689 100644 --- a/umd/index.d.ts +++ b/umd/index.d.ts @@ -1,3 +1,3 @@ -import { Long } from "../types.js"; +import { Long } from "./types.js"; export = Long; export as namespace Long;