{"name":"@module-federation/runtime-tools","version":"0.0.0-next-20231230034830","author":{"name":"zhanghang","email":"hanric.zhang@gmail.com"},"license":"MIT","_id":"@module-federation/runtime-tools@0.0.0-next-20231230034830","maintainers":[{"name":"heal","email":"TwoHeal@163.com"},{"name":"valorkin","email":"valorkin@gmail.com"},{"name":"zackarychapple","email":"zackarychapple30@gmail.com"},{"name":"ilteoood","email":"matteopietro.dazzi@gmail.com"},{"name":"gioboa","email":"giorgiob.boa@gmail.com"},{"name":"ranshamay","email":"ranshamay@gmail.com"},{"name":"zackljackson","email":"zackary.l.jackson@gmail.com"},{"name":"tysonrm","email":"tyson.midboe@gmail.com"}],"dist":{"shasum":"185c150a9eeff133f910205de86e1e62e6dab0e6","tarball":"http://registry-npm.ywwl.com/@module-federation/runtime-tools/download/@module-federation/runtime-tools-0.0.0-next-20231230034830.tgz","fileCount":28,"integrity":"sha512-MiJYt4muCiUoyAVyMB7p/Meqz305Tob7ecc5dQPUGF8XBQFfQ744qLdPIwO4mgMq5kl/Kks10MKdUAgJ42Whrg==","signatures":[{"sig":"MEQCIDczHtSmzSKs+pdMF9/qyTGANqIcf6wIMSLa6YB8EWwQAiBVJgWSQJow3pbGzHUFdj3HI3UYshxEeLQHcMgm8O8rFw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":93778,"size":20863,"noattachment":false,"key":"/@module-federation/runtime-tools/-/@module-federation/runtime-tools-0.0.0-next-20231230034830.tgz"},"main":"./dist/index.cjs","_from":"file:module-federation-runtime-tools-0.0.0-next-20231230034830.tgz","types":"./dist/index.cjs.d.ts","module":"./dist/index.esm.js","readme":"# `@module-federation/runtime`\n\n- Can be combined with the build plug-in to share basic dependencies according to policies to reduce the number of module downloads and improve the loading speed of modules.\n- Only consume part of the export of the remote module and will not fully download the remote module\n- The runtime calling process can be extended through the module-runtime plug-in mechanism\n\n## Usage\n\n```javascript\n// Can load modules using only the runtime SDK without relying on build plugins\n// When not using build plugins, shared dependencies cannot be automatically reused\nimport { init, loadRemote } from '@module-federation/runtime';\n\ninit({\n    name: '@demo/app-main',\n    remotes: [\n        {\n            name: \"@demo/app2\",\n            entry: \"http://localhost:3006/remoteEntry.js\"\n        },\n        {\n            name: \"@demo/app3\",\n            alias: \"app3\",\n            entry: \"http://localhost:2001/module-federation-manifest.json\"\n        },\n    ],\n});\n\n// Load by alias\nloadRemote<{add: (...args: Array<number>)=> number }>(\"app3/util\").then((md)=>{\n    md.add(1,2,3);\n});\n```\n\n### init\n\n- Type: `init(options: InitOptions): void`\n- It is used to dynamically register the module at runtime\n- InitOptions:\n\n```ts\ntype InitOptions {\n    name: string;\n    version?: string;\n    shared?: ShareInfos;\n};\n\ntype RemoteInfo = (RemotesWithEntry | RemotesWithVersion) & {\n  alias?: string;\n};\n\ninterface RemotesWithVersion {\n  name: string;\n  version: string;\n}\n\ninterface RemotesWithEntry {\n  name: string;\n  entry: string;\n}\n\ntype ShareInfos = {\n  // Package name and dependency basic information and sharing strategy\n  [pkgName: string]: Share;\n};\n\ntype Share = {\n  // Versions of shared dependencies\n  version: string;\n  // Which modules consume the current shared dependencies?\n  useIn?: Array<string>;\n  // Which module does the shared dependency come from?\n  from?: string;\n  // Get the factory function of the shared dependency instance. When the cached shared instance cannot be loaded, its own shared dependency will be loaded.\n  lib: () => Module;\n  // Sharing strategy, what strategy will be used to determine the reuse of shared dependencies\n  shareConfig?: SharedConfig;\n  // Dependencies between shares\n  deps?: Array<string>;\n  // The scope under which the current shared dependencies are placed, the default is default\n  scope?: string | Array<string>;\n};\n```\n\n- Example\n\n```js\nimport { init, loadRemote } from '@module-federation/runtime';\n\ninit({\n  name: '@demo/main-app',\n  remotes: [\n    {\n      name: '@demo/app2',\n      entry: 'http://localhost:3006/remoteEntry.js',\n    },\n    {\n      name: '@demo/app3',\n      alias: 'app3',\n      entry: 'http://localhost:2001/module-federation-manifest.json',\n    },\n  ],\n});\n```\n\n### loadRemote\n\n- Type: `loadRemote(id: string)`\n- Used to load initialized remote modules, when used with build plugins, it can be loaded directly through native `import(\"remoteName/expose\")` syntax, and the build plugin will automatically convert it into `loadRemote` usage.\n\n- Example\n\n```javascript\nimport { init, loadRemote } from '@module-federation/runtime';\n\ninit({\n  name: '@demo/main-app',\n  remotes: [\n    {\n      name: '@demo/app3',\n      alias: 'app3',\n      entry: 'http://localhost:2001/module-federation-manifest.json',\n    },\n  ],\n});\n\n// remoteName + expose\nloadRemote('@demo/app3/util').then((m) => m.add(1, 2, 3));\n\n// alias + expose\nloadRemote('app3/util').then((m) => m.add(1, 2, 3));\n```\n\n### loadShare\n\n- Type: `loadShare(pkgName: string)`\n- Gets the `share` dependency. When there are `share` dependencies that match the current `host` in the global environment, the existing and satisfying `share` dependencies will be reused first. Otherwise, load its own dependencies and store them in the global cache.\n- This `API` is generally not called directly by users, but is used by build plugins to convert their own dependencies.\n\n- Example\n\n```js\nimport { init, loadRemote, loadShare } from '@module-federation/runtime';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\n\ninit({\n  name: '@demo/main-app',\n  remotes: [],\n  shared: {\n    react: {\n      version: '17.0.0',\n      scope: 'default',\n      lib: () => React,\n      shareConfig: {\n        singleton: true,\n        requiredVersion: '^17.0.0',\n      },\n    },\n    'react-dom': {\n      version: '17.0.0',\n      scope: 'default',\n      lib: () => ReactDOM,\n      shareConfig: {\n        singleton: true,\n        requiredVersion: '^17.0.0',\n      },\n    },\n  },\n});\n\nloadShare('react').then((reactFactory) => {\n  console.log(reactFactory());\n});\n```\n\n### usePlugin\n\nUsed to extend the internal loading process of `ModuleFederation`, affecting the entire loading process through hook triggers and return values.\n\n- Example\n\n```ts\nimport { init } from '@module-federation/runtime';\n\n// mock get remote data remotes config\nfunction getDataConfig() {\n  return new Promise((resolve) => {\n    setTimeout(() => {\n      resolve({\n        remotes: [\n          {\n            name: '@demo/sub',\n            alias: 'sub',\n            entry: 'http://localhost:2001/module-federation-manifest.json',\n          },\n        ],\n      });\n    }, 2000);\n  });\n}\n\nfunction RemotesDataPlugin() {\n  return {\n    name: 'data-config',\n    async beforeRequest(args) {\n      const remotes = await getDataConfig();\n      origin.initOptions({\n        remotes,\n      });\n      return args;\n    },\n  };\n}\n\ninit({\n  name: '@demo/micro-app',\n  remotes: [],\n  pluigns: [RemotesDataPlugin()],\n});\n\nloadRemote('sub/utils').then((m) => {\n  m.add(1, 2, 3, 4);\n});\n```\n\n- Type\n\n```typescript\ntype Plugin = {\n  name: string;\n  core: {\n    beforeRequest: AsyncWaterfallHook<{\n      id: string;\n      options: Options;\n      origin: VmokHost;\n    }>;\n  };\n  snapshot: {};\n};\ntype usePlugin = (plugin: Plugin) => void;\n```\n\n### preloadRemote\n\n- Type\n\n```typescript\nasync function preloadRemote(preloadOptions: Array<PreloadRemoteArgs>) {}\n\ntype depsPreloadArg = Omit<PreloadRemoteArgs, 'depsRemote'>;\ntype PreloadRemoteArgs = {\n  // Preload the name and alias of the remote\n  nameOrAlias: string;\n  // Expose to be preloaded\n  // Default to preload all exposes\n  // Only the required expose will be loaded when an expose is provided\n  exposes?: Array<string>; // Default request\n  // Default is sync, only load synchronous code referenced in the expose\n  // Set to all to load synchronous and asynchronous references\n  resourceCategory?: 'all' | 'sync';\n  // Load all dependencies when no value is configured\n  // Only the resources needed will be loaded after configuring the dependency\n  depsRemote?: boolean | Array<depsPreloadArg>;\n  // No filtering of resources when not configured\n  // Will filter out unwanted resources after configuring\n  filter?: (assetUrl: string) => boolean;\n};\n```\n\n- Details\n\nThrough `preloadRemote`, module resources can be preloaded at an earlier stage to avoid waterfall requests. `preloadRemote` can preload the following content:\n\n- The `remoteEntry` of `remote`\n- `expose` of `remote`\n- Synchronous resources or asynchronous resources of `remote`\n- `remote` resources that `remote` depends on\n\n* Example\n\n```ts\nimport { init, preloadRemote } from '@module-federation/runtime';\ninit({\n  name: '@demo/preload-remote',\n  remotes: [\n    {\n      name: '@demo/sub1',\n      entry: 'http://localhost:2001/vmok-manifest.json',\n    },\n    {\n      name: '@demo/sub2',\n      entry: 'http://localhost:2001/vmok-manifest.json',\n    },\n    {\n      name: '@demo/sub3',\n      entry: 'http://localhost:2001/vmok-manifest.json',\n    },\n  ],\n});\n```\n","exports":{".":{"import":"./dist/index.esm.js","require":"./dist/index.cjs.js"},"./*":"./*","./runtime":{"import":"./dist/runtime.esm.js","require":"./dist/runtime.cjs.js"},"./webpack-bundler-runtime":{"import":"./dist/webpack-bundler-runtime.esm.js","require":"./dist/webpack-bundler-runtime.cjs.js"}},"_npmUser":{"name":"zackljackson","email":"zackary.l.jackson@gmail.com"},"_resolved":"/tmp/577b747aa185ebfacb5bcfa30692a334/module-federation-runtime-tools-0.0.0-next-20231230034830.tgz","_integrity":"sha512-MiJYt4muCiUoyAVyMB7p/Meqz305Tob7ecc5dQPUGF8XBQFfQ744qLdPIwO4mgMq5kl/Kks10MKdUAgJ42Whrg==","_npmVersion":"10.2.3","description":"- Can be combined with the build plug-in to share basic dependencies according to policies to reduce the number of module downloads and improve the loading speed of modules. - Only consume part of the export of the remote module and will not fully downloa","directories":{},"_nodeVersion":"18.19.0","dependencies":{"@module-federation/runtime":"0.0.0-next-20231230034830","@module-federation/webpack-bundler-runtime":"0.0.0-next-20231230034830"},"publishConfig":{"access":"public"},"typesVersions":{"*":{".":["./dist/index.cjs.d.ts"],"runtime":["./dist/runtime.cjs.d.ts"],"webpack-bundler-runtime":["./dist/webpack-bundler-runtime.cjs.d.ts"]}},"_hasShrinkwrap":false,"readmeFilename":"README.md","_npmOperationalInternal":{"tmp":"tmp/runtime-tools_0.0.0-next-20231230034830_1703908128110_0.8122681876461131","host":"s3://npm-registry-packages"},"publish_time":1703908128253,"_cnpm_publish_time":1703908128253,"dist-tags":{"latest":"0.6.1","next":"0.0.0-next-20240911025819"}}