Share npm dependencies across main and sub-packages in a split-repository setup using the shared and consumes configuration.
In mini-program development, the main package and sub-packages are sometimes developed in separate repositories and composed at integration time. When a sub-package depends on the same npm module as the main package, bundling it twice inflates the package size.MorJS provides shared and consumes configuration options that let the main package expose modules at runtime, so sub-packages can consume them without bundling their own copy.
Module sharing only works in bundle compile mode. It is disabled automatically for web, web-pro, and weex-pro targets.
The ModuleSharingAndConsumingPlugin (in packages/plugin-compiler/src/plugins/moduleSharingAndConsumingPlugin.ts) implements sharing through two mechanisms:
shared — generates a virtual mor.s.js (or mor.s.<suffix>.js) file that imports specified npm modules and attaches them to a global container (my.mor_shared for Alipay, wx.mor_shared for WeChat).
consumes — marks specified npm modules as externals of type var, so they are read from the global container at runtime instead of bundled.
Configure shared in the main package’s mor.config.ts:
// mor.config.ts (main package)export default defineConfig([ { name: 'alipay', target: 'alipay', compileType: 'miniprogram', // Share these npm modules with sub-packages shared: [ // String form: module name is used as both the import source and the export key 'lodash', 'dayjs', // Object form: { exportKey: importSource } // Useful when the key seen by consumers differs from the npm package name { 'my-utils': '@company/my-utils' } ] }])
This generates a virtual file with content similar to:
import * as shared1 from 'lodash';import * as shared2 from 'dayjs';import * as shared3 from '@company/my-utils';my.mor_shared = my.mor_shared || {};my.mor_shared['lodash'] = shared1;my.mor_shared['dayjs'] = shared2;my.mor_shared['my-utils'] = shared3;
When compiling a sub-package for integration into a host app, MorJS can generate a mor.p.js script that the host app runs to update its app.json with the sub-package’s pages.
export default defineConfig([ { name: 'alipay-sub', target: 'alipay', compileType: 'subpackage', // Default: true. Set to false to skip mor.p.js generation. generateAppJSONScript: true }])
The generated mor.p.js file is placed alongside the compiled output. The host application includes this script to dynamically register the sub-package’s pages into its app.json at integration update time.