Skip to content

Commit 691c02f

Browse files
authored
feat: create separate package.json for cjs module on build time (#687)
Create a separate package.json under `dist/cjs` on build time.
1 parent ae7d6a7 commit 691c02f

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

scripts/post_build.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const packageJson = require('../package.json');
77
const packageTemplate = require('./package.template.json');
88

99
const { version } = packageJson;
10-
const distPath = path.resolve(__dirname, '../dist');
10+
const getDistPath = (subPath) => path.resolve(__dirname, subPath != null ? `../dist${subPath}` : '../dist');
1111

1212
function appendVersionToTypeDefn() {
13-
const indexDtsPath = path.resolve(distPath, 'index.d.ts');
13+
const indexDtsPath = path.resolve(getDistPath(), 'index.d.ts');
1414
const indexDts = fs.readFileSync(indexDtsPath, 'utf8');
1515
const indexDtsWithVersion = indexDts.replace(
1616
'Type Definitions for @sendbird/uikit-react@{{ version }}',
@@ -41,7 +41,7 @@ function cleanupPackageJSON(json) {
4141
}
4242

4343
function movePackageJSON() {
44-
const packageJSONDistPath = path.resolve(distPath, 'package.json');
44+
const packageJSONDistPath = path.resolve(getDistPath(), 'package.json');
4545
const cleanedUpPackageJSON = cleanupPackageJSON(packageJson);
4646
fs.writeFileSync(
4747
packageJSONDistPath,
@@ -50,7 +50,31 @@ function movePackageJSON() {
5050
);
5151
}
5252

53+
function copyCJSPackageJSON() {
54+
const packageJSONDistPath = path.resolve(getDistPath('/cjs'), 'package.json');
55+
const cleanedUpPackageJSON = cleanupPackageJSON(packageJson);
56+
57+
delete cleanedUpPackageJSON.files;
58+
delete cleanedUpPackageJSON.scripts;
59+
delete cleanedUpPackageJSON.devDependencies;
60+
delete cleanedUpPackageJSON.module;
61+
62+
fs.writeFileSync(
63+
packageJSONDistPath,
64+
JSON.stringify({
65+
...cleanedUpPackageJSON,
66+
main: 'index.js',
67+
type: 'commonjs',
68+
typings: '../index.d.ts',
69+
}, null, 2),
70+
{ flag: 'w' },
71+
);
72+
}
73+
74+
5375
/** Add version information to index.d.ts in dist */
5476
appendVersionToTypeDefn();
5577
/** Copy content of package.json to dist, but remove unnecessary fields */
5678
movePackageJSON();
79+
/** Copy content of package.json to dist/cjs, to support cjs module separately */
80+
copyCJSPackageJSON();

0 commit comments

Comments
 (0)