React Webpack in Production Error #105 - reactjs

I'm kind of desperate here.
I'm working on a React application and use webpack to compile my bundle.js.
The problem is when i try to compile it for "production" i end up with a really nasty error :
"Minified React error #105; visit http://facebook.github.io/react/docs/error-decoder.html?invariant=105&args[]=HardwareKeyboardArrowDown for the full message or use the non-minified dev environment for full errors and additional helpful warnings."
Followed by a bunch of
"Uncaught TypeError: Cannot read property '__reactInternalInstance$qw6tjofxg1o' of null"
When i set my node.env to developement ('NODE_ENV': JSON.stringify('developement') ), it's working fine.
The link in the error says : "A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object" but i don't have any problem in dev mode, so i don't think its coming from my code and i can't find where i should look to solve this problem since dev mode doesn't tell anything more to me...
Here are my webpack config & package.json :
const webpack = require('webpack');
const path = require('path');
const buildPath = path.resolve(__dirname, 'build');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
const TransferWebpackPlugin = require('transfer-webpack-plugin');
const config = {
entry: [
'./src/app/app.js'
],
// Render source-map file for final build
devtool: 'source-map',
debug: true,
// output config
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js',
publicPath: '/public/'
},
plugins: [
// Define production build to allow React to strip out unnecessary checks
new webpack.DefinePlugin({
'process.env':{
'NODE_ENV': JSON.stringify('production')
}
}),
// Minify the bundle
new webpack.optimize.UglifyJsPlugin({
compress: {
// suppresses warnings, usually from module minification
warnings: false,
},
}),
// Allows error warnings but does not stop compiling.
new webpack.NoErrorsPlugin(),
// Transfer Files
],
module: {
loaders: [
{
test: /\.js$/, // All .js files
loaders: ['babel-loader'], // react-hot is like browser sync and babel loads jsx and es6-7
exclude: [nodeModulesPath],
},
{
test: /\.css$/,
loader: 'style-loader!css-loader',
include: /flexboxgrid/
},
{
test: /\.json$/, loader: "json-loader",
},
{ test: /\.scss?$/,
loader: 'style!css!sass',
include: path.join(__dirname, 'src', 'styles') },
{ test: /\.png$/,
loader: 'file' },
{ test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
loader: 'file'}
],
resolve: {
extensions: ['', '.js', '.jsx', '.css', '.json']
}
},
};
module.exports = config;
{
"name": "material-ui-example-webpack",
"version": "0.16.0",
"description": "Sample project that uses Material-UI",
"repository": {
"type": "git",
"url": "https://github.com/callemall/material-ui.git"
},
"scripts": {
"start": "webpack-dev-server --config webpack-dev-server.config.js --host $IP --port $PORT --hot --progress --inline --colors",
"clean-public": "npm run remove-public && mkdir public",
"remove-public": "node_modules/.bin/rimraf ./public",
"build:html": "babel-node tools/buildHtml.js",
"heroku-prebuild": "npm install && npm-run-all clean-public build:html",
"heroku-postbuild": "babel-node tools/build.js",
"prod-start": "babel-node tools/publicServer.js"
},
"private": true,
"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.8",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-2": "^6.18.0",
"html-webpack-plugin": "^2.24.1",
"npm-run-all": "1.8.0",
"redux-devtools": "^3.4.0",
"redux-devtools-dock-monitor": "^1.1.2",
"redux-devtools-log-monitor": "^1.3.0",
"rimraf": "2.5.2",
"transfer-webpack-plugin": "^0.1.4",
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"axios": "^0.16.1",
"babel-cli": "6.8.0",
"babel-loader": "^6.2.8",
"babel-polyfill": "6.8.0",
"babel-preset-es2015": "6.6.0",
"babel-preset-react": "6.5.0",
"babel-preset-stage-2": "^6.24.1",
"cheerio": "^0.20.0",
"colors": "1.1.2",
"compression": "^1.6.1",
"css-loader": "^0.27.3",
"express": "^4.15.2",
"json-loader": "^0.5.4",
"lint": "^1.1.2",
"material-ui": "^0.17.1",
"ncp": "^2.0.0",
"npm-run-all": "1.8.0",
"open": "0.0.5",
"react": "^15.4.1",
"react-component-queries": "^2.1.1",
"react-dom": "^15.4.1",
"react-fittext": "https://github.com/gianu/react-fittext",
"react-flexbox-grid": "^1.0.2",
"react-html-parser": "^1.0.3",
"react-masonry-component": "^5.0.5",
"react-redux": "^5.0.4",
"react-responsive": "^1.2.7",
"react-router": "^2.0.0-rc5",
"react-router-dom": "^4.0.0",
"react-sizeme": "^2.3.1",
"react-tap-event-plugin": "^2.0.1",
"redux": "^3.6.0",
"redux-form": "^6.6.3",
"redux-promise": "^0.5.3",
"rimraf": "2.5.2",
"serve-favicon": "^2.3.0",
"style-loader": "^0.16.0",
"transfer-webpack-plugin": "^0.1.4",
"webpack": "^1.13.3"
}
}
buildHtml script :
import fs from 'fs';
import cheerio from 'cheerio';
import colors from 'colors';
/*eslint-disable no-console */
console.log("buildHTML.js start");
fs.readFile('src/index.html', 'utf8', (err, markup) => {
if (err) {
return console.log(err);
}
const $ = cheerio.load(markup);
$('head').prepend('');
fs.writeFile('public/index.html', $.html(), 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log('index.html written to /public'.green);
});
});
And then build.js
/*eslint-disable no-console */
import webpack from 'webpack';
import webpackConfig from '../webpack-production.config';
import colors from 'colors';
import ncp from 'ncp';
process.env.NODE_ENV = 'production';
console.log("build.js start");
//Static files to import in /public (images/css)
var source = [["src/images","public/images"],["src/css","public/css"]];
function copyStaticFiles(path){
ncp(path[0], path[1], function (err) {
if (err) {
return console.error(err);
}})
}
console.log('Generating minified bundle for production via Webpack...'.blue);
webpack(webpackConfig).run((err, stats) => {
if (err) { // so a fatal error occurred. Stop here.
console.log(err.bold.red);
return 1;
}
const jsonStats = stats.toJson();
if (jsonStats.hasErrors) {
return jsonStats.errors.map(error => console.log(error.red));
}
if (jsonStats.hasWarnings) {
console.log('Webpack generated the following warnings: '.bold.yellow);
jsonStats.warnings.map(warning => console.log(warning.yellow));
}
console.log(`Webpack stats: ${stats}`);
source.forEach(copyStaticFiles);
console.log('Your app has been compiled in production mode and written to /public.'.green);
return 0;
});
I don't even know where to start and i can't find any documentation about this kind of errors.
I can provide more informations if needed.
Thanks for reading me

Ok so i dont have a object with a HardwareKeyboardArrowDown so i went looking in my dependencies and i found that it came from the matérial-ui dependency.
I forced the version 17.4 in my package.json and it worked !

The message gave you the name of the function (component) that returns the invalid object. HardwareKeyboardArrowDown.
So you should look at the return of its render function and make sure you return a valid React element (or null)
That means no undefined, array etc..

Related

React - Chrome does not report undefined JavaScript object property. Crashes

Since im building my webapp with React, when i try to set something into an undefined object property, the code processing crashes (reproducible with breakpoint debugging) and no errors are shown in the console log. Also the chrome react extension does not give me any errors.
So to finding a bug is more time consuming that it should be.
let changedJsonResponse = jsonResponse;
changedJsonResponse.data = newTableData; // changedJsonResponse.data is undefined
// throws no error & code proccess crashes
webpack.config.js
const webpack = require('webpack');
const path = require('path');
let config = {
devtool: 'eval-source-map',
entry: {
FooManagement: ['babel-polyfill', './domains/FooManagement/entry.jsx'],
},
output: {
path: path.resolve(__dirname, '../web/react/'),
filename: '[name].js'
},
externals: {
'jquery': 'window.jQuery',
'bootbox': 'window.bootbox',
},
resolve: {
alias: {
alias_shared: path.resolve(__dirname, 'shared'),
},
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: [/node_modules/],
loader: 'babel-loader',
options: {presets: ['es2015', 'react']}
},
{
test: /\.css/,
loader: 'style-loader!css-loader'
}
]
}
};
module.exports = config;
package.json
{
"author": "myself",
"version": "0.0.1",
"description": "",
"main": "index.jsx",
"scripts": {
"watch": "webpack -d --watch",
"build": "webpack -p"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"style-loader": "^0.19.0",
"webpack": "^3.8.1"
},
"dependencies": {
"axios": "^0.16.2",
"dateformat": "^3.0.2",
"is-uuid": "^1.0.2",
"lodash": "^4.17.4",
"react": "^16.0.0",
"react-cropper": "^1.0.1",
"react-dom": "^16.0.0",
"react-dropzone": "^4.2.3",
"react-router-dom": "^4.2.2",
"react-switchery-component": "0.0.7",
"uuid": "^3.1.0"
}
}
Chrome version: 62.0.3202.94
I was able to catch those errors in Axios .catch.
After a while i found the cause of the problem.

Unexpected token react-router in express server

{
"name": "shopping-cart-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "karma start",
"start": "node server.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"axios": "^0.9.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"body-parser": "^1.17.1",
"express": "^4.13.4",
"faker": "^4.1.0",
"marked": "^0.3.6",
"mongoose": "^4.9.6",
"nodemon": "^1.11.0",
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-bootstrap": "^0.31.0",
"react-dom": "^0.14.7",
"react-router": "^2.0.0"
},
"devDependencies": {
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"css-loader": "^0.23.1",
"expect": "^1.14.0",
"foundation-sites": "6.2.0",
"jquery": "^2.2.1",
"karma": "^0.13.22",
"karma-chrome-launcher": "^0.2.2",
"karma-mocha": "^0.2.2",
"karma-mocha-reporter": "^2.0.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.7.0",
"mocha": "^2.4.5",
"node-sass": "^3.4.2",
"sass-loader": "^3.1.2",
"script-loader": "^0.6.1",
"style-loader": "^0.13.0",
"webpack": "^1.12.13"
}
}
I am trying to implement server side routing for a react app i am working on. I am using react-router for this purpose. Here is part of my server.js code
const renderToString = require ('react-dom/server');
let match, RouterContext = require('react-router');
const routes = require ('/app/router');
const React = require('react');
app.get('*', (req, res) => {
match(
{ routes, location: req.url },
(err, redirectLocation, renderProps) => {
// in case of error display the error message
if (err) {
return res.status(500).send(err.message);
}
// in case of redirect propagate the redirect to the browser
if (redirectLocation) {
return res.redirect(302, redirectLocation.pathname + redirectLocation.search);
}
// generate the React markup for the current route
let markup;
if (renderProps) {
// if the current route matched we have renderProps
markup = renderToString(<RouterContext {...renderProps}/>);
} else {
// otherwise we can render a 404 page
markup = renderToString(<NotFoundPage/>);
res.status(404);
}
// render the index template with the embedded React markup
return res.render('index', { markup });
}
);
});
//my webpack config file
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: [
'script!jquery/dist/jquery.min.js',
'script!foundation-sites/dist/foundation.min.js',
'./app/app.jsx'
],
externals: {
jquery: 'jQuery'
},
plugins: [
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery'
})
],
output: {
path: __dirname,
filename: './public/bundle.js'
},
resolve: {
root: __dirname,
modulesDirectories: [
'node_modules',
'./app/components'
],
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0']
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/
}
]
},
sassLoader: {
includePaths: [
path.resolve(__dirname, './node_modules/foundation-sites/scss')
]
},
devtool: 'cheap-module-eval-source-map'
};
I get an unexpected token < at < RouterContext {...renderProps}/>.
I have looked over the tutorial am am working of off, but i am not so sure what i have done wrong. Any suggestion would be really appreciated
You are using the Babel presets react, es2015 and stage-0.
None of those presets includes the plugin you need to make the object spread syntax you are using work – this part of your code:
<RouterContext {...renderProps} />
You can fix your problem by adding the transform-object-rest-spread plugin to your Babel config like this:
module: {
loaders: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['transform-object-rest-spread']
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/
}
]
}
Don't forget to install the plugin using
npm install --save-dev babel-plugin-transform-object-rest-spread

State of the art webpack / React configuration

I'm trying to setup a webpack 2, babel, and React configuration for achieving:
Native ES6/ES7 features
Tree shaking builds
Hot reloading
I had a demo repo but it has distinct stuff mixed, like even JSHint and ESLint at the same time.
I'd like to get my setup up and running and get suggestions for best practices
So, as first option I tried to use babel-preset-env. Then after some dependencies being installed. I ran into this issue:
ERROR in ./src/main.jsx
Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level (3:0)
However, the first line in my code is import 'babel-polyfill'; then just import's.
This is how my Babel config file looks like:
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": ["last 2 versions"]
}
}
],
"react"
],
"plugins": [
"babel-plugin-transform-class-properties",
"transform-react-require"
]
}
This is what my development webpack config file looks like:
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const path = require('path');
const buildPath = path.resolve(__dirname, 'build');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
const TransferWebpackPlugin = require('transfer-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const config = {
//Entry points to the project
entry: [
'babel-polyfill',
'webpack/hot/dev-server',
'webpack/hot/only-dev-server',
path.join(__dirname, '../src/main.jsx')
],
//Config options on how to interpret requires imports
resolve: {
extensions: [".js", ".jsx"]
},
//Server Configuration options
devServer:{
contentBase: 'build', //Relative directory for base of server
devtool: 'eval',
hot: true, //Live-reload
inline: true,
port: 3000, //Port Number
host: 'localhost', //Change to '0.0.0.0' for external facing server
proxy: {
'^\/api': {
target: 'http://127.0.0.1:9090'
}
},
historyApiFallback: true
},
devtool: 'eval',
output: {
path: buildPath, //Path of output file
filename: 'app.js'
},
plugins: [
new webpack.DefinePlugin({
API_BASE: '""',
PRODUCTION: false,
'process.env.NODE_ENV': '"development"'
}),
//Enables Hot Modules Replacement
new webpack.HotModuleReplacementPlugin(),
//Allows error warnings but does not stop compiling. Will remove when eslint is added
new webpack.NoEmitOnErrorsPlugin(),
//Moves files
new TransferWebpackPlugin([
{from: 'www'}
], path.resolve(__dirname, "src")),
new ExtractTextPlugin("main.css")
],
module: {
rules: [
{
//React-hot loader and
test: /\.(js|jsx)$/, //All .js and .jsx files
loaders: [ 'babel-loader', 'react-hot-loader'],
//react-hot is like browser sync and babel loads jsx and es6-7
exclude: [nodeModulesPath]
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style',
use: 'css'
})
},
{
test: /\.svg$/,
loader: 'svg-sprite?' + JSON.stringify({
name: '[name]_[hash]',
prefixize: true
})
}
]
}
};
module.exports = config;
And this below is package.json
{
"name": "LumaHealth",
"version": "1.0.0",
"description": "LumaHealth",
"main": "start.js",
"scripts": {
"start": "webpack --config ./webpack/webpack.config.development.js",
"build": "webpack --config ./webpack/webpack.config.production.js",
"clean": "rm build/app.js"
},
"repository": {
"type": "git",
"url": "git#github.com:lumahealthhq/web-app.git"
},
"keywords": [],
"author": "Marcelo Oliveira",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-eslint": "^7.2.1",
"babel-plugin-react-require": "^3.0.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-preset-env": "^1.2.2",
"babel-preset-react": "^6.23.0",
"css-loader": "^0.26.4",
"enzyme": "^2.0.0",
"eslint": "^3.7.1",
"eslint-config-airbnb": "^14.1.0",
"eslint-loader": "^1.7.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.4.1",
"extract-text-webpack-plugin": "^2.1.0",
"html-webpack-plugin": "^2.28.0",
"nyc": "^10.1.2",
"postcss-loader": "^1.3.3",
"postcss-nested": "^1.0.0",
"react-addons-test-utils": "^15.4.1",
"sinon": "^1.17.2",
"style-loader": "^0.13.2",
"sw-precache": "^5.0.0",
"transfer-webpack-plugin": "^0.1.4",
"webpack": "^2.3.2",
"webpack-dev-server": "^2.4.2"
},
"dependencies": {
"babel-core": "^6.5.2",
"babel-eslint": "^7.0.0",
"babel-plugin-transform-react-require": "^1.0.1",
"babel-polyfill": "^6.23.0",
"co": "^4.6.0",
"express": "^4.12.3",
"file-loader": "^0.10.1",
"humps": "^2.0.0",
"isomorphic-fetch": "^2.2.1",
"local-storage": "^1.4.2",
"lodash": "^4.16.4",
"material-ui": "^0.17.0",
"moment": "^2.15.2",
"q": "^1.4.1",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-redux": "^5.0.3",
"react-router": "^3.0.2",
"react-router-redux": "^4.0.6",
"react-slick": "^0.14.4",
"react-tap-event-plugin": "^2.0.0",
"react-web-notification": "^0.2.3",
"redux": "^3.6.0",
"redux-form": "^6.1.1",
"redux-logger": "^2.7.0",
"redux-socket.io": "^1.3.1",
"redux-thunk": "^2.1.0",
"socket.io-client": "^1.7.2",
"url-loader": "^0.5.7",
"vanilla-masker": "^1.0.9"
}
}
I recently upgraded my boilerplate from webpack 1 to webpack 2, feel free to get any information / concept from the webpack config file there, hope it helps.
https://github.com/iroy2000/react-redux-boilerplate

Why is my webpack bundle.js bigger than 7.58MB

Why is my bundle.js so large?
What is wrong with my configuration file?
All my React projects tend to be incredibly large in file size (bundle.js is 7.58 mb). I have no idea why is it this large. I already have uglifyJS on, but this doesn't seem to help a lot.
Here are the details:
bundle.js 7.58 MB 0 [emitted] main
index.html 942 bytes [emitted]
My webpack.config.js
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const BUILD_DIR = path.resolve(__dirname, 'dist');
const APP_DIR = path.resolve(__dirname, 'src/components');
const DATA_DIR = path.resolve(__dirname, 'data');
const config = {
entry: APP_DIR + '/App.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?/,
include: APP_DIR,
loader: [
'babel'
],
query: {
presets: ["es2015", "react"]
}
},
{
test: /\.css$/,
loader:'style-loader!css-loader?importLoaders=1!postcss-loader'
},
{
test:/\.scss$/,
loader:'style-loader!css-loader?importLoaders=1!postcss-loader!sass-loader'
},
{
test: /\.html/,
loader:'html-loader'
},
{
test: /\.(json)([\?]?.*)$/,
include: DATA_DIR,
loader: "file-loader",
query:{
name:"data/[name].[ext]"
}
},
{
test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
loader: "file-loader",
query:{
name:"asserts/fonts/[name].[ext]"
}
},
{
test: /\.(gif|png|jpe?g)$/i,
include: DATA_DIR,
loader: "file-loader",
query:{
name:"data/images/[name]-[hash:5].[ext]"
}
}
]
},
postcss:[
require('autoprefixer')({
broswers:['last 5 versions']
})
],
devtool:'eval-source-map',
devServer:{
historyApiFallback:true,
hot:true,
inline:true,
proxy:{
'/api/*':{
target:'http://localhost:8081',
secure:false
}
}
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
}),
new htmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
title:'this is a title', //一个title 属性
inject:'body'
})
]
};
module.exports = config;
My package.json
{
"name": "react-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --progress --profile --colors --hot --inline --port 3000 --host 0.0.0.0",
"dev": "webpack -d --watch",
"webpack": "webpack -p --config webpack.config.js --colors --display-reasons --display-error-details --display-modules --sort-modules-by size"
},
"author": "Sharp",
"license": "MIT",
"dependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"webpack": "^1.12.8"
},
"devDependencies": {
"autoprefixer": "^6.7.7",
"axios": "^0.15.3",
"babel-plugin-import": "^1.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"css-loader": "^0.27.3",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.10.1",
"history": "^4.6.1",
"html-loader": "^0.4.5",
"html-webpack-plugin": "^2.28.0",
"lodash": "^4.17.4",
"node-sass": "^4.5.0",
"postcss-loader": "^1.3.3",
"react-addons-update": "^15.4.2",
"react-bootstrap": "^0.30.8",
"react-bootstrap-datetimepicker": "0.0.22",
"react-redux": "^5.0.3",
"react-router": "^3.0.2",
"redux": "^3.6.0",
"redux-logger": "^2.8.2",
"redux-thunk": "^2.2.0",
"remove": "^0.1.5",
"sass-loader": "^6.0.3",
"scss-loader": "0.0.1",
"style-loader": "^0.14.1",
"webpack-dev-server": "^1.16.3"
}
}
Does anyone have any idea how to fix this?
Caveat: OP's config is a webpack v1 config, while my answer is for v2.
You are using the type of source maps that are contained in the bundle itself:
devtool:'eval-source-map'
This type of source maps is for developmnent only, so if the bundle size is huge it's not an issue. So nothing is wrong with you configuration file per se, you just have to make two separate configs (maybe both exending a base config) for development and production, and use different source maps types in both.
See https://webpack.js.org/configuration/devtool/ for source map types that should be used in production and development. For production you could use something like cheap-source-map or not use source maps at all.
Generally source maps can be output as a standalone bundle/chunk or be contained in the code bundle itself, and have different levels of detail, from line/column info to no source maps at all. This is up to you to decide how much debugging info you need in production and if you are ok with making your sources publicly available.

Why do we need to use import 'babel-polyfill'; in react components?

I wonder if we user babel loader + all the presets, why do we need to include babel-polyfill anyway into our components? I just think that babel-loader should do all the job itself.
Examples were taken here https://github.com/reactjs/redux/tree/master/examples
What i am asking about is:
import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import App from './containers/App';
Here is package example:
{
"name": "redux-shopping-cart-example",
"version": "0.0.0",
"description": "Redux shopping-cart example",
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test mocha --recursive --compilers js:babel-register",
"test:watch": "npm test -- --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/reactjs/redux.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/reactjs/redux/issues"
},
"homepage": "http://redux.js.org",
"dependencies": {
"babel-polyfill": "^6.3.14",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.2.1",
"redux": "^3.2.1",
"redux-thunk": "^1.0.3"
},
"devDependencies": {
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.1.1",
"cross-env": "^1.0.7",
"enzyme": "^2.0.0",
"express": "^4.13.3",
"json-loader": "^0.5.3",
"react-addons-test-utils": "^0.14.7",
"redux-logger": "^2.0.1",
"mocha": "^2.2.5",
"node-libs-browser": "^0.5.2",
"webpack": "^1.9.11",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.9.1"
}
}
Here is webpack config example taken from https://github.com/reactjs/redux/tree/master/examples
var path = require('path')
var webpack = require('webpack')
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'webpack-hot-middleware/client',
'./index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [
{
test: /\.js$/,
loaders: [ 'babel?presets[]=react,presets[]=es2015,presets[]=react-hmre' ],
exclude: /node_modules/,
include: __dirname
},
{
test: /\.json$/,
loaders: [ 'json' ],
exclude: /node_modules/,
include: __dirname
}
]
}
}
Babel transpiles your code to something that browsers can understand, but the resulting code uses features that may or may not work in every single browser. For example Object.assign is not supported in all browsers, so babel-polyfill fills the holes. It's just a collection of polyfills that you would usually include anyway to have support for legacy browsers.
Consider this code:
const foo = {
name: 'Homer'
};
const bar = Object.assign({}, foo, {age: '?'});
console.log(Object.keys(foo), Object.keys(bar));
Babel will transpile this to the almost identical:
'use strict';
var foo = {
name: 'Homer'
};
var bar = Object.assign({}, foo, { age: '?' });
console.log(Object.keys(foo), Object.keys(bar));
because this is normal old school JS syntax. However, that doesn't mean that the native functions used are implemented in all browsers, so we need to include the polyfill.

Resources