Skip to content

Commit 4f4d7dc

Browse files
committed
change the output file structure and extension
1 parent 1308818 commit 4f4d7dc

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"url": "http://github.com/graphql/dataloader.git"
1818
},
1919
"type": "module",
20-
"main": "dist/cjs/index.js",
21-
"module": "dist/esm/index.js",
22-
"typings": "dist/esm/index.d.ts",
20+
"main": "dist/index.js",
21+
"module": "dist/index.mjs",
22+
"typings": "dist/index.d.ts",
2323
"scripts": {
2424
"test": "npm run lint && npm run check && npm run testonly",
2525
"test:ci": "npm run lint && npm run check && npm run testonly -- --coverage",
@@ -28,6 +28,7 @@
2828
"build:esm": "tsc -p tsconfig.esm.json",
2929
"build:cjs": "tsc -p tsconfig.cjs.json",
3030
"build": "npm run build:esm && npm run build:cjs",
31+
"postbuild": "node --experimental-strip-types ./post-build.ts",
3132
"watch": "node --watch-path=src --experimental-strip-types src/index.ts",
3233
"testonly": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest ",
3334
"prerelease": ". ./resources/prepublish.sh",

post-build.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { readdirSync, renameSync, rmSync } from 'fs';
2+
import { join, basename } from 'path';
3+
4+
function removeAllFiles(dirPath: string) {
5+
const files = readdirSync(dirPath, { withFileTypes: true })
6+
.filter(f => f.isFile())
7+
.map(f => join(f.parentPath, f.name));
8+
files.forEach(file => {
9+
rmSync(file, { recursive: true, force: true });
10+
});
11+
}
12+
13+
function moveAndRename(srcDir: string, destDir: string, newExt: string) {
14+
const files = readdirSync(srcDir, { withFileTypes: true })
15+
.filter(f => f.isFile())
16+
.map(f => join(f.parentPath, f.name));
17+
files.forEach(file => {
18+
const destFilePath = join(
19+
destDir,
20+
basename(file)
21+
.replace(/\.js\.map$/, `${newExt}.map`)
22+
.replace(/\.js$/, newExt),
23+
);
24+
25+
renameSync(file, destFilePath);
26+
console.log(`Moved and renamed: ${file} -> ${destFilePath}`);
27+
});
28+
29+
rmSync(srcDir, { recursive: true, force: true });
30+
}
31+
32+
const srcDirCjs = join(import.meta.dirname, './dist/cjs');
33+
const srcDirEsm = join(import.meta.dirname, './dist/esm');
34+
const destDir = join(import.meta.dirname, './dist');
35+
36+
removeAllFiles(destDir);
37+
moveAndRename(srcDirCjs, destDir, '.js');
38+
moveAndRename(srcDirEsm, destDir, '.mjs');

0 commit comments

Comments
 (0)