How to correctly publish a re-usable shared preset/config with Tailwind v4 and the Vite plugin? #18543
Replies: 2 comments
-
|
For the scenario of distributing a set of components (e.g. Vue.js components) with a build step (e.g. distributing
However, while this seemingly gets me a working version of my projects, it leads to style duplication because I effectively create one set of Tailwind styles for the library and one set for the consuming project. Commonly used utilities like What's worse, this duplication leads to CSS ordering problems. Depending on what I import first and where I put the |
Beta Was this translation helpful? Give feedback.
-
|
I was able to get a setup like what you described working for me. I think the key here is that I don't have my npm library really deal with tailwind at all and have the consuming library handle the whole setup. Here is how my vite.config.ts is configured in the npm library import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
import * as path from 'path'
export default defineConfig({
plugins: [vue(), tailwindcss()],
build: {
emptyOutDir: true,
lib: {
entry: path.resolve(__dirname, 'library/index.ts'),
name: 'MyProject',
fileName: (format) => `my-project.${format}.js`,
},
rollupOptions: {
external: ['vue', 'radix-vue', 'lucide-vue-next'],
output: {
globals: {
vue: 'Vue',
'radix-vue': 'RadixVue',
'lucide-vue-next': 'LucideVueNext',
},
},
},
outDir: 'dist',
},
resolve: {
alias: {
'@': path.resolve(__dirname, './library'),
},
},
})A key part of the package.json "exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/my-project.es.js",
"require": "./dist/my-project.umd.js"
},
"./styles.css": "./dist/my-project.css",
"./tailwind.preset.css": "./dist/tailwind.preset.css"
},Then for building the library into a npm package I used a few commands: Run your vite build and vue-tsc (if it is typescript) commands as normal This should output the built library into a folder named "dist". I then just simply copy the Tailwind preset CSS file containing my @theme stuff directly into the dist folder with no modification of the source code. Then I run npm pack to create the tarball After I have the npm package I can install in my consuming project like a normal npm package. To bring in the Vue <style> block styles and the Tailwind theme stuff I add a couple imports to the main CSS file for the consuming project. @import 'my-project/tailwind.preset.css';
@import 'my-project/styles.css';Then it just works. I pull in a component from the library and tailwind is able to find the classes applied to them and packages the correct CSS for me as well as I get the styles from the npm package applied to my theme. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on roughly a dozen projects using a component library package that needs migrating from Tailwind 3 to 4. This process is considerably hampered by a lack of documentation on how to migrate the JavaScript configuration format to an equivalent CSS setup.
The main problem I currently face is that I can't get the consuming projects to have styles for the components that are distributed via the component library package. I was hoping to isolate the process of compiling the final stylesheets to the consuming projects, but that doesn't seem to work with the compiled components coming from the component library package.
Approach 1
In the npm package, a bunch of (compiled) Vue.js components are distributed alongside a
tailwindPreset.cssfile that looks like this:In the consuming project, the main CSS file looks like this:
The component library also includes components that use
@applyin the Vue SFCstyleblock with an@referencestatement.Problem
This arrangement seems more or less sensible, but the consuming project is broken. Only Tailwind classes used in the consuming project have an effect; Tailwind classes used in the components distributed via the component library package don't.
How does one distribute a Tailwind preset and a bunch of Vue components and have a project consume them properly using the
@tailwindcss/viteVite plugin?Beta Was this translation helpful? Give feedback.
All reactions