babel-plugin-preval icon indicating copy to clipboard operation
babel-plugin-preval copied to clipboard

Usage with Vite?

Open alexgleason opened this issue 3 years ago • 5 comments

I'm migrating a legacy React TypeScript project from Webpack to Vite. I have some pretty substantial preval code.

Vite uses Babel internally, so I think it should be possible to use this with Vite. Has anyone succeeded in getting it working?

alexgleason avatar Jan 18 '23 03:01 alexgleason

@alexgleason I was able to get preval working with the following:

import {defineConfig} from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [
    react({
      babel: {
        plugins: ['preval'],
      },
    })
  ],
});

lobaak avatar Mar 23 '23 05:03 lobaak

Can you show any example of how you use this? @lobaak

istonejoeljonsson avatar Dec 08 '23 12:12 istonejoeljonsson

@lobaak Hey! Why did you specify react() twice?

ArturBaybulatov avatar Dec 18 '23 19:12 ArturBaybulatov

@ArturBaybulatov sorry, that is a typo. I've corrected.

@istonejoeljonsson we use the following to get the version of the library package

const version =
    preval`module.exports = process.env.npm_package_version` || '-';

lobaak avatar Jan 17 '24 01:01 lobaak

Hello, I believe you can use vite's virtual module feature for this: See here for more info: https://evomark.co.uk/development/making-your-tailwind-config-available-to-your-entire-application-with-vite/

maurer2 avatar Aug 17 '24 22:08 maurer2