@fulgurjs/federation 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -43,7 +43,7 @@ var import_node_fs = __toESM(require("fs"), 1);
43
43
  var import_node_path = __toESM(require("path"), 1);
44
44
 
45
45
  // src/version.ts
46
- var RUNTIME_VERSION = "2.0.0";
46
+ var RUNTIME_VERSION = "2.0.2";
47
47
 
48
48
  // src/options.ts
49
49
  var DEFAULT_FILENAME = "fulgurjs-remoteEntry.js";
@@ -402,28 +402,45 @@ var import_node_path2 = __toESM(require("path"), 1);
402
402
  function jsonReplacer(_k, v) {
403
403
  return v;
404
404
  }
405
- function genSharedFacade(specifier, exportNames) {
405
+ function genSharedFacade(specifier, exportNames, dynamic = false) {
406
406
  const names = (exportNames ?? []).filter(
407
407
  (n) => n !== "default" && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)
408
408
  );
409
- if (names.length === 0) {
409
+ if (!dynamic) {
410
+ if (names.length === 0) {
411
+ return [
412
+ `import * as __fulgurjs_facade from ${JSON.stringify(specifier)};`,
413
+ `export * from ${JSON.stringify(specifier)};`,
414
+ `export default __fulgurjs_facade.default ?? __fulgurjs_facade;`,
415
+ ""
416
+ ].join("\n");
417
+ }
410
418
  return [
411
419
  `import * as __fulgurjs_facade from ${JSON.stringify(specifier)};`,
420
+ ...names.map((n) => `export const ${n} = __fulgurjs_facade[${JSON.stringify(n)}];`),
421
+ `export default __fulgurjs_facade.default ?? __fulgurjs_facade;`,
422
+ ""
423
+ ].join("\n");
424
+ }
425
+ if (names.length === 0) {
426
+ return [
427
+ `const __fulgurjs_facade = await import(${JSON.stringify(specifier)});`,
412
428
  `export * from ${JSON.stringify(specifier)};`,
413
429
  `export default __fulgurjs_facade.default ?? __fulgurjs_facade;`,
414
430
  ""
415
431
  ].join("\n");
416
432
  }
417
433
  return [
418
- `import * as __fulgurjs_facade from ${JSON.stringify(specifier)};`,
419
- ...names.map((n) => `export const ${n} = __fulgurjs_facade[${JSON.stringify(n)}];`),
420
- `export default __fulgurjs_facade.default ?? __fulgurjs_facade;`,
434
+ `const __fulgurjs_facade = await import(${JSON.stringify(specifier)});`,
435
+ `const __fulgurjs_ns = { ...__fulgurjs_facade };`,
436
+ ...names.map((n) => `export const ${n} = __fulgurjs_ns[${JSON.stringify(n)}];`),
437
+ `export default __fulgurjs_ns.default ?? __fulgurjs_ns;`,
421
438
  ""
422
439
  ].join("\n");
423
440
  }
424
- function genSharedNsFacade(item, loadShareCall, exportNames) {
441
+ function genSharedNsFacade(item, loadShareCall, exportNames, dynamic = false) {
425
442
  const lines = [
426
- `import { loadShare as __fulgurjs_loadShare, unwrapDefault as __fulgurjsU } from "virtual:fulgurjs-runtime";`,
443
+ dynamic ? `const { loadShare: __fulgurjs_loadShare, unwrapDefault: __fulgurjsU } = await import("virtual:fulgurjs-runtime");` : `import { loadShare as __fulgurjs_loadShare, unwrapDefault as __fulgurjsU } from "virtual:fulgurjs-runtime";`,
427
444
  `const __fulgurjs_m = await ${loadShareCall};`,
428
445
  `const __fulgurjs_d = __fulgurjsU(__fulgurjs_m);`,
429
446
  `export default __fulgurjs_d;`
@@ -437,9 +454,9 @@ function genSharedNsFacade(item, loadShareCall, exportNames) {
437
454
  lines.push("");
438
455
  return lines.join("\n");
439
456
  }
440
- function genBindingFacade(item, bindings, loadShareCall) {
457
+ function genBindingFacade(item, bindings, loadShareCall, dynamic = false) {
441
458
  const lines = [
442
- `import { loadShare as __fulgurjs_loadShare, unwrapDefault as __fulgurjsU } from "virtual:fulgurjs-runtime";`,
459
+ dynamic ? `const { loadShare: __fulgurjs_loadShare, unwrapDefault: __fulgurjsU } = await import("virtual:fulgurjs-runtime");` : `import { loadShare as __fulgurjs_loadShare, unwrapDefault as __fulgurjsU } from "virtual:fulgurjs-runtime";`,
443
460
  `const __fulgurjs_m = await ${loadShareCall};`
444
461
  ];
445
462
  const seen = /* @__PURE__ */ new Set();
@@ -455,9 +472,9 @@ function genBindingFacade(item, bindings, loadShareCall) {
455
472
  lines.push("");
456
473
  return lines.join("\n");
457
474
  }
458
- function genRemoteBindingFacade(remoteSpec, bindings) {
475
+ function genRemoteBindingFacade(remoteSpec, bindings, dynamic = false) {
459
476
  const lines = [
460
- `import { loadRemote as __fulgurjs_loadRemote, unwrapDefault as __fulgurjsU } from "virtual:fulgurjs-runtime";`,
477
+ dynamic ? `const { loadRemote: __fulgurjs_loadRemote, unwrapDefault: __fulgurjsU } = await import("virtual:fulgurjs-runtime");` : `import { loadRemote as __fulgurjs_loadRemote, unwrapDefault: __fulgurjsU } from "virtual:fulgurjs-runtime";`.replace("unwrapDefault: __fulgurjsU", "unwrapDefault as __fulgurjsU"),
461
478
  `const __fulgurjs_m = await __fulgurjs_loadRemote(${JSON.stringify(remoteSpec)});`
462
479
  ];
463
480
  const seen = /* @__PURE__ */ new Set();
@@ -814,6 +831,10 @@ function parseImportClause(clause) {
814
831
  return { ns: nsMatch?.[1] ?? null, bindings, defaultLocal };
815
832
  }
816
833
  var JS_EXT_RE = /\.(m|c)?[jt]sx?$/;
834
+ function inSharedClosure(moduleId, roots) {
835
+ if (!roots || roots.length === 0) return false;
836
+ return roots.some((r) => moduleId.startsWith(r.root));
837
+ }
817
838
  function isTransformableId(id, allowNodeModules = false) {
818
839
  const clean = id.split("?")[0];
819
840
  if (!JS_EXT_RE.test(clean) && !clean.endsWith(".vue")) return false;
@@ -864,6 +885,7 @@ function remoteFacadeSpecFor(ctx, remoteName, exposeName, bindings) {
864
885
  }
865
886
  async function transformModule(code, id, ctx) {
866
887
  const { options } = ctx;
888
+ const clean = id.split("?")[0];
867
889
  if (options.shared.length === 0 && options.remotes.length === 0) return null;
868
890
  const candidates = [];
869
891
  for (const s2 of options.shared) candidates.push(s2.configKey.replace(/\/$/, ""));
@@ -950,7 +972,8 @@ export { ${nsAs[1]} };`);
950
972
  while (code[stmtEnd] === ";") stmtEnd++;
951
973
  const head = code.slice(stmtStart, stmtStart + 24);
952
974
  if (/^\s*(import|export)\s+type[\s{]/.test(head)) continue;
953
- const shared2 = matcher ? matchShared(spec2, matcher) : null;
975
+ const matched2 = matcher ? matchShared(spec2, matcher) : null;
976
+ const shared2 = matched2 && inSharedClosure(clean, ctx.sharedClosureRoots) ? null : matched2;
954
977
  const firstSeg2 = spec2.split("/")[0];
955
978
  const remote2 = remotesByKey.get(firstSeg2);
956
979
  const exposeName2 = remote2 ? remoteExposeName(spec2, remote2.key) : null;
@@ -983,7 +1006,8 @@ export { ${nsAs[1]} };`);
983
1006
  const literal = specExpr.match(/^['"](.*)['"]$/s);
984
1007
  if (!literal) continue;
985
1008
  const spec = remap(literal[1]);
986
- const shared = matcher ? matchShared(spec, matcher) : null;
1009
+ const matched = matcher ? matchShared(spec, matcher) : null;
1010
+ const shared = matched && inSharedClosure(clean, ctx.sharedClosureRoots) ? null : matched;
987
1011
  const firstSeg = spec.split("/")[0];
988
1012
  const remote = remotesByKey.get(firstSeg);
989
1013
  const exposeName = remote ? remoteExposeName(spec, remote.key) : null;
@@ -1298,7 +1322,7 @@ function syncViteCacheMarker(root, pluginVersion, log) {
1298
1322
  }
1299
1323
 
1300
1324
  // src/runtime-code.gen.ts
1301
- var runtime_code_gen_default = 'var X=/^v?(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?(?:-((?:[0-9A-Za-z-]+)(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z.-]+)?$/;function T(t){let n=X.exec(String(t).trim());return n?{major:Number(n[1]),minor:Number(n[2]??0),patch:Number(n[3]??0),pre:n[4]?n[4].split("."):[]}:null}function Y(t,n){if(t.length===0&&n.length===0)return 0;if(t.length===0)return 1;if(n.length===0)return-1;let o=Math.max(t.length,n.length);for(let r=0;r<o;r++){let l=t[r],m=n[r];if(l===void 0)return-1;if(m===void 0)return 1;let y=/^\\d+$/.test(l),E=/^\\d+$/.test(m);if(y&&E){let w=Number(l)-Number(m);if(w!==0)return w}else{if(y!==E)return y?-1:1;if(l!==m)return l<m?-1:1}}return 0}function v(t,n){let o=T(t),r=T(n);return!o||!r?t<n?-1:t>n?1:0:o.major!==r.major?o.major-r.major:o.minor!==r.minor?o.minor-r.minor:o.patch!==r.patch?o.patch-r.patch:Y(o.pre,r.pre)}var Z=/^[a-z]+[+:-]/i;function O(t,n){if(n===!1||n==null||n==="")return!0;let o=String(n).trim();return!o||o==="*"||o==="latest"||Z.test(o)?!0:o.split("||").some(r=>Q(t,r.trim()))}function Q(t,n){if(!n||n==="*")return!T(t)?.pre.length;let o=n.split(/\\s+-\\s+/);if(o.length===2){let m=o[0].trim(),y=o[1].trim();return!(!O(t,`>=${m}`)||!O(t,`<=${y}`))}let r=T(t);if(!r)return!1;let l=ee(n);if(l===null)return!0;for(let m of l)if(!ie(r,t,m))return!1;return!(r.pre.length>0&&!l.some(m=>m.ver.pre.length>0)&&!l.some(y=>y.ver.major===r.major&&y.ver.minor===r.minor&&y.ver.patch===r.patch))}function ee(t){let n=[],o=t.trim().split(/\\s+/).filter(Boolean);if(o.length===0)return null;for(let r of o){let l=/^(>=|<=|>|<|=|\\^|~)?(.*)$/.exec(r);if(!l)return null;let m=l[1],y=l[2],E=re(m,y);if(E===null)return null;n.push(...E)}return n}function re(t,n){let o=ne(n);if(!o)return null;let{ver:r,partial:l}=o;switch(t){case"^":{let m=te(r);return m?[{op:">=",ver:r},{op:"<",ver:m}]:null}case"~":{let m=l>=2?{major:r.major,minor:r.minor+1,patch:0,pre:[]}:{major:r.major+1,minor:0,patch:0,pre:[]};return[{op:">=",ver:r},{op:"<",ver:m}]}case">=":return[{op:">=",ver:r}];case">":return l===1?[{op:">=",ver:{major:r.major+1,minor:0,patch:0,pre:[]}}]:l===2?[{op:">=",ver:{major:r.major,minor:r.minor+1,patch:0,pre:[]}}]:[{op:">",ver:r}];case"<":return l===1?[{op:"<",ver:{major:r.major,minor:0,patch:0,pre:[]}}]:l===2?[{op:"<",ver:{major:r.major,minor:r.minor,patch:0,pre:[]}}]:[{op:"<",ver:r}];case"<=":return[{op:"<=",ver:r}];case"=":case void 0:return l===1?[{op:">=",ver:r},{op:"<",ver:{major:r.major+1,minor:0,patch:0,pre:[]}}]:l===2?[{op:">=",ver:r},{op:"<",ver:{major:r.major,minor:r.minor+1,patch:0,pre:[]}}]:[{op:"=",ver:r}];default:return null}}function te(t){return t.major>0?{major:t.major+1,minor:0,patch:0,pre:[]}:t.minor>0?{major:0,minor:t.minor+1,patch:0,pre:[]}:{major:0,minor:0,patch:t.patch+1,pre:[]}}function ne(t){let n=t.replace(/^v/,"").trim();if(!n||n==="*"||n==="x"||n==="X")return{ver:{major:0,minor:0,patch:0,pre:[]},partial:0};let o=n.split("."),r=[],l=0;for(let E of o.slice(0,3))if(/^\\d+$/.test(E))r.push(Number(E)),l++;else{if(E==="x"||E==="X"||E==="*")break;break}let m=n.split("-"),y=m.length>1?m.slice(1).join("-").split("+")[0].split("."):[];return l===0&&y.length===0?{ver:{major:0,minor:0,patch:0,pre:[]},partial:0}:{ver:{major:r[0]??0,minor:r[1]??0,patch:r[2]??0,pre:y},partial:l}}function oe(t,n){switch(n){case">=":return t>=0;case">":return t>0;case"<":return t<0;case"<=":return t<=0;case"=":return t===0}}function ie(t,n,o){let r=`${t.major}.${t.minor}.${t.patch}`+(t.pre.length?`-${t.pre.join(".")}`:"");if(o.upper)return!(!(v(r,`${o.ver.major}.${o.ver.minor}.${o.ver.patch}`+(o.ver.pre.length?`-${o.ver.pre.join(".")}`:""))>=0)||!(v(r,`${o.upper.major}.${o.upper.minor}.${o.upper.patch}`)<0));let l=v(r,`${o.ver.major}.${o.ver.minor}.${o.ver.patch}`+(o.ver.pre.length?`-${o.ver.pre.join(".")}`:"")),m=oe(l,o.op);return o.ver.pre.length>0&&t.pre.length===0&&(o.op===">"||o.op==="<")?!1:m}var g=class extends Error{code;details;constructor(n,o,r){super(`[fulgurjs:${n}] ${o}`),this.name="FgError",this.code=n,this.details=r}},S={REMOTE_LOAD_FAILED:"MFU-001",REMOTE_NAME_MISMATCH:"MFU-002",SHARE_STRICT_VERSION:"MFU-003",SHARE_NOT_AVAILABLE:"MFU-004",CONTAINER_REINIT_CONFLICT:"MFU-005",MODULE_NOT_EXPOSED:"MFU-006",PRELOAD_FAILED:"MFU-007",REMOTE_UNKNOWN:"MFU-008",EMPTY_EXPORTS:"MFU-009",SINGLETON_SKEW:"MFU-010"};var U="2.0.0";var A=15e3,P=2,ae=5,se=3e4;function ce(){let t={},n=new Map,o=[],r=new Map,l=new WeakMap,m={},y=()=>{for(let e of o)try{e.init?.(m)}catch(i){console.warn("[fulgurjs] runtimePlugin init failed:",e.name,i)}},E=e=>t[e]??={};function w(e){try{m.onRemoteError?.(e)}catch{}typeof window<"u"&&window.dispatchEvent(new CustomEvent("fulgurjs:error",{detail:e}))}function F(e="default"){return E(e),t}function V(e,i,a,c,R={}){let s=E(e||"default"),u=s[i]??={};u[a]||(u[a]={version:a,get:c,from:R.from??"unknown",eager:!!R.eager,loaded:!!R.loaded})}function I(e){for(let i of e){let a=n.get(i.name);n.set(i.name,{timeout:A,retries:P,...a,...i,state:a?.state??"idle",breakerState:a?.breakerState??{fails:0,openUntil:0},debug:a?.debug??{entry:i.entry,status:"idle"}})}}function D(e){I([e])}function H(e){o.push(...e),y()}async function K(e,i={}){let a=i.shareScope||"default",c=i.shareKey||e,s=E(a)[c]||{},u=Object.keys(s),d=i.requiredVersion,f=u.filter(p=>d===void 0||d===!1||O(p,d)),_;if(i.singleton&&u.length>0){let p=u.filter(h=>s[h].loaded);if(_=[...p.length?p:u].sort(v).pop(),u.length>1||!f.includes(_)){let h=s[_],j=`singleton skew "${c}": req "${d??"any"}" use ${_} from ${h.from}`;if(i.strictVersion){let M=new g(S.SHARE_STRICT_VERSION,j,{shareKey:c,requiredVersion:d});throw w({remote:h.from,error:M}),M}console.warn(`[fulgurjs:MFU-010] ${j}`)}}else if(_=[...f].sort(v).pop(),!_){if(i.strictVersion){let h=new g(S.SHARE_STRICT_VERSION,`no satisfying version for "${c}" (required "${d}") in share scope "${a}"`);throw w({remote:c,error:h}),h}if(i.fallback)return i.fallback();let p=new g(S.SHARE_NOT_AVAILABLE,`shared "${c}" (${d??"any"}) unavailable in scope "${a}" (no local fallback)`,{shareKey:c,requiredVersion:d});throw w({remote:c,error:p}),p}let $=s[_];if(m.resolveShare){let p=await m.resolveShare({shareKey:c,shareScope:a,requiredVersion:d,picked:$,available:Object.values(s)});p&&($=p)}return $.loaded=!0,$.get()}function q(e,i,a){return new Promise((c,R)=>{let s=setTimeout(()=>R(new Error(`timeout after ${i}ms: ${a}`)),i);e.then(u=>{clearTimeout(s),c(u)},u=>{clearTimeout(s),R(u)})})}let z=e=>new Promise(i=>setTimeout(i,e));function k(e,i){if(!e||typeof e.get!="function")throw new Error(`remoteEntry of "${i.name}" does not export a container interface (get/init)`);if(e.name&&i.name&&e.name!==i.name){let a=`remoteEntry self-reported name "${e.name}" mismatches configured name "${i.name}"`;if(i.promise)console.warn(`[fulgurjs] ${a} (promise-based remote, continuing)`);else throw Object.assign(new Error(a),{code:S.REMOTE_NAME_MISMATCH})}}async function L(e,i){return q(import(e),i.timeout??A,`load remoteEntry ${e}`)}async function x(e,i){let a=e.shareScope||"default";if(e.container){let s=l.get(e.container);if(s&&s!==a)throw new g(S.CONTAINER_REINIT_CONFLICT,`container "${e.name}" already initialized with share scope "${s}", cannot re-init with "${a}"`);return e.container}if(e.containerPromise)return e.containerPromise;let c=e.breakerState;if(c.openUntil>Date.now()){let s=new g(S.REMOTE_LOAD_FAILED,`circuit breaker open for remote "${e.name}" until ${new Date(c.openUntil).toISOString()}`,{remote:e.name});throw w({remote:e.name,error:s}),s}e.state="loading",e.debug.status="loading";let R=(async()=>{let s=[];if(e.promise)s.push(async()=>{let p=await e.promise(),h=typeof p=="string"?await L(p,e):p;return k(h,e),h});else for(let p of[e.entry,...e.fallback??[]])s.push(async()=>{let h=await L(p,e);return k(h,e),h});let u,d,f=!1,_=i?.retries??e.retries??P;for(let p of s){for(let h=0;h<=_;h++){let j=performance.now();try{u=await p(),e.lastLoadMs=Math.round(performance.now()-j),e.debug.lastLoadMs=e.lastLoadMs,f=!0;break}catch(M){if(d=M,M?.code)throw M;h<_&&await z(200*2**h)}}if(f)break}if(!f||!u){let p=[];throw/^https?:\\/\\//.test(e.entry)&&typeof location<"u"?(p.push(`1. is the remote dev server running? try opening ${e.entry} in the browser \\u2014 it must return JS, not HTML/an error`),p.push("2. is the URL correct in federation({ remotes })? dev and prod entries can differ"),p.push("3. CORS: the remote dev server must allow cross-origin requests (fulgurjs-federation enables server.cors automatically; a custom server config may have disabled it)")):(p.push(`1. is the remote deployed and reachable? try opening ${e.entry} in the browser \\u2014 it must return JS`),p.push("2. NGINX/CDN routing: the entry path must serve the remoteEntry JS file (check try_files / fallback rules)")),new g(S.REMOTE_LOAD_FAILED,`failed to load remote "${e.name}" from ${e.entry}: ${String(d?.message??d)}\n`+p.join(`\n`),{remote:e.name})}k(u,e);let $=l.get(u);if($&&$!==a)throw new g(S.CONTAINER_REINIT_CONFLICT,`container "${e.name}" already initialized with share scope "${$}", cannot re-init with "${a}"`);return typeof u.init=="function"&&await u.init(t),l.set(u,a),e.container=u,e.state="loaded",e.debug.status="loaded",c.fails=0,u})();e.containerPromise=R;try{return await R}catch(s){if(e.containerPromise=void 0,e.state="failed",e.debug.status="failed",e.debug.error=String(s?.message??s),c.fails+=1,c.fails>=ae&&(c.openUntil=Date.now()+se,c.fails=0),s instanceof g)throw w({remote:e.name,error:s}),s;if(s?.code)throw w({remote:e.name,error:s}),s;let u=new g(S.REMOTE_LOAD_FAILED,String(s?.message??s),{remote:e.name});throw w({remote:e.name,error:u}),u}}function N(e){let i=e.indexOf("/");return i===-1?{remote:e,module:""}:{remote:e.slice(0,i),module:G(e.slice(i+1))}}function G(e){return e?e.startsWith(".")?e:`./${e}`:""}async function J(e,i){let{remote:a,module:c}=N(e);m.beforeLoadRemote?.({remote:a,module:c});let R=n.get(a);if(!R)throw new g(S.REMOTE_UNKNOWN,`unknown remote "${a}". Register it via federation({ remotes }) or registerRemote().`,{remote:a});let s;try{s=await x(R,{retries:i?.retries})}catch(f){if(i?.fallbackModule)return console.error(`[fulgurjs] loadRemote("${e}") failed; returning fallbackModule (\\u663E\\u5F0F\\u964D\\u7EA7\\uFF0C\\u9519\\u8BEF\\u5DF2\\u900F\\u51FA)`,f),w({remote:a,error:f}),await i.fallbackModule();throw f}if(!c)return s;let u=`${a}@${R.shareScope||"default"}#${c}`;r.has(u)||r.set(u,s.get(c).catch(f=>{throw r.delete(u),f instanceof g||f?.code?f:new g(S.REMOTE_LOAD_FAILED,`failed to load module "${c}" from remote "${a}": ${String(f?.message??f)}`,{remote:a,module:c})}));let d;try{d=await r.get(u)}catch(f){if(i?.fallbackModule)return console.error(`[fulgurjs] loadRemote("${e}") failed; returning fallbackModule (\\u663E\\u5F0F\\u964D\\u7EA7\\uFF0C\\u9519\\u8BEF\\u5DF2\\u900F\\u51FA)`,f),w({remote:a,error:f}),await i.fallbackModule();throw f}if(m.afterLoadRemote?.({remote:a,module:c,module_ns:d}),d&&typeof d=="object"&&Object.keys(d).length===0){let f=new g(S.EMPTY_EXPORTS,`no exports: ${c} @ ${a}`,{remote:a,module:c});w({remote:a,error:f}),console.error(`[fulgurjs:MFU-009] ${f.message}`)}return d}function W(e){let i=n.get(e);if(!i)throw new g(S.REMOTE_UNKNOWN,`unknown remote "${e}"`,{remote:e});return x(i)}async function B(e,i={}){let{remote:a}=N(e),c=n.get(a);if(!c)throw new g(S.REMOTE_UNKNOWN,`unknown remote "${a}"`,{remote:a});let R=i.mode??"preload",s=(u,d)=>{if(typeof document>"u"||document.querySelector(`link[href="${u}"]`))return;let f=document.createElement("link");f.rel=d==="style"?"stylesheet":"modulepreload",f.href=u,R==="prefetch"&&(f.fetchPriority="low"),document.head.appendChild(f)};try{if(c.manifestUrl){let u=await fetch(c.manifestUrl,{cache:"no-cache"});if(u.ok){let d=await u.json();s(c.entry,"modulepreload");let f=d.exposes??{};for(let _ of Object.keys(f)){let $=f[_];$?.file&&s(new URL($.file,c.entry).href,"modulepreload");for(let p of $?.css??[])s(new URL(p,c.entry).href,"style")}return}}s(c.entry,"modulepreload")}catch(u){w({remote:a,error:new g(S.PRELOAD_FAILED,String(u?.message??u),{remote:a})})}}return typeof window>"u"||(window.__FULGURJS_SCOPE__=t,window.__FULGURJS_INFO__={get remotes(){let e={};for(let[i,a]of n)e[i]={...a.debug,entry:a.entry};return e},errors:[]}),{shareScopeMap:t,initSharing:F,registerShare:V,registerRemotes:I,registerRemote:D,registerPlugins:H,loadShare:K,loadRemote:J,getContainer:W,preloadRemote:B,parseSpec:N}}var C=globalThis,b=C.__FULGURJS_RUNTIME__??ce();C.__FULGURJS_RUNTIME__=b;try{Object.freeze(b)}catch{}var he=U;function ye(){return b}var Ee=b.initSharing,Re=b.registerShare,Se=b.registerRemotes,we=b.registerRemote,be=b.registerPlugins,_e=b.loadShare,$e=b.loadRemote,Me=b.getContainer,ve=b.preloadRemote,je=b.shareScopeMap;function Te(t){if(t&&typeof t=="object"&&"default"in t){let n=t.default;return n!==void 0?n:t}return t}var Oe=b;export{Oe as default,Me as getContainer,ye as getRuntime,Ee as initSharing,$e as loadRemote,_e as loadShare,ve as preloadRemote,be as registerPlugins,we as registerRemote,Se as registerRemotes,Re as registerShare,b as runtime,je as shareScopeMap,Te as unwrapDefault,he as version};\n';
1325
+ var runtime_code_gen_default = 'var X=/^v?(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?(?:-((?:[0-9A-Za-z-]+)(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z.-]+)?$/;function T(t){let n=X.exec(String(t).trim());return n?{major:Number(n[1]),minor:Number(n[2]??0),patch:Number(n[3]??0),pre:n[4]?n[4].split("."):[]}:null}function Y(t,n){if(t.length===0&&n.length===0)return 0;if(t.length===0)return 1;if(n.length===0)return-1;let o=Math.max(t.length,n.length);for(let r=0;r<o;r++){let l=t[r],m=n[r];if(l===void 0)return-1;if(m===void 0)return 1;let y=/^\\d+$/.test(l),E=/^\\d+$/.test(m);if(y&&E){let w=Number(l)-Number(m);if(w!==0)return w}else{if(y!==E)return y?-1:1;if(l!==m)return l<m?-1:1}}return 0}function v(t,n){let o=T(t),r=T(n);return!o||!r?t<n?-1:t>n?1:0:o.major!==r.major?o.major-r.major:o.minor!==r.minor?o.minor-r.minor:o.patch!==r.patch?o.patch-r.patch:Y(o.pre,r.pre)}var Z=/^[a-z]+[+:-]/i;function O(t,n){if(n===!1||n==null||n==="")return!0;let o=String(n).trim();return!o||o==="*"||o==="latest"||Z.test(o)?!0:o.split("||").some(r=>Q(t,r.trim()))}function Q(t,n){if(!n||n==="*")return!T(t)?.pre.length;let o=n.split(/\\s+-\\s+/);if(o.length===2){let m=o[0].trim(),y=o[1].trim();return!(!O(t,`>=${m}`)||!O(t,`<=${y}`))}let r=T(t);if(!r)return!1;let l=ee(n);if(l===null)return!0;for(let m of l)if(!ie(r,t,m))return!1;return!(r.pre.length>0&&!l.some(m=>m.ver.pre.length>0)&&!l.some(y=>y.ver.major===r.major&&y.ver.minor===r.minor&&y.ver.patch===r.patch))}function ee(t){let n=[],o=t.trim().split(/\\s+/).filter(Boolean);if(o.length===0)return null;for(let r of o){let l=/^(>=|<=|>|<|=|\\^|~)?(.*)$/.exec(r);if(!l)return null;let m=l[1],y=l[2],E=re(m,y);if(E===null)return null;n.push(...E)}return n}function re(t,n){let o=ne(n);if(!o)return null;let{ver:r,partial:l}=o;switch(t){case"^":{let m=te(r);return m?[{op:">=",ver:r},{op:"<",ver:m}]:null}case"~":{let m=l>=2?{major:r.major,minor:r.minor+1,patch:0,pre:[]}:{major:r.major+1,minor:0,patch:0,pre:[]};return[{op:">=",ver:r},{op:"<",ver:m}]}case">=":return[{op:">=",ver:r}];case">":return l===1?[{op:">=",ver:{major:r.major+1,minor:0,patch:0,pre:[]}}]:l===2?[{op:">=",ver:{major:r.major,minor:r.minor+1,patch:0,pre:[]}}]:[{op:">",ver:r}];case"<":return l===1?[{op:"<",ver:{major:r.major,minor:0,patch:0,pre:[]}}]:l===2?[{op:"<",ver:{major:r.major,minor:r.minor,patch:0,pre:[]}}]:[{op:"<",ver:r}];case"<=":return[{op:"<=",ver:r}];case"=":case void 0:return l===1?[{op:">=",ver:r},{op:"<",ver:{major:r.major+1,minor:0,patch:0,pre:[]}}]:l===2?[{op:">=",ver:r},{op:"<",ver:{major:r.major,minor:r.minor+1,patch:0,pre:[]}}]:[{op:"=",ver:r}];default:return null}}function te(t){return t.major>0?{major:t.major+1,minor:0,patch:0,pre:[]}:t.minor>0?{major:0,minor:t.minor+1,patch:0,pre:[]}:{major:0,minor:0,patch:t.patch+1,pre:[]}}function ne(t){let n=t.replace(/^v/,"").trim();if(!n||n==="*"||n==="x"||n==="X")return{ver:{major:0,minor:0,patch:0,pre:[]},partial:0};let o=n.split("."),r=[],l=0;for(let E of o.slice(0,3))if(/^\\d+$/.test(E))r.push(Number(E)),l++;else{if(E==="x"||E==="X"||E==="*")break;break}let m=n.split("-"),y=m.length>1?m.slice(1).join("-").split("+")[0].split("."):[];return l===0&&y.length===0?{ver:{major:0,minor:0,patch:0,pre:[]},partial:0}:{ver:{major:r[0]??0,minor:r[1]??0,patch:r[2]??0,pre:y},partial:l}}function oe(t,n){switch(n){case">=":return t>=0;case">":return t>0;case"<":return t<0;case"<=":return t<=0;case"=":return t===0}}function ie(t,n,o){let r=`${t.major}.${t.minor}.${t.patch}`+(t.pre.length?`-${t.pre.join(".")}`:"");if(o.upper)return!(!(v(r,`${o.ver.major}.${o.ver.minor}.${o.ver.patch}`+(o.ver.pre.length?`-${o.ver.pre.join(".")}`:""))>=0)||!(v(r,`${o.upper.major}.${o.upper.minor}.${o.upper.patch}`)<0));let l=v(r,`${o.ver.major}.${o.ver.minor}.${o.ver.patch}`+(o.ver.pre.length?`-${o.ver.pre.join(".")}`:"")),m=oe(l,o.op);return o.ver.pre.length>0&&t.pre.length===0&&(o.op===">"||o.op==="<")?!1:m}var g=class extends Error{code;details;constructor(n,o,r){super(`[fulgurjs:${n}] ${o}`),this.name="FgError",this.code=n,this.details=r}},S={REMOTE_LOAD_FAILED:"MFU-001",REMOTE_NAME_MISMATCH:"MFU-002",SHARE_STRICT_VERSION:"MFU-003",SHARE_NOT_AVAILABLE:"MFU-004",CONTAINER_REINIT_CONFLICT:"MFU-005",MODULE_NOT_EXPOSED:"MFU-006",PRELOAD_FAILED:"MFU-007",REMOTE_UNKNOWN:"MFU-008",EMPTY_EXPORTS:"MFU-009",SINGLETON_SKEW:"MFU-010"};var U="2.0.2";var A=15e3,P=2,ae=5,se=3e4;function ce(){let t={},n=new Map,o=[],r=new Map,l=new WeakMap,m={},y=()=>{for(let e of o)try{e.init?.(m)}catch(i){console.warn("[fulgurjs] runtimePlugin init failed:",e.name,i)}},E=e=>t[e]??={};function w(e){try{m.onRemoteError?.(e)}catch{}typeof window<"u"&&window.dispatchEvent(new CustomEvent("fulgurjs:error",{detail:e}))}function F(e="default"){return E(e),t}function V(e,i,a,c,R={}){let s=E(e||"default"),u=s[i]??={};u[a]||(u[a]={version:a,get:c,from:R.from??"unknown",eager:!!R.eager,loaded:!!R.loaded})}function I(e){for(let i of e){let a=n.get(i.name);n.set(i.name,{timeout:A,retries:P,...a,...i,state:a?.state??"idle",breakerState:a?.breakerState??{fails:0,openUntil:0},debug:a?.debug??{entry:i.entry,status:"idle"}})}}function D(e){I([e])}function H(e){o.push(...e),y()}async function K(e,i={}){let a=i.shareScope||"default",c=i.shareKey||e,s=E(a)[c]||{},u=Object.keys(s),d=i.requiredVersion,f=u.filter(p=>d===void 0||d===!1||O(p,d)),_;if(i.singleton&&u.length>0){let p=u.filter(h=>s[h].loaded);if(_=[...p.length?p:u].sort(v).pop(),u.length>1||!f.includes(_)){let h=s[_],j=`singleton skew "${c}": req "${d??"any"}" use ${_} from ${h.from}`;if(i.strictVersion){let M=new g(S.SHARE_STRICT_VERSION,j,{shareKey:c,requiredVersion:d});throw w({remote:h.from,error:M}),M}console.warn(`[fulgurjs:MFU-010] ${j}`)}}else if(_=[...f].sort(v).pop(),!_){if(i.strictVersion){let h=new g(S.SHARE_STRICT_VERSION,`no satisfying version for "${c}" (required "${d}") in share scope "${a}"`);throw w({remote:c,error:h}),h}if(i.fallback)return i.fallback();let p=new g(S.SHARE_NOT_AVAILABLE,`shared "${c}" (${d??"any"}) unavailable in scope "${a}" (no local fallback)`,{shareKey:c,requiredVersion:d});throw w({remote:c,error:p}),p}let $=s[_];if(m.resolveShare){let p=await m.resolveShare({shareKey:c,shareScope:a,requiredVersion:d,picked:$,available:Object.values(s)});p&&($=p)}return $.loaded=!0,$.get()}function q(e,i,a){return new Promise((c,R)=>{let s=setTimeout(()=>R(new Error(`timeout after ${i}ms: ${a}`)),i);e.then(u=>{clearTimeout(s),c(u)},u=>{clearTimeout(s),R(u)})})}let z=e=>new Promise(i=>setTimeout(i,e));function k(e,i){if(!e||typeof e.get!="function")throw new Error(`remoteEntry of "${i.name}" does not export a container interface (get/init)`);if(e.name&&i.name&&e.name!==i.name){let a=`remoteEntry self-reported name "${e.name}" mismatches configured name "${i.name}"`;if(i.promise)console.warn(`[fulgurjs] ${a} (promise-based remote, continuing)`);else throw Object.assign(new Error(a),{code:S.REMOTE_NAME_MISMATCH})}}async function L(e,i){return q(import(e),i.timeout??A,`load remoteEntry ${e}`)}async function x(e,i){let a=e.shareScope||"default";if(e.container){let s=l.get(e.container);if(s&&s!==a)throw new g(S.CONTAINER_REINIT_CONFLICT,`container "${e.name}" already initialized with share scope "${s}", cannot re-init with "${a}"`);return e.container}if(e.containerPromise)return e.containerPromise;let c=e.breakerState;if(c.openUntil>Date.now()){let s=new g(S.REMOTE_LOAD_FAILED,`circuit breaker open for remote "${e.name}" until ${new Date(c.openUntil).toISOString()}`,{remote:e.name});throw w({remote:e.name,error:s}),s}e.state="loading",e.debug.status="loading";let R=(async()=>{let s=[];if(e.promise)s.push(async()=>{let p=await e.promise(),h=typeof p=="string"?await L(p,e):p;return k(h,e),h});else for(let p of[e.entry,...e.fallback??[]])s.push(async()=>{let h=await L(p,e);return k(h,e),h});let u,d,f=!1,_=i?.retries??e.retries??P;for(let p of s){for(let h=0;h<=_;h++){let j=performance.now();try{u=await p(),e.lastLoadMs=Math.round(performance.now()-j),e.debug.lastLoadMs=e.lastLoadMs,f=!0;break}catch(M){if(d=M,M?.code)throw M;h<_&&await z(200*2**h)}}if(f)break}if(!f||!u){let p=[];throw/^https?:\\/\\//.test(e.entry)&&typeof location<"u"?(p.push(`1. is the remote dev server running? try opening ${e.entry} in the browser \\u2014 it must return JS, not HTML/an error`),p.push("2. is the URL correct in federation({ remotes })? dev and prod entries can differ"),p.push("3. CORS: the remote dev server must allow cross-origin requests (fulgurjs-federation enables server.cors automatically; a custom server config may have disabled it)")):(p.push(`1. is the remote deployed and reachable? try opening ${e.entry} in the browser \\u2014 it must return JS`),p.push("2. NGINX/CDN routing: the entry path must serve the remoteEntry JS file (check try_files / fallback rules)")),new g(S.REMOTE_LOAD_FAILED,`failed to load remote "${e.name}" from ${e.entry}: ${String(d?.message??d)}\n`+p.join(`\n`),{remote:e.name})}k(u,e);let $=l.get(u);if($&&$!==a)throw new g(S.CONTAINER_REINIT_CONFLICT,`container "${e.name}" already initialized with share scope "${$}", cannot re-init with "${a}"`);return typeof u.init=="function"&&await u.init(t),l.set(u,a),e.container=u,e.state="loaded",e.debug.status="loaded",c.fails=0,u})();e.containerPromise=R;try{return await R}catch(s){if(e.containerPromise=void 0,e.state="failed",e.debug.status="failed",e.debug.error=String(s?.message??s),c.fails+=1,c.fails>=ae&&(c.openUntil=Date.now()+se,c.fails=0),s instanceof g)throw w({remote:e.name,error:s}),s;if(s?.code)throw w({remote:e.name,error:s}),s;let u=new g(S.REMOTE_LOAD_FAILED,String(s?.message??s),{remote:e.name});throw w({remote:e.name,error:u}),u}}function N(e){let i=e.indexOf("/");return i===-1?{remote:e,module:""}:{remote:e.slice(0,i),module:G(e.slice(i+1))}}function G(e){return e?e.startsWith(".")?e:`./${e}`:""}async function J(e,i){let{remote:a,module:c}=N(e);m.beforeLoadRemote?.({remote:a,module:c});let R=n.get(a);if(!R)throw new g(S.REMOTE_UNKNOWN,`unknown remote "${a}". Register it via federation({ remotes }) or registerRemote().`,{remote:a});let s;try{s=await x(R,{retries:i?.retries})}catch(f){if(i?.fallbackModule)return console.error(`[fulgurjs] loadRemote("${e}") failed; returning fallbackModule (\\u663E\\u5F0F\\u964D\\u7EA7\\uFF0C\\u9519\\u8BEF\\u5DF2\\u900F\\u51FA)`,f),w({remote:a,error:f}),await i.fallbackModule();throw f}if(!c)return s;let u=`${a}@${R.shareScope||"default"}#${c}`;r.has(u)||r.set(u,s.get(c).catch(f=>{throw r.delete(u),f instanceof g||f?.code?f:new g(S.REMOTE_LOAD_FAILED,`failed to load module "${c}" from remote "${a}": ${String(f?.message??f)}`,{remote:a,module:c})}));let d;try{d=await r.get(u)}catch(f){if(i?.fallbackModule)return console.error(`[fulgurjs] loadRemote("${e}") failed; returning fallbackModule (\\u663E\\u5F0F\\u964D\\u7EA7\\uFF0C\\u9519\\u8BEF\\u5DF2\\u900F\\u51FA)`,f),w({remote:a,error:f}),await i.fallbackModule();throw f}if(m.afterLoadRemote?.({remote:a,module:c,module_ns:d}),d&&typeof d=="object"&&Object.keys(d).length===0){let f=new g(S.EMPTY_EXPORTS,`no exports: ${c} @ ${a}`,{remote:a,module:c});w({remote:a,error:f}),console.error(`[fulgurjs:MFU-009] ${f.message}`)}return d}function W(e){let i=n.get(e);if(!i)throw new g(S.REMOTE_UNKNOWN,`unknown remote "${e}"`,{remote:e});return x(i)}async function B(e,i={}){let{remote:a}=N(e),c=n.get(a);if(!c)throw new g(S.REMOTE_UNKNOWN,`unknown remote "${a}"`,{remote:a});let R=i.mode??"preload",s=(u,d)=>{if(typeof document>"u"||document.querySelector(`link[href="${u}"]`))return;let f=document.createElement("link");f.rel=d==="style"?"stylesheet":"modulepreload",f.href=u,R==="prefetch"&&(f.fetchPriority="low"),document.head.appendChild(f)};try{if(c.manifestUrl){let u=await fetch(c.manifestUrl,{cache:"no-cache"});if(u.ok){let d=await u.json();s(c.entry,"modulepreload");let f=d.exposes??{};for(let _ of Object.keys(f)){let $=f[_];$?.file&&s(new URL($.file,c.entry).href,"modulepreload");for(let p of $?.css??[])s(new URL(p,c.entry).href,"style")}return}}s(c.entry,"modulepreload")}catch(u){w({remote:a,error:new g(S.PRELOAD_FAILED,String(u?.message??u),{remote:a})})}}return typeof window>"u"||(window.__FULGURJS_SCOPE__=t,window.__FULGURJS_INFO__={get remotes(){let e={};for(let[i,a]of n)e[i]={...a.debug,entry:a.entry};return e},errors:[]}),{shareScopeMap:t,initSharing:F,registerShare:V,registerRemotes:I,registerRemote:D,registerPlugins:H,loadShare:K,loadRemote:J,getContainer:W,preloadRemote:B,parseSpec:N}}var C=globalThis,b=C.__FULGURJS_RUNTIME__??ce();C.__FULGURJS_RUNTIME__=b;try{Object.freeze(b)}catch{}var he=U;function ye(){return b}var Ee=b.initSharing,Re=b.registerShare,Se=b.registerRemotes,we=b.registerRemote,be=b.registerPlugins,_e=b.loadShare,$e=b.loadRemote,Me=b.getContainer,ve=b.preloadRemote,je=b.shareScopeMap;function Te(t){if(t&&typeof t=="object"&&"default"in t){let n=t.default;return n!==void 0?n:t}return t}var Oe=b;export{Oe as default,Me as getContainer,ye as getRuntime,Ee as initSharing,$e as loadRemote,_e as loadShare,ve as preloadRemote,be as registerPlugins,we as registerRemote,Se as registerRemotes,Re as registerShare,b as runtime,je as shareScopeMap,Te as unwrapDefault,he as version};\n';
1302
1326
 
1303
1327
  // src/index.ts
1304
1328
  function readRuntimeCode() {
@@ -1339,6 +1363,29 @@ function injectInitScript(html, scriptSrc) {
1339
1363
  }
1340
1364
  return tag + html;
1341
1365
  }
1366
+ var RUNTIME_CHUNK_NAME = "fulgurjs-runtime";
1367
+ var REMOTE_FACADE_CHUNK_NAME = "fulgurjs-remote-facades";
1368
+ function facadeChunkOf(id) {
1369
+ const bare = id.split("?")[0];
1370
+ if (bare === "virtual:fulgurjs-runtime" || bare === "virtual:fulgurjs-runtime-proxy") {
1371
+ return RUNTIME_CHUNK_NAME;
1372
+ }
1373
+ if (bare.startsWith("virtual:fulgurjs-shared:")) {
1374
+ const body = bare.slice("virtual:fulgurjs-shared:".length);
1375
+ if (body.startsWith("__remote__/")) {
1376
+ const remoteName = body.slice("__remote__/".length).split("/")[0] || "unknown";
1377
+ return REMOTE_FACADE_CHUNK_NAME + "-" + remoteName.replace(/[^A-Za-z0-9_-]/g, "_");
1378
+ }
1379
+ const shareKey = body.split("?")[0];
1380
+ return "fulgurjs-shared-" + (shareKey.replace(/[^A-Za-z0-9_-]/g, "_") || "unknown");
1381
+ }
1382
+ if (bare.startsWith("virtual:fulgurjs-shared-ns:") || bare.startsWith("virtual:fulgurjs-cjs-ns:")) {
1383
+ const prefix = bare.startsWith("virtual:fulgurjs-shared-ns:") ? "virtual:fulgurjs-shared-ns:" : "virtual:fulgurjs-cjs-ns:";
1384
+ const shareKey = bare.slice(prefix.length);
1385
+ return "fulgurjs-shared-" + (shareKey.replace(/[^A-Za-z0-9_-]/g, "_") || "unknown");
1386
+ }
1387
+ return null;
1388
+ }
1342
1389
  function federation(options) {
1343
1390
  const warnedUnknownPrefixes = /* @__PURE__ */ new Set();
1344
1391
  let remoteSchemaPromise = null;
@@ -1349,7 +1396,10 @@ function federation(options) {
1349
1396
  exposeFiles: {},
1350
1397
  resolvedSharedPaths: /* @__PURE__ */ new Map(),
1351
1398
  entryAbsPaths: /* @__PURE__ */ new Set(),
1352
- entryInitInjected: /* @__PURE__ */ new Set()
1399
+ entryInitInjected: /* @__PURE__ */ new Set(),
1400
+ manualChunkGroups: /* @__PURE__ */ new Map(),
1401
+ sharedClosureRoots: [],
1402
+ facadeDynamic: false
1353
1403
  };
1354
1404
  const pre = {
1355
1405
  name: "fulgurjs:core",
@@ -1417,6 +1467,43 @@ export { default } from ${JSON.stringify(url)};
1417
1467
  ;
1418
1468
  extra.build = { target: "es2022" };
1419
1469
  }
1470
+ if (normalized.remotes.length > 0 && normalized.devSharedSelf) {
1471
+ state.facadeDynamic = true;
1472
+ const userOutput = userConfig.build?.rollupOptions?.output;
1473
+ if (Array.isArray(userOutput)) {
1474
+ console.warn(
1475
+ formatFulgurjsDiagnostic({
1476
+ code: "BLD-006",
1477
+ symptom: "build.rollupOptions.output is an array; fulgurjs cannot inject the shared-facade chunk guard automatically",
1478
+ cause: "devSharedSelf \u95E8\u9762\u5316 + manualChunks \u5F3A\u5236\u5206\u7EC4\u53EF\u80FD\u5F62\u6210 chunk \u5FAA\u73AF\u4F9D\u8D56\uFF08\u8FD0\u884C\u65F6 TypeError\uFF1A\u534F\u5546\u51FD\u6570\u672A\u521D\u59CB\u5316\uFF09",
1479
+ fix: `\u5728\u6BCF\u4E2A output \u9879\u7684 manualChunks \u6700\u524D\u9762\u52A0\u5206\u652F\uFF1Aif (id.startsWith('virtual:fulgurjs-')) return 'fulgurjs-shared-facades'`
1480
+ })
1481
+ );
1482
+ } else {
1483
+ const userManualChunks = userOutput?.manualChunks;
1484
+ if (typeof userManualChunks === "function") {
1485
+ const userFn = userManualChunks;
1486
+ const wrapped = (id, meta) => {
1487
+ const facade = facadeChunkOf(id);
1488
+ if (facade) return facade;
1489
+ return userFn(id, meta);
1490
+ };
1491
+ const extraBuild = extra.build ??= {};
1492
+ extraBuild.rollupOptions = { ...extraBuild.rollupOptions ?? {}, output: { manualChunks: wrapped } };
1493
+ } else if (userManualChunks && typeof userManualChunks === "object") {
1494
+ state.manualChunkSpecsPending = Object.entries(userManualChunks).flatMap(
1495
+ ([group, specs]) => specs.map((s) => [group, s])
1496
+ );
1497
+ const wrapped = (id) => {
1498
+ const facade = facadeChunkOf(id);
1499
+ if (facade) return facade;
1500
+ return state.manualChunkGroups.get(id.split("?")[0]) ?? state.manualChunkGroups.get(id);
1501
+ };
1502
+ const extraBuild = extra.build ??= {};
1503
+ extraBuild.rollupOptions = { ...extraBuild.rollupOptions ?? {}, output: { manualChunks: wrapped } };
1504
+ }
1505
+ }
1506
+ }
1420
1507
  }
1421
1508
  for (const w of normalized.warnings) console.warn(`[fulgurjs] ${w}`);
1422
1509
  return extra;
@@ -1521,7 +1608,7 @@ export { default } from ${JSON.stringify(url)};
1521
1608
  if (!names.includes(compat)) names.push(compat);
1522
1609
  }
1523
1610
  }
1524
- return genSharedNsFacade(item, serializeShareCallForFacade(item), names);
1611
+ return genSharedNsFacade(item, serializeShareCallForFacade(item), names, state.facadeDynamic);
1525
1612
  }
1526
1613
  if (clean.startsWith("virtual:fulgurjs-shared:") && state.normalized) {
1527
1614
  const body = clean.slice("virtual:fulgurjs-shared:".length);
@@ -1530,14 +1617,14 @@ export { default } from ${JSON.stringify(url)};
1530
1617
  const item2 = state.normalized.shared.find((x) => x.shareKey === body);
1531
1618
  if (item2 && item2.import !== false) {
1532
1619
  const names = /^(\.|\/)/.test(item2.import) ? [] : enumerateCjsExports(item2.import, state.normalized.root);
1533
- return genSharedFacade(item2.import, names);
1620
+ return genSharedFacade(item2.import, names, state.facadeDynamic);
1534
1621
  }
1535
1622
  return null;
1536
1623
  }
1537
1624
  const entry = getFacadeEntry(raw.slice(f + 3));
1538
1625
  if (!entry) return null;
1539
1626
  if (entry.remoteName && entry.exposeName) {
1540
- return genRemoteBindingFacade(entry.remoteName + "/" + entry.exposeName, entry.bindings);
1627
+ return genRemoteBindingFacade(entry.remoteName + "/" + entry.exposeName, entry.bindings, state.facadeDynamic);
1541
1628
  }
1542
1629
  const item = state.normalized.shared.find((x) => x.shareKey === entry.shareKey);
1543
1630
  if (!item || item.import === false) return null;
@@ -1550,7 +1637,7 @@ export { default } from ${JSON.stringify(url)};
1550
1637
  "fallback: () => import(" + JSON.stringify(item.import) + ")"
1551
1638
  ];
1552
1639
  const call = "__fulgurjs_loadShare(" + JSON.stringify(item.shareKey) + ", { " + opts.join(", ") + " })";
1553
- return genBindingFacade(item, entry.bindings, call);
1640
+ return genBindingFacade(item, entry.bindings, call, state.facadeDynamic);
1554
1641
  }
1555
1642
  return null;
1556
1643
  },
@@ -1571,7 +1658,8 @@ ${code}`, map: null };
1571
1658
  options: state.normalized,
1572
1659
  rewriteShared: true,
1573
1660
  allowNodeModules,
1574
- cjsRequireRewrite: true
1661
+ cjsRequireRewrite: true,
1662
+ sharedClosureRoots: state.sharedClosureRoots
1575
1663
  });
1576
1664
  }
1577
1665
  return null;
@@ -1583,7 +1671,8 @@ ${code}`, map: null };
1583
1671
  // 双角色宿主可显式 devSharedSelf: true 参与协商
1584
1672
  rewriteShared: state.command === "build" || state.normalized.devSharedSelf,
1585
1673
  allowNodeModules,
1586
- cjsRequireRewrite: state.command === "build"
1674
+ cjsRequireRewrite: state.command === "build",
1675
+ sharedClosureRoots: state.sharedClosureRoots
1587
1676
  });
1588
1677
  },
1589
1678
  transformIndexHtml: {
@@ -1608,6 +1697,68 @@ ${code}`, map: null };
1608
1697
  async buildStart() {
1609
1698
  if (state.command !== "build" || !state.normalized) return;
1610
1699
  const n = state.normalized;
1700
+ if (state.manualChunkSpecsPending) {
1701
+ for (const [group, specifier] of state.manualChunkSpecsPending) {
1702
+ try {
1703
+ const r = await this.resolve(specifier, import_node_path5.default.join(n.root, "index.html"));
1704
+ if (r?.id) {
1705
+ state.manualChunkGroups.set(r.id, group);
1706
+ state.manualChunkGroups.set(r.id.split("?")[0], group);
1707
+ } else {
1708
+ console.warn(
1709
+ `[fulgurjs] manualChunks: could not resolve "${specifier}" for group "${group}"; those modules will fall back to automatic chunking. (Their shared-negotiation facades are still guarded.)`
1710
+ );
1711
+ }
1712
+ } catch {
1713
+ console.warn(
1714
+ `[fulgurjs] manualChunks: resolving "${specifier}" (group "${group}") failed; those modules will fall back to automatic chunking.`
1715
+ );
1716
+ }
1717
+ }
1718
+ state.manualChunkSpecsPending = void 0;
1719
+ }
1720
+ if (n.remotes.length > 0 && n.devSharedSelf && state.sharedClosureRoots.length === 0) {
1721
+ const rootKeys = /* @__PURE__ */ new Map();
1722
+ const pkgRootOf = (file) => {
1723
+ const NM = "/node_modules/";
1724
+ const i = file.lastIndexOf(NM);
1725
+ if (i === -1) return null;
1726
+ const rest = file.slice(i + NM.length);
1727
+ const segs = rest.split("/");
1728
+ const pkgDir = segs[0].startsWith("@") ? segs.slice(0, 2).join("/") : segs[0];
1729
+ return file.slice(0, i + NM.length) + pkgDir + "/";
1730
+ };
1731
+ for (const item of n.shared) {
1732
+ if (item.import === false) continue;
1733
+ const selfFile = await this.resolve(item.import, import_node_path5.default.join(n.root, "index.html")).then(
1734
+ (r) => r?.id?.split("?")[0] ?? null
1735
+ ).catch(() => null);
1736
+ for (const alias of item.aliases) {
1737
+ if (alias.endsWith("/")) continue;
1738
+ let file = null;
1739
+ if (selfFile) {
1740
+ try {
1741
+ const req = (0, import_node_module2.createRequire)(selfFile);
1742
+ file = req.resolve(`${alias}/package.json`, { paths: [selfFile] }).replace(/\/package\.json$/, "");
1743
+ } catch {
1744
+ file = null;
1745
+ }
1746
+ }
1747
+ if (!file) {
1748
+ file = await this.resolve(alias, import_node_path5.default.join(n.root, "index.html")).then(
1749
+ (r) => r?.id?.split("?")[0] ?? null
1750
+ ).catch(() => null);
1751
+ }
1752
+ if (!file) continue;
1753
+ const root = pkgRootOf(file);
1754
+ if (!root) continue;
1755
+ let keys = rootKeys.get(root);
1756
+ if (!keys) rootKeys.set(root, keys = /* @__PURE__ */ new Set());
1757
+ keys.add(item.shareKey);
1758
+ }
1759
+ }
1760
+ state.sharedClosureRoots = [...rootKeys.entries()].map(([root, keys]) => ({ root, keys }));
1761
+ }
1611
1762
  if (n.exposes.length > 0) {
1612
1763
  for (const e of n.exposes) {
1613
1764
  const resolved = await this.resolve(e.import, import_node_path5.default.join(n.root, "index.html"));
@@ -1738,12 +1889,25 @@ ${code}`, map: null };
1738
1889
  return { code: code.split("virtual:fulgurjs-runtime").join("virtual:fulgurjs-runtime-proxy"), map: null };
1739
1890
  }
1740
1891
  if (isPluginProcessedModule(code)) return null;
1741
- if (state.command === "build" && !/\.vue(\?|$)/.test(id)) return null;
1742
1892
  if (/type=(style|template)/.test(id)) return null;
1743
1893
  const isJsLike = /\.(m|c)?[jt]sx?$/.test(clean) || clean.endsWith(".vue");
1744
1894
  if (!isJsLike) return null;
1745
1895
  const devRewriteAll = state.command === "serve" && state.normalized.devSharedSelf;
1746
- if (clean.includes("node_modules") && !id.includes(".vite/deps") && !devRewriteAll) return null;
1896
+ const isPureRemoteBuild = state.normalized.exposes.length > 0 && state.normalized.remotes.length === 0;
1897
+ if (state.command === "serve" && clean.includes("node_modules") && !id.includes(".vite/deps") && !devRewriteAll) {
1898
+ return null;
1899
+ }
1900
+ if (state.command === "build" && !/\.vue(\?|$)/.test(id)) {
1901
+ const quickCheck = /require\s*\(\s*["']|(?:from|import)\s*["']/.test(code);
1902
+ if (!quickCheck) return null;
1903
+ const allowNodeModules = isPureRemoteBuild;
1904
+ return transformModule(code, id, {
1905
+ options: state.normalized,
1906
+ rewriteShared: true,
1907
+ allowNodeModules,
1908
+ sharedClosureRoots: state.sharedClosureRoots
1909
+ });
1910
+ }
1747
1911
  const rewriteShared = state.command === "build" || state.normalized.devSharedSelf;
1748
1912
  const remapSpecifier = (spec) => {
1749
1913
  if (state.normalized.shared.some((s) => s.aliases.includes(spec))) return spec;
@@ -1757,7 +1921,13 @@ ${code}`, map: null };
1757
1921
  if (!cleanPath.startsWith("/")) return null;
1758
1922
  return state.resolvedSharedPaths.get(cleanPath) ?? null;
1759
1923
  };
1760
- return transformModule(code, id, { options: state.normalized, remapSpecifier, rewriteShared });
1924
+ return transformModule(code, id, {
1925
+ options: state.normalized,
1926
+ remapSpecifier,
1927
+ rewriteShared,
1928
+ allowNodeModules: state.command === "build" ? isPureRemoteBuild || state.normalized.devSharedSelf : void 0,
1929
+ sharedClosureRoots: state.sharedClosureRoots
1930
+ });
1761
1931
  }
1762
1932
  };
1763
1933
  function warmResolvedSharedPaths(server) {