I am strugling with setting up basic compilation of my React project using Gulp & Webpack
Here is my Gulpfile:
function styles() {
return gulp.src('./src/less/styles.less')
.pipe(less())
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
}
function copyAssets() {
return gulp.src('./assets/**')
.pipe(gulpCopy('./dist/'))
.pipe(connect.reload());
}
function watchAssets() {
gulp.watch('assets/**/*', copyAssets);
gulp.watch('src/less/**/*.less', styles);
}
function webpackBuildgulp() {
return gulp.src('src/js/index.js')
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('dist/'));
}
gulp.task('default', gulp.parallel(styles, copyAssets, webpackBuildgulp, server, watchAssets));
Here is my Webpack config:
module.exports = () => {
const webpackConfig = {
entry: './src/js/index.js',
module: {
rules: [{
test: /\.jsx$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015', 'stage-0', 'react'],
},
},
}],
},
output: {
path: path.resolve(__dirname, '../dist'),
filename: 'app.js',
publicPath: 'http://localhost:1337/dist/',
},
resolve: {
modules: ['../src/js', 'node_modules'],
extensions: ['', '.js', 'jsx'],
},
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
}),
],
};
return webpackConfig;
};
Here is package.json deps tree:
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.24.0",
"babel-loader": "^7.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.7.7",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-react-jsx": "^6.7.5",
"babel-preset-airbnb": "^1.0.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.5.0",
"babelify": "^7.3.0",
"chalk": "^1.1.3",
"connect-modrewrite": "^0.9.0",
"envify": "^3.4.1",
"enzyme": "^2.4.1",
"eslint": "^3.11.1",
"eslint-config-airbnb": "^13.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.8.0",
"gulp": "github:gulpjs/gulp#4.0",
"gulp-callback": "0.0.3",
"gulp-connect": "^5.0.0",
"gulp-imagemin": "^3.0.3",
"gulp-less": "^3.1.0",
"gulp-uglify": "^2.0.0",
"gulp-watch": "^4.3.9",
"gutil": "^1.6.4",
"husky": "^0.11.6",
"isparta": "^4.0.0",
"jasmine-core": "^2.4.1",
"jsdom": "^8.4.1",
"karma": "^0.13.19",
"karma-babel-preprocessor": "^6.0.1",
"karma-browserify": "^5.0.0",
"karma-chrome-launcher": "^0.2.2",
"karma-coverage": "^1.1.0",
"karma-jasmine": "^0.3.6",
"karma-nyan-reporter": "^0.2.4",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sinon": "^1.0.4",
"less": "^2.6.1",
"less-plugin-autoprefix": "^1.5.1",
"less-plugin-clean-css": "^1.5.1",
"phantomjs-polyfill-object-assign": "0.0.2",
"phantomjs-prebuilt": "^2.1.4",
"react-addons": "^0.9.1-deprecated",
"react-addons-shallow-compare": "15.4.0",
"react-addons-test-utils": "15.4.0",
"react-asset-loader": "0.0.1",
"react-images": "^0.5.1",
"react-test-renderer": "15.4.0",
"require-dir": "^0.3.0",
"sinon": "^1.17.4",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.7.0",
"webpack-dev-middleware": "^1.10.0",
"webpack-stream": "^3.2.0",
"when": "^3.7.7"
},
"dependencies": {
"animate.css": "^3.5.1",
"axios": "^0.12.0",
"classnames": "^2.2.5",
"es6-promise": "^3.2.1",
"fullcalendar": "3.3.1",
"fullcalendar-scheduler": "1.5.0",
"intl": "^1.2.4",
"jquery": "2.1.4",
"jquery-timepicker": "^1.3.3",
"lodash": "^4.13.1",
"moment": "2.18.1",
"moment-range": "^3.0.2",
"qtip2": "^3.0.3",
"rc-calendar": "^7.6.5",
"rc-time-picker": "^2.2.2",
"react": "15.4.0",
"react-animated-number": "^0.4.2",
"react-countup": "^1.3.0",
"react-datepicker": "^0.41.1",
"react-dom": "15.4.0",
"react-dropzone": "^3.5.3",
"react-geosuggest": "^1.24.1",
"react-google-maps": "^4.11.0",
"react-google-tag-manager": "^1.1.2",
"react-helmet": "^3.1.0",
"react-imageloader": "^2.1.0",
"react-intl": "^2.1.3",
"react-maskedinput": "^3.2.4",
"react-redux": "^5.0.0",
"react-redux-toastr": "^3.8.5",
"react-router": "^2.5.2",
"react-scrollbar": "^0.4.1",
"react-sortable": "1.2.0",
"react-sparklines": "^1.6.0",
"react-tabs": "^0.7.0",
"react-typist": "^1.1.0",
"redux": "^3.5.2",
"redux-form": "^6.3.2",
"redux-logger": "^2.6.1",
"redux-mock-store": "^1.1.2",
"redux-thunk": "^2.1.0",
"riek": "^1.0.4",
"slick-carousel": "^1.6.0",
"spid-sdk-js": "^2.1.7",
"vivus": "^0.3.1"
},
However when I trie to compile my project typing gulp I get the following error:
Module parse failed: /src/js/index.js Unexpected token (42:2)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (42:2)
Here is the index.js file:
import ReactDOM from 'react-dom';
import React from 'react';
import { Provider } from 'react-redux';
import { Router, browserHistory } from 'react-router';
import { IntlProvider } from 'react-intl';
import ReduxToastr from 'react-redux-toastr';
import createStore from './store';
import { SIGNIN_USER, getCurrentUser } from './actions/auth';
import { getSalonsBasicInfo } from './actions/landingPage';
import { stuff } from './middleware/stuff';
import { checkSalon } from './actions/salon';
import routes from './routes';
import translations from './translations/en';
if (!window.Intl) {
require('intl');
}
const store = createStore();
const access_token = localStorage.getItem('access_token');
const refresh_token = localStorage.getItem('refresh_token');
store.dispatch({
type: INIT,
});
if (access_token || refresh_token) {
store.dispatch({
type: SIGNIN_USER,
});
store.dispatch(getCurrentUser());
store.dispatch(checkSalon());
}
localStorage.setItem('debug', false);
ReactDOM.render(
<Provider store={store}>
<div>
<ReduxToastr
timeOut={4000}
newestOnTop={false}
position="top-center"
/>
<IntlProvider locale={'en-US'} messages={translations}>
<Router history={browserHistory} routes={routes} />
</IntlProvider>
</div>
</Provider>,
document.getElementById('app'),
);
Someway, somehow, Babel fails to understand React code and doesnt compile it.
However when I run Babel manually:
babel src/js/index.js --presets es2015,react
It works perfectly.
What am I doing wrong in my Webpack configuration ?
Your babel-loader is only testing for .jsx files and you have saved your index as a .js file. Change test to test: /\.(js|jsx)$/, and see if that helps.
module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015', 'stage-0', 'react'],
},
},
}],
}
Related
I am working on an React App, and I can not open this app in Safari and IE.
For IE-11: SCRIPT1028: SCRIPT1028: Expected identifier, string or number
For Safari(10.1.2) : SyntaxError: Unexpected token '...'. Expected a property name.
my package.json file is as..
"dependencies": {
"#babel/core": "^7.0.0",
"#babel/plugin-proposal-object-rest-spread": "^7.6.2",
"#storybook/addon-backgrounds": "^3.4.8",
"#storybook/react": "^4.0.0-alpha.4",
"#types/d3": "^5.0.0",
"#types/debug": "^0.0.30",
"#types/enzyme": "^3.1.9",
"#types/jest": "^23.1.4",
"#types/moment-timezone": "^0.5.4",
"#types/react": "^16.9.11",
"#types/react-dom": "^16.9.3",
"#types/react-intl": "^2.3.16",
"#types/react-mentions": "2.4.0",
"#types/react-redux": "^7.1.5",
"#types/react-router": "^5.1.2",
"#types/react-router-dom": "^5.1.0",
"#types/react-select": "^1.2.6",
"#types/react-show-more": "^2.0.1",
"#types/reactour": "^1.13.1",
"#types/redux": "^3.6.0",
"#types/redux-form": "^7.3.1",
"#types/redux-logger": "^3.0.6",
"#types/storybook__addon-backgrounds": "^3.2.0",
"#types/storybook__react": "^3.0.8",
"#types/styled-components": "^4.1.19",
"awesome-typescript-loader": "^5.2.0",
"axios": "^0.18.0",
"babel-engine-plugin": "^0.3.0",
"babel-loader": "^7.0.0",
"babel-register": "^6.26.0",
"connected-react-router": "^4.4.1",
"cosed": "^1.1.8",
"d3": "^5.7.0",
"date-fns": "^1.29.0",
"debug": "^3.1.0",
"duplicate-package-checker-webpack-plugin": "^3.0.0",
"emoji-mart": "^2.6.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"gen-tester": "^3.1.2",
"history": "^4.10.1",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.1.0",
"husky": "^0.14.3",
"immer": "^1.3.1",
"jest": "^23.3.0",
"jest-environment-enzyme": "^6.0.2",
"jest-enzyme": "^6.0.2",
"jest-styled-components": "^6.0.0",
"lint-staged": "^7.2.0",
"moment-timezone": "^0.5.14",
"prettier": "^1.13.7",
"prop-types": "^15.7.2",
"react": "^16.11.0",
"react-cosed": "^1.0.9",
"react-dom": "^16.11.0",
"react-intl": "^2.8.0",
"react-redux": "^5.1.1",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-select": "^1.2.1",
"reactour": "^1.15.4",
"redux": "^4.0.0",
"redux-cosed": "^1.0.1",
"redux-form": "^8.2.6",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0",
"styled-components": "^4.4.1",
"ts-jest": "^23.0.0",
"tslint": "^5.9.1",
"tslint-config-prettier": "^1.13.0",
"tslint-no-circular-imports": "^0.5.0",
"typescript": "^3.0.1",
"uglifyjs-webpack-plugin": "^1.2.7",
"url-loader": "^1.0.1",
"webpack": "^4.15.1",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.5",
"webpack-merge": "^4.1.3"
},
"devDependencies": {
"#types/lodash.debounce": "^4.0.4",
"firebase-tools": "^6.1.0",
"lodash.debounce": "^4.0.8"
}
If some more details needed please let me know.
Please suggest, what do i need to do.... Thanks
My webpack.js file is as....
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const {
CheckerPlugin,
TsConfigPathsPlugin,
} = require('awesome-typescript-loader');
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
const ROOT = path.resolve(__dirname, '..');
module.exports = {
module: {
rules: [
{
test: /.tsx?$/,
use: 'awesome-typescript-loader',
exclude: /node_modules/,
},
{
test: /.(png|jpg|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 25000,
},
},
],
},
{
test: /.html$/,
use: 'html-loader',
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
modules: [path.resolve(ROOT, 'shared'), 'node_modules'],
plugins: [new TsConfigPathsPlugin()],
},
plugins: [
new HtmlWebPackPlugin({
template: path.join(ROOT, 'shared', 'index.html'),
filename: './index.html',
}),
new CheckerPlugin(),
new DuplicatePackageCheckerPlugin(),
],
};
I think babel-loader will work but can't find any way to use babel-loader. I need this app to run on ie11 and safari as well.
Please suggest some solution...
The issue is that by default the application was being rendered on IE7, that is - not on IE11/Edge that supports the transpiled build. So I had to mention the meta information to let the browser know that the intended browser is IE11/Edge. Add this to the head section of your index.html:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Now you might be getting some errors in the console that read SCRIPT5009: 'Set' is undefined - refer to the below link relating to polyfills for the Set collection type:
React JavaScript Environment Requirements
For more information on the issue see:
React app not rendering in IE11 and below
Because you are opening it in Microsoft edge, try opening it in latest chrome version... problem will be solved.....this works for ReactJs projects
I am getting this error when I install babel-core in dependencies, If I Install babel-core in Devdependencies it works, but I need babel-core in dependencies, How to surpass this issue? Is there a way in which I can install Babel-core in Dependencies and not get this error?
ERROR in ./node_modules/babel-core/lib/helpers/resolve.js
Module not found: Error: Can't resolve 'module' in '/app/node_modules/babel-core/lib/helpers'
# ./node_modules/babel-core/lib/helpers/resolve.js 34:14-31
# ./node_modules/babel-core/lib/transformation/file/options/build-
configchain.js
This is my webpack config.dll file,
How to go by this issue?
const os = require('os');
const path = require('path');
const webpack = require('webpack');
const HappyPack = require('happypack');
const paths = require('./paths');
const vendors = Object.keys(require(path.join(__dirname, '../package')).dependencies); // eslint-disable-line
let outputPath = path.join(__dirname, '..', 'dev', 'static', 'js');
if (process.env.NODE_ENV === 'production') {
outputPath = path.join(__dirname, '..', 'build', 'static', 'js');
}
const config = {
node: {
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
},
entry: {
vendor: vendors,
},
output: {
path: outputPath,
filename: '[name].dll.js',
library: '[name]_[hash]',
publicPath: '/static/js/',
},
plugins: [
new HappyPack({
loaders: ['babel-loader'],
threads: process.env.BUILD_CPUS ? parseInt(process.env.BUILD_CPUS) : ((os.cpus().length - 1) || 1),
}),
new webpack.DllPlugin({
path: path.join(__dirname, '..', 'config', '[name]-manifest.json'),
name: '[name]_[hash]',
context: path.resolve(__dirname),
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
],
module: {
rules: [
{
test: /\.js$/,
include: path.join(__dirname, '../node_modules/nested-set'),
loader: 'happypack/loader',
},
],
},
resolve: {
modules: ['node_modules'],
extensions: ['.js', '.json'],
alias: {
react: path.join(paths.appNodeModules, '/react'),
'react-dom': path.join(paths.appNodeModules, '/react-dom'),
},
},
};
if (process.env.NODE_ENV === 'production') {
config.plugins.push(new webpack.optimize.UglifyJsPlugin());
}
module.exports = config;
And these are my dependencies,
"devDependencies": {
"add-asset-html-webpack-plugin": "^2.0.1",
"amdefine": "^1.0.1",
"autoprefixer": "6.7.*",
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"babel-loader": "^7.1.1",
"babel-plugin-dynamic-import-webpack": "^1.0.1",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app": "2.2.*",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-runtime": "^6.23.0",
"case-sensitive-paths-webpack-plugin": "^2.1.1",
"chalk": "1.1.*",
"connect-history-api-fallback": "1.3.*",
"cpr": "^2.2.0",
"cross-spawn": "4.0.*",
"css-loader": "0.26.*",
"detect-port": "1.1.*",
"dotenv": "^4.0.0",
"eslint": "3.19.*",
"eslint-config-airbnb": "14.1.*",
"eslint-config-react-app": "0.6.*",
"eslint-loader": "1.6.*",
"eslint-plugin-flowtype": "2.21.*",
"eslint-plugin-import": "2.2.*",
"eslint-plugin-jsx-a11y": "4.0.*",
"eslint-plugin-react": "6.10.*",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "0.10.*",
"fs-extra": "0.30.*",
"happypack": "^4.0.0-beta.1",
"has-flag": "^2.0.0",
"html-webpack-plugin": "^2.29.0",
"http-proxy-middleware": "0.17.*",
"jest": "^20.0.4",
"json-loader": "0.5.*",
"locate-path": "^2.0.0",
"object-assign": "4.1.*",
"postcss-loader": "1.2.*",
"preload-webpack-plugin": "^1.2.2",
"promise": "7.1.*",
"react-dev-utils": "^3.0.2",
"react-error-overlay": "^1.0.9",
"style-loader": "0.13.*",
"url-loader": "0.5.*",
"webpack": "^3.1.0",
"webpack-dev-server": "^2.5.1",
"whatwg-fetch": "2.0.2"
},
"dependencies": {
"ajv": "4.10.0",
"babel-core": "^6.26.0",
"babel-polyfill": "^6.26.0",
"copy-to-clipboard": "3.0.*",
"core-js": "^2.5.1",
"downloadjs": "1.4.*",
"es-abstract": "^1.7.0",
"function-bind": "^1.1.0",
"has": "^1.0.1",
"html2canvas": "^0.5.0-beta4",
"jspdf": "^1.3.5",
"marked": "0.3.3",
"marksy": "5.0.*",
"material-ui": "0.18.*",
"moment": "2.18.1",
"nested-set": "^1.0.0-rc0",
"object.entries": "^1.0.4",
"pdfjs-dist": "^1.9.528",
"prop-types": "15.5.1*",
"react": "15.4.*",
"react-cellblock": "3.0.*",
"react-chatview": "0.2.*",
"react-code-splitting": "^1.1.1",
"react-datepicker": "0.52.0",
"react-dom": "15.4.*",
"react-infinite": "^0.12.1",
"react-intl": "2.3.*",
"react-joyride": "1.11.*",
"react-pdf": "^2.0.0",
"react-redux": "5.0.*",
"react-router": "4.0.*",
"react-router-dom": "4.0.*",
"react-sortable-tree": "^1.5.3",
"react-tap-event-plugin": "2.0.*",
"react-virtualized": "^9.9.0",
"react-visibility-sensor": "^3.10.1",
"redux": "3.6.*",
"striptags": "2.2.1",
"url-parse": "^1.1.9",
"uuid": "^3.1.0"
},
I have tried to fix my errors according to some answers. But I can't solve.
I am using webpack 1.15.0 and babel-loader 6.4.1.
Please help me to parse jsx file.
Here is the error message.
ERROR in ./~/react-file-viewer/src/components/file-viewer.jsx Module
parse failed:
E:\code-bucket\university\node_modules\react-file-viewer\src\components\file-viewer.jsx
Unexpected token (74:6) You may need an appropriate loader to handle
this file type. SyntaxError: Unexpected token (74:6)
Here is my webpack.config.js file.
{
var path = require('path');
module.exports = {
entry: './client/js/app.js',
output: {
path: path.resolve('www/build/js'),
filename: 'app.bundle.js',
pathinfo: false
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react'],
plugins: ['transform-object-rest-spread']
},
exclude: /node_modules/
},
{
test: /\.css$/, // Transform all .css files required somewhere within an entry point...
loaders: ['style-loader', 'css-loader', 'postcss-loader'] // ...with PostCSS
}
]
},
postcss: function() {
return [
require('postcss-import')({ // Import all the css files...
onImport: function (files) {
files.forEach(this.addDependency); // ...and add dependecies from the main.css files to the other css files...
}.bind(this) // ...so they get hot–reloaded when something changes...
}),
require('postcss-simple-vars')(), // ...then replace the variables...
require('postcss-focus')(), // ...add a :focus to ever :hover...
require('autoprefixer')({ // ...and add vendor prefixes...
browsers: ['last 2 versions', 'IE > 8'] // ...supporting the last 2 major browser versions and IE 8 and up...
}),
require('postcss-reporter')({ // This plugin makes sure we get warnings in the console
clearMessages: true
})
];
},
target: "web", // Make web variables accessible to webpack, e.g. window
stats: {
colors: true
},
devtool: 'source-map'
};
}
Here is my package.json file.
{
"name": "university",
"version": "1.0.0",
"description": "Sample Application with React and the Lightning Design System",
"engines": {
"node": "4.2.1"
},
"main": "server.js",
"scripts": {
"webpack": "webpack",
"start": "node server.js",
"bundle": "build-bundle src/apps/ -o dist -v 1.0.0"
},
"author": "Christophe Coenraets <ccoenraets#gmail.com> (http://coenraets.org/)",
"license": "ISC",
"dependencies": {
"autoprefixer": "^7.1.4",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"bcryptjs": "^2.4.3",
"body-parser": "^1.14.2",
"build-bundle": "^2.0.8",
"compression": "^1.6.1",
"connect-busboy": "0.0.2",
"connect-multiparty": "^2.0.0",
"css-loader": "^0.28.7",
"cssnano": "^3.10.0",
"express": "^4.13.4",
"file-loader": "^0.11.2",
"filesize": "^3.5.10",
"fine-uploader": "^5.15.0",
"fontfaceobserver": "^2.0.13",
"fs": "0.0.1-security",
"fs-extra": "^4.0.2",
"gulp": "^3.9.1",
"json2csv": "^3.11.4",
"loader-utils": "^1.1.0",
"method-override": "^2.3.5",
"moment": "^2.11.2",
"multer": "^1.3.0",
"mysql": "^2.14.1",
"object.assign": "^4.0.4",
"path": "^0.12.7",
"pg": "^4.4.4",
"postcss-cssnext": "^3.0.2",
"postcss-focus": "^2.0.0",
"postcss-import": "^11.0.0",
"postcss-loader": "^2.0.6",
"postcss-reporter": "^5.0.0",
"react-addons-css-transition-group": "^15.6.2",
"react-addons-transition-group": "^15.6.2",
"react-animate-height": "^0.10.3",
"react-bootstrap": "^0.31.3",
"react-csv": "^1.0.8",
"react-file-download": "^0.3.5",
"react-file-viewer": "^0.3.48",
"react-fileupload": "^2.4.0",
"react-fine-uploader": "^1.0.7",
"react-html-table-to-excel": "^2.0.0",
"react-redux": "^5.0.6",
"react-router": "^2.0.0-rc5",
"react-strap": "0.0.1",
"react-swipeable-views": "^0.12.8",
"react-transition-group": "^2.2.1",
"react-upload-file": "^2.0.0-beta.6",
"reactstrap": "^4.8.0",
"reactstrap-tether": "^1.3.4",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"sass": "^1.0.0-beta.2",
"style-loader": "^0.18.2",
"write": "^1.0.3",
"write-file": "^1.0.0",
"write-file-p": "^1.0.6"
},
"devDependencies": {
"babel-core": "^6.4.5",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-3": "^6.3.13",
"history": "^1.17.0",
"postcss": "^6.0.12",
"postcss-simple-vars": "^4.1.0",
"react": "^0.14.9",
"react-addons-linked-state-mixin": "^0.14.7",
"react-dom": "^0.14.9",
"react-onclickoutside": "^4.5.0",
"react-router": "^1.0.3",
"webpack": "^1.15.0"
}
}
I have first react app - which is 5.5Mb when using developer mode and devtool: "source-map". Now I tried to write webpack for distribution mode like this:
var webpack = require('webpack');
var path = require('path');
var fs = require('fs')
var BUILD_DIR = path.resolve(__dirname, 'public/javascripts');
var APP_DIR = path.resolve(__dirname, 'frontend');
const babelSettings = JSON.parse(fs.readFileSync(".babelrc"))
var config = {
entry: APP_DIR + '/app.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js',
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
use : ['babel-loader']
}
,
{
test: /\.css$/,
use: ["style-loader",{
loader: "css-loader",
options: {
minimize: true
}
}],
},
{
test: /\.less$/,
use: ["style-loader,less-loader"]
},
{
test: /\.svg$/,
loaders: [
{
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
{
loader: 'react-svg-loader',
query: {
jsx: true
}
}
],
exclude: /node_modules/
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
screw_ie8: true
}
})
]
};
babelSettings.plugins.push("transform-react-inline-elements");
babelSettings.plugins.push("transform-react-constant-elements");
module.exports = config;
I run it with :
webpack --config webpack-dist.config.js -p --progress --production --optimize-minimize
The output is still 2.2Mb which is unacceptable size.What config did I miss?
Edit packages.json looks like this:
{
"name": "project",
"version": "0.1.0",
"private": true,
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"babel-preset-stage-3": "^6.22.0",
"babelify": "^7.3.0",
"browserify": "^13.3.0",
"css-loader": "^0.28.0",
"exorcist": "^0.4.0",
"gulp-livereload": "^3.8.1",
"gulp-sourcemaps": "^2.4.0",
"less": "^2.7.2",
"less-loader": "^2.2.3",
"react-edit-inline": "^1.0.8",
"react-image-fallback": "^4.0.1",
"react-scripts": "0.8.5",
"redux-devtools-extension": "^2.13.0",
"remote-redux-devtools": "^0.5.0",
"riek": "^1.0.7",
"style-loader": "^0.13.1",
"svg-react-loader": "^0.4.0-beta.2",
"vinyl-buffer": "^1.0.0",
"watchify": "^3.8.0",
"webpack": "^2.3.2"
},
"dependencies": {
"axios": "^0.16.0",
"barcoder": "^2.0.1",
"escape-string-regexp": "^1.0.5",
"frisbee": "^1.1.7",
"i18next": "^5.0.0",
"i18next-browser-languagedetector": "^1.0.1",
"i18next-xhr-backend": "^1.3.0",
"immutability-helper": "^2.1.1",
"lodash": "^4.17.4",
"qrcode.react": "^0.6.1",
"rc-time-picker": "^2.3.3",
"react": "^15.4.2",
"react-autosuggest": "^8.0.0",
"react-bootstrap": "^0.30.7",
"react-cards": "^0.2.2",
"react-collapse": "^2.3.3",
"react-cookie": "^1.0.4",
"react-dom": "^15.4.2",
"react-dropzone": "^3.10.0",
"react-height": "^2.2.0",
"react-highlight-words": "^0.6.0",
"react-i18next": "^2.0.0",
"react-infinite-scroller": "^1.0.4",
"react-input-autosize": "^1.1.0",
"react-isolated-scroll": "^0.1.0",
"react-loaders": "^2.3.0",
"react-motion": "^0.4.7",
"react-number-input": "^15.0.0-rc2",
"react-numeric-input": "^2.0.7",
"react-popover": "^0.4.4",
"react-preload": "^0.5.0",
"react-redux": "^5.0.2",
"react-redux-form": "^1.5.4",
"react-router-dom": "^4.0.0",
"react-select": "^1.0.0-rc.3",
"react-svg-loader": "^1.1.1",
"react-switch-button": "^2.1.1",
"react-tabs": "^0.8.2",
"react-tagsinput": "^3.14.0",
"redux": "^3.6.0",
"redux-thunk": "^2.2.0",
"simple-react-pdf": "^1.0.6",
"style-it": "^1.5.5",
"throttle-debounce": "^1.0.1",
"validator": "^6.2.1"
},
Use the excellent Webpack Bundle Analyzer to look at what is taking up space in your build.
Also, try setting NODE_ENV=production before you run webpack, it makes a difference.
What decreased bundle size for me (with Webpack 4) is adding
module.exports = {
mode: 'production', // ← this
...
}
(inside webpack.config.js). From almost 1 MB to 158 kB.
Trying to compile my code with the webpack loader which i have set to ignore node_modules and have not been able to bypass the error below. I've tried clearing out my node_modules and reinstalling as well as adding and removing packages but have been unable to clear the block:
/home/mgarf/Documents/testApp/node_modules/babel-plugin- transform-react-jsx/lib/index.js:16
var visitor = require("babel-helper-builder-react-jsx")({ /*istanbul ignore next*/
^
TypeError: require(...) is not a function
at exports.default (index.js:14:17)
at Function.memoisePluginContainer (/home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:134:13)
at Function.normalisePlugin (/home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:168:32)
at /home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:208:30
at Array.map (native)
at Function.normalisePlugins (/home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:180:20)
at OptionManager.mergeOptions (/home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:300:36)
at /home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:371:14
at /home/mgarf/Documents/testApp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:392:24
at Array.map (native)
Here is my webpackconfig snippet.
var webpack = require('webpack');
require('style-loader');
require('css-loader');
require('babel-register')({
ignore: /\.css$/,
});
...
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
plugins: [
['react-transform', {
transforms: [
{
transform: 'react-transform-hmr',
imports: ['react'],
locals: ['module']
}, {
transform: 'react-transform-catch-errors',
imports: ['react', 'redbox-react']
}
]
}]
]
}
},
{ test: /\.css$/, exclude: /node_modules/, loader: "style-loader!css-loader" }
]
}
and package.json:
"dependencies": {
"async": "^1.5.2",
"bcrypt-nodejs": "^0.0.3",
"body-parser": "^1.15.1",
"bookshelf": "^0.10.0",
"compression": "^1.6.2",
"cookie-parser": "^1.4.1",
"dotenv": "^2.0.0",
"express": "^4.13.4",
"express-validator": "^2.20.4",
"jsonwebtoken": "^5.7.0",
"knex": "^0.11.7",
"moment": "^2.12.0",
"morgan": "^1.7.0",
"mysql": "^2.10.2",
"nodemailer": "^2.3.0",
"nunjucks": "^2.4.1",
"react": "^15.2.1",
"react-cookie": "^0.4.5",
"react-dom": "^15.2.1",
"react-redux": "^4.4.5",
"react-router": "^2.4.0",
"react-transform-catch-errors": "^1.0.2",
"react-transform-hmr": "^1.0.4",
"redbox-react": "^1.2.4",
"redux": "^3.3.1",
"redux-thunk": "^2.0.1",
"request": "^2.69.0",
"webpack": "^1.12.14",
"webpack-dev-middleware": "^1.6.1",
"webpack-hot-middleware": "^2.10.0",
"whatwg-fetch": "^0.11.0"
},
"devDependencies": {
"babel-core": "^6.7.2",
"babel-loader": "^6.2.4",
"babel-plugin-react-transform": "^2.0.2",
"babel-polyfill": "^6.7.2",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-plugin-react-transform": "^2.0.0-beta1",
"babel-plugin-rewire": "^1.0.0-rc-3",
"babel-plugin-transform-export-extensions": "^6.8.0",
"babel-register": "^6.8.0",
"chai": "^3.5.0",
"css-loader": "^0.23.1",
"enzyme": "^2.3.0",
"fetch-mock": "3.0.2",
"mocha": "^2.4.5",
"react-addons-test-utils": "^15.0.2",
"react-date-picker": "^5.3.21",
"redux-mock-store": "^1.0.2",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0",
"style-loader": "^0.13.1",
"supertest": "^1.2.0"
},
"engines": {
"node": "6.1.0"
}
}