diff --git a/shared/package.json b/shared/package.json index 3377f3d..ee97032 100644 --- a/shared/package.json +++ b/shared/package.json @@ -5,8 +5,8 @@ "type": "module", "scripts": { "prebuild": "barrelsby --directory src --delete --single", - "build:esm": "tsc -p tsconfig.esm.json", - "build:cjs": "tsc -p tsconfig.cjs.json", + "build:esm": "tsc -p tsconfig.esm.json && node scripts/postbuild-esm.cjs", + "build:cjs": "tsc -p tsconfig.cjs.json && find dist/cjs -name \"*.js\" -type f -exec sh -c 'mv \"$1\" \"${1%.js}.cjs\"' _ {} \\; && node scripts/postbuild.cjs", "build": "npm run prebuild && npm run build:esm && npm run build:cjs", "lint": "eslint ." }, @@ -14,7 +14,7 @@ ".": { "types": "./dist/types/index.d.ts", "import": "./dist/esm/index.js", - "require": "./dist/cjs/index.js" + "require": "./dist/cjs/index.cjs" } }, "types": "./dist/types/index.d.ts", diff --git a/shared/scripts/postbuild-esm.cjs b/shared/scripts/postbuild-esm.cjs new file mode 100644 index 0000000..e236e53 --- /dev/null +++ b/shared/scripts/postbuild-esm.cjs @@ -0,0 +1,30 @@ +const fs = require('fs'); +const path = require('path'); + +// Recursively process all .js files in esm directory +function processDirectory(dir) { + const files = fs.readdirSync(dir); + + files.forEach(file => { + const filePath = path.join(dir, file); + const stat = fs.statSync(filePath); + + if (stat.isDirectory()) { + processDirectory(filePath); + } else if (file.endsWith('.js')) { + // Read the file + let content = fs.readFileSync(filePath, 'utf8'); + + // Replace import/export statements to add .js extension + // Replace from "./path" with from "./path.js" (for relative imports only) + content = content.replace(/from ["'](\.[^"']*?)(? { + const filePath = path.join(dir, file); + const stat = fs.statSync(filePath); + + if (stat.isDirectory()) { + processDirectory(filePath); + } else if (file.endsWith('.cjs')) { + // Read the file + let content = fs.readFileSync(filePath, 'utf8'); + + // Replace require statements to add .cjs extension + content = content.replace(/require\("([^"]*?)(?