Skip to content
Open
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
3 changes: 2 additions & 1 deletion d2js/js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ include changes to the main d2 project.**

## Next

- Fix TypeScript signatures
- Fixed ESM and CJS builds of d2.js [#2286](https://github.com/terrastruct/d2/issues/2286) + [#1448](https://github.com/terrastruct/d2/issues/1448)


## [0.1.22]
### March 20, 2025
Expand Down
2 changes: 1 addition & 1 deletion d2js/js/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { build } from "bun";
import { copyFile, mkdir, writeFile, readFile, rm } from "node:fs/promises";
import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";

const __dirname = new URL(".", import.meta.url).pathname;
Expand Down
Binary file modified d2js/js/bun.lockb
Binary file not shown.
34 changes: 16 additions & 18 deletions d2js/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,33 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"main": "./dist/node-cjs/index.js",
"module": "./dist/node-esm/index.js",
"types": "./index.d.ts",
"exports": {
".": {
"types": "./index.d.ts",
"browser": "./dist/browser/index.js",
"import": {
"browser": "./dist/browser/index.js",
"default": "./dist/node-esm/index.js",
"types": "./index.d.ts"
},
"require": {
"default": "./dist/node-cjs/index.js",
"types": "./index.d.ts"
},
"import": "./dist/node-esm/index.js",
"require": "./dist/node-cjs/index.js",
"default": "./dist/node-esm/index.js"
},
"./worker": "./dist/browser/worker.js"
"./worker": {
"browser": "./dist/browser/worker.js",
"import": "./dist/node-esm/worker.js",
"require": "./dist/node-cjs/worker.js",
"default": "./dist/node-esm/worker.js"
}
},
"files": [
"dist",
"index.d.ts"
],
"types": "./index.d.ts",
"scripts": {
"build": "./make.sh build",
"test": "bun test test/unit",
"test:unit": "bun test test/unit",
"test:integration": "bun test test/integration",
"test:all": "bun run test && bun run test:integration",
"test": "bun run test:unit && bun run test:integration",
"dev": "bun --watch dev-server.js"
},
"keywords": [
Expand All @@ -56,6 +54,6 @@
],
"license": "MPL-2.0",
"devDependencies": {
"bun": "latest"
}
"bun": "latest"
}
}
39 changes: 37 additions & 2 deletions d2js/js/test/integration/cjs.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import { expect, test, describe } from "bun:test";
import { describe, expect, test } from "bun:test";

describe("D2 CJS Integration", () => {
test("can require and use CJS build", async () => {
test("can require main entry point without error", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

these tests are still importing from the whole path, is there a way to test that it matches what the module/main path is defined as? e.g. require(d2js) and it resolves to the right one. otherwise it looks like there's no tests that exercise the change for module/main export paths.

expect(() => {
const module = require("../../dist/node-cjs/index.js");
expect(module).toBeDefined();
expect(module.D2).toBeDefined();
expect(typeof module.D2).toBe("function");
}).not.toThrow();
});

test("worker module file exists", () => {
const fs = require("fs");
const path = require("path");
const workerPath = path.resolve(__dirname, "../../dist/node-cjs/worker.js");
expect(fs.existsSync(workerPath)).toBe(true);
});

test("exported D2 class is constructable", () => {
const { D2 } = require("../../dist/node-cjs/index.js");
expect(() => new D2()).not.toThrow();
});

test("module exports match expected structure", () => {
const module = require("../../dist/node-cjs/index.js");
expect(module).toHaveProperty("D2");
expect(typeof module.D2).toBe("function");
});

test("can access both named and default exports", () => {
const module = require("../../dist/node-cjs/index.js");
const { D2 } = require("../../dist/node-cjs/index.js");

expect(module.D2).toBe(D2);
expect(module.D2).toBeDefined();
});

test("can compile a diagram", async () => {
Comment on lines +4 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems to have redundancy. how many of these are actually necessary (was failing before) if the "can compile a diagram" test was passing?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(same reply to the esm test changes)

const { D2 } = require("../../dist/node-cjs/index.js");
const d2 = new D2();
const result = await d2.compile("x -> y");
Expand Down
29 changes: 27 additions & 2 deletions d2js/js/test/integration/esm.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import { expect, test, describe } from "bun:test";
import { D2 } from "../../dist/node-esm/index.js";
import { describe, expect, test } from "bun:test";

describe("D2 ESM Integration", () => {
test("can import main entry point without error", async () => {
const module = await import("../../dist/node-esm/index.js");
expect(module).toBeDefined();
expect(module.D2).toBeDefined();
expect(typeof module.D2).toBe("function");
});

test("worker module file exists", () => {
const fs = require("fs");
const path = require("path");
const workerPath = path.resolve(__dirname, "../../dist/node-esm/worker.js");
expect(fs.existsSync(workerPath)).toBe(true);
});

test("exported D2 class is constructable", () => {
return import("../../dist/node-esm/index.js").then(({ D2 }) => {
expect(() => new D2()).not.toThrow();
});
});

test("can import all exports from package.json exports field", async () => {
const mainModule = await import("../../index.d.ts");
expect(mainModule).toBeDefined();
});

test("can import and use ESM build", async () => {
const { D2 } = await import("../../dist/node-esm/index.js");
const d2 = new D2();
const result = await d2.compile("x -> y");
expect(result.diagram).toBeDefined();
Expand Down