storybook: Error importing styles.scss to storybook
The bug I followed this tutorial in starting storybook. Though the tutorial is using less, I decided to use scss.
When I import my styles.scss into the config.js in .storybook with this configuration in webpack.config.js
const path = require("path");
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader"],
include: path.resolve(__dirname, "../")
}
]
}
};
as per https://storybook.js.org/configurations/custom-webpack-config/, it gives me an error.
If I try to change the webpack.config.js to this solution: it doesn’t do anything.
Reproduce Steps to reproduce the behavior:
- Go to https://www.learnstorybook.com/angular/en/get-started/ and use sass styling.
- Continue through https://www.learnstorybook.com/angular/en/simple-component/
- Upon setting config.js instead of styles.less use styles.scss
- Upon setting webpack.config.js use this config instead:
const path = require("path");
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader"],
include: path.resolve(__dirname, "../")
}
]
}
};
from official tutorial.
And then install node-sass.
Then run npm run storybook.
Expected behavior I should be able to see styles from whatever is in styles.scss
Screenshots
Code snippets package.json
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"node-sass": "^4.11.0",
"rxjs": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.2",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.1.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2",
"@babel/core": "^7.3.3",
"babel-loader": "^8.0.5",
"@storybook/angular": "^4.1.12",
"@storybook/addon-notes": "^4.1.12",
"@storybook/addon-actions": "^4.1.12",
"@storybook/addon-links": "^4.1.12",
"@storybook/addons": "^4.1.12"
}
config.js
import { configure } from '@storybook/angular';
import '../src/styles.scss';
// automatically import all files ending in *.stories.ts
const req = require.context('../src/stories', true, /.stories.ts$/);
function loadStories() {
req.keys().forEach(filename => req(filename));
}
configure(loadStories, module);
webpack.config.js
const path = require("path");
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader"],
include: path.resolve(__dirname, "../")
}
]
}
};
System:
- OS: Windows10
- Device: Laptop
- Browser: chrome
- Framework: angular
- Addons: [if relevant]
- Version: [“@angular/core”: “~7.2.0”, “@storybook/angular”: “^4.1.12”]
Source Code HERE.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 14
- Comments: 35 (10 by maintainers)
@codestitch little fix for scss (sass-loader):
to config.js file. Work for me with HMR.
this works fine for local scss files, but not working for importing scss files from
node_modules. any idea ?Try this solution, it worked for me, the only difference is I used this configuration for LESS.
I had a difficult time using custom config. Logged the https://github.com/storybookjs/storybook/issues/11052
Solved it!
As a work around i mutated the config object to inject common scss. For more explaination & implementation checkout the blog i wrote on this topic.
Sure @albertjke. You can check my repo at https://github.com/codestitch/sbnx.
it’s using version 5.0.6 though. What I did there is I added this line:
import '!style-loader!css-loader!../stories/stories.scss';to config.js file.
PS. When running
npm run storybookyou will encounter an error as I purposely do that for some issue. you can remove the ListComponent in the story to view the working one.My bad. I looked at the iframe and the style are already there. Sorry for wasting your time @tmeasday .
So, this code
import '!style-loader!css-loader!../stories/styles.scss';works in config.js instead of import"../stories/styles.scss";orrequire('../stories/styles.scss');!but the webpack.config.js example in the documentation really doesn’t work.
Thanks a lot.
Same here 😦
To universally
@importSass styles, including from node_modules, something like this in yourwebpackFinalinmain.js:(n.b. exact sass-loader options vary, this is sass-loader@8, webpack@5)
At the top of my .storybook/config.js I added:
I think you can import your styles in the same way. PS: The only way this works is to add at the very top of the file, in the first line
@magisters-org
This works but it outputs:
Here is another solution I found, because I got
Invalid CSS after "": expected 1 selector or at-rule: https://github.com/storybookjs/storybook/issues/6467#issuecomment-481425563 (fix for the full-control example here)@shiftgeist here’s more about the deprecation https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#Deprecate-webpack-extend-mode
is there a way of adding a global style in all the stories?