How to use libraries built with Vanilla Extract in development mode with Vite (optimizeDeps issue) #1051
-
Hi, I've created a UI component library using vanilla extract called gboost-ui. I built the library by simply using
However, if I install gboost-ui locally and link it to my Vite app then the development server successfully runs! I think this is because the gboost-ui is not being pre-bundled so A potential solution to this issue is that I could add to my vite.config.ts:
BUT I have CJS dependencies within
but then I got the error:
Does anyone have a solution? There are likely a number of way to solve this issue, but I think the best would be to try and use Vite Docs:
VE files are "special files" so an esbuild plugin should be used to handle them but the vanilla extract one gives me an error as shown above. UPDATE: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Solved! import { defineConfig, loadEnv } from "vite";
import { createHtmlPlugin } from "vite-plugin-html";
import { vanillaExtractPlugin as veVitePlugin } from "@vanilla-extract/vite-plugin";
import { vanillaExtractPlugin as veEsbuildPlugin } from "@vanilla-extract/esbuild-plugin";
import tsconfigPaths from "vite-tsconfig-paths";
import _react from "@vitejs/plugin-react";
// https://github.com/vitejs/vite/issues/10481
const react = _react as unknown as typeof _react.default;
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd());
return {
optimizeDeps: {
esbuildOptions: {
plugins: [veEsbuildPlugin({ runtime: true })],
},
},
plugins: [
react(),
createHtmlPlugin({ inject: { data: env } }),
tsconfigPaths(), // needed for baseUrl tsconfig option
veVitePlugin(),
],
resolve: {
alias: [
{
// https://ui.docs.amplify.aws/getting-started/installation#vite
find: "./runtimeConfig",
replacement: "./runtimeConfig.browser",
},
],
},
};
});
|
Beta Was this translation helpful? Give feedback.
Solved!
The key here is the line:
plugins: [veEsbuildPlugin({ runtime: true })],
in the below vite.config.ts: