vite-plugin-federation icon indicating copy to clipboard operation
vite-plugin-federation copied to clipboard

Uncaught ReferenceError: __rf_placeholder__shareScope is not defined

Open bramdeba opened this issue 1 year ago • 12 comments

Versions

  • vite-plugin-federation: 1.2.2
  • vite: 4.0.1

Reproduction

https://github.com/bramdeba/vite-plugin-federation-issue

Additional Details
image

Steps to reproduce

  • git clone https://github.com/bramdeba/vite-plugin-federation-issue
  • navigate to project directory with terminal
  • run yarn to install dependencies
  • run yarn serve:host and yarn serve:remote simultaneously
  • navigate to http://localhost:4300

What is Expected?

Expected is that you would see a -component, imported from the remote module.

What is actually happening?

Nothing is rendered, and you get an error in the console:

Uncaught ReferenceError: __rf_placeholder__shareScope is not defined
    at Ki (index-38bc938c.js:40:58371)
    at index-38bc938c.js:40:58803
Ki @ index-38bc938c.js:40
(anonymous) @ index-38bc938c.js:40

bramdeba avatar Apr 08 '23 14:04 bramdeba

In case you want a workaround put the following in index.html <script> const __rf_placeholder__shareScope={} </script>

salindae25 avatar Apr 08 '23 18:04 salindae25

Same issue here

ureciocais avatar Apr 18 '23 13:04 ureciocais

same here

oychao avatar Apr 25 '23 08:04 oychao

I have 1 remote and 2 hosts, this issue only happens on one of the hosts. I have same versions and configs.

this part of the generated code is missing for the host where I receive the error:

function get(name, remoteFrom) {
                    return __federation_import(name).then(module => () => {
                        if (remoteFrom === 'webpack') {
                            return Object.prototype.toString.call(module).indexOf('Module') > -1 && module.default ? module.default : module
                        }
                        return module
                    })
                }

                const wrapShareModule = remoteFrom => {
                    return {
                        'react':{'18.2.0':{get:()=>get('./__federation_shared_react.js', remoteFrom), loaded:1}}
                    }
                };

Instead I have this:

const wrapShareModule = remoteFrom => {
                    return {
                        __rf_placeholder__shareScope
                    }
                };

error:

dex-b09da57a.js:65912 Uncaught ReferenceError: __rf_placeholder__shareScope is not defined
    at wrapShareModule (index-b09da57a.js:65912:25)
    at index-b09da57a.js:65939:64

mossen avatar May 04 '23 04:05 mossen

Resolved it!

Seems this is related to the tsconfig.json where baseUrl is not matching the paths.

{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "baseUrl": "./src",
    "paths": {
      "src/*": ["*"],
    },
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

mossen avatar May 04 '23 04:05 mossen

I have the same problem. The problem I found was in the virtual module.

The plugin uses === when resolving virtual module file names.

In fact, it is possible for a project to have a module name with an absolute path to the project that does not match, so there is no text substitution.

see link

He110te4m avatar May 08 '23 08:05 He110te4m

@bramdeba remove the viteTsConfigPaths({ root: '../../' }) in your host application and you should be fine.

https://github.com/bramdeba/vite-plugin-federation-issue/blob/3d3ce8563f9be5d0b9e665f507fa8fb766ada8df/apps/host/vite.config.ts#L22C5-L24C8

stefan-karlsson avatar Jun 26 '23 21:06 stefan-karlsson

@bramdeba remove the viteTsConfigPaths({ root: '../../' }) in your host application and you should be fine.

https://github.com/bramdeba/vite-plugin-federation-issue/blob/3d3ce8563f9be5d0b9e665f507fa8fb766ada8df/apps/host/vite.config.ts#L22C5-L24C8

That is not an option in my case, because the build fails with

vite v4.3.9 building for development...
✓ 21 modules transformed.
✓ built in 472ms
[originjs:federation] Cannot read properties of undefined (reading 'config')
file: virtual:__federation__

 >  NX   Cannot read properties of undefined (reading 'config')

nemonemi avatar Jul 18 '23 08:07 nemonemi

Resolved it!

Seems this is related to the tsconfig.json where baseUrl is not matching the paths.

{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "baseUrl": "./src",
    "paths": {
      "src/*": ["*"],
    },
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

Thanks!!

marcin-kopanski avatar Oct 04 '23 14:10 marcin-kopanski

Any solution besides this? I use this plugin on monorepo setup so I cannot remove viteTsConfigPaths configuration from my vite

@bramdeba remove the viteTsConfigPaths({ root: '../../' }) in your host application and you should be fine.

https://github.com/bramdeba/vite-plugin-federation-issue/blob/3d3ce8563f9be5d0b9e665f507fa8fb766ada8df/apps/host/vite.config.ts#L22C5-L24C8

lewisloofis avatar Dec 11 '23 10:12 lewisloofis

Resolved it!

Seems this is related to the tsconfig.json where baseUrl is not matching the paths.

{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "baseUrl": "./src",
    "paths": {
      "src/*": ["*"],
    },
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

Thanks!

marcin-kopanski avatar Mar 05 '24 11:03 marcin-kopanski

image image

federation({ name: 'host-app', remotes: { "remote-xxx":"http://localhost:5011/assets/remoteEntry.js", }, shared: ['vue', 'pinia'] }) 我遇到的情况是 remotes 必须要有值,才会被当成宿主app,当实际上我只需要动态加载,remotes不需要赋值 🙅🏻‍♀️ -- The situation I encountered is that remotes must have a value before they can be regarded as the host app. In fact, I only need to load dynamically, and remotes do not need to be assigned a value.

LomyCool avatar Mar 28 '24 07:03 LomyCool