I'm trying to figure out how to globally import and use stylus variables in my Vue Vite project. How can I achieve this and access the variables within the script section of my Single File Component (SFC)?
Below is an excerpt from my Vite config:
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
css: {
preprocessorOptions: {
styl: {
additionalData: `@import "@/styles/styles.styl"`
}
}
}
})
Inside my styles.styl
file, I define a variable like so:
contentSideMargin = 50px
When attempting to use the style from styles.styl
in my SFC as shown below, it doesn't seem to work:
<style lang="stylus" scoped>
#main-container
padding: $contentSideMargin /* have also tried `contentSideMargin` */
</style>
Any insights or solutions would be greatly appreciated.
—
Update: Including my package.json for reference. There are no visible errors, but the variable seems to be passed directly into the CSS rather than its value being applied.
{
"name": "project",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"pinia": "^2.0.17",
"pug": "^3.0.2",
"vue": "^3.2.37",
"vue-router": "^4.1.3"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.0.0",
"stylus": "^0.58.1",
"stylus-loader": "^7.0.0",
"vite": "^3.0.0"
}
}