Sass failed to import using "~" from the node_modules folder - css

I'm using Angular 6 to develop a single page web application,
and I added the following ngx-toast library to my project.
when I added the following Sass into my project and when I used "~" instead of the relative path it failed to load the libraries:
// regular style toast
#import '~ngx-toastr/toastr.css';
// bootstrap style toast
// or import a bootstrap 4 alert styled design (SASS ONLY)
// should be after your bootstrap imports, it uses bs4 variables, mixins, functions
#import '~ngx-toastr/toastr-bs4-alert';
// if you'd like to use it without importing all of bootstrap it requires
#import '~bootstrap/scss/functions';
#import '~bootstrap/scss/variables';
#import '~bootstrap/scss/mixins';
#import '~ngx-toastr/toastr-bs4-alert';
but it's work when I'm using the relative path:
// regular style toast
#import '../../node_modules/ngx-toastr/toastr.css';
// bootstrap style toast
// or import a bootstrap 4 alert styled design (SASS ONLY)
// should be after your bootstrap imports, it uses bs4 variables, mixins, functions
#import '../../node_modules/ngx-toastr/toastr-bs4-alert';
// if you'd like to use it without importing all of bootstrap it requires
#import '../../node_modules/bootstrap/scss/functions';
#import'../../node_modules/bootstrap/scss/variables';
#import '../../node_modules/bootstrap/scss/mixins';
#import '../../node_modules/ngx-toastr/toastr-bs4-alert';
my component modules
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
of course that I can leave it like that, or just download the actual css from the manual, but it's bothering me that's it's fails to import since it should work.
any solutions?

As per the SASS Docs, ~ will point to src/ folder, when we import any SCSS files. We need to tell angular to include node_modules/ path, when we import any sass file. This can be achieved using stylePreprocessorOptions property inside angular.json.
"styles": [
...
],
"stylePreprocessorOptions": {
"includePaths": [
"../node_modules/ngx-toastr"
]
}

Related

Issue displaying SCSS styles with vue.js

I'm creating a vue.js web app, and I'd like to use SCSS to help with the styling. I've installed npm i node-sass sass-loader and I've created a vue.config.js at root level. In it I have this set
module.exports = {
css: {
loaderOptions: {
scss: {
additionalData: `#import "#/styles/global.scss";`
}
}
}
};
This is SCSS the folder structure
In every subfolder to the main styles folder I have a _all.scss that then imports every containing .scss file for that given subfolder. And in the global.scss all the subfolders _all.scss files gets imported.
// styles/base/_all.scss
#import 'reset.scss';
#import 'variables.scss';
// global.scss
#import 'base/all';
#import 'components/all';
My issue is that my web app doesn't load any of the .scss files or styles imported via the main global.scss file. I get no build errors, and if I for example remove the _variables.scss file, then I get an error that other .scss files can't access the declared scss variables. So that means that my .scss import method is woriking, but the styles aren't shown somehow.
Do I have to use every vue components <style> tag to do all the styling, or can I go about doing it the way I've structured it now?
I prefer not messing with the webpack config.
you can import your style in the main.js also
import '#/styles/global.scss';

Style of React component is not working on Heroku

index.jsx
import 'react-date-range/dist/styles.css'
import 'react-date-range/dist/theme/default.css'
I deployed a Rails app(Rails + React.js) on Heroku.
But styles of react-date-range component are not loaded on Heroku even it is working on local.
What is the way to fix this issue?
Obviously, you're using Webpack and you have configs for loading CSS, instead of using index.jsx add these two pre-compiled CSS into your root of CSS by this:
#import url("react-date-range/dist/styles.css");
#import url("react-date-range/dist/theme/default.css");
If the url function doesn't work try without it:
#import "react-date-range/dist/styles.css";
#import "react-date-range/dist/theme/default.css";
If these two guys aren't in your final bundle file, simply, use relative path, because it is a little hard to add Webpack configuration that Webpack understand node_modules absolute path of libraries inside css-loader.
#import "../node_modules/react-date-range/dist/styles.css";
#import "../node_modules/react-date-range/dist/theme/default.css";

How to use the bootstrap 4 SASS mixins with customization in Angular 7 without importing multiple bootstraps

In my angular.json this is what I have
"styles": [
"src/assets/styles.scss"
],
and in my styles.scss, I added my custom.scss
#import 'vendor/bootstrap/scss/custom';
in my custom.scss, this is what I have
// custom 24 grid system
$grid-columns: 24;
#import "node_modules/bootstrap/scss/bootstrap";
In my SASS partials files, I can use the bootstrap 4 mixins without importing my custom.scss file but for a scss file that's coupled with an Angular component, I have to import custom.scss file like this to use any of the bootstrap 4 mixins.
#import 'vendor/bootstrap/scss/custom';
but if I do this, in my style sheet, I see bootstrap being imported twice. How can I avoid duplicate imports? I've tried to add my custom.scss straight to my angular.json file but that also duplicated the import.

How to override Bootstrap theme colors in bootstrapped react/redux app?

probably a newbie questions (starting with REACT) but I am unable to change the theme colors in my newly bootstrapped react application.
I read some answers but unfortunately, they did not work for me.
I have the following entry point:
import React from 'react';
import ReactDOM from 'react-dom';
import { App } from './components/App';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap';
import './styles/app.scss';
ReactDOM.render(<App />, document.getElementById('app'));
and I have app.scss style sheet in src/styles:
#import '_settings.scss';
#import '_base.scss';
#import './components/_header.scss';
I tried changing the theme colors here but it doesn't seem to do anything:
#import "node_modules/bootstrap/scss/bootstrap";
$theme-colors: (
"primary": #991196,
"danger": #ff4136
);
Could someone point out to me how this works since all the answers and tuturials I came across seem to be using different approaches.
I am using the following version of Bootstrap:
"bootstrap": "^4.0.0"
First refer this, Bootstrap webpack
This is one way of using bootstrap in reactjs projects.
You are importing compiled version of bootstrap css by doing this
import 'bootstrap/dist/css/bootstrap.min.css'; //Dont do this
Instead you should do this
In your app.scss file,
first define your color
$primary: #13C7CD;
$danger: #red;
then
import scss version of bootstrap
#import '~bootstrap/scss/bootstrap';
~ means webpack will search for it in node_modules
After this import the app.scss file in your main js file(can be index.js or app.js)
import './styles/app.scss';
How this works
When you define your color variables before importing bootstrap scss, the
resulting complied css will have your color values
You can make the overrides and import Bootstrap entirely in the app.scss file. It's important the Bootstrap is imported after the theme color changes to override the !default theme-colors defined in bootstrap _variables.scss.
#import '_settings.scss';
#import '_base.scss';
#import './components/_header.scss';
/* import only the Bootstrap files needed for customizations */
#import "node_modules/bootstrap/scss/functions";
#import "node_modules/bootstrap/scss/variables";
/* make the customizations */
$theme-colors: (
"primary": #991196,
"danger": #ff4136
);
/* import bootstrap to set changes */
#import "node_modules/bootstrap/scss";
When the react app is built and the SASS is compiled then generated CSS will contain your customizations and Bootstrap so that you won't need to separately import bootstrap in react.
Demo: https://www.codeply.com/go/Tho2TEwoI1
The simplest way...
Just import/define YOUR styles before BOOTSTRAP styles. That's it! 'Happy hacking'

Setup webpack sass-loader to separate styles

In my app, I have such folder structure for styles
styles
head
main
Head folder contains styles which supposed to be in <style> tag to fast render first screen.
The main folder contains styles which supposed to be compiled into style.css.
How can I setup web pack sass-loader to do that?
In your loaders in your webpack config add:
{
test: /\.scss$/,
loader: 'style!css?modules=true&localIdentName=[name]__[local]___[hash:base64:5]!sass'
}
Then for head and main you can name them with the scss extension and
import them like this (in your JS file)
import head from './head.scss'
import '!style!css?sass!<path-to-your-main>main.scss'
Then you can use it in your JSX like this:
<p className={head.myHeaderStyle}>Some text</p>

Resources