Here is the error I receive:
Uncaught Error: Module parse failed: Unexpected token (15:5)
You may need an appropriate loader to handle this file type.
| /* stylelint-disable at-rule-no-unknown */
| html,
> body {
| width: 100%;
| height: 100%;
at eval (antd.css:1)
at Object../node_modules/antd/dist/antd.css (index.js:228)
at __webpack_require__ (index.js:20)
at eval (App.js:10)
at Module../KQShopping/frontend/src/App.js (index.js:97)
at __webpack_require__ (index.js:20)
at eval (index.js:2)
at Module../KQShopping/frontend/src/index.js (index.js:109)
at __webpack_require__ (index.js:20)
at index.js:84
webpack config file:
const path = require('path');
const fs = require('fs');
const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './ant-theme-vars.less'), 'utf8'));
module.exports = {
module: {
rules: [
{
loader: 'babel-loader',
test: /\.(js|jsx)$/,
exclude: /node_modules/,
options: {
plugins: [
['import', { libraryName: "antd", style: true }]
]
},
},
{
test: /\.less$/,
use: [
{loader: "style-loader"},
{loader: "css-loader"},
{loader: "less-loader",
options: {
modifyVars: themeVariables
}
}
]
}
]
}
};
App.js:
import React from "react";
import ReactDOM from "react-dom";
import { DatePicker, message } from "antd";
import "antd/dist/antd.css";
class App extends React.Component {
state = {
date: null,
};
handleChange = date => {
message.info(`Selected Date: ${date ? date.format("YYYY-MM-DD") : "None"}`);
this.setState({ date });
};
render() {
const { date } = this.state;
return (
<div style={{ width: 400, margin: "100px auto" }}>
<DatePicker onChange={this.handleChange} />
<div style={{ marginTop: 20 }}>
Selected Date: {date ? date.format("YYYY-MM-DD") : "None"}
</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"));
Babel config file:
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": ["transform-class-properties", ["import", { "libraryName": "antd", "style": "true" }]]
}
I followed multi tutorials and how-tos but I end up with an error every time and I have no idea how to fix this error since I have just started learning about babel and webpack with small experince.
In this problem I followed exactly this docs https://ant.design/docs/react/getting-started and I still end up with an error
In the end, I used CSS, in the webpack config file add this:
{
use: ['style-loader', 'css-loader'],
test: /\.css$/
}
You shouldn't need to import the antd css at the top of App.js. I also think that the babel plugin should have style set to css in the babel config file (that's how our config file is set up anyway!).
[
'import',
{
'libraryName': 'antd',
'style': 'css'
}
]
Our less loader also has javascriptEnabled set to true:
test: /\.less$/,
use: [{
loader: 'style-loader' // creates style nodes from JS strings
},
{
loader: 'css-loader' // translates CSS into CommonJ
},
{
loader: 'less-loader', // compiles Less to CSS
options: {
javascriptEnabled: true
}
}]
Related
I am getting an html file from an external source and wanting to render it within a modal in react. It looks like it is receiving the file properly but I am getting this error.
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| <script language="javascript" ...
Here is my webpack.config,js file
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isProduction = process.env.NODE_ENV == 'production';
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader';
const config = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
},
plugins: [
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
module: {
rules: [
{
test: /\.(js|jsx)$/i,
loader: 'babel-loader',
},
{
test: /\.css$/i,
use: [stylesHandler,'css-loader'],
},
{
test: /\.s[ac]ss$/i,
use: [stylesHandler, 'css-loader', 'sass-loader'],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset',
},
{
test: /\.html$/i,
loader: "html-loader",
},
// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
],
},
};
module.exports = () => {
if (isProduction) {
config.mode = 'production';
config.plugins.push(new MiniCssExtractPlugin());
} else {
config.mode = 'development';
}
return config;
};
and here is my App.js file where my react code is located.
import './App.css';
import DOMPurify from 'dompurify';
import Modal from 'react-modal';
import htmlFile from './payee-verification-dropin.html'
import { useState } from 'react';
function App() {
const [showModal, setShowModal]= useState(false);
const myHTML = htmlFile;
const mySafeHTML = DOMPurify.sanitize(myHTML);
return (
<div className="App">
<Modal
isOpen={showModal}
onRequestClose={() => setShowModal(false)}
style={{
overlay: {},
content: {
position: "absolute",
top: "10%",
left: "25%",
width: "839px",
height: "81%",
borderRadius: "10px",
backgroundColor: "#FAFAFA",
padding: "0px",
},
}}
>
<div dangerouslySetInnerHTML={{_html: mySafeHTML }} />
<h1>Hello</h1>
</Modal>
<button onClick={() => setShowModal(true)}>CLick</button>
</div>
);
}
export default App;
Lert me know if you need more info to diagnose this problem.
Thank you!
I ran npx webpack init in order to set up webpack and I ran npm install --save-dev html-loader to try and install an html loader.
How to config next.js to support Simultaneous tailwind css with css modules?
I want tailwindcss wrap whole project:
// /tailwind.scss
:global {
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
}
// /test-module.css
.example {
font-size: 36px;
}
// /pages/_app.jsx
import '../talwind.scss';
...
And in a sample component:
// /components/my-component.jsx
import css from '../test-module.css';
const Test = () => (
<div className={`bg-red-500` ${css.example}}>Test Tailwind with CSS</div>
);
A solution is split webpack style loader. A loader for global css another for css modules loader so webpack loader is looks like below:
{
test: /\.s?[ac]ss$/,
exclude: /\.global.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !isProduction,
reloadAll: true,
},
},
// 'css-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: {
localIdentName: '[name]__[local]___[hash:base64:5]',
},
},
},
'postcss-loader',
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
{
test: /\.global.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !isProduction,
reloadAll: true,
},
},
'css-loader',
'postcss-loader',
],
},
I had the same problem, I did the following and it worked:
const Test = () => (
<div className={["bg-red-500", css.example].join(" ")}>
Test Tailwind with CSS
</div>
);
You need to write classname like this
const App = () => {
<h1 className={[style[`bg-teal-600`], style.holi].join(' ')}>Holi</h1>;
};
Recently I decided to build my own setup for react & webpack
So I faced some problems with UI libraries like antd or Bulma when import component like button the style of it doesn't work
this is my index.js
import React from 'react';
import ReactDom from 'react-dom';
import 'antd/dist/antd.css';
import { Button } from 'antd';
import './index.css';
const Index = () => (
<div>
<Button type="primary">Primary</Button>
</div>
)
;
ReactDom.render(<Index/>,document.getElementById('root'));
and this is my output
(I uploaded the image on imgur because I'm not allowed to upload images on StackOverflow until I got 10 reputations)
this is my webpack setting code
const path= require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports={
entry:path.resolve(__dirname,'src/index.js'),
output: {
path: path.resolve(__dirname,'dist'),
filename: "bundle.js"
},
module: {
rules: [
{
test:/\.js$/,
use:"babel-loader",
exclude:/node_modules/,
},
{
test:/\.(css|scss)$/,
use:[
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
camelCase: true,
localIdentName: '[local]___[hash:base64:5]',
},
},
'sass-loader',
]
},
{
test:/\.less/,
use:[
"less-loader",
]
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true,
disable: true
}
}
]
},
{
test:/\.(jpg|png|gif)$/i,
exclude:/node_modules/,
use:{
loader: "url-loader",
options:{
limit:8000
}
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname,'src/index.html')
})
]
};
I ran into a similar issue, here's how I managed to solve it;
Replace your webpack rule for less files with this:
{
test: /\.less$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
{ loader: "less-loader", options: { javascriptEnabled: true } }
]
}
NB: You should have style-loader, css-loader and less-loader installed.
Import antd.less instead of antd.css
In your index.js file, use import "antd/dist/antd.less" instead of import "antd/dist/antd.css" ensure you have less installed too.
Hope this helps!!
Im using webpack and css-loader and style-loader to enable css modules in my React app. These are the following setup:
Webpack config:
module.exports = {
mode: "development",
entry: __dirname + "/app/index.js",
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: true,
localIdentName: "[name]__[local]___[hash:base64:5]"
}
}
]
}
]
},
output: {
filename: "bundle.js",
path: __dirname + "/build"
},
plugins: [HTMLWebpackPluginConfig]
};
And in my React component I've coded this:
import React from "react";
import styles from "./Carousel.css";
class Carousel extends React.Component {
render() {
return (
<div className={styles["carousel"]}>
<img
className={styles["test"]}
src="https://i2.wp.com/beebom.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg?resize=640%2C426"
/>
</div>
);
}
}
export default Carousel;
In my Carousel.css file:
.carousel {
background-color: red;
.test {
width: 200px;
}
}
When I check the rendered HTML, I can see carousel class and its properties coming in the parent div. But the child img tag shows the class name but no property is associated with it.
Any idea what Im doing wrong here?
EDIT:: Sam's suggestions worked and Im summarising the changes that solved it:
Since nesting is a feature of css, we need to use sass or less. And for that I used postcss-loader.
Updated webpack config rules section:
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: true,
localIdentName: "[name]__[local]___[hash:base64:5]"
}
},
*"postcss-loader"*
]
}
Also added a postcss.config.js file like this:
module.exports = {
plugins: [
require("postcss-nested")({
/* ...options */
})
]
};
And added postcss-loader, postcss-nested packages using npm install -D option.
How are you importing the css file ?
You can follow the below way to import too,
In your component,
import ‘styles.css’
In HTML element,
<div className='carousel'>
<div className='test'></div>
</div>
In webpack config,
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
Not sure what I'm doing wrong here, where I console.log(style) it is just a blank object but defined. No errors.
Login.js - component
import React, {Component} from 'react';
import {connect} from 'react-redux';
import PropTypes from 'prop-types';
import CSSModules from 'react-css-modules';
import { Row, Col } from 'antd';
import styles from './Login.css';
import Logo from '../../components/Logo/Logo';
class Login extends Component {
constructor (props) {
super(props);
}
render () {
const {dispatch} = this.props
console.log(styles);
return (
<Row type="flex" justify="space-around" align="middle" className="container">
<Col sm={16} md={16} lg={9}>
<div className={styles.content}>
<h1>Sign In</h1>
<Logo size="large" className="logo" />
</div>
</Col>
</Row>
)
}
}
Login.propTypes = {
data: PropTypes.object,
history: PropTypes.object,
dispatch: PropTypes.func
}
function select (state) {
return {
data: state
}
}
export default connect(select)(CSSModules(Login, styles));
Login.css - nothing special going on here
body {
background-color: #f0f0f0;
}
.container {
width: 100vw;
height: 100vh;
}
.content {
position: relative;
background-color: #ffffff;
padding: 30px 20px 20px 20px;
}
and the webpack.config.js most likely the culprit but I can't seem to figure out the issue
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
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: path.resolve('dist'),
filename: 'index_bundle.js'
},
devServer: {
historyApiFallback: true,
},
mode: "development",
module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
}, {
test: /\.js$/,
exclude: /node_modules/,
use: "babel-loader"
}, {
test: /\.jsx?$/,
exclude: /node_modules/,
use: "babel-loader"
}
]
},
plugins: [
HtmlWebpackPluginConfig,
new ExtractTextPlugin({filename: 'style.css'})
]
}
The app compiles and runs fine just the react-css-module isn't namespacing the styles and the styles aren't being applied to the element.
Its seems you are missing in css-loader:
modules: true to enable CSSModules spec for css-loader.
Check the doc.
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader?modules"
}
I had the same problem .it start to work after I add
modules=true
in the webpack.config.js
as follows.
module:{
rules:
[
{
test:/\.css$/,
loader:'style-loader!css-loader?modules=true'
}
]
}