Skip to content

Commit

Permalink
fix: set conditional exports in package.json (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Dowling <[email protected]>
  • Loading branch information
andipaetzold and oliverdowling authored Nov 24, 2023
1 parent 3d80225 commit ef65273
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"main": "lib/node/index.js",
"browser": "lib/browser/index.js",
"types": "lib/types/index.d.ts",
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"node": "./lib/node/index.js",
"default": "./lib/browser/index.js"
},
"./package.json": "./package.json"
},
"sideEffects": false,
"type": "module",
"files": [
Expand Down
30 changes: 30 additions & 0 deletions test/packageJSON.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readFileSync, existsSync } from "node:fs";
import { it } from "mocha";
import { expect } from "chai";

function getPathsFromPackageJSON(): string[] {
const packageJSON = JSON.parse(readFileSync("package.json", "utf-8"));

const flatten = (obj: unknown): string[] => {
if (typeof obj !== "object" || !obj) {
return [];
}

const values = Object.values(obj);
return values.flatMap((value) => (typeof value === "string" ? [value] : flatten(value)));
};

const paths = [packageJSON["main"], packageJSON["browser"], packageJSON["types"], ...flatten(packageJSON["exports"])];

return paths;
}

describe("package.json", () => {
it("all entry points exist", () => {
const paths = getPathsFromPackageJSON();

for (const path of paths) {
expect(existsSync(path), `Path ${path} does not exist`).to.be.true;
}
});
});

0 comments on commit ef65273

Please sign in to comment.