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

feat: add subpath exports #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
"type": "git",
"url": "https://github.com/receter/my-component-library"
},
"sideEffects": [
"**/*.css"
],
"scripts": {
"dev": "vite",
"build": "tsc --p ./tsconfig-build.json && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"prepack": "node scripts/update-exports.mjs",
"preview": "vite preview"
},
"peerDependencies": {
Expand All @@ -43,5 +41,30 @@
"vite": "^4.5.3",
"vite-plugin-dts": "^3.7.3",
"vite-plugin-lib-inject-css": "^2.0.1"
}
},
"exports": {
".": {
"types": "./dist/main.d.ts",
"default": "./dist/main.js"
},
"./Button": {
"types": "./dist/components/Button/index.d.ts",
"default": "./dist/components/Button/index.js"
},
"./Input": {
"types": "./dist/components/Input/index.d.ts",
"default": "./dist/components/Input/index.js"
},
"./Label": {
"types": "./dist/components/Label/index.d.ts",
"default": "./dist/components/Label/index.js"
}
},
"sideEffects": [
"**/*.css",
"./dist/main.js",
"./dist/components/Button/index.js",
"./dist/components/Input/index.js",
"./dist/components/Label/index.js"
]
}
29 changes: 29 additions & 0 deletions scripts/update-exports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readdir, readFile, writeFile } from 'node:fs/promises';

const corePackageJsonPath = new URL('../package.json', import.meta.url);
const corePackageJson = JSON.parse(await readFile(corePackageJsonPath, 'utf-8'));

const coreComponentsDirectoryPath = new URL('../lib/components/', import.meta.url);
const coreComponentsDirectory = await readdir(coreComponentsDirectoryPath);

const paths = {
'.': {
types: './dist/main.d.ts',
default: './dist/main.js'
}
};

for (const item of coreComponentsDirectory) {
const filesInDirectory = await readdir(new URL(`${item}/`, coreComponentsDirectoryPath));
if (filesInDirectory.length) {
paths[`./${item}`] = {
types: `./dist/components/${item}/${filesInDirectory[0].replace(/\.tsx$/, '.d.ts')}`,
default: `./dist/components/${item}/${filesInDirectory[0].replace(/\.tsx$/, '.js')}`
};
}
}

corePackageJson.exports = paths;
corePackageJson.sideEffects = ["**/*.css", ...Object.values(paths).map(e => e.default)];

await writeFile(corePackageJsonPath, JSON.stringify(corePackageJson, null, ' '));