Skip to content

Commit 47acb2b

Browse files
author
Nir Maoz
authored
Add package.exports (#433)
* Add package.exports
1 parent 9a3d0a2 commit 47acb2b

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

package.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build:entryPoints": "ts-node ./scripts/createEntrypoints.ts",
2222
"build:copyCleanPackageJSON": "ts-node scripts/copyPackageJsonToSrc.ts",
2323
"build:injectPackageVersion": "ts-node ./scripts/injectPackageVersionToDistFiles.ts",
24+
"build:updatePackageExports": "ts-node scripts/updatePackageJsonExports.ts",
2425
"lint": "eslint src __TESTS__ --color --ext .ts",
2526
"start": "rollup -c rollup.dev.config.js -w",
2627
"bundlewatch": "bundlewatch --config ./bundlewatch.config.js",
@@ -91,5 +92,24 @@
9192
"dependencies": {
9293
"@types/lodash.clonedeep": "^4.5.6",
9394
"lodash.clonedeep": "^4.5.0"
95+
},
96+
"exports": {
97+
"./bundles/umd/package.json": "./bundles/umd/package.json",
98+
"./package.json": "./package.json",
99+
"./actions/*": "./actions/*.js",
100+
"./assets/*": "./assets/*.js",
101+
"./backwards/*": "./backwards/*.js",
102+
"./bundles/*": "./bundles/*.js",
103+
"./config/*": "./config/*.js",
104+
"./instance/*": "./instance/*.js",
105+
"./qualifiers/*": "./qualifiers/*.js",
106+
"./sdkAnalytics/*": "./sdkAnalytics/*.js",
107+
"./transformation/*": "./transformation/*.js",
108+
"./types/*": "./types/*.js",
109+
".": {
110+
"node": "./index.js",
111+
"require": "./bundles/umd/base.js",
112+
"default": "./index.js"
113+
}
94114
}
95-
}
115+
}

scripts/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ npm run build:UMD
1313
npm run build:entryPoints
1414
npm run build:docs
1515
npm run build:injectPackageVersion
16+
npm run build:updatePackageExports
1617

1718
#cp CHANGELOG.md to dist
1819
cp ./CHANGELOG.md ./dist

scripts/updatePackageJsonExports.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* This script Adds "exports" property to package.json.
3+
* It fills it with key-value pars for sub-directories of /dist,
4+
* excluding /internal
5+
*/
6+
7+
// eslint-disable-next-line @typescript-eslint/no-var-requires
8+
const {resolve} = require('path');
9+
// eslint-disable-next-line @typescript-eslint/no-var-requires
10+
const {writeFile, readdirSync} = require('fs');
11+
// eslint-disable-next-line @typescript-eslint/no-var-requires
12+
const packageJson = require('../package.json');
13+
14+
interface Dirent {
15+
isDirectory: () => boolean;
16+
name: string;
17+
}
18+
19+
// List of sub-directories under ./dist
20+
const distSubDirectoriesArray: string[] = readdirSync(resolve('./dist'), {withFileTypes: true})
21+
// Filter out files
22+
.filter((dirent: Dirent) => dirent.isDirectory())
23+
// Get directory name
24+
.map((dirent: Dirent) => `./${dirent.name}`)
25+
// Exclude /internal
26+
.filter((dir: string) => !dir.toLowerCase().includes('/internal'))
27+
// Sort alphabetically
28+
.sort();
29+
30+
// Convert directory string to object of {[directory/*]: directory/*.js}
31+
const distSubDirectoriesObj: Record<string, string> = distSubDirectoriesArray
32+
.reduce((obj, dir) => ({...obj, ...{[`${dir}/*`]: `${dir}/*.js`}}), {}); //
33+
34+
// Generate updated package json object
35+
const resultPackageJson = JSON.stringify({
36+
...packageJson,
37+
exports: {
38+
"./bundles/umd/package.json": "./bundles/umd/package.json",
39+
"./package.json": "./package.json",
40+
...distSubDirectoriesObj,
41+
".": {
42+
"node": "./index.js",
43+
"require": "./bundles/umd/base.js",
44+
"default": "./index.js"
45+
}
46+
}
47+
}, null, 2);
48+
49+
// Update (overwrite) package.json
50+
writeFile(resolve('./package.json'), resultPackageJson, (err: Error) => {
51+
if (!err) {
52+
console.log('Successfully updated package.json exports field');
53+
} else {
54+
throw `Failed to updated package.json exports field: ${err}`;
55+
}
56+
});

src/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
"@types/lodash.clonedeep": "^4.5.6",
1010
"lodash.clonedeep": "^4.5.0"
1111
},
12+
"exports": {
13+
"./bundles/umd/package.json": "./bundles/umd/package.json",
14+
"./package.json": "./package.json",
15+
"./actions/*": "./actions/*.js",
16+
"./assets/*": "./assets/*.js",
17+
"./backwards/*": "./backwards/*.js",
18+
"./bundles/*": "./bundles/*.js",
19+
"./config/*": "./config/*.js",
20+
"./instance/*": "./instance/*.js",
21+
"./qualifiers/*": "./qualifiers/*.js",
22+
"./sdkAnalytics/*": "./sdkAnalytics/*.js",
23+
"./transformation/*": "./transformation/*.js",
24+
"./types/*": "./types/*.js",
25+
".": {
26+
"node": "./index.js",
27+
"require": "./bundles/umd/base.js",
28+
"default": "./index.js"
29+
}
30+
},
1231
"main": "./bundles/umd/base.js",
1332
"browser": "./index.js",
1433
"module": "./index.js",

0 commit comments

Comments
 (0)