Loaders for both Semantic UI React and CSS Files - reactjs

I stumbled upons some errors when i tried to use loaders for semantic-ui-css/semantic.min.css and normal css files at the same time. I was able to load in respectively the semantic.min.css and the normal css files when i did it indepentently, but not at the same time.
Loader for Semantic UI (It works)
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
},
},
},
{
test: /\.css$/,
include: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: 'css-loader',
}
],
})
},
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = config;
Loader for the normal CSS files (It works)
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = config;
Loaders for both at the same time (Does not work)
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
loader : 'babel-loader'
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
},
},
},
{
test: /\.css$/,
include: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: 'css-loader',
}
],
})
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = config;

This is because you are defining two tests for .css in your loaders. The first test is evaluating is true for all .css files that are included in the project so all CSS is resolving with your first loader rule. If you want to use two rules for .css files then you need to get more specific with your test. I'd put the node_modules rule second. And for the first rule, you can use exclude to make sure the loader skips anything in node_modules which will then go to your next loader.

Related

what is the right webpack configuration for scss files with style, css and sass loader

at first i was getting error that scss files are not readable, when i tried to add options for sass loader to set its path to node_modules. But after that i could not solve this error. Is there anyother way to add multiple loaders and options for them?
this is my webpack.config.js"
var webpack = require('webpack');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
var BundleTracker = require('webpack-bundle-tracker');
var ExtractText = require('extract-text-webpack-plugin');
module.exports = {
entry: path.join(__dirname, 'assets/src/js/index'),
output: {
path: path.join(__dirname, 'assets/dist'),
filename: '[name]-[hash].js'
},
plugins: [
new BundleTracker({
path: __dirname,
filename: 'webpack-stats.json'
}),
new ExtractText({
filename: '[name]-[hash].css'
}),
new CleanWebpackPlugin(),
],
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
loader: ['style-loader', 'css-loader'],
},
{
test: /\.scss$/,
use:[{
loader: "sass-loader",
options: {
"includePaths": [
path.resolve(__dirname, 'node_modules')
]
}
},
{
loader: "style-loader"
},
{
loader: "css-loader"
}
]
},
],
},
};```[this is the error i am getting][1]
[1]: https://i.stack.imgur.com/6O8ct.png

Invalid configuration object in webpack.config.js

I get contiously this error
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
my webpack.config.js is as this
var path = require('path');
var hwp = require('html-webpack-plugin');
module.exports = {
entry: path.join(__dirname, '/src/index.js'),
output: {
filename: 'build.js',
path: path.join(__dirname, '/dist')
},
module: {
rules: [
{ test: /\.tsx?$/, loader: ['ts-loader'] },
{ test: /\.css$/, loader: "style-loader!css-loader" },
{
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
}]
},
{ test: /\.(otf|ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
loader: 'file-loader?name=./Scripts/dist/[name].[ext]' }
]
},
plugins:[
new hwp({template:path.join(__dirname, '/src/index.html')})
]
}
can somebody help me, I have tried many samples of webpack.config.js but they don't work. is it really so hard to work with react?
I am new in react. I know how to code, but I can not build a project of my own
try the following snippet:
var path = require("path");
var hwp = require("html-webpack-plugin");
module.exports = {
entry: path.join(__dirname, "/src/index.js"),
output: {
filename: "build.js",
path: path.join(__dirname, "/dist")
},
module: {
rules: [
{ test: /\.tsx?$/, loader: ["ts-loader"] },
{ test: /\.css$/, loader: "style-loader!css-loader" },
{
test: /\.scss$/,
use: [
{
loader: "style-loader" // creates style nodes from JS
},
{
loader: "css-loader" // translates CSS into CommonJS
},
{
loader: "sass-loader" // compiles Sass to CSS
}
]
},
{
test: /\.(otf|ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
loader: "file-loader?name=./Scripts/dist/[name].[ext]"
}
]
},
plugins: [new hwp({ template: path.join(__dirname, "/src/index.html") })]
};

add scss (bootstrap) compilation with output css file in react project

I have a react/express project
I want to be able to customize the Bootstrap 4 and output a CSS file which will contain the customized boostrap.css.
I have npm installed the loaders bootstrap needs ( postcss-loader,sass-loader,style-loader etc), although in web I saw an example with ExtractTextPlugin in order to get output file
when I run the webpack config I get an error
" throw new Error("'output.filename' is required, either in config file or as --output-filename");"
Any tutorial/help how to accomplish compiling customized bootstrap in seperate output file?
webpack config
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
//root file for the client/browser app
entry: './src/client/client.js',
//output file and its location
output: {
filename: 'bundle.js',
path: path.resolve(__dirname,'public'),
},
//run babel for every filename
module: {
rules: [
{
test: /\.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets:
[
'react',
'stage-0',
[
'env',
{
targets:
{
browsers:
['last 2 versions']
}
}
]
]
}
}
]
},
};
module.exports = {
// root file for the client/browser app
entry: './src/client/styles/main.scss',
// output file and its location
output: {
filename: 'styles2.css',
path: path.resolve(__dirname,'public'),
},
module: {
loaders: [
// ...
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css!sass')
}
]
},
plugins: [
new ExtractTextPlugin('public/style.css', {
allChunks: true,
})
]
};
You need to remove the extra module.exports. I do not see the need to have two of these as you can simply achieve the same thing inside a single module.exports.
You can achieve this like so:
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
//root file for the client/browser app
entry: [
'./src/client/client.js',
'./src/client/styles/main.scss'
],
//output file and its location
output: {
filename: 'bundle.js',
path: path.resolve(__dirname,'public'),
},
//run babel for every filename
module: {
rules: [
{
test: /\.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets:[
'react',
'stage-0',
[
'env',
{
targets:
{
browsers:
['last 2 versions']
}
}
]
]
}
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
}
],
plugins: [
new ExtractTextPlugin('public/style.css', {
allChunks: true,
})
]
},};

Webpack error: You may need an appropriate loader to handle this file type

I am new to webpack and react and am trying to set up an application. From looking at previous questions I think there must be something wrong with how I am setting up babel-loader, but I'm unable to see what my mistake is. Is anyone else able to see it?
webpack.config.js:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: [
{
test: /\.jsx?/,
include: APP_DIR,
loaders: ["babel-loader"],
query: {
presets: ['es2015', 'react']
}
}
]
};
module.exports = config;
babel.rc
{
"presets": ["es2015", "react"]
}
Index.jsx
import React from 'react';
import ReactDom from 'react-dom';
class App extends React.Component {
render() {
return <p>Hello React!</p>;
}
}
ReactDom.render(<App />, document.getElementById('app'));
Here is the documentation for the module options object: https://webpack.github.io/docs/configuration.html#module
If you have babel-preset-2015 and babel-preset-react npm modules installed and the following webpack.config.js (babel.rc file isn't needed with the query presets) should work:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?/,
include: APP_DIR,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
}]
}
};
module.exports = config;
Change you webpack file to include the babel-loader within quotes and included in an array of loaders as shown below. In modules you define an array of loaders to handle different types of files but a single loader for a particular type.
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'waves/client/public');
var APP_DIR = path.resolve(__dirname, 'waves/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js?$/,
exclude: /(node_modules|bower_components)/,
include: APP_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015'],
}
}
]
},
};
module.exports = config;

CSS loading throw error using webpack

I have implemented webpack config like
/* eslint-disable no-var */
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'webpack-hot-middleware/client',
'./src/main'
],
devtool: 'eval-source-map',
output: {
path: __dirname,
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel'],
include: path.join(__dirname, 'src')
},
{
test: /\.css$/, // Only .css files
loader: 'style!css' // Run both loaders
}
]
}
};
and I have installed the css-loader style-loader for loding the css on my page and I am using the bootstrap.min.css as below
require('../../node_modules/bootstrap/dist/css/bootstrap.min.css');
it's throw error like
Bootstrap is using the Glyphicons font. You also need to have a loader for the font:
{
test: /\.woff$/,
loader: 'url?limit=100000'
}
Source: christianalfoni.github.io/react-webpack-cookbook/Inlining-fonts
Or:
{
test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
loader: 'file-loader'
}
Source: stackoverflow.com/a/31183889/2378031

Resources