I am using laravel for backend and ReactJS for the frontend. I tried to use the asyncComponent method for loading components when needed, to decrease load time. The issue is that I am getting an error in dynamic import below:
const asyncCheckout = asyncComponent(() => {
return import('./containers/Checkout/Checkout');
});
I am getting error Unexpected token error on import keyword. I tried installing babel-plugin-syntax-dynamic-import
but it still gives Unexpected token error. I am new to React and I would appreciate any help that can point me in the right direction. My .babelrc file has latest and react and env as presets. Thanks you.
My .babelrc file that is in project root:
{
"presets": [
"env"
],
"plugins": ["transform-class-properties"]
}
Web pack config, from laravel mix:
/**
* As our first step, we'll pull in the user's webpack.mix.js
* file. Based on what the user requests in that file,
* a generic config object will be constructed for us.
*/
require('../src/index');
require(Mix.paths.mix());
/**
* Just in case the user needs to hook into this point
* in the build process, we'll make an announcement.
*/
Mix.dispatch('init', Mix);
/**
* Now that we know which build tasks are required by the
* user, we can dynamically create a configuration object
* for Webpack. And that's all there is to it. Simple!
*/
let WebpackConfig = require('../src/builder/WebpackConfig');
module.exports = new WebpackConfig().build();
And my package.json file:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.17",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-preset-react": "^6.23.0",
"bootstrap-sass": "^3.3.7",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"dependencies": {
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"react-bootstrap": "^0.31.5",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-social-login": "^3.4.2",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"watch": "^1.0.2"
}
}