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

Discussion: make Vite more friendly to library development (like storybook) #725

Closed
csr632 opened this issue Aug 19, 2020 · 10 comments
Closed
Labels
enhancement New feature or request

Comments

@csr632
Copy link
Member

csr632 commented Aug 19, 2020

Do NOT ignore this template or your issue will have a very high chance to be closed without comment.

Describe the bug

I am developing a component library, using vite as a local dev environment (like storybook). I also build the demos with vite and deploy it, so that our designers can see my demos.

In my case, the component library consists of many component, and each component is a separate package. So it is a monorepo, like this :

packages
├── button
│   ├── demos
│   │   ├── _theme.tsx
│   │   ├── demo1$.tsx
│   │   ├── demo2$.tsx
│   │   └── index$.md
│   ├── src
│   │   ├── Card
│   │   │   ├── demos
│   │   │   │   ├── demo1.tsx
│   │   │   │   └── demo2.tsx
│   │   │   ├── index.tsx
│   │   │   └── style.module.css
│   │   ├── index.tsx
│   │   └── style.module.css
│   ├── declare.d.ts
│   ├── index.html
│   ├── package.json
│   ├── tsconfig.json
│   └── vite.config.ts
└── card
    ├── demos
    │   ├── _theme.tsx
    │   ├── demo1$.tsx
    │   ├── demo2$.tsx
    │   └── index$.md
    ├── src
    │   ├── index.tsx
    │   └── style.module.css
    ├── declare.d.ts
    ├── index.html
    ├── package.json
    ├── tsconfig.json
    └── vite.config.ts

I make it works (see the reproduction repo, but there is some flaws and hacks in it. It is because that vite keeps assuming the whole project is a vite app. (In my case, it is actually a package for my component).

Here is some flaws that make vite not an ideal for library development. You can observe them in the reproduction repo.

  1. I want to move the index.html into /demos, because it is config for demos, not the component package. I am not able to do that because I have set vite root to /(because demos import things from /src), and vite hard-code html path as ${root}/index.html. (You can try to move it in the reproduction repo)
  2. Vite reads #{root}/package.json and "treat it like a config for vite". But in the case of library development, it is made for the npm package!
    1. I am forced to put @pika/react and @pika/react-dom inside package.json#dependencies. All users of my package will install them! I have created an issue at vite-plugin-react.
    2. The "package.json#sideEffects" has effects on vite build, which is unexpected. After investagte, it is because the rollup-plugin-node-resolve used by vite accidentally reads it.
    3. As the comment points out, "type": "module" inside package.json also has surprising effects on vite.
  3. I have to manually list many devDependencies inside vite.config.ts#optimizeDeps#include. Because:
    • Dependencies of my library demos should be devDependencies of the package. But vite only optimizes package.json#dependencies by default.

I love vite. I think it can be a very handy tool for library development. (Better than storybook!)

Reproduction

https://github.com/csr632/test-vite/tree/lib-monorepo

Based on the reproduction repo, how can the workflow and file structure be improved?

System Info

  • required vite version: 1.0.0-rc.4
  • required Operating System: MacOS
  • required Node version: 12.x
@csr632 csr632 changed the title Vite is NOT friendly to library development Discussion: make Vite more friendly to library development Aug 20, 2020
@stefandesu
Copy link

Another issue that I found that might be relevant for this is that if you would like to use ES modules outside of Vite (which you can do by adding "type": "module" to your package.json), Vite can't load the vite.config.js file anymore. My workaround currently is to rename it to vite.config.cjs and specify it via the --config flag.

@csr632
Copy link
Member Author

csr632 commented Aug 21, 2020

Another issue that I found that might be relevant for this is that if you would like to use ES modules outside of Vite (which you can do by adding "type": "module" to your package.json), Vite can't load the vite.config.js file anymore. My workaround currently is to rename it to vite.config.cjs and specify it via the --config flag.

I have added your statement into the issue body.

@csr632 csr632 changed the title Discussion: make Vite more friendly to library development Discussion: make Vite more friendly to library development (like storybook) Aug 24, 2020
@intrnl
Copy link
Contributor

intrnl commented Aug 25, 2020

I've honestly never encountered a package where the demo's config and (dev) dependencies are declared in the root package directory. You might want to move it all to the demo folder entirely (example), or move each of the demo into its own (private) package

@csr632
Copy link
Member Author

csr632 commented Aug 25, 2020

@intrnl
In the example you give, how can you run this demo project using the /src/source-code? This demo project seems to be a seperate yarn project(it has its own yarn.lock). If that is the case, I will have to manually yarn link the source project into each demo projects.

@intrnl
Copy link
Contributor

intrnl commented Aug 25, 2020

wished i found a better example, but here it is

@csr632
Copy link
Member Author

csr632 commented Nov 27, 2020

Related issue: #330

@peey
Copy link

peey commented Dec 16, 2020

Related Issue and PR on storybook: storybookjs/storybook#10654, storybookjs/storybook#13384

@csr632
Copy link
Member Author

csr632 commented Jan 8, 2021

With Vite 2.x, we can now achieve ideal monorepo layout:

packages
├── button
│   ├── demos
│   │   ├── demo1.tsx
│   │   └── demo2.tsx
│   ├── src
│   │   ├── declare.d.ts
│   │   ├── index.tsx
│   │   ├── style.module.css
│   │   └── tsconfig.json
│   ├── package.json
│   └── tsconfig.json
├── card
│   ├── demos
│   │   ├── demo1.tsx
│   │   └── demo2.tsx
│   ├── src
│   │   ├── declare.d.ts
│   │   ├── index.tsx
│   │   ├── style.module.css
│   │   └── tsconfig.json
│   ├── package.json
│   └── tsconfig.json
└── demos
    ├── pages
    │   ├── _theme.tsx
    │   ├── $.tsx
    │   └── 404$.tsx
    ├── index.html
    ├── package.json
    ├── tsconfig.json
    └── vite.demos.ts

I have updated the demo repo: https://github.com/csr632/test-vite/tree/lib-monorepo

I think all problems in this issue are solved. Closing this issue...

@csr632 csr632 closed this as completed Jan 8, 2021
@blowsie
Copy link

blowsie commented May 25, 2021

@github-actions
Copy link

This issue has been locked since it has been closed for more than 14 days.

If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants