question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:31
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
csr632commented, 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.

0reactions
github-actions[bot]commented, Jul 16, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a React component library with Vite and Typescript
Most of the time, we use our favorite tool to create a React ... This article will show how to create a library...
Read more >
First-class Vite support in Storybook - JS.ORG
Vite is a next-gen build tool that has taken the web development world by storm. It gives you fast startup, instant reloads, widespreadΒ ......
Read more >
Speed up Storybook with Vite and SWC β€” with the help of Nx
Vite for Storybook in your Nx workspace​​ Vite is a build tool that ensures faster load times, faster updates, smaller server start times,...
Read more >
React Component Library with Vite and Deploy in NPM
This tutorial is designed to help you build a React component using React and vite with a typescript template and then publish it...
Read more >
Frontend library development with vite and tailwindcss
Using vite as a Storybook bundler eliminates the need for an extra bundler. For Storybook, you can use the vite bandler instead of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found