getPublicPath

  • Type: string

  • Required: No

  • Default value: undefined

  • Effective condition: only effective when exposes is set

  • Purpose: Used to set a dynamic publicPath. The value must be a stringified function or a stringified return expression. When other consumers load this provider, the string in getPublicPath will be executed via new Function to obtain the return value, which will be used as the publicPath prefix for the module’s static assets.

    • Function form: function(){ return window.cdn_prefix }
    • Return form: return "https:" + window.navigator.cdn_host + "/resource/app/"
  • Example:

In the example below, getPublicPath is set. When other consumers load this provider, the string in getPublicPath will be executed using new Function to obtain the value, which will be used as the publicPath prefix for the module’s static assets.

NOTE

getPublicPath must be a stringified function or a stringified return expression.

NOTE

If you're using the module federation webpack plugin and want to set a dynamic publicPath, you should set __webpack_public_path__ = window.cdn_prefix in the getPublicPath function body.

rspack.config.ts
module.exports = {
  plugins: [
    new ModuleFederationPlugin({
      name: 'provider',
      exposes: {
        './Button': './src/components/Button.tsx',
      },
      // ...
      getPublicPath: `function() {return "https:" + window.navigator.cdn_host + "/resource/app/"}`,
    }),
  ],
};