repack icon indicating copy to clipboard operation
repack copied to clipboard

How to config repack to run index.js code when run ScriptManager.loadScript in native side

Open trinhhcse opened this issue 1 year ago • 1 comments

Ask your Question

Hi Team. I try to run miniapp chunk:

  • With repack v2: Call ChunkManager.loadChunk (Run in Native Side) -> The code in index.js of miniapp will evaluate
  • With repack v3 . ScriptManager.loadScript (Run in Native Side) -> The code in index.js of miniapp not evaluate (Run in Super app showcase) Resource link:
  • V3 webpack config: https://github.com/callstack/super-app-showcase/blob/main/packages/dashboard/webpack.config.mjs
  • How to config repack to run index.js code of the miniapp when running ScriptManager.loadScript in native side from v3 (Webpack config, Bootstrap script, Plugin)?

Thanks team.

trinhhcse avatar Apr 11 '24 04:04 trinhhcse

@trinhhcse i'm a little confused about your setup - what are you trying to achieve? ScriptManager.loadScript should be used to load either ModuleFederation containers or other chunks obtained via splitChunksPlugin.

jbroma avatar Apr 15 '24 14:04 jbroma

Thanks for your response.

I want to load code in index.js file of miniapp project when using ScriptManager.loadScript to execute miniapp.bundle from native without run Federated.importModule('MiniAppChunkID', './index.js'),

webpack config miniapp
    new Repack.plugins.ModuleFederationPlugin({
    name: 'MiniAppChunkID',
    exposes: {
    
      'MiniAppChunkID': './index.js',
    },
    }

Im try

async function repackScriptStartup() {
      repackRuntime.shared.loadScriptCallback.push([$chunkId$]);
      let chunkId = $chunkId$;
      if (chunkId != 'main') {
        let scope = 'default';
        if (
          repackRuntime.shared.scopes &&
          (!repackRuntime.shared.scopes[scope] ||
            !repackRuntime.shared.scopes[scope].__isInitialized)
        ) {
          await repackRuntime.shared.initSharing(scope);
          repackRuntime.shared.scopes[scope].__isInitialized = true;
        }
        const container = (self as any)[chunkId];
        if (!container.__isInitialized) {
          container.__isInitialized = true;
          // Initialize the container, it may provide shared modules
          await container.init(repackRuntime.shared.scopes[scope]);
        }
        const factory = await container.get(chunkId);
        const exports = factory();
        return exports;
      }
    }

After that. It work

trinhhcse avatar May 13 '24 03:05 trinhhcse