0
votes

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: "/",
        }
    };
1
Can you provide your webpack config?ReasonX7
@ReasonX7 I edited my question.ketan
import() is not supported on Node.js yetᆼᆺᆼ
I can see you've installed @babel/preset-env & @babel/preset-react presets, but they're not used in your config. Try to add: { options: { presets: ['@babel/preset-env', '@babel/preset-react'] } }ReasonX7
Did you see base usage example for babel-loader? github.com/babel/babel-loader#usageReasonX7

1 Answers

1
votes

I've googled for react loadable babel 7 and what I found is this pull request, which is still open: https://github.com/jamiebuilds/react-loadable/pull/151. Most likely the library doesn't have active support at the moment.

The good news, is that you use React v16.7 that already supports lazy loading. Checkout docs: https://reactjs.org/docs/code-splitting.html#reactlazy. It doesn't require any additional babel presets or plugins - you should be find to use it with settings that you already have.