loadable-components icon indicating copy to clipboard operation
loadable-components copied to clipboard

Invariant Violation: loadable: SSR requires `@loadable/babel-plugin`, please install it

Open zhenliliu opened this issue 7 years ago • 30 comments

🐛 Bug Report

I have configed '@loadable/babel-plugin' in babel.config.js, but when i use SSR to render react file ,then i got this error message image

this is my config file image

and this is SSR image

this is client side route file image

i want to get some help and to fix it

zhenliliu avatar Nov 27 '19 15:11 zhenliliu

Try to hoist loadable plugin. I reckon plugin-syntax-dynamic-import "eats" all imports before loadable plugin.

theKashey avatar Nov 27 '19 21:11 theKashey

Try to hoist loadable plugin. I reckon plugin-syntax-dynamic-import "eats" all imports before loadable plugin.

i have try do it, but it still not working

zhenliliu avatar Nov 28 '19 02:11 zhenliliu

I have similar trouble

KleinMaximus avatar Nov 29 '19 09:11 KleinMaximus

I've double-checked the babel plugin code, and it seems to be legitly not working - the problem is "classical" - https://jamie.build/babel-plugin-ordering.html - different plugins are conflicting with each other. It's easy to fix it, as explained in the linked page, but it would make loadable plugin slightly slower. However, we don't have a choice.

theKashey avatar Nov 29 '19 21:11 theKashey

I've double-checked the babel plugin code, and it seems to be legitly not working - the problem is "classical" - https://jamie.build/babel-plugin-ordering.html - different plugins are conflicting with each other. It's easy to fix it, as explained in the linked page, but it would make loadable plugin slightly slower. However, we don't have a choice.

but How to fix this problem

zhenliliu avatar Dec 08 '19 12:12 zhenliliu

I have similar trouble, SSR requires @loadable/babel-plugin, please install it

lovewinders avatar Dec 18 '19 02:12 lovewinders

@lovewinders - trouble is with @loadable/babel-plugin (however you are correct, it's required)

theKashey avatar Dec 18 '19 02:12 theKashey

@lovewinders - trouble is with @loadable/babel-plugin (however you are correct, it's required)

my question issue is https://github.com/gregberge/loadable-components/issues/484

lovewinders avatar Dec 18 '19 03:12 lovewinders

@theKashey any update on how to solve this? I am facing the same issue.

shad-k avatar Dec 25 '19 14:12 shad-k

Update: you can't solve it, there is a really small change to be made to the babel plugin, but I can't find enough time to tackle it. However, I could do the change, open a PR, and let you test it, so we would share a feature development burden.

theKashey avatar Dec 25 '19 22:12 theKashey

@theKashey sure I can test it. Let me know once you've made a PR.

shad-k avatar Dec 26 '19 05:12 shad-k

The "fix" is really super simple, see PR. It would be great if any of you would just copy-paste this gist to node_modules/@loadable/babel-plugin/lib/index.js and check if it works. 99% that it is.

theKashey avatar Jan 02 '20 21:01 theKashey

Hi @theKashey, Thank you for your PR. I tried your fix solution, but it seems the error is still there. BTW, I think your gist is meant to replace node_modules/@loadable/babel-plugin/lib/index.js.

1846689910 avatar Jan 03 '20 00:01 1846689910

:( Just to double check - could you edit a file with loadable to burst possible compilation cache. If it will would not work - then I would gently ask someone for an example to find the real root cause.

theKashey avatar Jan 03 '20 02:01 theKashey

I have the same issue while wrap @loadable-component as a function call, like so:

// Loadable component
import React from 'react'
import loadable from '@loadable/component'

export default loader => loadable(loader, { fallback: <p> Loading...</p> })

and call it from routes definition:

// routes.js
import Loadable from 'components/Loadable'

const Index = Loadable(() => import('@/pages/Index'))

after remove this wrap and import @loadbale/component directly, errors was gone:

// routes.js
import loadable from '@loadbale/component'

const Index = loadable(() => import('@/pages/Index'))

Hope to be helpful :)

chen86860 avatar Apr 01 '20 07:04 chen86860

Hi, I had the similar problem. Im my case i wasn't using .bablerc. I have configured bable in webpack. webpack.config.[evn].js

module.exports = {
  mode: 'production',
  .
  .
  .
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: require.resolve('babel-loader'),
            options: {
              babelrc: false,
              plugins: ['@loadable/babel-plugin'],
            },
          },
     }
   ]
}]}
}

smsivaprakaash avatar Jun 04 '20 12:06 smsivaprakaash

The "fix" is really super simple, see PR. It would be great if any of you would just copy-paste this gist to node_modules/@loadable/babel-plugin/lib/index.js and check if it works. 99% that it is.

Hi @theKashey I have same issue and i copy-paste it but it not helped me , what can i do for solving it ?

tsconfig

{
  "compilerOptions": {
    "module": "commonjs", // for Node and export
    "target": "es2015", // version of export file it can be any version
    "outDir": "lib", // exported in the folder
    "declaration": true, // add .d.ts file in outDir
    "sourceMap": true, // add .map file outDir  . debugger  forbid implicit any
    "jsx": "react", // transform JSX
    "strict": true, //enable strict  features
    "strictNullChecks": true, //check and compile JS
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true, // forexample for testing jest
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": false, //for casing erro in the route file
    "skipLibCheck": true,
    "lib": ["es6", "dom"],
    "noImplicitReturns": true,
    "noEmit": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    // "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "isolatedModules": false,
    "rootDir": ".",
    "allowJs": true,
    "plugins": [
      { "transform": "typescript-transform-paths" },
      { "transform": "typescript-transform-paths", "afterDeclarations": true }
    ]
  },
  "include": ["./src/**/*", "./server", "./scripts"],
  "exclude": ["node_modules"]
}

Routes.tsx

const MainAuth = loadable(() => import('../Pages/auth/main/index'), {
	fallback: <div>...loading</div>
});

babel.config

  plugins: [
    '@loadable/babel-plugin', 
             ]

server/render

	const extractor = new ChunkExtractor({ statsFile })
	// console.log('extractor', extractor)
	const sheet = new ServerStyleSheet();
	const context = {};
	const extractorNew = extractor.collectChunks(sheet.collectStyles(
		<Provider store={store}>
			<StyleSheetManager sheet={sheet.instance} >
				<StaticRouter location={req.path} context={context}>
					<div>{renderRoutes(Routes)}</div>
				</StaticRouter>
			</StyleSheetManager>
		</Provider>
	))
	const content = renderToString(
		extractorNew
	);
       ....

tmohammad78 avatar Jul 03 '20 07:07 tmohammad78

i have this issue only when i create my own custom loadable function (per the docs):

import baseLoadable from '@loadable/component';

export const loadable = (importCb, options) => {
	return baseLoadable(
		() => importCb().then(module => {
                       // do some stuff
                       return module;
                 }),
		options
	);
}

using the library loadable function works

suhanw avatar Aug 05 '20 11:08 suhanw

@suhanw @chen86860 Hi! I faced the same problem as you and managed to solve it via "magic-comments". If you still want to use your own custom functions you can try to use this approach too: https://loadable-components.com/docs/babel-plugin/#magic-comments

For example, based on @suhanw comment, try to call your custom loadable function like this: loadable(/* #__LOADABLE__ */ () => import ('./someModule'), {options})

EgorVasilev avatar Sep 08 '20 11:09 EgorVasilev

I ran into this problem as well. We have a wrapper around loadable that we also call loadable so that the Babel plugin can recognise it (as per the docs), but it still didn't work and adding magic comments didn't work either.

After a while I realised we also have a lazyLoadOnVisible(() => import('...'), options) which also wraps loadable and this was the source of the problem. Adding the magic comment to those calls solved it!

So for us it's working as documented. The wrapper we call loadable works without magic comments, the wrapper that is called something else needs the magic comments to work.

So for anyone else finding this issue:

  • The babel plugin converts () => import('...') to an object with some metadata
  • If the base loadable() function gets a function instead of an object, this error happens
  • Make sure you have added the Babel plugin (and possibly check plugin ordering)
  • Make sure the plugin can recognise which () => import('...') to convert either by:
    • Wrap it in any function called loadable() OR
    • Preceded by /* #__LOADABLE__ */

Ephem avatar Mar 04 '21 11:03 Ephem