Skip to content

Commit

Permalink
Update build to fix CommonJS consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Dec 20, 2024
1 parent d24b36c commit d39c7e0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ config.gypi
!scripts/
!scripts/*js

# include dist js
# include dist .cjs, .mjs, .js, and types
!dist/**/*js
!dist/**/*js.map
!dist/**/*.d.ts
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
"homepage": "https://photostructure.github.io/fs-metadata/",
"type": "module",
"// exports": "https://nodejs.org/api/packages.html#conditional-exports",
"types": "./dist/types/index.d.ts",
"main": "./dist/cjs/index.cjs",
"module": "./dist/esm/index.js",
"exports": {
".": {
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.cjs"
},
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
}
"types": "./dist/types/index.d.ts",
"require": "./dist/cjs/index.cjs",
"import": "./dist/esm/index.js",
"default": "./dist/esm/index.js"
}
},
"repository": {
Expand All @@ -37,8 +36,9 @@
"prep": "run-s clean prebuild tsc",
"tsc": "run-p tsc:*",
"tsc:esm": "tsc -p tsconfig.esm.json",
"tsc:types": "tsc -p tsconfig.types.json",
"tsc:cjs": "tsc -p tsconfig.cjs.json",
"posttsc:cjs": "echo '{ \"type\": \"commonjs\" }' > dist/cjs/package.json",
"posttsc:cjs": "node scripts/fix-cjs.mjs",
"tsc:tests": "tsc -p tsconfig.json",
"watch": "tsc --watch --project tsconfig.watch.json",
"docs": "typedoc --out docs src/index.ts",
Expand Down
35 changes: 35 additions & 0 deletions scripts/fix-cjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { readFile, readdir, unlink, writeFile } from "node:fs/promises";
import { join } from "node:path";

async function fixCjsFiles(dir) {
const files = await readdir(dir, { withFileTypes: true });

for (const file of files) {
const fullPath = join(dir, file.name);

if (file.isDirectory()) {
await fixCjsFiles(fullPath);
continue;
}

// First read and fix content
if (file.name.endsWith(".js") || file.name.endsWith(".cjs")) {
const content = await readFile(fullPath, "utf8");
const fixedContent = content.replace(
/require\("(.+?)\.js"\)/gm,
'require("$1.cjs")',
);

// Write fixed content to .cjs file
const newPath = fullPath.replace(/\.js$/, ".cjs");
await writeFile(newPath, fixedContent);

// Remove old .js file if it exists
if (newPath !== fullPath) {
await unlink(fullPath);
}
}
}
}

await fixCjsFiles("./dist/cjs");
2 changes: 0 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"compilerOptions": {
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "node16",
"outDir": "./dist/esm/",
"outDir": "./dist/esm",
"moduleResolution": "node16"
},
"files": ["src/index.ts"]
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.esm.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"outDir": "./dist/types"
}
}

0 comments on commit d39c7e0

Please sign in to comment.