0

I am going to implement code splitting with server side rendering. For that i am using react-loadable.

But, i am getting following error while using import.

I show other posts but, nothing work for me.

You may need an appropriate loader to handle this file type. | var Content = (0, _reactLoadable.default)({ | loader: function loader() { return import('./components/Content'); | },

My code looks like:

import Loadable from 'react-loadable';

const Content = Loadable({
  loader: () => import('./components/Content'),
  loading: Loading
});

package.json

"dependencies": {
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "acorn-dynamic-import": "^4.0.0",
    "express": "^4.16.4",
    "isomorphic-fetch": "^2.2.1",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-loadable": "^5.5.0",
    "react-redux": "^5.1.1",
    "react-router": "^4.3.1",
    "react-router-config": "^1.0.0-beta.4",
    "react-router-dom": "^4.3.1",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.2.0",
    "@babel/preset-env": "^7.3.1",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^24.0.0",
    "babel-loader": "^7.1.5",
    "css-loader": "^1.0.1",
    "cypress": "^3.1.3",
    "enzyme": "^3.8.0",
    "enzyme-adapter-react-16": "^1.7.1",
    "enzyme-to-json": "^3.3.5",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "fetch-mock": "^7.2.5",
    "jest": "^24.0.0",
    "jest-fetch-mock": "^2.0.1",
    "json-loader": "^0.5.7",
    "nodemon": "^1.18.9",
    "npm-run-all": "^4.1.5",
    "open": "0.0.5",
    "redux-devtools": "^3.4.2",
    "redux-mock-store": "^1.5.3",
    "style-loader": "^0.23.1",
    "uglifyjs-webpack-plugin": "^2.0.1",
    "webpack": "^4.26.1",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.14",
    "webpack-node-externals": "^1.7.2"
  },
  "babel": {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ],
    "plugins": [
      "@babel/plugin-transform-runtime",
      "@babel/plugin-proposal-class-properties",
      "react-loadable/babel",
      "@babel/plugin-syntax-dynamic-import"
    ]
  }

webpack.config.js

return {
        context: path.join(__dirname, "src"),
        entry: './index.js',
        mode: isProduction ? "production" : "development",
        devtool: isProduction ? "none" : "source-map",

        resolve: {
            extensions: [".js", ".jsx"]
        },

        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    loader: "babel-loader",
                    exclude: /node_modules/,
                    options: {
                        presets: ['@babel/preset-env', '@babel/preset-react'] 
                    }
                },
                {
                    test: /\.(ttf|eot|svg|woff|png|jpg)$/,
                    loader: "file-loader",
                    options: {
                        name: "[path][name].[ext]?[hash]"
                    }
                },
                {
                    test: /\.css$/,
                    use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
                }
            ]
        },

        optimization: isProduction ? {
            minimizer: [
              new UglifyJsPlugin({
                cache: true,
                parallel: true,
                sourceMap: true
              }),
              new OptimizeCSSAssetsPlugin({})
            ]
          } : {},

        devServer: {
            contentBase: path.resolve(__dirname, "dist"),
            historyApiFallback: true,
        },

        output: {
            filename: 'bundle.js',
            path: path.resolve(__dirname, "dist"),
            publicPath: "/",
        }
    };
4

1 回答 1

1

我已经用谷歌搜索了react loadable babel 7,我发现的是这个拉取请求,它仍然是开放的:https ://github.com/jamiebuilds/react-loadable/pull/151 。很可能该库目前没有积极的支持。

好消息是,您使用React v16.7的已经支持延迟加载。结帐文档:https ://reactjs.org/docs/code-splitting.html#reactlazy 。它不需要任何额外的babel预设或插件 - 您应该可以使用已有的设置来使用它。

于 2019-01-29T20:15:25.323 回答