DEV Community

David Rickard
David Rickard

Posted on

3

Dependency Injection with Vite and TypeScript

Writing a cross-platform web app means you need to bundle different code with each platform. I discussed the approach with Webpack and NormalModuleReplacementPlugin in an earlier post.

In Vite you can do this in vite.config.ts:

import alias from '@rollup/plugin-alias';

...

plugins: [
    {
      enforce: 'pre',
      ...alias({
        entries: [
          {
            find: /\/MainRepository$/,
            replacement: '/TauriRepository',
          },
        ],
      }),
    },
],
Enter fullscreen mode Exit fullscreen mode

This would find any module called MainRepository and swap it out for the TauriRepository module in the same folder.

enforce: 'pre' is important here, otherwise Vite has already run its pipeline and it's too late. If you're just using Rollup without Vite then you should be able to use the alias entry directly.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay