I'm getting crazy about the configuration of my React-Typescript-Webpack application. It is built from scratch - no create-react-app - and should work with the UI library "antd" and "storybook" as well.
After countless hours I now receive following error:
ERROR in ./node_modules/antd/lib/button/style/index.less 1:0
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
... which tells that webpack does not recognize the settings for less in webpack.config.js, right?
I just don't understand why, because I thought that I added everything necessary ... Can you please help and tell me what I'm doing wrong here?
Here is webpack.config.js:
const path = require("path");
const fs = require("fs");
const lessToJs = require("less-vars-to-js");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
mode: "none",
entry: {
app: path.join(__dirname, "src", "index.tsx"),
},
target: "web",
resolve: {
extensions: [".ts", ".tsx", ".js", ".less"],
},
module: {
rules: [
{
loader: "babel-loader",
exclude: /node_modules/,
test: /\.js$/,
options: {
plugins: [["import", { libraryName: "antd", style: true }]],
},
},
{
test: /\.less$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
{loader: "less-loader"},
],
},
{
use: ["style-loader", "css-loader"],
test: /\.css$/,
},
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: "/node_modules/",
},
],
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist"),
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "index.html"),
}),
],
};
And the babel.rc file:
{
"presets": [
"#babel/preset-env",
"#babel/preset-typescript",
"#babel/preset-react"
],
"plugins": [
"#babel/proposal-class-properties",
"#babel/proposal-object-rest-spread",
"#babel/plugin-proposal-class-properties",
["import",
{
"libraryName": "antd",
"style": true // or 'css'
}]
]
}
And the dependencies from package.json:
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-proposal-object-rest-spread": "^7.10.4",
"#babel/preset-env": "^7.10.4",
"#babel/preset-react": "^7.10.4",
"#babel/preset-typescript": "^7.10.4",
"antd": "^4.4.1",
"babel-plugin-import": "^1.13.0",
"css-loader": "^3.6.0",
"less": "2.7.2",
"less-vars-to-js": "^1.3.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"style-loader": "^1.2.1"
},
"devDependencies": {
"#babel/core": "^7.10.4",
"#storybook/addon-actions": "^5.3.19",
"#storybook/addon-links": "^5.3.19",
"#storybook/addons": "^5.3.19",
"#storybook/react": "^5.3.19",
"#types/antd": "latest",
"#types/react": "^16.8.24",
"#types/react-dom": "^16.0.5",
"#types/webpack": "4.1.4",
"babel-loader": "^8.1.0",
"html-webpack-plugin": "^4.3.0",
"less-loader": "^6.2.0",
"ts-loader": "^6.2.1",
"typescript": "^3.4.3",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.11.0"
}
For example, I use antd in App.tsx just like this:
import React, { FC } from 'react';
import { Button } from 'antd';
import './App.css';
import 'antd/dist/antd.css';
const App: FC = () => (
<div className="App">
<Button type="primary">Button</Button>
</div>
);
export default App;
When I run npm run build / npm start, everything works fine. The error appears when running npm run storybook as stated in the storybook docs.
Full error stack:
ERROR in ./node_modules/antd/lib/button/style/index.less 1:0
Module parse failed: Unexpected character '#' (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
> #import '../../style/themes/index';
| #import '../../style/mixins/index';
| #import './mixin';
# ./node_modules/antd/lib/button/style/index.js 5:0-23
# ./stories/2-Button.stories.js
# ./stories sync ^\.\/(?:(?:(?!\.)(?:(?:(?!(?:|[\\/])\.).)*?)[\\/])?(?!\.)(?=.)[^\\/]*?\.stories\.js[\\/]?)$
# ./.storybook/generated-entry.js
# multi ./node_modules/#storybook/core/dist/server/common/polyfills.js ./node_modules/#storybook/core/dist/server/preview/globals.js ./.storybook/generated-entry.js (webpack)-hot-middleware/client.js?reload=true&quiet=true
ERROR in ./node_modules/antd/lib/style/index.less 1:0
Module parse failed: Unexpected character '#' (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
> #import './themes/index';
| #import './core/index';
|
# ./node_modules/antd/lib/button/style/index.js 3:0-33
# ./stories/2-Button.stories.js
# ./stories sync ^\.\/(?:(?:(?!\.)(?:(?:(?!(?:|[\\/])\.).)*?)[\\/])?(?!\.)(?=.)[^\\/]*?\.stories\.js[\\/]?)$
# ./.storybook/generated-entry.js
# multi ./node_modules/#storybook/core/dist/server/common/polyfills.js ./node_modules/#storybook/core/dist/server/preview/globals.js ./.storybook/generated-entry.js (webpack)-hot-middleware/client.js?reload=true&quiet=true
Help will be much appreciated!
Please notice you have two rules for .less,
One executes after the other completes.
Try taking one of them off please.
I had the same problem and here are the steps I did to resolve;
My package.json,
"css-loader": "^3.5.3",
"less": "^3.12.2",
"less-loader": "^7.0.0",
"style-loader": "^1.2.0",
"url-loader": "^4.1.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
My webpack rules config,
{
test: /\.less$/,
use: [
"style-loader",
"css-loader",
{
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
},
Note: Before less 3.11, the javascriptEnabled is directly inside options
Related
i am configuring the webpack 4 in my react project (create-react-app)
i have installed all the require loaders/file to configure my webpack, but there is always some different error, (some time its about sass-loader/css-loader/style-loader)
Now am dealing with (eot,woff,woff2,ttf) files error.
i don't know, i am conflicting the versions of node-sass/webpack/loaders or what.
please help me to configuring webpack in my project.
i am attaching the configuration files and errors screen.
Here is my devdependencies in my package-json file.
"devDependencies": {
"#babel/core": "^7.17.9",
"#babel/preset-env": "^7.16.11",
"#babel/preset-react": "^7.16.7",
"babel-loader": "^8.2.5",
"css-loader": "^3.6.0",
"file-loader": "^6.2.0",
"laravel-echo": "^1.11.2",
"less": "^4.1.2",
"less-loader": "^5.0.0",
"node-sass": "^4.14.1",
"pusher-js": "^7.0.3",
"raw-loader": "^4.0.2",
"react-error-overlay": "6.0.9",
"sass": "^1.51.0",
"sass-loader": "^7.1.0",
"style-loader": "^1.3.0",
"ts-loader": "^9.2.8",
"url-loader": "^4.1.1",
"webpack": "^4.42.0",
"webpack-cli": "^4.9.2"
}
webpack.config.js file
const path = require("path");
module.exports = {
mode: "production",
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
publicPath: "/dist/",
},
// ...add the babel-loader and preset
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
// {
// test: /\.(scss|css|sass)$/,
// use: ["sass-loader", "style-loader", "css-loader"],
// },
{
test: /\.(scss|css|sass)$/,
use: [
// style-loader
{
loader: "style-loader",
},
// css-loader
{
loader: "css-loader",
options: {
modules: true,
},
},
// sass-loader
{
loader: "sass-loader",
options: {
modules: true,
},
},
],
},
{
test: /\.(woff2|woff|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: "file-loader",
// options: {
// name: "[name].[ext]",
// outputPath: "fonts/",
// },
},
],
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: "file-loader",
},
],
},
],
},
// ...add resolve to .jsx extension
resolve: {
extensions: ["*", ".js", ".jsx"],
},
};
.babelrc
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
Kindly check your themes scss and comment imports one by one and try to make build. In my case scss files were causing issues.
Im tring to compile my react frontend app but I got a couple of error about "..." syntax:
ERROR in condition.jsx
Module build failed: SyntaxError: Unexpected token (25:10)
23 | show_table : undefined,
24 | fa_count : 0,
> 25 | ...this.state
| ^
26 | }
condition.jsx extends (with OOP) another component so I need ...this.state to merge parent state with the local state.
When launcing it with npm start, it works perfectly but the compiler seems doesn't want that syntax.
UPDATED:
This is my current webpack settings:
const webpack = require('webpack');
const path = require('path');
const Uglify = require("uglifyjs-webpack-plugin");
var plugins = [];
plugins.push(new Uglify());
var config = {
context: __dirname + '/src',
entry: {
javascript: "./index.js",
},
plugins: plugins,
output: {
path: path.join(__dirname,'../static/js/'),
filename: 'bundle.js',
},
devServer: {
inline: true,
port: 8080
},
resolveLoader: {
modules: [path.join(__dirname, 'node_modules')]
},
module: {
loaders: [
{
test:/\.(js|jsx)?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['env','react']
}
},
{
test: /\.css$/,
loader: 'style-loader'
},
{
test: /\.css$/,
loader: 'css-loader',
query: {
modules: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
},
{
test: /\.svg$/,
use: [
{
loader: "babel-loader"
},
{
loader: "react-svg-loader",
options: {
jsx: true // true outputs JSX tags
}
}
]
}
]
},
}
module.exports = env => {
return config;
}
Lanching with this command:
./node_modules/.bin/webpack --config webpack.config.js
In a comment, you said that you don't have any babelrc. Then I already re-read the Webpack offical document and take this sample code from there for you:
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env'],
plugins: [require('#babel/plugin-proposal-object-rest-spread')]
}
}
}
]
after installing the babel-plugin-transform-object-rest-spread package, you could follow this sample code to update your webpack config. Read more about it: Webpack Loader
This is the combination that works for me, I'm using the babel-preset-stage-2 instead.
In the webpack.config.js:
....
module: {
rules: [
{
test:/\.(js|jsx)?$/,
use: ['babel-loader']
},
....
]
}
....
I create a .babelrc file in the root folder and its content is:
{
"presets": ["env", "react", "stage-2"],
....
}
And this is my package.json file:
{
"name": "demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --open"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"clean-webpack-plugin": "^0.1.19",
"css-loader": "^0.28.11",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.9.0",
"react-hot-loader": "^4.3.3",
"sass-loader": "^7.0.3",
"style-loader": "^0.21.0",
"webpack": "^4.15.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
},
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"uuid": "^3.3.2"
}
}
Hopefully, it works for you.
You didn't tell about your configuration. But I assume babel and webpack. This seems to be an issue with your babel config. Try this plugin:
https://www.npmjs.com/package/babel-plugin-transform-object-rest-spread
After you've installed it, add
"plugins": ["transform-object-rest-spread"]
To your .babelrc file and run the webpack again.
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";
I get the following error when running webpack:
Error in ./src/index.jsx
Module parse failed: .../src/index.jsx Unexpected token (10:18)
You may need an appropriate loader to handle this file type.
I believe I have all the correct loaders set up, please tell me if I missed something.
package.json:
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"koa": "^1.2.4",
"koa-ejs": "^3.0.0",
"koa-route": "^2.4.2",
"koa-router": "^5.4.0",
"koa-static": "^2.0.0",
"koa-webpack-dev-middleware": "^1.2.2",
"node-sass": "^3.10.1",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"react-hot-loader": "^1.3.0",
"react-redux": "^4.4.5",
"react-router": "^2.8.1",
"redux": "^3.6.0",
"redux-thunk": "^2.1.0",
"sass-loader": "^4.0.2",
"style-loader": "^0.13.1",
"webpack": "^1.13.2"
},
"devDependencies": {
"babel-core": "^6.17.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.2.6",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"babel-preset-react-hmre": "^1.1.1",
"css-loader": "^0.25.0",
"eslint": "^3.7.1",
"eslint-loader": "^1.5.0",
"eslint-plugin-react": "^6.4.1",
"koa-webpack-hot-middleware": "^1.0.3",
"react-hot-loader": "^1.3.0",
"redux-devtools": "^3.3.1",
"webpack-dev-server": "^1.16.2"
}
webpack.config.js:
const webpack = require('webpack');
const path = require('path');
const ROOT_PATH = path.resolve(__dirname);
module.exports = {
devtool: process.env.NODE_ENV === 'production' ? '' : 'source-map',
entry: [
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
path.resolve(ROOT_PATH, 'src/index.jsx')
],
module: {
preLoaders: [{
test: /\.jsx?$/,
loaders: process.env.NODE_ENV === 'production' ? [] : ['eslint'],
include: path.resolve(ROOT_PATH, 'src/index.jsx')
}],
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'stage-0', 'react'],
cacheDirectory: true,
env: {
development: {
presets: ['react-hmre']
}
}
},
include: './src'
},
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
path: process.env.NODE_ENV === 'production'
? path.resolve(ROOT_PATH, 'dist')
: path.resolve(ROOT_PATH, 'build'),
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: path.resolve(ROOT_PATH, 'build'),
historyApiFallback: true,
hot: true,
inline: true,
progress: true
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
};
src/index.jsx:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
const main = () => {
const app = document.createElement('div');
document.body.appendChild(app);
ReactDOM.render(<App />, app);
};
main();
I have also tried without any presets using a .babelrc file:
{
"presets": ["es2015", "stage-0", "react"]
}
Running babel src/index.jsx does work as expected, so I think this is an issue with the webpack configuration.
My include path for the js(x) loader was wrong. Fixed using
include: path.resolve(ROOT_PATH, './src')
instead of
include: './src'
I have a bootstrap theme that came with JS and CSS files that I'd like to integrate into my react app. I'm running into issues requiring the JS files because they don't export modules appropriately or properly define variables (using babel loader). I'd like to be able to require the JS in my app but not run them through babel. I'd also like to be able to use webpack's chunking and minification on these files if possible.
How do I go about setting this up?
Edit
I'm pretty sure what I need in reference to babel is the exclude config parameter. Unfortunately no matter what I try the exclude config isn't honored.
{
test: /\.js/,
loaders: [ 'react-hot', 'babel' ],
include: [
path.join(__dirname, 'src', 'app')
],
exclude: [
path.join(__dirname, 'src', 'semantic-v1.1.2')
]
},
Here's the error I receive:
ERROR in ./src/semantic-v1.1.2/assets/js/imagesloaded.pkgd.js
Module not found: Error: Cannot resolve module 'eventEmitter' in /Users/bradr/Dropbox (Personal)/Development/ritasfoods-com/src/semantic-v1.1.2/assets/js
# ./src/semantic-v1.1.2/assets/js/imagesloaded.pkgd.js 731:2-735:24
I'm sure I'm missing something simple.
Full webpack.config.js:
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: 'eval',
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/app/index'
],
output: {
path: path.join(__dirname, 'static'),
filename: 'bundle.js',
publicPath: '/static/'
},
module: {
noParse: [
/aws\-sdk/, // Hack to be able to import aws sdk.
],
loaders: [
{
test: /\.js/,
loaders: [ 'react-hot', 'babel' ],
include: path.join(__dirname, 'src/app'),
exclude: path.resolve('src', 'semantic-v1.1.2')
},
{ test: /\.css$/, loader: "style-loader!css-loader" },
{ test: /\.md$/, loader: "html!markdown?gfm=false" },
{ test: /\.html/, loader: 'html' },
{ test: /\.yaml/, loader: 'json!yaml' },
{ test: /\.(woff|woff2)$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
{ test: /\.ttf$/, loader: "file-loader" },
{ test: /\.eot$/, loader: "file-loader" },
{ test: /\.svg$/, loader: "file-loader" },
{ test: /\.(jpg|png)$/, loader: 'url' }
],
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
],
resolve: {
// Removes the need to specify file type in imports.
extensions: ['', '.js', '.json'],
alias:{
theme: path.resolve( __dirname, 'src', 'semantic-v1.1.2', 'assets')
}
}
};
.babelrc
{
"presets": ["es2015", "stage-0", "react"],
}
package.json
{
"name": "ritasfoods-com",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"start": "node server.js",
"build-prod": "./node_modules/webpack/bin/webpack.js -p --config webpack.config.prod.js --progress --colors",
"deploy-prod": "ops/deploy-prod"
},
"repository": {
"type": "git",
"url": "..."
},
"author": "Brad Reynolds",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.6.0",
"babel-loader": "^6.2.4",
"babel-plugin-transform-class-properties": "^6.6.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"html-loader": "^0.4.3",
"node-sass": "^3.4.2",
"react-hot-loader": "^1.3.0",
"redux-devtools": "^3.2.0",
"redux-devtools-dock-monitor": "^1.1.1",
"redux-devtools-log-monitor": "^1.0.11",
"sass-loader": "^3.1.2",
"style-loader": "^0.13.1",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1"
},
"dependencies": {
"aws-sdk": "^2.2.40",
"babel-polyfill": "^6.8.0",
"babel-preset-stage-0": "^6.5.0",
"es6-promise": "^3.1.2",
"events": "^1.1.0",
"exports-loader": "^0.6.3",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.8.5",
"html-loader": "^0.4.3",
"imports-loader": "^0.6.5",
"invariant": "^2.2.1",
"jquery": "^2.2.1",
"less": "^2.6.1",
"less-loader": "^2.2.3",
"markdown-loader": "^0.1.7",
"mustache": "^2.2.1",
"numeral": "^1.5.3",
"pluralize": "^1.2.1",
"react": "15.0.1",
"react-dom": "15.0.1",
"react-redux": "^4.4.5",
"react-router": "^2.0.0",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.0.1",
"url-loader": "^0.5.7"
}
}
server.js
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config.js');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(8080, 'localhost', function (err, result) {
if (err) {
return console.log(err);
}
console.log('Listening at http://localhost:8080/');
});
I bit the bullet and installed every JS file from the bootstrap theme via npm. It was a PITA as I had to configure a few via webpack. But it's done so I can move on.
I do this for image assets for a chrome extension. The app itself doesn't use them, but Chrome does, so I need them copied into the build folder.
Create an extra entry for static files. Your entry is a js file that just requires all the static files. You will end up with a useless extra bundle as a byproduct, but all your files will be copied over to your build folder.
I'll show you the example of images first because I have working code to pull that from.
webpackconfig.js
// ...
entry: {
// ...
'img': './img/index.js'
},
output: {
// dynamic bundle name for multiple outputs
filename: '[name].js'
// ...
},
rules: {
// ...
{
test: /\.png|svg|jpg|gif$/,
use: [
{
// copy loaded files to
// output_base_path/input_relative_path/filename.extension
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
useRelative: true
}
},
{
loader: 'image-webpack-loader',
options: { /* ... */ }
}
]
}
}
src/img/index.js
// This is the only line in this file.
// Recursively require all png or jpg from current folder
require.context('./', true, /\.(png|jpg)$/)
So now I'll get speculative and try to translate it to your use-case as best I can without being able to test it:
webpackconfig.js
// ...
entry: {
// ...
'static': './static/index.js'
},
output: {
filename: '[name].js'
// ...
},
rules: {
// ...
{
// I'm guessing the string being tested here is a relative path.
// If I'm wrong, this regex will need some tweaking.
// Matches everything in assets folder
test: /^assets\/.+/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
useRelative: true
}
},
{
// Not sure if you actually need this loader, but it's something I
// would try if file loader is choking on your files, especially
// if you are processing multiple filetypes.
loader: 'raw-loader'
}
]
}
}
src/assets/index.js
// Let's just match everything. Putting images in this folder will
// probably give you headaches. Limit it to text files and handle images
// separately.
require.context('./', true, /.+/)
Here's some info on require.context and file-loader