Please refer my files for this project.
The issue that I am facing is that when I apply some custom css (LESS file) to any element what happens is that first my bootstrap css gets applied and after a fraction of second my custom css gets applied.
I am able to see the lag.
For example, first my div is 100% width and after a lag I see my custom width of 300px getting applied.
index.js
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter,
Route,
browserHistory,
Switch
} from 'react-router-dom';
import {
HomePage,
LoginPage,
SignupPage,
ForgotPasswordPage
} from './screens';
import {
Provider
} from 'react-redux';
import 'bootstrap/dist/css/bootstrap.min.css';
import './style/main.less';
import configureStore from './store';
const store = configureStore();
class App extends React.Component {
render() {
return (
<Provider store={store}>
<BrowserRouter history={browserHistory}>
<Switch>
<Route exact path="/" component={HomePage} />
<Route exact path="/login" component={LoginPage} />
<Route exact path="/register" component={SignupPage} />
<Route exact path="/forgot-password" component={ForgotPasswordPage} />
</Switch>
</BrowserRouter>
</Provider>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'development',
entry: {
app: ['./src/index.js']
},
output: {
path: path.join(__dirname, './build'),
publicPath: '/',
filename: 'bundle.[name].[hash].js'
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
],
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['react', 'env', 'stage-1']
}
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.less$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader', options: {
sourceMap: true
}
}, {
loader: 'less-loader', options: {
sourceMap: true
}
}]
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.json']
}
};
Simply writing my css in LESS files. Calling the main LESS file and bootstrap css in the index.js. Using classes on the div.
Related
When working with scss modules in typescript environment, my modules are stored in a property called default
Button.style.scss
.button {
background-color: black;
}
index.tsx
import * as React from 'react';
import * as styles from './Button.module.scss';
console.log(styles);
export const Button = () => (
<button className={styles.default.button}>Hello</button>
);
Console.log output
Module {__esModule: true, default: {button: "_2t432kRILm79F3WhLGdN6N"}, Symbol(Symbol.toStringTag): "Module"}
Why is that ? I cannot properly generate types with typed-scss-modules because of that.
EDIT:
Storybook config is
const path = require('path');
module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.#(js|jsx|ts|tsx)'],
addons: ['#storybook/addon-links', '#storybook/addon-essentials'],
webpackFinal: async (config) => {
config.module.rules.push({
test: /\.scss$/,
use: ['style-loader', 'css-loader?modules=true', 'sass-loader'],
include: path.resolve(__dirname, '../'),
});
return config;
},
};
You need to adjust your css-loader rule to have the "esModule": false option, like follows:
{
loader: 'css-loader',
options: {
esModule: false,
}
},
{
loader: 'sass-loader',
}
This ensures import statements will return just the string, only
I am trying to import global css file inside react javascript file. I am importing in following way:
import React from "react";
import MainWrapper from "./components";
import "./global.css";
const App = () => {
return (
<MainWrapper />
);
};
export default App;
My global.css file looks like following:
#import '/node_modules/antd/dist/antd.css';
i.anticon.anticon-menu-unfold.trigger, i.anticon.anticon-menu-fold.trigger {
margin: auto;
display: flex;
justify-content: center;
padding: 12px;
color: white;
}
i.anticon.anticon-menu-fold.trigger{
float: right;
}
Here above, I am overidding predefined css of ANT Design. Here it is not able to override ant's predefined css.
My webpack.config.js looks like following:
const HtmlWebPackPlugin = require("html-webpack-plugin");
const WebapckManifestPlugin = require("webpack-manifest-plugin");
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"#babel/preset-env",
"#babel/preset-react"
],
plugins: [
"#babel/plugin-syntax-dynamic-import",
"#babel/plugin-proposal-class-properties"
]
}
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
},
// {
// test: /\.css$/,
// use: [
// "style-loader"
// ,
// "css-loader"
// ]
// },
{
test: /\.css$/,
use:
[
{
loader: "style-loader",
},
// {
// loader: "isomorphic-style-loader",
// },
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: {
localIdentName: "[name]__[local]___[hash:base64:5]"
}
}
}
]
},
{
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: "url-loader?limit=100000" }
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./public/index.html",
filename: "./index.html"
}),
new WebapckManifestPlugin({
template: "./public/manifest.json"
})
],
resolve: {
extensions: ["*",".js",".jsx"]
}
}
However, look below. I am importing css file in below js file. It works
import React, { useState } from "react";
import { Layout, Menu, Icon } from "antd";
// import useStyles from 'isomorphic-style-loader/useStyles'
import style from "./style.css";
const Dashboard = () => {
// useStyles(style);
return(
<div className={style.dashboard}>
karan
</div>
);
};
export default Dashboard;
My style.css file looks like this:
.dashboard{
text-align: center;
}
I think the problem is with webpack.config.js.
When I change style syntax from:
className={style.dashboard}
To:
className="dashboard"
And import statement from:
import style from "./style.css";
To:
import "./style.css";
And webpack.config.js test: /.css$/ to:
{
test: /\.css$/,
use: [
"style-loader"
,
"css-loader"
]
}
Everythings works then.
I built react app from scratch. I'm trying to add react animated slider for my site from npm package. Unfortunatelly only images are shown - but one below another, I mean not in one row - and also navigations arrow are above images, even if styles are attached. When I use this npm package with create-react-app it works fine, as expected, but in my own boilerplate it brokes. I'm asking for help. Here's my code:
webpack.config.js:
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: ["babel-polyfill", "./src/index.js"],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(css|scss)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
]
},
{
test: /\.(woff|svg|png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './assets'
}
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: "style.css" }),
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
})
]
};
slider.js component:
import React, { Component } from 'react';
import Slider from 'react-animated-slider';
import 'react-animated-slider/build/horizontal.css';
import one from '../assets/slider/one.jpg';
import two from '../assets/slider/two.jpg';
import three from '../assets/slider/three.jpg';
class Carousel extends Component {
render() {
const content = [one, two, three];
return (
<Slider>
{content.map((article, index) => <div key={index}>
<img src={article} alt='wtz' />
</div>)}
</Slider>
);
}
}
export default Carousel;
app.js component:
import React from 'react';
import classes from './App.scss';
import Header from '../header/Header';
import Nav from '../nav/Navigation';
import Carousel from '../carousel/Carousel';
const app = () => {
return (
<div className={classes.container}>
<Header />
<Nav />
<Carousel />
</div>
);
};
export default app;
I think that somthing must be wrong with bundling, I don't know. Also my console tells that everything is correct.
You problem is that you have the same rule for sass and for css, sass-loader is being applied to css too.
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: ["babel-polyfill", "./src/index.js"],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(css)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}
}
]
},
{
test: /\.(scss)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
]
},
{
test: /\.(woff|svg|png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './assets'
}
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: "style.css" }),
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
})
]
};
I've installed bootstrap using
npm install bootstrap#4.0.0-alpha.6 --save
in my dependencies I have:
"dependencies": {
"bootstrap": "^4.0.0-alpha.6",
"react": "^15.6.1",
"react-dom": "^15.6.1"
}
in my styles.scss
#import '~bootstrap/scss/bootstrap';
and here's my webpack file:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/dist',
filename: 'build.js'
},
devServer: {
historyApiFallback: true
},
module: {
loaders: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ },
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader?importLoaders=1&modules&localIdentName=[name]_[local]_[hash:base64:5]',
'sass-loader',
],
}
]
},
plugins: [HtmlWebpackPluginConfig]
}
but when I add container class in my header component
import React from 'react';
export class Header extends React.Component {
render() {
return(
<header className="container">header here</header>
);
}
}
export default Header;
it looks like this:
and in build.js I can't find .container class. .row as well etc.
inside bootstrap.scss>>import grid container looks this:
#if $enable-grid-classes {
.container {
#include make-container();
#include make-container-max-widths();
}
}
and in _variables.scss inside bootstrap this variable is true
$enable-grid-classes: true !default;
so what's wrong?
You have to import your .scss file in your component and apply in html. Like:
import React from 'react';
import styles from './styles.scss'; // your .scss file
export class Header extends React.Component {
render() {
return(
<header className={styles.container}>header here</header>
);
}
}
export default Header;
I can't figure out how to render css with the webpack sass-loader.
App.js file:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Test from "./Test";
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<Test />
</div>
);
}
}
export default App;
Test.jsx file:
import React, { Component } from 'react';
import './Test.scss';
class Test extends Component {
render() {
return (
<p className="intro">
Test
</p>
);
}
}
export default Test;
Test.scss file:
.intro{
background-color: #dddddd;
color: red;
}
webpack.config.dev.js file:
{
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
],
},
{
test: /\.scss$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
}, {
loader: "css-loader" // translates CSS into CommonJS
}, {
loader: "sass-loader" // compiles Sass to CSS
}]
}
Image:
Result
Expected
I'm doing something wrong with the sass loader. What is it? Please help.
{
exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.scss$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
}
I added /.scss$/ here, the code is running successfully, thank you all
This is how I use sass-loader together with the help of extract-text-webpack-plugin to create a separate file instead of just including it to bundle.js file.
I'm using webpack v2.6.1
// webpack-config.js
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module: {
rules: [
{ test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader']}) },
{ test: /\.scss$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader', 'sass-loader']}) },
],
},
plugins: [
new ExtractTextPlugin('[name]-[chunkhash].css'),
]
and import it to my index.js, like so:
import '../public/assets/sass/style.scss';