Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodeRuntimePlugin is not usable outside webpack bundles #2968

Open
5 tasks done
MadaraUchiha-314 opened this issue Sep 19, 2024 · 7 comments
Open
5 tasks done

nodeRuntimePlugin is not usable outside webpack bundles #2968

MadaraUchiha-314 opened this issue Sep 19, 2024 · 7 comments

Comments

@MadaraUchiha-314
Copy link
Contributor

Describe the bug

I am using Module Federation Runtime + nodeRuntimePlugin from @module-federation/node/runtimePlugin as provided in the example here: https://module-federation.io/plugin/plugins/index.html#configuration

I am getting the following error:

ReferenceError: [ Federation Runtime ]: __webpack_require__ is not defined
    at <anonymous> (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/node_modules/@module-federation/node/src/runtimePlugin.ts:234:9)
    at beforeInit (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/node_modules/@module-federation/node/src/runtimePlugin.ts:337:9)
    at SyncWaterfallHook.emit (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/node_modules/@module-federation/runtime/dist/index.cjs.js:448:34)
    at FederationHost.formatOptions (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/node_modules/@module-federation/runtime/dist/index.cjs.js:1925:108)
    at new FederationHost (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/node_modules/@module-federation/runtime/dist/index.cjs.js:2016:29)
    at init (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/node_modules/@module-federation/runtime/dist/index.cjs.js:2027:30)
    at run (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/src/index.ts:9:3)
    at nodeRuntimePlugin (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/src/index.ts:32:1)
    at Object.<anonymous> (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/src/index.ts:32:5)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);

This is because nodeRuntimePlugin references __webpack_require__ and my bundle is not generated using webpack.

This needs to be fixed as it make the nodeRuntimePlugin unusable outside of webpack.

Reproduction

https://github.com/MadaraUchiha-314/nailsmith

Used Package Manager

npm

System Info

NA

Validations

@MadaraUchiha-314
Copy link
Contributor Author

@ScriptedAlchemy
Copy link
Member

This relies on the chunks having been built by webpack in the first place. We patch the chunk loader so it still needs to be a runtime encoded to loading webpack chunk formats

@ScriptedAlchemy
Copy link
Member

how node federation plugin works is:

const chunkMap = {
chunkID: 'chunkPath'
}

webpack_require.f.readFile = (chunkID)=>{
installChunkModules(fs.readFile(chunkMap[chunkId]))
}

We depend on webpacks runtime to augment the filesystem

@MadaraUchiha-314
Copy link
Contributor Author

We depend on webpacks runtime to augment the filesystem

I am curious whether we can use this or not: https://nodejs.org/api/module.html#hooks

I will write a quick POC and get back.

@MadaraUchiha-314
Copy link
Contributor Author

@ScriptedAlchemy I updated my minimal example to use webpack but it still fails

https://github.com/MadaraUchiha-314/nailsmith

Error:

shell-remote-entry.js:6130
/******/ 		execOptions.factory.call(module.exports, module, module.exports, execOptions.require);
         		                    ^

TypeError: Cannot read properties of undefined (reading 'call')
    at __webpack_require__ (shell-remote-entry.js:6130:32)
    at fn (shell-remote-entry.js:6314:21)
    at shell-remote-entry.js:5782:186
    at /Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/dist/index.js:379:29
    at Module.get (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/dist/index.js:358:37)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RemoteHandler.loadRemote (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/dist/index.js:1603:37)
    at async run (/Users/rohithr31/code/github.com/MadaraUchiha-314/nailsmith/packages/bff/dist/index.js:28929:20)

@MadaraUchiha-314
Copy link
Contributor Author

I was able to move past the above bug. I'll talk about the bug and the fix later. It's an interesting one.

But where we are stuck at is when we are calling revalidate() API to flush out the cache for remotes.

The problem is that that code taps into webpack + federation internals:

 __webpack_require__.federation.instance.moduleCache.clear();

https://github.com/module-federation/core/blob/main/packages/node/src/utils/hot-reload.ts#L56

@ScriptedAlchemy Is there a way to implement this module cache clear without tapping into webpack internals and rather use the globalThis.__FEDERATION__. __INSTANCES__ or something like that ?

@ScriptedAlchemy
Copy link
Member

Send pr. Sure we can adjust to use global.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants