Как правильно настроить динамический импорт для роутов?

Пробую использовать динамический импорт для компонентов роутинга в приложении react, была ошибка о том, что import undefined token, установил @babel/plugin-syntax-dynamic-import, заменил es-lint paser на babel-eslint, теперь линтер не ругается на return import(), но проблема с импортом не решается

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


Вот что говорит webpack
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.


webpack
{
        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'
          ]
        }
      },
  • Вопрос задан
  • 141 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы