How to fire HMR update when local dependency changes in monorepo #7155
Replies: 6 comments 2 replies
-
I have this problem as well. I get a full page refresh |
Beta Was this translation helpful? Give feedback.
-
I'm having this problem too trying to get HMR working with a React project in a monorepo. I've seen this discussion (unfortunately it's locked) #1491 where @LinusBorg (hope you don't mind me tagging you - sorry if not!) managed to get this working for a Vue project. I'm not quite sure what the magic part if as the I'm trying to work out how this works, and hopefully apply to my React project. |
Beta Was this translation helpful? Give feedback.
-
I recently ran into this problem and found a couple of approaches Approach 1You could run Approach 2With this approach we tell vite to import from the neighbouring source which is probably better vite configThe first thing is we add an alias to the vite / rollup settings within import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import * as path from 'path';
// https://vitejs.dev/config/
export default defineConfig({
resolve:{
alias:{
'mylib' : path.resolve(__dirname, '../mylib/src/components/index.ts')
},
},
plugins: [vue()]
}) This way when we do an import from mylib, it will use the source in the monorepo The file we're referencing here tscondigIf you're using volar / vscode and want to pick up on any changes to types / auto completion we can also update tsconfig.json Under {
"compilerOptions": {
"baseUrl": ".",
"paths": {
"mylib": ["../mylib/src/components/index.ts"]
}
...
}
} package configFor the package.json file, if you're using yarn workspaces.
Other ExamplesLooking at these links I think it suggests vite should already be doing this automatically? I also tried this, but it didn't seem to work, maybe because I'm using windows I think in this example what he's doing is running multiple build tasks at the same time via vscode one for the library and one for the main app |
Beta Was this translation helpful? Give feedback.
-
One thing to make sure is to really avoid circular imports. This is what caused HMR not working for me. Let's say I had a shared These 2 files looked something like this: // hooks/useForm.ts
import { useForm as useHookForm } from 'react-hook-form'
export const useForm = <TSchema extends z.ZodSchema>(schema: TSchema) => {
return useHookForm<z.infer<typeof schema>>({ resolver: zodResolver(schema) })
} Consider also the fact that I had an barrel // hooks/index.ts
export { useForm } from './useForm'
export { useLoginForm } from './useLoginForm' Now let's see what caused HMR not to work: // hooks/useLoginForm.ts
1. import { useForm } from 'hooks' ❌ <~ This breaks HMR, saving this file causes the page to fully reload
2. import { useForm } from './' ❌ <~ This breaks HMR, saving this file causes the page to fully reload
3. import { useForm } from './useForm' ✔️ <~ This is the valid way, HMR kicks in on file save
const loginFormSchema = z.object({ email: z.string().email() })
export const useLoginForm = useForm(loginFormSchema) Caveats regarding these
|
Beta Was this translation helpful? Give feedback.
-
@kuubson did you advance on this? I have an issue with this too. |
Beta Was this translation helpful? Give feedback.
-
One issue I'm having with this is even if I alias the dependency. Both projects alias |
Beta Was this translation helpful? Give feedback.
-
I have a strange problem using vite in Vue 3 monorepo setup. I have several locally builded as library and linked to main app dependencies. Libraries are in commonjs format, with custom tsconfigs, package.json and other staff. While serving main app there is no HMR updates. What i'm doing wrong? Googled all over internet, but no luck
Beta Was this translation helpful? Give feedback.
All reactions