Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#### Features 🚀

- Fixed ESM and CJS builds of d2.js [#2286](https://github.com/terrastruct/d2/issues/2286) + [#1448](https://github.com/terrastruct/d2/issues/1448)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There's a separate changelog for d2js. Hasn't been updated in a bit, but that's where it should go: https://github.com/terrastruct/d2/blob/master/d2js/js/CHANGELOG.md

- ASCII renders. Output `txt` for d2 to render diagrams as ASCII art [#2572](https://github.com/terrastruct/d2/pull/2572)
- `cross` arrowhead shape is available [#2190](https://github.com/terrastruct/d2/pull/2190)
- `style.underline` support for class fields and methods [#2544](https://github.com/terrastruct/d2/pull/2544)
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.
33 changes: 16 additions & 17 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:all": "bun run test",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👍 , though I think it's just bun test now.

▶ bun run test
error: "/bin/test" exited with code 1
note: a package.json script "test" was not found

"dev": "bun --watch dev-server.js"
},
"keywords": [
Expand All @@ -56,6 +54,7 @@
],
"license": "MPL-2.0",
"devDependencies": {
"bun": "latest"
"bun": "latest",
"typescript": "^5.9.2"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Where's this used?

}
}
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Loading