I'm using @storybook/vue": "^6.5.10"
. My components are styled by the <style>
block in the bottom of each .vue file. There are also some global CSS (Sass) files that are compiled by the Rails webpacker.
To reproduce the global CSS files in my stories, I wrote decorators (example below) that recreate various CSS contexts. The problem is that the CSS I load in one decorator is applying to my other components/stories and I don't understand why!
My simplified decorator:
// THESE π STYLES ARE SHOWING UP IN STORIES THAT DO NOT USE THIS DECORATOR!
import '!style-loader!css-loader!sass-loader!../../app/javascript/styles/site.sass';
const sitePackDecorator = (story) => {
// other irrelevant stuff
return {
components: { story },
template: '<story />'
}
}
;
export {sitePackDecorator}
Then in my story files, I apply it at the component level like this:
import MyComponent from '../app/javascript/src/site/components/assets/MyComponent'
import { sitePackDecorator } from './utilities/sitePackDecorator';
export default {
title: 'My Component',
component: MyComponent,
parameters: {
layout: 'fullscreen'
},
decorators: [sitePackDecorator]
};
const Template = (args, { argTypes }) => ({
components: { MyComponent },
props: Object.keys(argTypes),
template: '<MyComponent v-bind="$props" />',
})
Here's my Rails configs for webpack / webpacker:
const { environment } = require('@rails/webpacker')
const { VueLoaderPlugin } = require('vue-loader')
const vue = require('./loaders/vue')
const sass = require('./loaders/sass')
const pug = require('./loaders/pug')
const customConfig = require('./alias')
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin())
environment.loaders.prepend('sass', sass)
environment.loaders.prepend('vue', vue)
environment.loaders.prepend('pug', pug)
environment.config.merge(customConfig)
environment.plugins.prepend("CleanWebpackPlugin", new CleanWebpackPlugin());
module.exports = environment
...and main.js references this rails config like this:
const custom = require('../config/webpack/development.js');
module.exports = {
"stories": [
"../stories/**/*.stories.mdx",
"../stories/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions"
],
"framework": "@storybook/vue",
webpackFinal: async (config) => {
return { ...config, module: { ...config.module, rules: custom.module.rules } };
},
}
Note: I ran yarn storybook --debug-webpack
and I see:
info => Using implicit CSS loaders
. I'm not sure what this means, but sounds potentially relevant.
Things I tried that did NOT work
- Moving the
import !style-loader!...
into the story file - Not adding the offending decorator anything. Even when unused, the styles still get loaded!
- Moving the
import !style-loader!...
down into the arrow function of the decorator. This causes an error becauseimport
has to be at the top level. - Changing to
import '.../site.sass'
- Changing to a named import (
import styles from '.../site.sass'
) and then callinguse()
on the imported object.
import '.../site.sass'
. Did that answer your question? β Hanhanaimport
s apply globally? β Hanhana