Skip to content

Commit

Permalink
feat: support runtime global module registry (#1574)
Browse files Browse the repository at this point in the history
* feat: support runtime global module registry

* docs: update config docs
  • Loading branch information
xusd320 authored Sep 9, 2024
1 parent 4899e87 commit 60f8c90
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub struct OutputConfig {
pub skip_write: bool,
#[serde(deserialize_with = "deserialize_cross_origin_loading")]
pub cross_origin_loading: Option<CrossOriginLoading>,
pub global_module_registry: bool,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down Expand Up @@ -722,7 +723,8 @@ const DEFAULT_CONFIG: &str = r#"
"preserveModules": false,
"preserveModulesRoot": "",
"skipWrite": false,
"crossOriginLoading": false
"crossOriginLoading": false,
"globalModuleRegistry": false,
},
"resolve": { "alias": [], "extensions": ["js", "jsx", "ts", "tsx"] },
"mode": "development",
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/generate/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub(crate) fn runtime_code(context: &Arc<Context>) -> Result<String> {
.optimization
.as_ref()
.map_or(false, |o| o.concatenate_modules.unwrap_or(false)),
global_module_registry: context.config.output.global_module_registry,
};
let app_runtime = app_runtime.render_once()?;
let app_runtime = app_runtime.replace(
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/generate/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pub struct AppRuntimeTemplate {
pub is_browser: bool,
pub concatenate_enabled: bool,
pub cross_origin_loading: Option<String>,
pub global_module_registry: bool,
}
6 changes: 6 additions & 0 deletions crates/mako/templates/app_runtime.stpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createRuntime(makoModules, entryModuleId, global) {
<% if global_module_registry { %>
var modulesRegistry = (
(typeof globalThis !== "undefined" ? globalThis : self).__mako_module_registry =
((typeof globalThis !== "undefined" ? globalThis : self).__mako_module_registry || {}));
<% } else { %>
var modulesRegistry = {};
<% } %>

function requireModule(moduleId) {
var cachedModule = modulesRegistry[moduleId];
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ Output related configuration.
- `preserveModules`, whether to preserve the module directory structure (Bundless Only)
- `preserveModulesRoot`, preserve the root directory of the module directory structure (Bundless Only)
- `crossOriginLoading`, control the `crossorigin` attribute of the `script` tag and `link` tag for load async chunks
- `globalModuleRegistry`, whether enable shared module registry across multi entries

### optimization

Expand Down
1 change: 1 addition & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ e.g.
- `preserveModules`,是否保留模块目录结构(仅适用于 Bundless)
- `preserveModulesRoot`,是否保留模块目录结构的根目录(仅限 Bundless)
- `crossOriginLoading`,控制异步 chunk 加载时 `script``link` 标签的 `crossorigin` 属性值
- `globalModuleRegistry`,是否允许在多 entry 之间共享模块注册中心

### optimization

Expand Down
4 changes: 4 additions & 0 deletions e2e/fixtures/config.global-module-registry/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const assert = require("assert");

require('./dist/common');
assert(require('./dist/A').common === require('./dist/B').common, 'global module registry should work');
25 changes: 25 additions & 0 deletions e2e/fixtures/config.global-module-registry/mako.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"entry": {
"A": "./src/entryA.ts",
"B": "./src/entryB.ts"
},
"minify": false,
"platform": "node",
"cjs": true,
"codeSplitting": {
"strategy": "advanced",
"options": {
"groups": [
{
"name": "common",
"allowChunks": "all",
"minSize": 1,
"minChunks": 2
}
]
}
},
"output": {
"globalModuleRegistry": true
}
}
3 changes: 3 additions & 0 deletions e2e/fixtures/config.global-module-registry/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const common = {
value: 1
}
2 changes: 2 additions & 0 deletions e2e/fixtures/config.global-module-registry/src/entryA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { common } from "./common";

2 changes: 2 additions & 0 deletions e2e/fixtures/config.global-module-registry/src/entryB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { common } from "./common";

0 comments on commit 60f8c90

Please sign in to comment.