Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create npm package with rslib? #679

marek0209 started this conversation in General
Discussion options

Hi, I would like to use rslib to build my react component package which I publish through npm.
How can I use rslib for this purpose? From what I can see the dist is not generating package.json.

You must be logged in to vote

Replies: 2 comments 7 replies

Comment options

the dist is not generating package.json

What is this mean?

You must be logged in to vote
6 replies
Comment options

I created a library by using "pnpm create rslib" and used the react template, and I would expect that when I have bundle:false set then a package.json file would be generated in dist. Do I understand correctly that in this case each component is treated as a separate entrypoint?
So in this case I would expect an exports section in package.json with a list of components.

This is my config:

import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';
export default defineConfig({
 source: {
 entry: {
 index: ['./src/**'],
 },
 },
 lib: [
 {
 bundle: false,
 dts: true,
 format: 'cjs',
 output: {
 cleanDistPath: true,
 distPath: {
 root: './dist/rslib',
 },
 },
 },
 ],
 output: {
 target: 'web',
 },
 plugins: [pluginReact()],
});
Comment options

Do you mean that you are a monorepo with lots of components which have their own package.json? If so, Rslib do not compile any package.json file so there will not be package.json in dist of each component. Writing exports in root package.json is enough.

Comment options

If you want each component has its own npm packages, you should build and publish them separately.

Comment options

Thanks, I managed to create a proper package.json with export however I have a problem with rslib build library imports, example of an error during vitest testing:

Error: Cannot find module ‘/my_app/node_modules/lodash/capitalize’ imported from my_app/node_modules/my_rslib_lib/esm/utils/myUtils.js
Did you mean to import ‘lodash/capitalize.js’?

Otherwise you have to add /index.js at the end of the mui component import.

Is there any way to solve this?

Comment options

Please provide a minimal reproduction to help us address the issue, we cannot assist with troubleshooting based on the information currently provided.
Thanks.

Why reproduction is required

Comment options

You can probably set up your exports in package.json to something generic like the following to achieve what you need:

"exports": {
 ".": {
 "types": "./dist/index.d.ts",
 "require": "./dist/index.cjs",
 "import": "./dist/index.js",
 }
 "./*": {
 "types": ["./dist/*/index.d.ts", "./dist/*.d.ts"],
 "require": ["./dist/*/index.cjs", "./dist/*.cjs"],
 "types": ["./dist/*/index.d.js", "./dist/*.js"],
 }
}
You must be logged in to vote
1 reply
Comment options

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /