Skip to content

Commit

Permalink
Chore(tools): Support multiple exports declaration in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jul 4, 2024
1 parent 95d2d65 commit 85a3d40
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions tools/setup-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,30 @@ paths.forEach(async (pkgPath) => {
pkg.scripts = pkg.scripts ?? {};
pkg.files = ['dist/**/*'];

function exportDef(defaultPath, types) {
if (emitDeclaration) {
return { types, default: defaultPath };
}
return defaultPath;
}

// If the package supports Typescript, then apply the configs.
delete pkg.exports['.'];
pkg.exports = {
'.': {
import: {
types: emitDeclaration ? './dist/esm/types/index.d.mts' : undefined,
default: './dist/esm/index.mjs',
},
require: {
types: emitDeclaration ? './dist/cjs/types/index.d.ts' : undefined,
default: './dist/cjs/index.js',
},
import: exportDef('./dist/esm/index.mjs', './dist/esm/types/index.d.mts'),
require: exportDef('./dist/cjs/index.js', './dist/cjs/types/index.d.ts'),
},
...pkg.exports,
};

pkg.main = pkg.exports['.'].require.default;
pkg.typings = emitDeclaration ? pkg.exports['.'].require.types : undefined;
if (emitDeclaration) {
pkg.main = pkg.exports['.'].require.default;
pkg.typings = pkg.exports['.'].require.types;
} else {
pkg.main = pkg.exports['.'].require;
pkg.typings = undefined;
}

pkg.scripts = {
tsc: 'yarn run tsc:esm && yarn run tsc:cjs',
Expand Down

0 comments on commit 85a3d40

Please sign in to comment.