PROD and DEV CSS rendering inconsistent in Nuxt3
P粉336536706
2023-08-27 09:48:04
<p>I built a Vue3 component library (let's call it MY-LIBRARY) using Nuxt3, using tailwind v2.
I have a side project called MY-SIDE-PROJECT, built with laravel-mix (v6) and Vue3, which uses tailwind v2.
I have another Vue3 project (let's call it My-PROJECT) built with Nuxt3, also using tailwind v2. </p>
<p>In MY-PROJECT, I use components from MY-LIBRARY to render the page. </p>
<p>When I build my project in production using the <code>npm run build</code> script that triggers <code>nuxt build</code>, my page is served, but my A warning appears in the console, for example: </p>
<pre class="brush:php;toolbar:false;">[WARNING] CSS nesting syntax is not supported in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14 ") [unsupported-css-nesting]
<stdin>:1:62181:
1 │ ...-cell}@media (min-width:1024px){&--bordered-max-lg,&--hidden-max...
╵</pre>
<p><strong>EDIT: This issue has been resolved, the CSS for MY-LIBRARY was not bundled correctly. </strong></p>
<p><strong>Still, the CSS in my project page is not being interpreted correctly. </strong></p>
<p>I want pages in MY-PROJECT to properly serve CSS from MY-LIBRARY when built in production. </p>
<p>I have tried using nuxt's configuration in MY-PROJECT, but the combination of Nuxt3/Vue3 and tailwind v2 is not well documented on the internet. I can't really move to tailwind3 at the moment because the project has some external compatibility requirements.</p>
<p>This is the nuxt.config.js file for my project: </p>
<pre class="brush:php;toolbar:false;">export default defineNuxtConfig({
...
css: [
'@/assets/styles/main.scss'
],
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
},
build: {
postcss: {
postcssOptions: {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
}
}
}
})</pre>
<p>This is the tailwind.config.js file for my project: </p>
<pre class="brush:php;toolbar:false;">module.exports = {
important: true,
presets: [
require('MY-LIBRARY/tailwind.config')
],
purge: {
content: [
"./components/**/*.{js,vue,ts}",
"./pages/**/*.vue",
"./nuxt.config.{js,ts}",
"./app.vue",
]
},
theme: {
extend: {
...
}
},
variants: {
...
}
}</pre>
<p>I've tried adding postcss.config.js in MY-PROJECT but it didn't change anything. </p>
<p>If you have any suggestions on how to solve my problem, I would be very grateful!
Thanks in advance. </p>
<p><strong>Edit: </strong> After trying a few things, it seems the main problem is the commands <code>npm run dev</code> (run locally) and <code>npm run build ; npm run start</code> (when deploying, but I reproduce locally too) renders unevenly. CSS is not rendered similarly.这是我的项目的 package.json:</p>
<pre class="brush:php;toolbar:false;">{
"name": "MY-PROJECT",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev --port=3000",
"docker": "npm run dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"start": "nuxt start"
},
"engines": {
"node": "16.x",
"npm": "8.x"
},
"dependencies": {
"MY-LIBRARY": "^1.1.2",
"@pinia/nuxt": "^0.4.7",
"autoprefixer": "^10.4.14",
"axios": "^1.3.4",
"luxon": "^3.3.0",
"nuxt": "^3.3.1",
"nuxt-proxy": "^0.4.1",
"pinia": "^2.0.33",
"sass": "^1.60.0",
"tailwindcss": "^2.2.17",
"ufo": "^1.1.1",
"vue-i18n": "^9.2.2",
"vue3-lottie": "^2.5.0"
},
"devDependencies": {
"@nuxtjs/tailwindcss": "4.2.1",
"@types/node": "^18.14.2",
"@vue/eslint-config-prettier": "^7.1.0",
"@vue/eslint-config-typescript": "^11.0.2",
"eslint": "^8.34.0",
"eslint-plugin-vue": "^9.9.0",
"npm-run-all": "^4.1.5",
"postcss": "^8.4.21",
"prettier": "^2.8.4",
"sass-loader": "^13.2.2"
},
"overrides": {
"vue": "latest"
}
}</pre>
<p><br /></p>
I finally understand where the problem lies. In my tailwind configuration, I need to specify that I also want to apply tailwind to components imported from MY-LIBRARY.
Thanks to @JSONDerulo for the exchange, which allowed me to pinpoint the source.