Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the package.json exports and actually provide the typescript types #839

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@rollup/plugin-commonjs": "^28.0.0",
"cross-env": "^7.0.3",
"depcheck": "^1.4.7",
"dree": "^5.1.5",
"eslint": "^9.7.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-notice": "^1.0.0",
Expand Down
19 changes: 14 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@
},
"publishConfig": {
"module": "./build/esm/index.js",
"main": "./build/cjs/index.js",
"types": "./build/index.d.ts",
"main": "./build/cjs/index.cjs",
"types": "./build/esm/index.d.ts",
"exports": {
"import": "./build/esm/index.js",
"require": "./build/cjs/index.js",
"types": "./build/index.d.ts"
".": {
"module": "./build/esm/index.js",
"import": {
"types": "./build/esm/index.d.ts",
"default": "./build/esm/index.js"
},
"require": {
"types": "./build/cjs/index.d.cts",
"default": "./build/cjs/index.cjs"
}
},
"./package.json": "./package.json"
}
},
"files": [
Expand Down
19 changes: 14 additions & 5 deletions packages/mui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,21 @@
},
"publishConfig": {
"module": "./build/esm/index.js",
"main": "./build/cjs/index.js",
"types": "./build/index.d.ts",
"main": "./build/cjs/index.cjs",
"types": "./build/esm/index.d.ts",
"exports": {
"import": "./build/esm/index.js",
"require": "./build/cjs/index.js",
"types": "./build/index.d.ts"
".": {
"module": "./build/esm/index.js",
"import": {
"types": "./build/esm/index.d.ts",
"default": "./build/esm/index.js"
},
"require": {
"types": "./build/cjs/index.d.cts",
"default": "./build/cjs/index.cjs"
}
},
"./package.json": "./package.json"
}
},
"files": [
Expand Down
19 changes: 14 additions & 5 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,21 @@
},
"publishConfig": {
"module": "./build/esm/index.js",
"main": "./build/cjs/index.js",
"types": "./build/index.d.ts",
"main": "./build/cjs/index.cjs",
"types": "./build/esm/index.d.ts",
"exports": {
"import": "./build/esm/index.js",
"require": "./build/cjs/index.js",
"types": "./build/index.d.ts"
".": {
"module": "./build/esm/index.js",
"import": {
"types": "./build/esm/index.d.ts",
"default": "./build/esm/index.js"
},
"require": {
"types": "./build/cjs/index.d.cts",
"default": "./build/cjs/index.cjs"
}
},
"./package.json": "./package.json"
}
},
"files": [
Expand Down
19 changes: 14 additions & 5 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@
},
"publishConfig": {
"module": "./build/esm/index.js",
"main": "./build/cjs/index.js",
"types": "./build/index.d.ts",
"main": "./build/cjs/index.cjs",
"types": "./build/esm/index.d.ts",
"exports": {
"import": "./build/esm/index.js",
"require": "./build/cjs/index.js",
"types": "./build/index.d.ts"
".": {
"module": "./build/esm/index.js",
"import": {
"types": "./build/esm/index.d.ts",
"default": "./build/esm/index.js"
},
"require": {
"types": "./build/cjs/index.d.cts",
"default": "./build/cjs/index.cjs"
}
},
"./package.json": "./package.json"
}
},
"files": [
Expand Down
5 changes: 4 additions & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

import commonjs from '@rollup/plugin-commonjs';

/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input: 'lib/src/index.js',
plugins: [commonjs()],
output: [
{
file: 'build/cjs/index.js',
file: 'build/cjs/index.cjs',
format: 'cjs',
interop: 'auto',
},
Expand Down
49 changes: 49 additions & 0 deletions scripts/prepack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,56 @@
const PACKAGE_PATH = 'package.json';
const PACKAGE_PATH_BACKUP = 'package.json-prepack';

import * as dree from 'dree';
import * as fs from 'fs';
import * as path from 'path';

// Ensure the typescript types are in the build folder as cjs and esm variants
const walkOptions = {
depth: 100,
};
const tree = dree.scan('./lib/src', walkOptions);

function treeWalk(child) {
if (!child.path.endsWith('.d.ts')) {
return;
}
// Copy the types for cjs under the build/cjs folder
const cjsPath = child.path.replace('lib/src', 'build/cjs');

// If it is a directory, create the directory
if (child.type === 'directory') {
fs.mkdirSync(cjsPath, { recursive: true });
} else {
// Ensure target directory exists
fs.mkdirSync(path.dirname(cjsPath), { recursive: true });
fs.copyFileSync(child.path, cjsPath.replace('.d.ts', '.d.cts'));
}

// Copy the types for esm under the build/esm folder
const esmPath = child.path.replace('lib/src', 'build/esm');

// If it is a directory, create the directory
if (child.type === 'directory') {
fs.mkdirSync(esmPath, { recursive: true });
} else {
// Ensure target directory exists
fs.mkdirSync(path.dirname(esmPath), { recursive: true });
fs.copyFileSync(child.path, esmPath);
}
}

// Copy the types to the build folder while preserving the folder structure.
// It also copies each .d.ts to a file with .d.cts extension to be used in the cjs build.
// Be aware that children is recursive. A child can have children.
function treeWalker(node) {
treeWalk(node);
if (node.children) {
node.children.forEach(treeWalker);
}
}

treeWalker(tree);

const packageContent = JSON.parse(fs.readFileSync(PACKAGE_PATH));

Expand Down
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2720,6 +2720,14 @@ domutils@^3.0.1, domutils@^3.1.0:
domelementtype "^2.3.0"
domhandler "^5.0.3"

dree@^5.1.5:
version "5.1.5"
resolved "https://registry.yarnpkg.com/dree/-/dree-5.1.5.tgz#7341107e5c5c694cad4e3f903ab2beec29864386"
integrity sha512-4VZ5m1EgV+467S2LdTUxCE1L98ikzGFW7PTE8FlPSk7jvkbqeKQN1vCfwQ6w1vYVgAZMSooyvoae5F/LQWozhw==
dependencies:
minimatch "^10.0.1"
yargs "^17.7.2"

eastasianwidth@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
Expand Down Expand Up @@ -4504,6 +4512,13 @@ min-indent@^1.0.0:
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==

minimatch@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-10.0.1.tgz#ce0521856b453c86e25f2c4c0d03e6ff7ddc440b"
integrity sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==
dependencies:
brace-expansion "^2.0.1"

minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4, minimatch@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
Expand Down
Loading