React.semantic ui's Icones are not loading with webpack middle ware - node.js

I am trying to build a react application using React Semantic.
I am using webpack-dev-server, webpack-hot-middleware and webpack-dev-middleware to automating the tasks.
I installed react semantic ui through npm.
npm install --save semantic-ui-css semantic-ui-react
however, webpack seems to not loading ./node_modules/semantic-ui-css/themes/assets/fonts/ icons and it is not accessabile for the html page.
there is no errors in console, it just does not show the icon.
If I do not import semantic-ui-css in react app and includ in with CDN, it works fine.
App.js
import React from 'react';
import 'semantic-ui-css/semantic.min.css';
import { Container, Icon } from 'semantic-ui-react';
class App extends React.Component{
render(){
return (
<Container fluid>
<h1 id='test'>ReactJs</h1>
<hr/>
<Icon name='search' />
</Container>
)
}
}
export default App;
webpack.config.dev.js
import path from 'path';
import webpack from 'webpack';
export default {
devtool: 'eval-source-map',
entry: [
'webpack-hot-middleware/client',
path.join(__dirname, '/client/index.js')
],
output:{
path: '/',
publicPath: '/',
filename: 'bundle.js'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin,
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module:{
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'client'),
loaders: ['react-hot-loader', 'babel-loader']
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader','sass-loader'],
},
{
test: /\.scss$/,
exclude: /node_modules/,
loaders: ['raw-loader', 'sass-loader'] // sass-loader not scss-loader
},
{
test: /\.sass$/,
loaders: ["style-loader", "css-loader", "sass-loader"]
},
{
test: /\.svg$/,
loader: 'svg-loader'
},
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
{ test: /\.(ttf|eot|svg|png)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
]
},
resolve:{
extensions: ['.js']
}
}
Is there any loader or package to include icons from semantic ui package?

I am also using SUI-React with webpack. I was having a similar problem, however for me the icon files were 'packed' by webpack, but weren't being loaded correctly. Are you sure that webpack isn't finding them at all?
I eventually found that the source of my issue was the webpack configuration: my output path was a dist subdirectory, from which the index.html loaded bundle.js. This meant that webpack (and file-loader) made the urls relative to that directory. Once I changed my output path to the parent directory, and changed the output file to be dist/bundle.js, everything worked as expected.
My webpack.config.js specifies the entrypoint as yours does (although only one entrypoint, I'm not using webpack-hot-middleware), and my loaders are:
loaders: [
{
test: /\.jsx?/,
include: SRC_DIR,
loader: 'babel'
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'file-loader'
}
]
I'm loading the css using
require("!style-loader!css-loader!semantic-ui-css/semantic.min.css")
in the index file rather than using webpack loaders, however I think that should be equivalent to what you're doing.
Maybe your .css loader rule shouldn't include the sass-loader?
Also, the loader configuration option use seems to have been added in webpack v2, so if you're using webpack v1, that should be loaders as you have for the other rules.
Hope you can figure it out!

Related

Webpack doesn't render sass correctly when not exporting file

I am trying to configure Webpack 4 to use Sass for a MERN project. When I use mini-css-extract-plugin and export a CSS file and then link it in the index.html file it works. However, when I try to bundle the css with the bundle.js file, it will not render the css and the odd thing is, it does not render just some parts of it. I tried following the official bootstrap webpack installation, but it did not work as intended.
Here are the files:
webpack.config.js:
const path = require('path');
// const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './client/src/index.js',
mode: 'development',
module: {
rules: [
{
test: /\.s?css$/,
exclude: /node_modules/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
localIdentName: '[name]__[local]__[hash:base64:5]',
modules: true,
importLoaders: 1,
sourceMap: true,
},
},
{
loader: 'sass-loader'
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
options: {
presets: ['#babel/preset-env']
}
},
{
test: /\.jsx$/,
exclude: /node_modules/,
loader: "babel-loader",
options: {
presets: ['#babel/preset-env']
}
}
]
},
output: {
path: path.resolve(__dirname, 'client/public'),
filename: 'bundle.js'
}
}
./client/src/index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import 'bootstrap';
import '../sass/style.scss'
import App from './components/App';
ReactDOM.hydrate(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
)
I bealive that webpack 4 support MiniCssExtractPlugin , the other loaders that are you trying to use dont work correctly .
Check de Docs and try it :
https://webpack.js.org/plugins/mini-css-extract-plugin/

Importing SVG files - font-awesome with Webpack

I have a project with webpack which uses svg. Certain inline svg files are translated correctly but those from font-awesome are not.
I import font awesome with:
import 'font-awesome-webpack';
On the webpack side I have:
{
test: /\.svg$/,
loaders: [
'babel-loader',
{
loader: 'react-svg-loader',
query: {
jsx: true,
},
},
],
}],
This works fine but displays a square instead of font-awesome icons. Now following other fixes I should add a loader config for other non-inline SVGs, such as:
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url-loader?limit=10000&mimetype=application/font-woff"
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "file-loader"
}
As soon as I add the above lines and attempt to launch my project the following error is displayed:
Uncaught (in promise) Error: Invalid tag:/f01bf49c263128347d1c47cdc55eff66.svg
The issue comes clearly from the way I load Svgs following my wbepack config, but regardless of the different ways I import my svgs, the same error continues to break my project.
Is it possible that the two webpack config create a conflict?
What am I doing wrong with my webpack config?
FYI solution is:
webpack config file:
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff',
}, {
test: /\.(ttf|otf|eot|svg)(\?v=[a-z0-9]\.[a-z0-9]\.[a-z0-9])?$/,
use: [{
loader: 'file-loader',
options: {
outputPath: 'fonts',
publicPath: 'fonts',
},
}],
},

Configure Webpack Loaders For a Shared Folder?

Help appreciated!
My question is how do I get webpack to compile assets from a shared folder into the appropriate content script?
I have a Chrome Extension using react-redux-chrome. Their file structure is:
-build
-content
-event
-popup
package.json
gulpfile.babel.js
manifest.json
The gulp file watches for changes and compiles the appropriate javascript, manifest, and html files into build.
Content, event, and popup all have their own webpack configs.
// content/webpack.config.js
const path = require('path');
module.exports = {
entry: [
'./content/src/scripts/index.js'
],
output: {
filename: 'content.js',
path: path.join(__dirname, '../', 'build'),
publicPath: '/'
},
resolve: {
extensions: ['', '.js', '.jsx', '.scss', '.json'],
modulesDirectories: ['node_modules'],
},
module: {
loaders: [
{
test: /\.(jsx|js)?$/,
loader: 'babel',
exclude: /(node_modules)/,
include: path.join(__dirname, 'src'),
query: {
presets: ['es2015', 'react']
}
},
{ test: /\.css$/, loader: "style-loader!css-loader" },
]
}
};
My extension has two content scripts, so my folders look like this...
-build
-content1
-content2
-shared
-event
-popup
package.json
gulpfile.babel.js
manifest.json
Everything works fine until I try to import a React module from the shared folder. The shared folder does not have a webpack config. I get the error: Unexpected token. You may need an appropriate loader to handle this file type.
How do I get webpack to compile assets from the shared folder into the appropriate content script? I tried an alias, but it did not work.
alias: {
shared: path.normalize('../../../../../../shared')
}
I had a brain fart... just needed to make a new loader for the directory. In the content1/webpack.config.js and content2/webpack.config.js files...
{
test: /\.(jsx|js)?$/,
loader: 'babel',
exclude: /(node_modules)/,
include: path.join(__dirname, '../shared'),
query: {
presets: ['es2015', 'react']
}
},

WebPack-Dev-Server error: require is not defined

Webpack itself is working fine, but the webpack-dev-server is not. Basically, webpack created 2 build files for me, a back-end bundle and a front-end bundle. So, I have a webpack-config.js for each of these. I want to develop my front-end code with webpack-dev-server, as you can see from my webpack-config file for my front-end-bundle.js below. When I run web-pack-dev server, it is able to find and build my front-end.js and index.html, but nothing renders in the console and it gives me a "Uncaught ReferenceError: require is not defined"
// var nodeExternals = require('webpack-node-externals');
var webpack = require('webpack');
module.exports = {
entry: './browser/entry.js',
output: {
path: './builds',
filename: 'frontend.js'
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"development"'
}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': '"development"'
}
})
],
module: {
loaders: [
{
test: [/\.es6$/, /\.js$/, /\.jsx$/],
exclude: 'node_modules',
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-1']
}
},
{
test: /\.json$/,
loader: 'json-loader'
},
{
test: /\.html$/,
loader: 'html-loader'
},
]
},
resolve: {
extensions: ['', '.js', '.es6', '.json'],
root: '/Users/johnhenry/Desktop/GAMR/gamr/browser'
},
devServer: {
contentBase: 'builds/dev-build'
},
target: 'node',
// externals: [nodeExternals()]
}
The error is triggered by this in my front-end build (it is only in the dev server build, not in the non-dev-server webpack build):
function(module, exports) {
module.exports = require("url");
If anyone has insight into this, it would be much appreciated
Try adding:
target: 'web'
to your module block.
I had the same error and if anyone is still struggling with this, this solution also helped me:
... There are 2 ways to solve the issue:
1. (Recommended) Don't activate webpack-node-externals in your Webpack browser config, but let Webpack indeed bundle those modules
when targeting the web (this is probable what you want to do)
Have the external modules loaded in some other way in the browser, and add the appropriate importType flag to the webpack-node-externals configuration (either var for scripts or amd for AMD)
more details here: https://github.com/liady/webpack-node-externals/issues/17#issuecomment-284222729
I hit this issue when a webpack.config.js from a node app for the base of a react app.
I had the following:
target: 'web'
but still ran in to the same issue.
Removing reference to webpack-node-externals solved it, which does make sense when you think about what node-externals is actually doing.
I had below rule in my webpack.config.js
rules: [
{
test: /\.js$/,
use:['script-loader']
}
]
Removing above rule from webpack.config.js removed the error.
Hope this helps.

React not loading from babel.rc file

I am receiving a "module parse failed" error when trying to load my react app with Babel6 Stage-1. We initially ran browserify but I am now trying to port us completely to Babel6.
babel.rc file
{
"presets": ["stage-1", "react"],
"env": {
"development": {
"presets": ["react-hmre"]
}
}}
webpack config
var path = require('path');
var webpack = require('webpack');
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'eventsource-polyfill', // necessary for hot reloading with IE
'webpack-hot-middleware/client',
'./src/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [{
test: /\.jsx?/,
loaders: ['babel'],
include: path.join(__dirname, 'src'),
},
{
test: /\.css$/,
exclude: /node_modules/,
loader: 'style!css'
}]
}
};
I have tried reading the documentation on Babel6 and it seems like I need to include react in my presets along with Stage-1.
I have done both and npm installed:
babel preset stage 1, babel preset react, and babel preset react hmre
Any ideas on how I can get react loading again?
Fixed by adding es2015 to the babel.rc file

Resources