https://github.com/GRIM4CE/nuxt-css-module-issue
In vue-component import style from './index.css' and set css-module in a computed property to imported style object: computed: { style() { return style }. In template html add <section :class="style.container"> css-module to a element.
index.css
.container {
background-color: aqua;
width: 100vw;
height: 100vh;
}
css json object is imported and css-module classes are populated in vue-component, styles render as expected
empty object is returned from import, css-module classes are not populated and styles don't render
I originally stumbled upon this issue after creating a private component library for work that utilized css-modules to switch between to different types of themes on package config. Everything looked great on our vue projects but once I uploaded the package to Nuxt it was obvious the package styles weren't working correctly.
I'm not completely sure if this is an issue with Nuxt but I've tested Vue-cli-3 by enabling css-modules and the expected behavior works by default. I dug through both Nuxt and Vue-cli-3 build loaders and don't see anything immediately that would cause different behavior between the two. It was suggested I delete the resourceQuery from the css-loader, this allowed css-modules to work as expected but broke normal template styles.
Hi @GRIM4CE
vue-loader will resolve style with module attribute and inject the CSS modules locals object into the component as a computed property with the name $style.
Could you try below ?
<template>
<section :class="$style.container">
<h1>hey</h1>
<h2 class="test">hey</h2>
</section>
</template>
<script>
export default {
}
</script>
<style module>
@import './index.css';
</style>
<style>
h1 {
font-size: 100px;
}
.test {
font-size: 100px;
}
</style>
Another tricky way is append module to resource query for making css-laoder use css modules.
<template>
<section :class="style.container">
<h1>hey</h1>
<h2 class="test">hey</h2>
</section>
</template>
<script>
- import style from './index.css'
+ import style from './index.css?module'
export default {
computed: {
style() {
console.log(style)
return style
}
}
}
</script>
<style>
h1 {
font-size: 100px;
}
.test {
font-size: 100px;
}
</style>
Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:
Issues that are labeled as pending will not be automatically marked as stale.
Thank you for your help! ?module was the trick I was looking for, someone on stack overflow recommended it to me a month or so ago.
Most helpful comment
Hi @GRIM4CE
vue-loaderwill resolvestylewithmoduleattribute and inject the CSS modules locals object into the component as a computed property with the name$style.Could you try below ?
Another tricky way is append
moduleto resource query for making css-laoder use css modules.