Ad
Unexpected Token When Lazy Loading Components
I'm attempting to refactor a growing React App to use lazy loading. Taking the following as an example:
import React, { Component } from 'react'
import { render } from 'react-dom'
import Loadable from 'react-loadable';
const Orders = Loadable({
loader: () => import('./Orders'),
loading() {
return <div>Loading...</div>
}
});
My webpack compile always fails with:
Module build failed: SyntaxError: Unexpected token
...
> 24 | loader: () => import('./Orders'),
It's clearly the import that is choking the code, but I don't understand why.
My .babelrc
file looks like this:
{
"presets": ["env", "react"]
}
Ad
Answer
So following up on T.J. Crowder's comment to my original question I found the babel dynamic import plugin
Installing this with yarn:
yarn add babel-plugin-syntax-dynamic-import -dev
Then adding it to my .babelrc, thus:
{
"presets": ["env", "react"],
"plugins": ["syntax-dynamic-import"]
}
fixed the Unexpected token
issue.
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad