Unable to configure Jest in my React App - reactjs

I'm just starting with tests right now, trying to set up the Jest with my project, but it has been painful.. I am trying to do the React App tutorial, with the Webpack Tutorial, and I'm facing if this error:
C:\Users\myuser\Desktop\myapp\__tests__\Link.test.js:2
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules\jest-runtime\build\transform.js:320:12)
I'm using Node v7.5.0 and Webpack 2. I installed all the necessary dependencies and I also tried to sort with jest configuration, but it didn't sort my problem.
My package.json looks like this:
{
"name": "myapp",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"jest": {
"modulePaths": ["<rootDir>/src/"],
"moduleDirectories": ["node_modules"],
"transform": {
"^.+\\.jsx?$": "babel-jest"
},
"transformIgnorePatterns": [
"<rootDir>/(node_modules)/"
]
},
"scripts": {
"start": "webpack-dev-server --progress --watch",
"build": "webpack --progress",
"test": "jest --verbose"
},
"devDependencies": {
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-jest": "^18.0.0",
"babel-loader": "^6.2.10",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0",
"babel-polyfill": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"css-loader": "^0.26.1",
"eslint": "^3.15.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-import-resolver-webpack": "^0.8.1",
"eslint-loader": "^1.6.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jest": "^1.0.2",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.9.0",
"extract-text-webpack-plugin": "^1.0.1",
"html-webpack-plugin": "^2.28.0",
"jest": "^18.1.0",
"jest-cli": "^19.0.2",
"node-sass": "^4.5.0",
"react-hot-loader": "3.0.0-beta.6",
"react-test-renderer": "^15.4.2",
"sass-loader": "^5.0.1",
"style-loader": "^0.13.1",
"url-loader": "^0.5.8",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
},
"dependencies": {
"chart.js": "^2.5.0",
"classnames": "^2.2.5",
"file-loader": "^0.11.1",
"flexboxgrid": "^6.3.1",
"google-map-react": "^0.22.3",
"intl": "^1.2.5",
"intl-locales-supported": "^1.0.0",
"lodash": "^4.17.4",
"material-ui": "^0.17.0",
"moment": "^2.17.1",
"node-schedule": "^1.2.1",
"normalize.css": "^5.0.0",
"re-base": "^2.6.0",
"react": "^15.4.2",
"react-chartjs-2": "^2.0.5",
"react-dom": "^15.4.2",
"react-flexbox-grid": "^0.10.2",
"react-router-dom": "^4.0.0",
"react-scrollbar": "^0.5.1",
"react-tap-event-plugin": "^2.0.1",
"webpack": "^2.2.1"
}
}
My .babelrc file looks like this:
{
"presets": [
["es2015", {"modules": false}],
"stage-2",
"react"
],
"plugins": [
"react-hot-loader/babel"
],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
I also set the environment to test, but it didn't make any effect.
My Webpack file looks like this:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const plugins = [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './src/index.html'),
inject: true,
favicon: path.resolve(__dirname, './src/images/favicon.ico'),
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('dev'),
},
}),
];
module.exports = {
devtool: 'cheap-eval-source-map',
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./index.jsx',
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public'),
publicPath: '/',
},
context: path.resolve(__dirname, 'src'),
resolve: {
modules: [path.resolve(__dirname, './src'), path.resolve(__dirname, './node_modules')],
extensions: ['.js', '.jsx'],
},
devServer: {
hot: true,
port: 3000,
contentBase: path.resolve(__dirname, 'public'),
historyApiFallback: true,
publicPath: '/',
},
module: {
rules: [
{
test: /(\.js|\.jsx)$/,
enforce: 'pre',
exclude: /node_modules/,
use: {
loader: 'eslint-loader',
options: {
failOnWarning: false,
failOnError: false,
},
},
},
{
test: /(\.js|\.jsx)$/,
use: ['babel-loader'],
exclude: /node_modules/,
},
{
test: /(\.scss|\.css)$/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true,
},
},
{ loader: 'sass-loader' },
],
},
{
test: /\.(png|jpg|)$/,
use: ['url-loader?limit=200000'],
},
],
},
plugins: plugins,
};
Can someone help me with this issue? Thanks!

Related

How to stop infinite refresh in react webpack app?

When I change the TS file, Webpack doesn't stop refreshing the page.
Console says: #ebpack 5.66.0 compiled successfully
I've googled this and tried different plugins, but they didn't work.
The mild refresh plugin just stopping the refresh and excluding JS files from ts-loader doesn't work at all.
The related issue in git didn't give the result as well.
webpack.config.json:
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const HTMLInlineCSSWebpackPlugin = require("html-inline-css-webpack-plugin").default;
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const htmlWebpackPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html",
inlineSource: '.(js|css)$',
inject: 'body',
});
module.exports = {
entry: ['./src/index.tsx', './src/main.css'],
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
fallback: {
"path": require.resolve("path-browserify"),
"os": require.resolve("os-browserify/browser"),
"url": require.resolve("url"),
"tty": require.resolve("tty-browserify"),
"minimatch": require.resolve("minimatch"),
"process": require.resolve("process")
},
},
module: {
rules: [{
test: /\.tsx?$/,
exclude: [/node_modules/, /\.js$/, /\.d\.ts$/, /dist/],
loader: 'ts-loader',
options: {
configFile: 'tsconfig.json',
}
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
],
},
output: {
filename: 'bundle.js',
publicPath: '',
},
plugins: [
new webpack.ProvidePlugin({
process: 'process',
}),
new MiniCssExtractPlugin({
filename: "main.css",
chunkFilename: "mainc.css"
}),
htmlWebpackPlugin,
new HTMLInlineCSSWebpackPlugin(),
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/bundle/]),
]
};
package.json:
{
"name": "browser",
"version": "0.0.0",
"scripts": {
"build": "webpack --mode development",
"start": "webpack serve --mode development --port 3000"
},
"devDependencies": {
"#types/chrome": "^0.0.163",
"#types/jquery": "^3.5.10",
"#types/node": "^16.11.7",
"#types/react": "^17.0.34",
"#types/react-dom": "^17.0.11",
"#types/vscode": "^1.62.0",
"#types/vscode-webview": "^1.57.0",
"css-loader": "^6.5.1",
"html-inline-css-webpack-plugin": "^1.11.1",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.4.6",
"react-dev-utils": "^12.0.0",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4",
"webpack": "^5.66.0",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.7.3"
},
"dependencies": {
"ace-builds": "^1.4.13",
"axios": "^0.24.0",
"bootstrap": "^5.1.3",
"jquery": "^3.6.0",
"minimatch": "^5.0.1",
"mobx": "^6.3.6",
"mobx-react": "^7.2.1",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"react": "^17.0.2",
"react-ace": "^9.5.0",
"react-bootstrap": "^2.0.2",
"react-dom": "^17.0.2",
"redoc": "^2.0.0-rc.66",
"styled-components": "^5.3.5",
"tty-browserify": "0.0.1",
"url": "^0.11.0",
"uuid": "^8.3.2"
}
}
You better change InlineChunkHtmlPlugin tests from /bundle/ to /runtime/.
Replace
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/bundle/])
With
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/runtime~.+[.]tsx/])
OR
new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/runtime/])

Google spreadsheet api throws crypto.createSign error in react app with webpack

I have a react app with webpack recently upgraded to version 5.
I am getting the following error and need help in fixing it
Uncaught (in promise) TypeError: crypto.createSign is not a function
at Object.sign (index.js:151)
at Object.jwsSign [as sign] (sign-stream.js:32)
at GoogleToken.requestToken (index.js:225)
at GoogleToken.getTokenAsyncInner (index.js:163)
at GoogleToken.getTokenAsync (index.js:142)
at GoogleToken.getToken (index.js:94)
at JWT.refreshTokenNoCache (jwtclient.js:158)
at JWT.refreshToken (oauth2client.js:143)
at JWT.authorizeAsync (jwtclient.js:139)
at JWT.authorize (jwtclient.js:135)
when the following code is executed (The credentials are correct)
const { GoogleSpreadsheet } = require("google-spreadsheet");
.
.
.
let submitOrderDetails = async () => {
const SPREADSHEET_ID = "working-id-here";
const doc = new GoogleSpreadsheet(SPREADSHEET_ID);
// this fails
await doc.useServiceAccountAuth({
client_email: creds.client_email,
private_key: creds.private_key,
});
.
.
.
}
webpack.config.json
const HtmlWebPackPlugin = require("html-webpack-plugin");
const path = require("path");
const webpack = require("webpack");
module.exports = {
context: __dirname,
entry: "./src/index.js",
output: {
path: path.join(__dirname, "build"),
filename: "main.js",
publicPath: "/",
},
target: "web",
devServer: {
historyApiFallback: true,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.scss$/,
use: [
"style-loader", //3. Inject styles into DOM
"css-loader", //2. Turns css into commonjs
"sass-loader", //1. Turns sass into css
],
},
{
test: /\.(png|jp?g|svg|gif)?$/,
use: {
loader: "file-loader",
},
},
],
},
plugins: [
new HtmlWebPackPlugin({
template: path.join(__dirname, "public/index.html"),
filename: "index.html",
}),
new webpack.ProvidePlugin({
process: "process/browser",
}),
],
resolve: {
fallback: {
fs: false,
tls: false,
net: false,
path: false,
zlib: false,
http: false,
https: false,
stream: false,
crypto: false,
"crypto-browserify": require.resolve("crypto-browserify"),
os: require.resolve("os-browserify/browser"),
child_process: false,
},
},
};
package.json
{
"name": "my-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"crypto": "^1.0.1",
"crypto-browserify": "^3.12.0",
"fs": "0.0.1-security",
"google-spreadsheet": "^3.2.0",
"googleapis": "^67.0.0",
"http": "0.0.1-security",
"https-browserify": "^1.0.0",
"net": "^1.0.2",
"os": "^0.1.2",
"path": "^0.12.7",
"prettier": "^2.5.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.3.0",
"react-scripts": "4.0.1",
"stream": "0.0.2",
"tls": "0.0.1",
"zlib": "^1.0.5"
},
"scripts": {
"start": "webpack serve --mode development",
"build": "webpack --mode production",
"pretty": "prettier --write \"./**/*.*\""
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "^7.16.5",
"#babel/plugin-proposal-class-properties": "^7.16.5",
"#babel/plugin-transform-runtime": "^7.16.5",
"#babel/preset-env": "^7.16.5",
"#babel/preset-react": "^7.16.5",
"babel-loader": "^8.2.3",
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^5.2.7",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.4.5",
"node-sass": "^6.0.1",
"optimize-css-assets-webpack-plugin": "^6.0.1",
"process": "^0.11.10",
"sass-loader": "^12.4.0",
"style-loader": "^2.0.0",
"terser-webpack-plugin": "^5.3.0",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^3.11.3",
"webpack-merge": "^5.8.0"
}
}
Answering my own question. I updated the webpack config by adding fallbacks.
Here are the updated webpack.config.json and package.json
webpack.config.json
const HtmlWebPackPlugin = require("html-webpack-plugin");
const path = require("path");
const webpack = require("webpack");
module.exports = {
context: __dirname,
entry: "./src/index.js",
output: {
path: path.join(__dirname, "build"),
filename: "main.js",
publicPath: "/",
},
target: "web",
devServer: {
historyApiFallback: true,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.scss$/,
use: [
"style-loader", //3. Inject styles into DOM
"css-loader", //2. Turns css into commonjs
"sass-loader", //1. Turns sass into css
],
},
{
test: /\.(png|jp?g|svg|gif)?$/,
use: {
loader: "file-loader",
},
},
],
},
plugins: [
new HtmlWebPackPlugin({
template: path.join(__dirname, "public/index.html"),
filename: "index.html",
}),
new webpack.ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
],
resolve: {
fallback: {
fs: false,
tls: "empty",
child_process: false,
util: require.resolve("util/"),
buffer: require.resolve("buffer"),
assert: require.resolve("assert/"),
http: require.resolve("stream-http"),
path: require.resolve("path-browserify"),
https: require.resolve("https-browserify"),
os: require.resolve("os-browserify/browser"),
stream: require.resolve("stream-browserify"),
crypto: require.resolve("crypto-browserify"),
"crypto-browserify": require.resolve("crypto-browserify"),
},
},
};
package.json
{
"name": "sigmaprints",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "webpack serve --mode development",
"build": "webpack --mode production",
"pretty": "prettier --write \"./**/*.*\""
},
"devDependencies": {
"#babel/core": "^7.12.1",
"#babel/plugin-proposal-class-properties": "^7.12.1",
"#babel/plugin-transform-runtime": "^7.14.5",
"#babel/preset-env": "^7.12.1",
"#babel/preset-react": "^7.12.1",
"babel-loader": "^8.1.0",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^6.2.1",
"cross-env": "^7.0.2",
"css-loader": "^5.0.0",
"file-loader": "^6.2.0",
"css-minimizer-webpack-plugin": "^1.1.5",
"process": "0.11.10",
"html-webpack-plugin": "^4.5.0",
"node-sass": "^4.14.1",
"optimize-css-assets-webpack-plugin": "^5.0.4",
"prettier": "^2.1.2",
"sass-loader": "^10.0.3",
"style-loader": "^2.0.0",
"terser-webpack-plugin": "^4.2.3",
"webpack": "^5.1.2",
"webpack-cli": "^4.0.0",
"webpack-dev-server": "^3.11.0",
"webpack-merge": "^5.2.0"
},
"browser": {
"crypto": false
},
"dependencies": {
"assert": "^2.0.0",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"google-spreadsheet": "^3.1.15",
"http-browserify": "^1.7.0",
"https-browserify": "^1.0.0",
"net": "^1.0.2",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-router-dom": "^5.2.0",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"tls": "0.0.1",
"util": "^0.12.4"
}
}

React Jest - Test suite failed to run for PNG files even after adding mockFiles and identity-obj-proxy

I'm trying to Unit test my React component which imports another component that contains .png file. Because of this image, my test suits are failing to run.
I tried all the possible solutions with the below configuration, still no luck.
Please suggest, this is my project configuration.
This is my jest.config.json
{
"testRegex": "((\\.|/*.)(spec))\\.js?$"
}
My package.json
{
"name": "Application name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
"test": "jest"
},
"jest": {
"moduleNameMapper": {
".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy"
}
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.8.7",
"#babel/polyfill": "^7.10.1",
"#babel/preset-env": "^7.10.2",
"#babel/preset-react": "^7.8.3",
"axios-mock-adapter": "^1.18.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.6",
"css-loader": "^3.5.3",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^2.5.0",
"file-loader": "^6.0.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.0.1",
"prettier": "^2.0.5",
"react-hot-loader": "^4.12.19",
"redux-mock-store": "^1.5.4",
"style-loader": "^1.2.1",
"url-loader": "^4.1.0",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
},
"dependencies": {
"#material-ui/core": "^4.10.2",
"#material-ui/icons": "^4.9.1",
"#material-ui/lab": "^4.0.0-alpha.56",
"axios": "^0.19.2",
"core-js": "^3.6.5",
"d3-sankey-diagram": "^0.7.3",
"husky": "^4.2.5",
"prop-types": "^15.7.2",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-google-charts": "^3.0.15",
"react-intl": "^4.6.3",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-test-renderer": "^16.13.1",
"redux": "^4.0.5",
"redux-saga": "^1.1.3",
"regenerator-runtime": "^0.13.5"
}
}
My Webpack.config.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: ['./src/index.js', '#babel/polyfill'],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.css$/i,
exclude: /node_modules/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
},
},
],
},
{
test: /\.(jpe?g|gif|png|svg)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
},
},
],
},
{
test: /\.(gif|png|jpe?g)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/images/',
},
},
],
},
],
// loaders: [{ test: /\.jsx?$/, loader: 'babel' }],
},
devServer: {
historyApiFallback: true,
},
resolve: {
extensions: ['*', '.js', '.jsx'],
},
output: {
path: path.join(__dirname, '/dist'),
filename: 'bundle.js',
publicPath: '/',
},
plugins: [new webpack.HotModuleReplacementPlugin()],
devServer: {
contentBase: './dist',
hot: true,
},
};
My .babelrc file
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
Finally my test file:
import React from 'react';
import Enzyme, { shallow, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import MyPackage from '../Components/index';
Enzyme.configure({ adapter: new Adapter() });
describe('Some name', () => {
it('This should be loaded', () => {
const wrapper = shallow(<MyPackage />);
const x= wrapper.find('#selector').debug();
expect(x.prop('length')).toBe(1);
});
});
I tried adding mocks/fileMock.js with
export default '';
and mocks/styleMocks.js
module.exports = {};
still I see this error -
● Test suite failed to run
D:\ReactStuff\folder\myproject\src\assets\images\mylogo.png:1
�PNG
SyntaxError: Invalid or unexpected token
1 | import React from 'react';
> 2 | import { HeaderLogo} from '../assets/images/mylogo.png';
| ^
3 |
4 | const Header = () => {
5 | return (
What am I missing here. Please suggest

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

Webpack producing no input

I am aware that this is quite a common problem with Webpack, but I am a total newbie and the solutions I've found so far seem to be very specific to the single project. However, the scenario is the same: Webpack compiling the project correctly and then showing a blank page, without reporting any errors.
Here is my webpack.config.js (I made it following tutorials):
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
export default () => ({
entry: [
path.join(__dirname, 'src/index.jsx'),
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './App.html'
}),
],
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel',
options: {
babelrc: false,
presets: [
['es2015', { modules: false }],
'react',
],
}
}
]
},
{
test: /\.(css|scss|sass)$/,
loader: 'style!css!sass',
},
]
},
});
module.exports = config;
Here my package.json:
{
"name": "smart-fit",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"dependencies": {
"chartjs": "^0.3.24",
"interactjs": "^1.2.8",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"moment": "^2.18.1",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-hot-loader": "^3.0.0-beta.3",
"react-table": "^5.6.0"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"css-loader": "^0.28.1",
"extract-text-webpack-plugin": "^2.0.0-beta.4",
"html-webpack-plugin": "^2.28.0",
"loader-utils": "^1.1.0",
"node-sass": "^4.5.2",
"sass-loader": "^6.0.3",
"style-loader": "^0.17.0",
"webpack": "^2.1.0-beta.25",
"webpack-dev-server": "^2.1.0-beta.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Webpack version is the lastest and the project is written using React and ES5.

Resources