Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

chore: build project to emit cjs and esm using rollup #93

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: lint
run: npm run lint
- name: compile typescript
run: npx tsc
run: npm run build && npx tsc
- name: test
run: npm run test
- name: generate coverage
Expand Down
2 changes: 1 addition & 1 deletion examples/bill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const zero = new Decimal128("0");
const one = new Decimal128("1");
Expand Down
2 changes: 1 addition & 1 deletion examples/floor.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

function floor(d: Decimal128): Decimal128 {
return d.round(0, "floor");
Expand Down
2 changes: 1 addition & 1 deletion examples/mortgage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";
import { pow } from "./pow.mjs";

const one = new Decimal128("1");
Expand Down
2 changes: 1 addition & 1 deletion examples/normalize.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

function normalize(d: Decimal128): string {
// Decimal128 object
Expand Down
2 changes: 1 addition & 1 deletion examples/pow.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

function pow(a: Decimal128, b: number): Decimal128 {
let result = a;
Expand Down
2 changes: 1 addition & 1 deletion examples/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Examples from the Intl.NumberFormat spec

import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

let minusOnePointFive = new Decimal128("-1.5");
let zeroPointFour = new Decimal128("0.4");
Expand Down
828 changes: 578 additions & 250 deletions package-lock.json

Large diffs are not rendered by default.

29 changes: 21 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
{
"name": "decimal128",
"version": "12.2.0",
"main": "src/decimal128.js",
"main": "dist/cjs/decimal128.cjs",
"module": "dist/esm/decimal128.mjs",
"typings": "dist/esm/decimal128.d.ts",
"exports": {
".": {
"import": "./dist/esm/decimal128.mjs",
"require": "./dist/cjs/decimal128.cjs",
Comment on lines +9 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check out nodejs/node#52174, this pattern is not ideal and we should use the alternative mentioned in the thread.

"types": "./dist/esm/decimal128.d.ts"
}
},
"type": "module",
"description": "Partial implementation of IEEE 754 Decimal128 decimal floating-point numbers",
"directories": {
"test": "tests"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/jessealama/decimal128.js.git"
},
"sideEffects": false,
"scripts": {
"build": "rollup -c",
"test": "jest",
"format": "prettier . --write",
"lint": "prettier . --check",
Expand All @@ -34,21 +47,21 @@
"author": "Jesse Alama <[email protected]>",
"license": "ISC",
"devDependencies": {
"@types/jest": "^29.5.1",
"@rollup/plugin-typescript": "^11.1.6",
"@types/jest": "^29.5.12",
"c8": "^9.1.0",
"jest": "^29.5.0",
"jest": "^29.7.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was updating jest and ts-jest necessary in this PR?

"jest-light-runner": "^0.6.0",
"prettier": "^3.0.0",
"ts-jest": "^29.1.0",
"rollup": "^4.14.1",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why we need tslib since importHelpers aren't used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likely a mistake, PR raised by Jesse in #121 no longer has this

"typescript": "^5.1.3"
},
"licenses": [
{
"type": "BSD-2-Clause",
"url": "https://opensource.org/license/bsd-2-clause/"
}
],
"exports": {
".": "./src/decimal128.mjs"
}
]
}
37 changes: 37 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @ts-check
import typescript from "@rollup/plugin-typescript";

/**
* @param {'esm' | 'cjs'} format
* @returns { import("rollup").RollupOptions }
*/
const createConfig = (format) => {
const extension = format === "esm" ? "mjs" : "cjs";
return {
input: "src/decimal128.mts",
output: [
{
sourcemap: true,
interop: "esModule",
preserveModules: true,
dir: `./dist/${format}`,
format: format,
entryFileNames: `[name].${extension}`,
},
],
plugins: [
typescript({
tsconfig: "tsconfig.build.json",
outDir: `dist/${format}`,
sourceMap: false,
}),
],
};
};

/**
* @type { import("rollup").RollupOptions[] }
*/
const config = [createConfig("esm"), createConfig("cjs")];

export default config;
2 changes: 1 addition & 1 deletion tests/abs.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
Expand Down
2 changes: 1 addition & 1 deletion tests/add.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
Expand Down
2 changes: 1 addition & 1 deletion tests/common.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { countSignificantDigits } from "../src/common.mjs";
import { countSignificantDigits } from "../dist/esm/common.mjs";

describe("significant digits", () => {
test("basic example", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/constructor.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;

Expand Down
2 changes: 1 addition & 1 deletion tests/divide.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

describe("division", () => {
test("simple example", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/equals.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const nan = new Decimal128("NaN");
Expand Down
2 changes: 1 addition & 1 deletion tests/lessthan.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const nan = new Decimal128("NaN");
Expand Down
2 changes: 1 addition & 1 deletion tests/multiply.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const examples = [
["123.456", "789.789", "97504.190784"],
Expand Down
2 changes: 1 addition & 1 deletion tests/neg.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
Expand Down
2 changes: 1 addition & 1 deletion tests/rational.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rational } from "../src/rational.mjs";
import { Rational } from "../dist/esm/rational.mjs";
describe("constructor", () => {
test("cannot divide by zero", () => {
expect(() => new Rational(1n, 0n)).toThrow(RangeError);
Expand Down
2 changes: 1 addition & 1 deletion tests/remainder.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

const a = "4.1";
const b = "1.25";
Expand Down
2 changes: 1 addition & 1 deletion tests/round.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";
import * as string_decoder from "string_decoder";
import { expectDecimal128 } from "./util.js";

Expand Down
2 changes: 1 addition & 1 deletion tests/subtract.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";
import { expectDecimal128 } from "./util.js";

const MAX_SIGNIFICANT_DIGITS = 34;
Expand Down
2 changes: 1 addition & 1 deletion tests/tostring.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

export function expectDecimal128(a, b) {
let lhs = a instanceof Decimal128 ? a.toString({ normalize: false }) : a;
Expand Down
2 changes: 1 addition & 1 deletion tests/valueof.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../dist/esm/decimal128.mjs";

describe("valueOf", () => {
test("throws unconditionally", () => {
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["./src"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, but I don't quite get this: which files were included so far? And why not just add the include statement to the original tsconfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which files were included so far

In the original tsconfig, I suppose, all files will be included.
I believe that is fine since the original tsconfig is mainly used by VS Code / ide.

why not just add the include statement to the original tsconfig?

Adding includes to the original tsconfig will prevent tsconfig from being applied to files outside of the src folder.


My intention here with tsconfig.buid.json include is to restrict rollup's typescript compiler include scope to files within src folder only

}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "nodenext" /* Specify what module code is generated. */,
"module": "NodeNext" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "nodenext" /* Specify how TypeScript looks up a file from a given module specifier. */,
"moduleResolution": "NodeNext" /* Specify how TypeScript looks up a file from a given module specifier. */,
jessealama marked this conversation as resolved.
Show resolved Hide resolved
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
Expand Down