vite icon indicating copy to clipboard operation
vite copied to clipboard

feat(lib): allow multiple entries

Open schummar opened this issue 3 years ago β€’ 21 comments
trafficstars

Description

Hi, I think it's a common occurrence that libraries provide multiple entrypoints, so it would be nice if Vite's library mode supported this scenario. Since rollup does the heavy lifting, the main challenge is to provide a nice and simple interface.

The idea would be for entry to have rollup's InputOption interface and to tweak fileName and the resulting file names and chunk names accordingly. All without chaning the behavior for existing cases of course.

Example:

lib: {
  entry: {
      primary: 'src/index.ts',
      secondary: 'src/secondary.ts'
  },
  formats: ['es', 'cjs']
}
// => primary.es.js, primary.cjs.js, secondary.es.js, secondary.cjs.js

or

lib: {
  entry: ['src/index.ts', 'src/secondary.ts'],
  formats: ['es', 'cjs']
}
// => index.es.js, index.cjs.js, secondary.es.js, secondary.cjs.js

Additional context

There is an issue regarding the topic, though the proposed interface differs: https://github.com/vitejs/vite/issues/4530 Some discussion has been going on here: https://github.com/vitejs/vite/discussions/1736


What is the purpose of this pull request?

  • [ ] Bug fix
  • [x] New Feature
  • [ ] Documentation update
  • [ ] Other

Before submitting the PR, please make sure you do the following

  • [x] Read the Contributing Guidelines.
  • [x] Read the Pull Request Guidelines and follow the Commit Convention.
  • [x] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • [x] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • [x] Ideally, include relevant tests that fail without this PR but pass with it.

schummar avatar Feb 22 '22 23:02 schummar

for this the entry points would need to be added to the package json exports

Niputi avatar Feb 22 '22 23:02 Niputi

after https://github.com/vitejs/vite/pull/6827 file names will be a bit different to give correct file names

Niputi avatar Feb 22 '22 23:02 Niputi

for this the entry points would need to be added to the package json exports

Indeed. But this is not Vite's concern, right? It should probably be mentioned in the docs under "Recommended package.json for your lib".

schummar avatar Feb 22 '22 23:02 schummar

yup. should be mentioned in docs

Niputi avatar Feb 23 '22 00:02 Niputi

I have added some docs and another check. Is there anything obvious missing?

schummar avatar Feb 24 '22 07:02 schummar

I very much look forward for this to be merged. to reply, I think the array option can benefit from a little bit more highlight than just a comment... but it’s a much needed feature and thank you

e-rouge avatar Mar 05 '22 22:03 e-rouge

@bluwy I see that the to-be-discussed label has been added. How does that work? I'm happy to discuss any open questions.

schummar avatar Mar 09 '22 07:03 schummar

it will be discussed at a team about the approach and if it should go in

Niputi avatar Mar 09 '22 07:03 Niputi

That means we will be discussing about this in the next team meeting (if we have the time) πŸ˜„ I'd like to review this PR myself too, but we'll have to see if the new API is good to go.

bluwy avatar Mar 09 '22 07:03 bluwy

Alright, let me know if I can contribute/clarify/justify something or if you require changes. Thanks!

schummar avatar Mar 09 '22 07:03 schummar

Need this feature +1. Any update?

The solution proposed in #4530 seems to be more flexible, with the possibility of specifying different formats and UMD names for different lib entries

pd4d10 avatar Apr 03 '22 14:04 pd4d10

The solution proposed in #4530 seems to be more flexible, with the possibility of specifying different formats and UMD names for different lib entries

True, it's a bit more flexible. However it's also more complex to implement and has other limitations. Also, if you're aiming for maximum flexibility I would argue it doesn't go far enough. What it you want to configure e.g. different rollupOptions, sourcemaps etc. for different entrypoints? Then maybe passing an array of configs at the top level makes the most sense.

I think we are not talking about mutually exclusive features here, both havbe their advantages:

  • This PR: + Lightweight: mostly passing the config along to rollup + Should be enogh for most cases: Most libs export multiple entrypoints with the same formats/settings, I assume + Does code splitting builds. That means that shared code between entry points is move to separate chunks and not duplicated - Does not cover all uses cases - Rollup does not support code splitting builds for UMD: https://github.com/rollup/rollup/issues/2072

  • Multiple configs on lib or root level: + Very flexible - Boilerplate: Probably one will have to repeat a lot of options - No shared chunks between entrypoints

schummar avatar Apr 10 '22 17:04 schummar

Possibly the UMD (and IIFE) case should be covered better in this PR as well. Currently you would see an error when trying to have multiple entrypoints and one of the formats is UMD. I think good behavior would be: For other formats, just feed it into rollup as is currently done. For UMD and IIFE, extra builds could be triggered per entrypoint.

I'd be happy to build it, but before putting more effort in it I would wait for the team's feedback, if the api makes sense for them at all.

schummar avatar Apr 10 '22 17:04 schummar

@schummar Need this feature +1. Any update?

palmerye avatar Jun 10 '22 08:06 palmerye

Any updates?

kasvith avatar Jun 14 '22 19:06 kasvith

Haven't heard anything for quite some time. @Niputi @bluwy?

schummar avatar Jun 14 '22 19:06 schummar

We had a big backlog while we are working to release v3, so we haven't had time yet to review this one in a team meeting. We'll comment once we do.

patak-dev avatar Jun 14 '22 20:06 patak-dev

Thanks, this is a really valuable feature for vite

kasvith avatar Jun 14 '22 20:06 kasvith

+1 for the head up. It's a titanic work making adequately a multientry build in lib mode, at least with Web Components libs.

mrcego avatar Jul 10 '22 10:07 mrcego

:100: After #7275, this would fix #9086

SteveDesmond-ca avatar Jul 15 '22 16:07 SteveDesmond-ca

πŸ’― After #7275, this would fix #9086

How to use it ? I don't think its work

rowthan avatar Jul 26 '22 08:07 rowthan

+1 from me.

boogiefromzk avatar Aug 15 '22 16:08 boogiefromzk

Another +1 over here. Just knowing this will land at some point (hopefully soon) changes radically architecture decisions and approach to multi-module/project layout. Thanks everyone for getting this PR in! 🍰

philcockfield avatar Aug 15 '22 20:08 philcockfield

I have rebased the changes onto current main now. Still awaiting the team's feedback, but the p3-significant label is probably a good sign 😁

schummar avatar Aug 16 '22 21:08 schummar

Outline for possible multi entries support for umd and iife: https://github.com/schummar/vite/commit/22fda780ec135b14f4d0f66b2f04496e629f7bfb

Needs fine tuning obviously, but works in principle. With the expected downside of course, that no code splitting takes place and redundant code is put into files. E.g.:

vite v3.0.8 building for production...
βœ“ 2 modules transformed.
dist/myLib.mjs            1.04 KiB / gzip: 0.58 KiB
dist/myLibSecondary.mjs   1.07 KiB / gzip: 0.62 KiB // Imports myLib
βœ“ 3 modules transformed.
dist/myLib.umd.js   1.22 KiB / gzip: 0.72 KiB
βœ“ 5 modules transformed.
dist/myLibSecondary.umd.js   2.23 KiB / gzip: 0.74 KiB // Duplicates content of myLib

schummar avatar Aug 16 '22 23:08 schummar

@schummar this is great! I tried running it locally and get a Multiple entry points are not supported when output formats include "umd" or "iife". error. Any thoughts on how we might get around that?

drewloomer avatar Aug 29 '22 20:08 drewloomer

@drewloomer that's a limitation of Rollup (see https://github.com/rollup/rollup/issues/2072), which we might be able to work around.

I have described a possible solution before (https://github.com/vitejs/vite/pull/7047#issuecomment-1094334777, https://github.com/vitejs/vite/pull/7047#issuecomment-1217258714) but it will create redundant output, which might or might not be a deal breaker. If you want you can try it out from this branch: https://github.com/schummar/vite/tree/feature/libMultiEntryUMD

schummar avatar Aug 29 '22 21:08 schummar

@drewloomer it just occurred to me that the confusion could also come from "es" and "umd" being the default output format, which will then just show an error. That is unfortunate indeed. Maybe one way to solve it could be, to change the default format when using multiple entry points...

For now the best way to configure it, is to use formats: ['es', 'cjs'], if you don't specifically need umd output.

schummar avatar Aug 30 '22 11:08 schummar

Is this landing any time soon? πŸ₯Ί

hmendes00 avatar Aug 31 '22 19:08 hmendes00

Would #10116 (once it gets merged) make this PR redundant? Or are they doing slightly different things?

SteveDesmond-ca avatar Sep 15 '22 15:09 SteveDesmond-ca