question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@loadable/babel-plugin doesn't work with declared functions

See original GitHub issue

🐛 Bug Report

I have a case where I need to pass a loader function to a @loadable/component and the @loadable/babel-plugin do not work as expected. The function passed as an argument, won’t be capture by the babel plugin, making the InnerComponent fail on SSR with a

SSR requires @loadable/babel, please install it

To Reproduce

Running the following will produce the above error when trying to render on server-side:

// react-router v4 route.js file

const loadBuses = () =>
  import(/* webpackChunkName: "buses" */ './components/BusesContainer/BusesContainer.page');

export default [
  {
    path: '/',
    exact: true,
    component: asyncComponent({
      loader: loadBuses
    })
  }];
// asyncComponent.js
export function asyncComponent({ loader }) {
  const LoadableComponent = loadable(loader);

  const AsyncRouteComponent = props => {
    return <LoadableComponent {...props} />;
  };

  return AsyncRouteComponent;
}

After running babel with the @loadable/babel-plugin outputs:

var loadBuses = function loadBuses() {
  return import(
  /* webpackChunkName: "buses" */
  './components/BusesContainer/BusesContainer.page');
};

Expected behavior

I’ll expect that the @loadable/babel-plugin transpiles the loader function with the right methods so It can be used by @loadable/component:

var loadBuses = function loadBuses() {
  return {
      chunkName: function chunkName() {
        return "buses";
      },
      isReady: function isReady(props) {
        if (typeof __webpack_modules__ !== 'undefined') {
          return !!__webpack_modules__[this.resolve(props)];
        }

        return false;
      },
      requireAsync: function requireAsync() {
        return import(
        /* webpackChunkName: "buses" */
        './components/BusesContainer/BusesContainer.page');
      },
      requireSync: function requireSync(props) {
        var id = this.resolve(props);

        if (typeof __webpack_require__ !== 'undefined') {
          return __webpack_require__(id);
        }

        return eval('module.require')(id);
      },
      resolve: function resolve() {
        if (require.resolveWeak) {
          return require.resolveWeak("./components/BusesContainer/BusesContainer.page");
        }

        return require('path').resolve(__dirname, "./components/BusesContainer/BusesContainer.page");
      }
    }

Run npx envinfo --system --binaries --npmPackages @loadable/component,@loadable/server,@loadable/webpack-plugin,@loadable/babel-plugin --markdown --clipboard

## System:
 - OS: macOS 10.14.1
 - CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
 - Memory: 555.12 MB / 16.00 GB
 - Shell: 5.3 - /bin/zsh
## Binaries:
 - Node: 8.10.0 - /usr/local/bin/node
 - Yarn: 1.12.3 - /usr/local/bin/yarn
 - npm: 6.4.1 - ~/.npm-packages/bin/npm
## npmPackages:
 - @loadable/babel-plugin: ^5.2.2 => 5.2.2
 - @loadable/component: ^5.2.2 => 5.2.2
 - @loadable/server: ^5.2.2 => 5.2.2
 - @loadable/webpack-plugin: ^5.2.2 => 5.2.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gregbergecommented, Feb 5, 2019

Done in #192

0reactions
lfantonecommented, Feb 6, 2019

Amazing!! 🎊

Read more comments on GitHub >

github_iconTop Results From Across the Web

loadable component require not found with webpack
I am trying to learn how to do code-splitting through dynamic imports, but unfortunately I am getting an error as follows:
Read more >
babel/plugin-syntax-dynamic-import
Working with Webpack and @babel/preset-env. Currently, @babel/preset-env is unaware that using import() with Webpack relies on Promise internally.
Read more >
Babel plugin - Loadable Components
Install the babel plugin first: npm install --save-dev @loadable/babel-plugin ... The load function is not detected, you have to name it loadable ....
Read more >
rollup.js
Load the module code, but don't make any new objects available. import './module.js'; ... Export a value that has been previously declared:
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
Initial familiarity with ES2015 modules doesn't hurt, either. ... Install Rollup's Babel plugin. npm install --save-dev rollup-plugin-babel # Install the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found