Answer the question
In order to leave comments, you need to log in
How to correctly set up dynamic import for routes?
I'm trying to use dynamic import for routing components in a react application, there was an error that import undefined token, installed @babel/plugin-syntax-dynamic-import, replaced es-lint paser with babel-eslint, now the linter does not swear at return import (), but the problem with import is not solved
import React, { lazy } from 'react'
import { Switch, Route } from 'react-router-dom'
// import Main from 'Containers/Main'
// import Donation from 'Containers/Donation'
// import DonationDetailsContainer from 'Containers/DonationDetails'
// import Page404 from 'Containers/404'
const Main = lazy(() => import('Containers/Main'))
const Donation = lazy(() => import('Containers/Donation'))
const DonationDetails = lazy(() => import('Containers/DonationDetails'))
const Page404 = lazy(() => import('Containers/404'))
const Routing = () => (
<Switch>
<Route exact path={'/'} component={Main} />
<Route exact path={'/donation'} component={Donation} />
<Route exact path={'/donation/needy'} component={Donation} />
<Route exact path={'/donation/foundations'} component={Donation} />
<Route exact path={'/donation/needy/:id'} component={DonationDetails} />
<Route exact path={'/donation/foundations/:id'} component={DonationDetails} />
<Route path='*' component={Page404} name='404' />
</Switch>
)
export default Routing
i 「wdm」: Compiling...
× 「wdm」:
ERROR in ./source/scripts/routing/index.js 13:9
Module parse failed: Unexpected token (13:9)
You may need an appropriate loader to handle this file type.
|
| var Main = lazy(function () {
> return import('Containers/Main');
| });
| var Donation = lazy(function () {
@ ./source/scripts/index.jsx 16:0-32 27:25-32
@ multi (webpack)-dev-server/client?http://localhost:3000 (webpack)/hot/dev-server.js ./source/scripts/index.jsx
i 「wdm」: Failed to compile.
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
emitError: true,
failOnError: true
}
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
include: path.resolve('./source/scripts'),
loader: 'babel-loader',
query: {
cacheDirectory: true,
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-runtime'
]
}
},
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question