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
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"
]
}
.less
files. Try addingresolve: {extensions: ['.js', '.jsx', '.less']},
to the webpack.config.js file – Agney