Skip to content

Commit 6f51b05

Browse files
committed
Automatically list exports
1 parent c621540 commit 6f51b05

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
},
99
"main": "dist/lib/index.js",
1010
"types": "dist/lib/index.d.ts",
11+
"module": "dist/lib/index.js",
12+
"exports": {
13+
".": "./dist/lib/index.js",
14+
"./next": "./dist/next.js",
15+
"./mui": "./dist/mui.js",
16+
"./Tabs": "./dist/Tabs.js",
17+
"./Header": "./dist/Header.js",
18+
"./DarkModeSwitch": "./dist/DarkModeSwitch.js",
19+
"./Alert": "./dist/Alert.js"
20+
},
1121
"scripts": {
1222
"build": "ts-node src/scripts/build",
1323
"yarn-link": "ts-node src/scripts/yarn-link.ts",
@@ -23,7 +33,8 @@
2333
"grant_exec_perms": "node dist/bin/tools/grant_exec_perms",
2434
"storybook": "yarn build && yarn only-include-used-icons && start-storybook -p 6006",
2535
"build-storybook": "yarn build && yarn only-include-used-icons && build-storybook",
26-
"only-include-used-icons": "node dist/bin/only-include-used-icons.js"
36+
"only-include-used-icons": "node dist/bin/only-include-used-icons.js",
37+
"list-exports": "ts-node src/scripts/list-exports.ts"
2738
},
2839
"bin": {
2940
"copy-dsfr-to-public": "dist/bin/copy-dsfr-to-public.js",
@@ -39,7 +50,7 @@
3950
},
4051
"husky": {
4152
"hooks": {
42-
"pre-commit": "lint-staged -v"
53+
"pre-commit": "yarn list-exports && lint-staged -v"
4354
}
4455
},
4556
"author": "u/garronej",

src/scripts/build.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,21 @@ import { oppa } from "oppa";
104104
return {
105105
...packageJsonParsed,
106106
"main": packageJsonParsed["main"].replace(/^dist\//, ""),
107-
"types": packageJsonParsed["types"].replace(/^dist\//, "")
107+
"types": packageJsonParsed["types"].replace(/^dist\//, ""),
108+
"module": packageJsonParsed["module"].replace(/^dist\//, ""),
109+
"exports": Object.fromEntries(
110+
Object.entries(packageJsonParsed["exports"]).map(([path, obj]) => [
111+
path,
112+
Object.fromEntries(
113+
Object.entries(obj as Record<string, string>).map(
114+
([type, path]) => [
115+
type,
116+
path.replace(/^\.\/dist\//, "./")
117+
]
118+
)
119+
)
120+
])
121+
)
108122
};
109123
})(),
110124
null,

src/scripts/list-exports.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as fs from "fs";
2+
import { join as pathJoin } from "path";
3+
import { getProjectRoot } from "../bin/tools/getProjectRoot";
4+
import { exclude } from "tsafe/exclude";
5+
6+
const packageJsonFilePath = pathJoin(getProjectRoot(), "package.json");
7+
8+
const packageJsonParsed = JSON.parse(fs.readFileSync(packageJsonFilePath).toString("utf8"));
9+
10+
fs.writeFileSync(
11+
packageJsonFilePath,
12+
Buffer.from(
13+
JSON.stringify(
14+
{
15+
...packageJsonParsed,
16+
"exports": {
17+
".": `./${packageJsonParsed["module"]}`,
18+
...Object.fromEntries(
19+
fs
20+
.readdirSync(pathJoin(getProjectRoot(), "src"))
21+
.map(basename => {
22+
const match = basename.match(/^([^.]+)\.tsx?$/);
23+
24+
if (match === null) {
25+
return undefined;
26+
}
27+
28+
return match[1];
29+
})
30+
.filter(exclude(undefined))
31+
.sort()
32+
.reverse()
33+
.map(name => [`./${name}`, `./dist/${name}.js`])
34+
)
35+
}
36+
},
37+
null,
38+
2
39+
),
40+
"utf8"
41+
)
42+
);

0 commit comments

Comments
 (0)