-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Better (package-based) monorepo support #10447
Comments
Hi, any news on this? I have the same concern about the build system picking up changes in libraries. I tried with |
I'm very interested in this issue as well. |
EDIT: Actually I figured it out, I tried using Turborepo alongside Vite and got the results I wanted using a monorepo structure, it works with hot reloading too. So it sounds like a documentation issue if they can describe how to use Turborepo |
You can pay attention to this tool, which may solve your problem vite-run |
This is needed, I have a yarn workspaces repo, There are two packages,
|
Very surprised this hasn't received more attention considering that Vite is ostensibly all about dev ergonomics? I mean, I'm sure the team has their hands full but this seems like it'd be a pretty standard way to set up a modern-day monorepo. FYI I'm probably just salty so ignore me 😬 This is the first I'm seeing that hmr/fast refresh isn't supported at all by Vite when used in monorepos lol. A workaround I'm about to try is enabling rollup's build watcher with edit: Ok I did manage to get hmr working, in my case it was very simple but also took a while to figure out due to recent changes in Vite's module resolution mechanism: #11114 Problem I had copy + pasted the default lib mode settings provided in the docs: https://vitejs.dev/guide/build.html#library-mode. Specifically, Vite suggests the following
The problem is that in a dev environment, the So, if my understanding is correct, pointing the
|
@jkhaui thanks, works like a charm. I don't fully understand the implication of this setting (if there is any). "exports": {
".": {
"import": "./src/my-lib.mjs",
},
"./sub-package": {
"import":"../sub-package/src/sub-package.mjs"
}
} From what I gather, this setting just points the import to the entry file.
Is that correct? And what's the impact of this setting once the library is built? |
hope to see more progress on this. |
I just opened a discussion to something that is somewhat related to this: #15466 Basically I'm wondering if Vite could support something like Parcel's This way, even though your This sounds like an awesome feature to have (if it doesn't exist already). |
The issue @samvy just linked has another workaround: overloading |
This comment was marked as off-topic.
This comment was marked as off-topic.
+1 - there is, as far as I am aware, no path to package-based monorepos that make use of peer dependencies to work without significant compromise of dev ergonomics in vite. The canonical solution of using injected dependencies, because of the 'node_modules' heuristic for determining whether to care about changes while the dev server is running basically kills any chance of hot module reloading. There is at least some community, and industry movement to build tooling and workflows to support this: being the two efforts I think that have the most impetus behind them at present. Is it something that the vite team have already got a plan for it/have ruled it out? I started a discussion some months ago to see if there was some alternate path and the solution eventually involved horrible hacks around symlinked packages - #14672 |
One alternative to updating the package.json to point to the source files, is to use the resolve.alias option in the Vite config to map the package names to the source files. e.g., with the following package.json {
"name": "my-lib",
"type": "module",
"exports": {
".": {
"import": "./dist/my-lib.js",
}
}
} You could have this in your vite.config. export default defineConfig({
// ...
resolve: {
alias: {
'my-lib': path.resolve(__dirname, 'packages/my-lib/src/my-lib.mjs')
}
}
}) This is a basic example with a package that contains just a single root export. For packages with more complex exports, you should be able to use Regex based aliases |
@WadePeterson How do you handle peer dependencies that you want to externalize? For example, a few libraries require context providers, so a singleton need to be referenced by both the module that depends on it, and the main app. Right now it seems the aliased resolver will use the local |
For standard build, you should be able to configure e.g.: const globals = {
react: 'React',
'react-dom': 'ReactDOM',
};
export default defineConfig({
// ...
build: {
rollupOptions: {
external: Object.keys(globals), // ['react', 'react-dom']
output: {
format: 'iife',
name: 'MyBundle',
globals,
},
},
},
}); This doesn't apply to Vite in import createExternal from 'vite-plugin-external';
const globals = {
react: 'React',
'react-dom': 'ReactDOM',
};
export default defineConfig(({command}) => {
return {
// ...
plugins: [
// ... other plugins
// only add this in dev server mode
...(command === 'serve' ? [createExternal({externals: globals})] : []),
],
build: {
// ... same build config as above, though `external`/`globals` from that don't actually apply in dev server
}
};
}); |
Has anybody figured out a way to force re-bundling of an injected dependency? They're gaining quite a lot of traction and I think a significant effort on monorepo support is available if it's even just possible to trigger re-bundling when an injected dependency changes. I did some experimenting with the Environment API, and with trying to trick the cache into re-optimizing but it seems like no matter what vite just ignores changes to imported injected dependencies? |
Description
The documentation on setting up a monorepo with Vite is pretty minimal. (Compare
nx
's, for example.) I don't fully understand this sentence:More importantly, though, is this part:
Since most of my code is in the libraries, not the app, this means I'll have to reboot constantly, which would seem to defeat the purpose of Vite.
Is Vite not intended for monorepos? (Not for package-based monorepos?) Whether the answer is yes or no, could the documentation be expanded to better address the question? If the answer is yes, could we have some sort of
--watch
feature that rebuilds on changes in linked packages in a monorepo?Suggested solution
--watch
or equivalent feature to automatically pick up changes to packages in a monorepo.Additional context
My repository is a monorepo consisting of a few apps and a large set of libraries, each constituting a package:
Apps reference libraries by importing them in
package.json
(andtsconfig.json
), and libraries import other libraries the same way, forming a (rather complicated) DAG.Validations
The text was updated successfully, but these errors were encountered: