Skip to content

Commit ce65ca8

Browse files
committed
fix exports
1 parent 2f99aa0 commit ce65ca8

File tree

6 files changed

+150
-3
lines changed

6 files changed

+150
-3
lines changed

.fatherrc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { defineConfig } from "father";
22

33
export default defineConfig({
4-
cjs: {},
5-
esm: {},
4+
esm: {
5+
output: "es",
6+
},
67
targets: {
78
node: 14,
89
},

es/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { NormalizedReadResult } from "read-pkg-up";
2+
/**
3+
* read package.json
4+
* for example
5+
* - builtin module
6+
* - not configured 'main' 'module' and 'exports' field
7+
* - terser has multiple package.json
8+
*
9+
* @param name module id
10+
* @param cwd dir
11+
* @returns {@see NormalizedReadResult}
12+
*/
13+
export declare function readPackage(name: string, cwd: string): NormalizedReadResult | undefined;

es/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { readPackageUpSync } from "read-pkg-up";
2+
import { findModuleDir, fixTerser } from "./utils.js";
3+
/**
4+
* read package.json
5+
* for example
6+
* - builtin module
7+
* - not configured 'main' 'module' and 'exports' field
8+
* - terser has multiple package.json
9+
*
10+
* @param name module id
11+
* @param cwd dir
12+
* @returns {@see NormalizedReadResult}
13+
*/
14+
export function readPackage(name, cwd) {
15+
const dir = findModuleDir(name, cwd);
16+
const result = readPackageUpSync({
17+
cwd: dir
18+
});
19+
if (!result) {
20+
throw new Error(` not found package.json in ${dir} `);
21+
}
22+
return fixTerser(result);
23+
}

es/utils.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { type NormalizedReadResult } from "read-pkg-up";
2+
/**
3+
* found module dir path
4+
* - if module is builtin, Convert module name to directory name.
5+
* - if module is not builtin, return module dir path.
6+
*
7+
* @param name module id
8+
* @param cwd dir
9+
* @returns module entry file path or package.json path
10+
*/
11+
export declare function findModuleDir(name: string, cwd: string): any;
12+
/**
13+
* fix terser package.json path
14+
* because of terser has multiple package.json , so read-pkg-up return wrong package.json
15+
@example
16+
```
17+
.
18+
├── CHANGELOG.md
19+
├── LICENSE
20+
├── PATRONS.md
21+
├── README.md
22+
├── bin
23+
├── dist
24+
│ ├── bundle.min.js
25+
│ └── package.json // <--- this is wrong package.json
26+
├── lib
27+
├── main.js
28+
├── node_modules
29+
├── package.json // <--- this is correct package.json
30+
└── tools
31+
```
32+
*
33+
* @param readResult {@see NormalizedReadResult}
34+
* @returns {@see NormalizedReadResult}
35+
*/
36+
export declare function fixTerser(readResult: NormalizedReadResult): NormalizedReadResult | undefined;

es/utils.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import path from "node:path";
2+
import isBuiltinModule from "is-builtin-module";
3+
import resolveFrom from "resolve-from";
4+
import { readPackageUpSync } from "read-pkg-up";
5+
6+
/**
7+
* found module dir path
8+
* - if module is builtin, Convert module name to directory name.
9+
* - if module is not builtin, return module dir path.
10+
*
11+
* @param name module id
12+
* @param cwd dir
13+
* @returns module entry file path or package.json path
14+
*/
15+
export function findModuleDir(name, cwd) {
16+
if (isBuiltinModule(name)) {
17+
throw new Error(`Built-in module '${name}' cannot has no package.json`);
18+
}
19+
let anchorPointFilePath;
20+
try {
21+
// if target module is builtin module, Convert module name to directory name.
22+
// Please note that if you have not used a polyfill, an error of 'cannot find module 'xxx/package.json'' will be thrown.
23+
anchorPointFilePath = resolveFrom(cwd, name);
24+
} catch (error) {
25+
/**
26+
* because of target module is not configured entry in package.json
27+
* - not configured 'main' 'module' and 'exports' field
28+
*/
29+
anchorPointFilePath = resolveFrom(cwd, `${name}/package.json`);
30+
}
31+
return path.dirname(anchorPointFilePath);
32+
}
33+
34+
/**
35+
* fix terser package.json path
36+
* because of terser has multiple package.json , so read-pkg-up return wrong package.json
37+
@example
38+
```
39+
.
40+
├── CHANGELOG.md
41+
├── LICENSE
42+
├── PATRONS.md
43+
├── README.md
44+
├── bin
45+
├── dist
46+
│ ├── bundle.min.js
47+
│ └── package.json // <--- this is wrong package.json
48+
├── lib
49+
├── main.js
50+
├── node_modules
51+
├── package.json // <--- this is correct package.json
52+
└── tools
53+
```
54+
*
55+
* @param readResult {@see NormalizedReadResult}
56+
* @returns {@see NormalizedReadResult}
57+
*/
58+
export function fixTerser(readResult) {
59+
if (readResult.packageJson.private !== true) {
60+
return readResult;
61+
}
62+
const terserReadResult = readPackageUpSync({
63+
cwd: path.join(path.dirname(readResult.path), "..")
64+
});
65+
return terserReadResult;
66+
}

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
"homepage": "https://github.com/sheinsight/tpl-base",
66
"packageManager": "pnpm@8.0.0",
77
"type": "module",
8+
"exports": {
9+
".": {
10+
"import": "./es/index.js",
11+
"default": "./es/index.js"
12+
},
13+
"./package.json": "./package.json"
14+
},
815
"repository": {
916
"type": "git",
1017
"url": "git+ssh://git@github.com/sheinsight/tpl-base.git"
1118
},
1219
"files": [
13-
"dist"
20+
"dist",
21+
"es"
1422
],
1523
"publishConfig": {
1624
"access": "public",

0 commit comments

Comments
 (0)