I am learning React now and I come across the concept of "React CSS Modules". I like the idea of CSS scoping and I want to use it in my code. I found babel-plugin-react-css-modules this plugin to make codes look cleaner. However, I have some problems setting it up in my codes.
Here is my .babelrc file
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": [
[
"babel-plugin-react-css-modules",
{
"webpackHotModuleReloading": true,
"autoResolveMultipleImports": true
}
]
]
}
My webpack.config.js:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.join(__dirname, "/dist"),
filename: "index_bundle.js",
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: ["#babel/preset-env"],
cacheDirectory: true,
},
},
},
{
test: /\.css$/i,
use: [
"style-loader",
{
loader: "css-loader", //generating unique classname
options: {
importLoaders: 1, // if specifying more loaders
modules: true,
sourceMap: false,
localIdentName:
"[path]___[name]__[local]___[hash:base64:5]", //babel-plugin-css-module format
//localIdentName: "[path][name]__[local]" //recommended settings by cssloader#local-scope , this option generate unique classname for compiled css
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html",
}),
],
};
And my package.json:
{
"name": "react-webpack",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"start": "npx webpack server --mode development --open --hot",
"build": "webpack --mode production"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-plugin-react-css-modules": "^5.2.6",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"#babel/core": "^7.14.0",
"#babel/preset-env": "^7.14.1",
"#babel/preset-react": "^7.13.13",
"babel-loader": "^8.2.2",
"css-loader": "^5.2.4",
"html-webpack-plugin": "^5.3.1",
"postcss-scss": "^3.0.5",
"style-loader": "^2.0.0",
"webpack": "^5.36.2",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.11.2"
}
}
If I do npm start, I get the error:
modules by path ./src/css/*.css 1.42 KiB
./src/css/styles.css 1.38 KiB [built] [code generated]
./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].use[1]!./src/css/styles.css 39 bytes [built] [code generated] [1 error]
./src/index.js 194 bytes [built] [code generated]
./src/components/App.js 388 bytes [built] [code generated]
ERROR in ./src/css/styles.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].use[1]!./src/css/styles.css)
Module build failed (from ./node_modules/css-loader/dist/cjs.js):
ValidationError: Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options has an unknown property 'localIdentName'. These properties are valid:
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }
For more details, I have my code here. I appreciate your help!
BTY, I read the possible solutions here, but no luck.
babel-plugin-react-css-modules is heavily outdated and not maintained;
however there is its up-to-date fork #dr.pogodin/babel-plugin-react-css-modules maintained by me :)
Related
I have a website that I've had up for maybe a couple of years now, and I think it's starting to get outdated. I haven't touched it in a while, so I'm not exactly sure what is going on with it. I'm not exactly sure what the issue is, but it might have to do with my webpack version, or maybe my react version, I'm not really sure.
The issue started because I was trying to run webpack --mode production because I wanted to remake the bundle.js file, and when I would run this command, the file wouldn't create the bundle.js. Eventually I somehow came to the conclusion that there was something wrong with my webpack version, and so I updated it to the most recent version that was compatible with my dependencies which was version 4.46.0. I tried the command again, and this time it gives me this error:
[0] ./src/index.js 666 bytes {0} [built] [failed] [1 error]
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unexpected token (6:4)
4 |
5 | ReactDOM.render(
> 6 | <App />,
| ^
7 | document.getElementById("root")
8 | );
I tried reinstalling different versions of babel-loader, but the least bad error seems to be with the version 7.1.5. I also tried deleting my node-modules and then running npm install. Here is my package.json:
{
"name": "mywebsite",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --mode production",
"set-dev-server": "webpack-dev-server --open --mode development",
"start": "npm run build && npm run set-dev-server",
"deploy": "gh-pages -d dist",
"publish-demo": "npm run build && npm run deploy"
},
"repository": {
"type": "git",
"url": "git+my/github/link"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "my/github/link/issues"
},
"homepage": "my/github/link",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"file-loader": "^1.1.11",
"gh-pages": "^1.2.0",
"html-webpack-plugin": "^3.2.0",
"image-webpack-loader": "^7.0.1",
"jquery": "^3.5.1",
"node-css": "^0.1.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"sass-loader": "^7.3.1",
"style-loader": "^0.21.0",
"styled-components": "^5.2.1",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"#babel/core": "^7.13.8",
"babel-loader": "^7.1.5",
"css-loader": "^5.1.1",
"node-sass": "^5.0.0",
"schema-utils": "^3.0.0",
"webpack": "^4.46.0"
}
}
From what I found online the issue could also have to do with the webpack.config.js, but all of the issues I could find were from really old versions of webpack (2 or 3?), but anyways here is what I have in that file:
const path = require("path");
const webpack = require("webpack");
const bundlePath = path.resolve(__dirname, "dist/");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: path.join(__dirname, "dist/index.html"),
filename: "index.html"
});
module.exports = {
entry: path.join(__dirname, "src/index.js"),
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: { presets: ['env'] }
},
{
test: /\.scss$/,
use: [ 'style-loader', 'css-loader', 'sass-loader' ]
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true, // webpack#1.x
disable: true, // webpack#2.x and newer
},
},
],
}
]
},
resolve: { extensions: ['*', '.js', '.jsx'] },
output: {
publicPath: bundlePath,
filename: "bundle.js"
},
devServer: {
contentBase: path.join(__dirname,'dist/'),
port: 3000,
publicPath: "http://localhost:3000/"
},
plugins: [ new webpack.HotModuleReplacementPlugin(), htmlWebpackPlugin ]
};
Looks like you are using a jsx token <> while your file is named index.js. Try naming it as index.jsx
I'm new to React, webpack, Babel.... all of it. Trying to setup a vanilla project where I want to utilize Salesforce Lightning Design System for React. npm run start shows the following error:
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL] /Users/brandor/test/react_tut/src/index.js: Cannot find module 'babel-preset-env'
Require stack:
- /Users/brandor/test/react_tut/node_modules/#salesforce/babel-preset-design-system-react/index.js
I'm not sure why it's stating that 'babel-preset-env' is required. Shouldn't that have been installed when I installed the slds modules with the following?
$ npm install #salesforce-ux/design-system #salesforce/design-system-react
If I do install the module (along with one other it complains about) then I get something that seems to be related to this issue, but I'm using what looks like the correct presets in my babel.config.js file.
The other error is:
TypeError: /Users/brandor/test/react_tut/src/components/App.js: Cannot read property 'bindings' of null which
Not sure if this is an issue with webpack config, babel, or incorrect version of something.
package.json
{
"name": "react_tut",
"version": "1.0.0",
"description": "Boilerplate stuff",
"main": "index.js",
"scripts": {
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development --open --hot"
},
"license": "MIT",
"dependencies": {
"#salesforce-ux/design-system": "^2.11.6",
"#salesforce/design-system-react": "^0.10.18",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-export-extensions": "^6.22.0",
"core-js": "^3.6.4",
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.4",
"#babel/preset-env": "^7.8.4",
"#babel/preset-flow": "^7.8.3",
"#babel/preset-react": "^7.8.3",
"babel-loader": "^8.0.6",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"css-loader": "^3.4.2",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.9.0",
"url-loader": "^4.0.0",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
}
}
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './src/index.js',
resolve: {
modules: ['src', 'node_modules'],
extensions: ['.js', '.jsx']
},
output: {
path: path.join(__dirname, '/dist'),
filename: 'index_bundle.js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
include: [
path.join(__dirname, 'src/js'),
path.join(__dirname, 'node_modules/#salesforce/design-system-react'),
],
exclude: [
path.join(
__dirname,
'node_modules/#salesforce/design-system-react/node_modules',
),
],
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
},
],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
},
{
test: /\.(svg|gif|jpe?g|png)$/,
loader: 'url-loader?limit=10000'
},
{
test: /\.(eot|woff|woff2|ttf)$/,
loader: 'url-loader?limit=30&name=assets/fonts/webfonts/[name].[ext]'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src', 'index.html'),
}),
],
};
babel.config.js
'use strict';
const presets = ['#babel/preset-react', '#babel/preset-flow', '#salesforce/babel-preset-design-system-react'];
module.exports = {
presets: [
...presets,
[
'#babel/preset-env',
{
modules: false,
corejs: '3',
useBuiltIns: 'usage',
},
],
],
env: {
test: {
presets: [
...presets,
[
'#babel/preset-env',
{
targets: { node: 'current' },
corejs: '3',
useBuiltIns: 'usage',
},
],
],
},
},
};
Trying to set up a react web app but keep running into this error after trying to implement semantic-UI by putting import "semantic-ui-css/semantic.min.css"; in index.jsx.
I npm installed css-loader and style-loader as thats the error I was getting prior to this new error.
My guess is a webpack.config needs to be adjusted to handle the loaders differently but I'm unsure on how to proceed with this.
ERROR in ./node_modules/semantic-ui-css/themes/default/assets/fonts/outline-icons.woff
Module parse failed: C:\Users\Shawn\Documents\GitHub\Galavue\Galavue\node_modules\semantic-ui-css\themes\default\assets\fonts\outline-icons.woff Unexpected character ' ' (1:4)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
# ./node_modules/css-loader/dist/cjs.js!./node_modules/semantic-ui-css/semantic.min.css 15:42-101
# ./node_modules/semantic-ui-css/semantic.min.css
# ./react-client/src/index.jsx
Package.json
{
"name": "galavue",
"version": "0.0.0",
"description": "Galavue",
"main": "server.js",
"author": "Shawn",
"scripts": {
"dev": "webpack -d --watch",
"start": "node ./server/index.js",
"build": "webpack -p",
"react-dev": "webpack -d --watch",
"server-dev": "nodemon server/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/www.github.com/shawnSFU.git"
},
"license": "ISC",
"bugs": {
"url": "https://github.com/www.github.com/shawnSFU/issues"
},
"homepage": "https://github.com/www.github.com/shawnSFU#readme",
"dependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"body-parser": "^1.17.2",
"express": "^4.15.3",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router": "^4.1.2",
"react-router-dom": "^4.1.2",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^0.85.0",
"webpack": "^3.4.1"
},
"devDependencies": {
"css-loader": "^2.1.0",
"style-loader": "^0.23.1"
}
}
webpack.config.js
//defines the entry and output points for our application
const path = require('path');
const SRC_DIR = path.join(__dirname, '/react-client/src');
const DIST_DIR = path.join(__dirname, '/react-client/dist');
const webpack = require('webpack');
module.exports = {
entry: `${SRC_DIR}/index.jsx`,
output: {
path: DIST_DIR,
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.css'],
},
module: {
rules: [
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.png$/,
loader: 'url-loader?limit=100000&minetype=image/png'
},
{
test: /\.jpg/,
loader: 'file-loader'
},
{
test: /\.jsx?/,
include: SRC_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
]
};
There are a few things you need to do. Ensure that you have use ~ for CSS imports from node_modules like:
import '~semantic-ui-css/semantic.min.css';
Second, this CSS file semantic.min.css also references *.woff files. I believe it is used for referencing external font files. You would need either url-loader or file-loader to handle those types of files. Sample loader configuration for url-loader would look like:
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
options: {
limit: 10000,
},
}
Further documentation:
url-loader
file-loader
I have been trying to setup React in my .Net Core project and I am getting build failed errors when trying to run WebPack.
I have tried updating ts-loader and webpack versions and trying different solutions from GitHub and StackOverflow but these didn't work.
Webpack console error
Entrypoint global = home-bundle.js home-bundle.js.map
[0] ./ClientApp/Home/home.tsx 289 bytes {0} [built] [failed] [1 error]
WARNING in configuration
The 'mode' option has not been set.
Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
ERROR in ./ClientApp/Home/home.tsx
Module build failed: TypeError: Cannot read property 'ts' of undefined
at getLoaderOptions (D:\MyFirstProject\Travel\node_modules\ts-loader\dist\index.js:70:44)
at Object.loader (D:\MyFirstProject\Travel\node_modules\ts-loader\dist\index.js:23:19)
webpack.config
module.exports = [
{
entry: {
global: "./ClientApp/Home/home.tsx"
},
output: {
filename: "home-bundle.js",
path: __dirname + "/wwwroot/app/",
sourceMapFilename: "home-bundle.js.map"
},
devtool: "source-map",
resolve: {
extensions: [".ts", ".tsx", ".js"],
modules: ["node_modules"]
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader"
},
{
enforce: "pre",
test: /\.tsx?$/,
use: "source-map-loader",
exclude: /node_modules/
}
]
}
}
];
package.json
{
"dependencies": {
"#types/es6-promise": "0.0.33",
"#types/lodash": "4.14.84",
"#types/react": "15.0.11",
"#types/react-dom": "0.14.23",
"es6-promise": "4.1.1",
"lodash": "4.17.4",
"react": "15.6.2",
"react-dom": "15.6.2",
"source-map-loader": "0.2.3",
"ts-loader": "2.3.7",
"typescript": "2.3.4",
"webpack": "3.8.1"
},
"devDependencies": {
"ts-loader": "2.3.7",
"url-loader": "0.5.9",
"webpack-livereload-plugin": "1.0.0"
},
"scripts": {
"webpack": "webpack -p",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack -w"
}
}
I just got same error when upgrading from webpack 3 to 4.
Solution for me was to upgrade ts-loader from 3.1.1 to 4.4.2
Hope it helps.
Fix and solution:
Thanks to #BoyWithSilver, long story short, here are what I did to fix my problems:
added .less into extensions section
removed unnecessary codes in webpack.config.json e.g. removed the extract-text-webpack-plugin
downgrade less from 3.0.1 to 2.7.0 fixed heaps of errors: length of undefined in index.less file in antd.
Update 3
Here is my package.json
{
"name": "mobx-reactjsx",
"version": "1.0.0",
"description": dfsdfsdfsd",
"scripts": {
"start": "webpack-dev-server --config webpack.config.js --progress --inline",
"lint": "eslint src"
},
"keywords": [
"react",
"reactjs",
"boilerplate",
"mobx",
"starter-kit",
"firebase",
"re-base"
],
"author": "Winston Fan",
"license": "MIT",
"homepage": "https://horizontalvision.azurewebsites.net/",
"devDependencies": {
"autoprefixer": "^8.0.0",
"babel-core": "^6.9.1",
"babel-loader": "^7.1.2",
"babel-plugin-import": "^1.6.4",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"css-loader": "^0.28.9",
"extract-text-webpack-plugin": "^3.0.2",
"less": "^3.0.1",
"less-loader": "^4.0.5",
"less-vars-to-js": "^1.2.1",
"postcss-loader": "^2.1.0",
"style-loader": "^0.20.2",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.7.1"
},
"dependencies": {
"antd": "^3.2.1",
"firebase": "^4.9.1",
"mobx": "^3.5.1",
"mobx-react": "^4.4.1",
"mobx-react-devtools": "^4.2.15",
"re-base": "^3.2.2",
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
}
Update 2:
After removed the context option :
// context: path.resolve(__dirname, 'dist'),
in webpage.config.js
added
javascriptEnabled: true
under less-loader in webpack.config.json
I received these errors.
ERROR in ./node_modules/antd/lib/button/style/index.less Module build
failed:
/* stylelint-disable
declaration-bang-space-before,no-duplicate-selectors */ ^ Cannot read
property 'length' of undefined
in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
(line 1, column -1) # ./node_modules/antd/lib/button/style/index.js
5:0-23 # ./src/index.js # multi
(webpack)-dev-server/client?http://localhost:8080 ./src/index.js
ERROR in ./node_modules/antd/lib/style/index.less Module build failed:
/* stylelint-disable
declaration-bang-space-before,no-duplicate-selectors */ ^ Cannot read
property 'length' of undefined
in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
(line 1, column -1) # ./node_modules/antd/lib/button/style/index.js
3:0-33 # ./src/index.js # multi
(webpack)-dev-server/client?http://localhost:8080 ./src/index.js
ERROR in ./node_modules/antd/lib/style/index.less Module build failed:
ModuleBuildError: Module build failed:
/* stylelint-disable
declaration-bang-space-before,no-duplicate-selectors */ ^ Cannot read
property 'length' of undefined
in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
(line 1, column -1)
at runLoaders (F:\temp\Playground\mobx-reactjsx\node_modules\webpack\lib\NormalModule.js:195:19)
at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:364:11
at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:230:18
at context.callback (F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:111:13)
at
at process._tickCallback (internal/process/next_tick.js:169:7) # ./node_modules/antd/lib/style/index.less #
./node_modules/antd/lib/button/style/index.js # ./src/index.js #
multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js
ERROR in ./node_modules/antd/lib/button/style/index.less Module build
failed: ModuleBuildError: Module build failed:
/* stylelint-disable
declaration-bang-space-before,no-duplicate-selectors */ ^ Cannot read
property 'length' of undefined
in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
(line 1, column -1)
at runLoaders (F:\temp\Playground\mobx-reactjsx\node_modules\webpack\lib\NormalModule.js:195:19)
at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:364:11
at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:230:18
at context.callback (F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:111:13)
at
at process._tickCallback (internal/process/next_tick.js:169:7) # ./node_modules/antd/lib/button/style/index.less #
./node_modules/antd/lib/button/style/index.js # ./src/index.js #
multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js
Child extract-text-webpack-plugin
node_modules/extract-text-webpack-plugin/dist
node_modules/css-loader/index.js!node_modules/less-loader/dist/cjs.js??ref--1-2!node_modules/antd/lib/style/index.less:
[0] ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js?{"modifyVars":{"#primary-color":"#193D71"},"root":"F://temp//Playground//mobx-reactjsx","javascriptEnabled":true}!./node_modules/antd/lib/style/index.less
302 bytes {0} [built] [failed] [1 error]
ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js?{"modifyVars":{"#primary-color":"#193D71"},"root":"F://temp//Playground//mobx-reactjsx","javascriptEnabled":true}!./node_modules/antd/lib/style/index.less
Module build failed:
/* stylelint-disable declaration-bang-space-before,no-duplicate-selectors */
^
Cannot read property 'length' of undefined
in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
(line 1, column -1) Child extract-text-webpack-plugin
node_modules/extract-text-webpack-plugin/dist
node_modules/css-loader/index.js!node_modules/less-loader/dist/cjs.js??ref--1-2!node_modules/antd/lib/button/style/index.less:
[0] ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js?{"modifyVars":{"#primary-color":"#193D71"},"root":"F://temp//Playground//mobx-reactjsx","javascriptEnabled":true}!./node_modules/antd/lib/button/style/index.less
302 bytes {0} [built] [failed] [1 error]
ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js?{"modifyVars":{"#primary-color":"#193D71"},"root":"F://temp//Playground//mobx-reactjsx","javascriptEnabled":true}!./node_modules/antd/lib/button/style/index.less
Module build failed:
/* stylelint-disable declaration-bang-space-before,no-duplicate-selectors */
^
Cannot read property 'length' of undefined
in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
(line 1, column -1) webpack: Failed to compile.
Update 1:
Here is my webpack.config.json
var path = require('path');
var webpack = require('webpack');
const fs = require('fs');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './scripts/ant-theme-vars.less'), 'utf8'));
module.exports = {
watch: true,
context: path.resolve(__dirname, './src'),
devtool: 'source-map',
entry: [
'./src/index.js'
],
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name].bundle.js'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin('style.css')
],
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [{
test: /\.jsx?$/,
use: ['babel-loader'],
include: path.join(__dirname, 'src')
},
{
test: /\.less$/,
use: ExtractTextPlugin.extract([
{loader: "css-loader"},
{loader: "less-loader",
options: {
modifyVars: themeVariables,
root: path.resolve(__dirname, './')
}
}
])
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: "css-loader"
})
},]
}
};
now error message becomes:
ERROR in multi (webpack)-dev-server/client?http://localhost:8080
./src/index.js Module not found: Error: Can't resolve './src/index.js'
in '/Users/Winston/tmp/Playground/mobx-reactjsx/src' # multi
(webpack)-dev-server/client?http://localhost:8080 ./src/index.js
(project structure)
I do have index.js under projectfolder/src
I'm trying to integrate Ant Design with Less support and the Import on Demand feature which is here:
https://ant.design/docs/react/use-with-create-react-app
in Advanced Guide section.
In this section, it uses react-app-rewired but my project uses webpack 3(my project is based on this https://github.com/mobxjs/mobx-react-boilerplate), so cannot use the rewired command in my case.
So I found another article:
https://medium.com/#GeoffMiller/how-to-customize-ant-design-with-react-webpack-the-missing-guide-c6430f2db10f
Here is my code:
package.json
"start": "webpack-dev-server --hot --open",
webpack.config.json
const fs = require('fs');
const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './scripts/ant-theme-vars.less'), 'utf8'));
....
module:{
.....
rules:[
.....
test: /\.less$/,
use: [
{loader: "style-loader"},
{loader: "css-loader"},
{loader: "less-loader",
options: {
modifyVars: themeVariables
}
}
]
]
}
index.js
import {Button} from 'antd';
<Button type="primary">Hi!</Button>
.babelrc
{
"presets": [
"react",
"es2015",
"stage-1"
],
"plugins": [
["import", {"libraryName": "antd", "style": true} ],
"transform-decorators-legacy"
]
}
Here is the error messages from console:
For me i just had to use import 'antd/dist/antd.min.css
instead of import 'antd/dist/antd.css
https://github.com/ant-design/ant-design/issues/33327#issuecomment-997355323
The error seems to be coming from the inability to resolve .less files. Adding this to webpack.config.js should help resolve them:
resolve: {
extensions: ['.js', '.jsx', '.less']
},
You have specified context as src folder in the structure. This means the entry has to be changed to ./index.js or just ./index
If you did not mean to have context key, removing it will also do the work.
Webpack configuration (webpack.config.js):
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
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 = {
watch: true,
context: path.resolve(__dirname, './src'),
entry: {
app: './index.js',
},
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name].bundle.js',
},
devtool: 'source-map',
resolve: {
alias: {
moment: 'moment/src/moment'
},
},
module: {
rules: [
{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['env','react','stage-2']
}
},
{
loader: 'babel-loader',
exclude: /node_modules/,
test: /\.js$/,
options: {
presets: ['env','react','stage-2'],
plugins: [
['import', { libraryName: "antd", style: true }]
]
},
},
{
test: /\.scss?$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
loader: 'css-loader!sass-loader'
})
},
{
test: /\.less$/,
use: ExtractTextPlugin.extract([
{loader: "css-loader"},
{loader: "less-loader",
options: {
modifyVars: themeVariables,
root: path.resolve(__dirname, './')
}
}
])
},
{
test: /\.(woff|woff2|eot|ttf|otf|svg|png|jpg,pdf)$/,
loader: "file-loader"
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
]
},
plugins: [
new ExtractTextPlugin('style.css')
],
devServer: {
port: 9000,
contentBase: 'dist'
}
};
And package.json:
{
"name": "ant-design-app",
"version": "1.0.0",
"description": "Ant design",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config webpack.config.js --progress --inline",
"build": "webpack -p --config webpack.config.js"
},
"repository": {
"type": "git",
"url": ""
},
"author": "Sivadass",
"license": "MIT",
"dependencies": {
"antd": "^3.1.4",
"moment": "^2.20.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-import": "^1.6.3",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^0.28.9",
"extract-text-webpack-plugin": "^2.1.2",
"file-loader": "^0.11.2",
"less": "^2.7.3",
"less-loader": "^4.0.5",
"less-vars-to-js": "^1.2.1",
"node-sass": "^4.7.2",
"postcss-loader": "^2.0.10",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"sugarss": "^1.0.1",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.11.1"
}
}
Ant theme variables configuration (ant-theme-vars.less):
#primary-color: #4099ff;
#font-size-base: 16px;
#font-family : "Proxima Nova", Helvetica Neue, Helvetica, Arial, sans-serif;
#btn-font-size-lg: 15px;
#btn-padding-lg : 8px 32px;
#btn-padding-sm : 4px 16px;
Solution for React :
in index.js folder
import 'antd/dist/antd.css
replace with
import 'antd/dist/antd.min.css
I just added that import statement into my index.scss file, then the warnings were disappeared
#import "antd/dist/antd.css";