@flatjs/evolve 2.0.13 → 2.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- import{existsSync,readFileSync}from"node:fs";import{isAbsolute,join}from"node:path";import{ensureSlash,urlJoin}from"@flatjs/common";import _ from"lodash";import{devReactFastRefresh}from"../../constants.js";import{allowPx2remForModule}from"../../helpers/allow-px2rem-for-module.js";import{getHtmlPluginConfig}from"../../helpers/get-html-plugin-config.js";import{getPackageDir}from"../../helpers/get-pacakge-dir.js";import{normalizeEvolveEntryName}from"../../helpers/normalize-entry-map.js";import{injectFederationScripts}from"../../helpers/script-injects.js";import{shouldEnableReactFastRefresh}from"../../helpers/should-enable-react-fast-refresh.js";const getPageMainHtml=async(e,t,r)=>{const l=getPackageDir(),a=readFileSync(join(l,"./templates/main.html"),"utf-8"),o=[],i=r.projectVirtualPath.replace(/^\//,"");for(const[l,a]of Object.entries(r.entryMap)){const s=a.options?.servePageMainLinkFn||(e=>e),n=Object.keys(e).includes(l),m=normalizeEvolveEntryName(l,i),c=r.devServer?.pageProxy||"/pages",p=urlJoin(t,[c,m],{env:"me"});o.push({name:l.replace(i,"").replace(/^\//,""),link:s(p,{hostUri:t,entryName:l,virtualPath:i}),flagText:n?"serve":"static",isServed:n?1:0})}const s={title:"@flatjs/evolve",modules:o.sort(((e,t)=>t.isServed-e.isServed))};return _.template(a)(s)},getPageModuleHtml=async(e,t,r,l,a)=>{const{entryMap:o,projectVirtualPath:i}=a,s=Object.keys(o).sort(((e,t)=>t.length-e.length)),n=getPackageDir(),m=s.find((e=>{const r=normalizeEvolveEntryName(e,i);return ensureSlash(t.path.replace(/^\//,""),!0).startsWith(ensureSlash(r,!0))}));if(!m){const e=readFileSync(join(n,"./templates/module-404.html"),"utf-8");return _.template(e)({title:"404 Not Found",errorMeta:[{name:"@flatjs/evolve workspace",value:n},{name:"served entry names",value:JSON.stringify(s)},{name:"module path",value:`${t.path}`}]})}const c=o[m],p=c.options,d=e[m],u=d?.devServerHostUri||r,f=p?.serveModuleTemplate||"./templates/module.html",g=join(a.projectCwd,"./templates/module.html"),h=readFileSync(isAbsolute(f)?f:existsSync(g)?g:join(n,"./templates/module.html"),"utf-8"),v=a.devServer,S=v?.defaultServeGlobalData?await v.defaultServeGlobalData(c,r):{},j={mode:"development",envCdn:urlJoin(r,["public"])};let y=normalizeEvolveEntryName(m,i);v?.bundleDirResolver&&(y=v?.bundleDirResolver(y,{currEntry:m,projectVirtualPath:i}));const P=v?.pageProxy||"/pages",b={title:getHtmlPluginConfig("title",j,p?.title),favicon:getHtmlPluginConfig("favicon",j,p?.favicon),viewport:allowPx2remForModule([m,c],a)?getHtmlPluginConfig("viewport",j,p?.viewport):"",headBeforeHtmlTags:getHtmlPluginConfig("headBeforeHtmlTags",j,p?.headBeforeHtmlTags),inlineScripts:getHtmlPluginConfig("inlineScripts",j,p?.inlineScripts),headBeforeStyles:getHtmlPluginConfig("headBeforeStyles",j,p?.headBeforeStyles),headBeforeScripts:getHtmlPluginConfig("headBeforeScripts",j,p?.headBeforeScripts),bodyAfterScripts:getHtmlPluginConfig("bodyAfterScripts",j,p?.bodyAfterScripts),moduleFederationScripts:injectFederationScripts({me:[urlJoin(r,["public"])]},a.multiHtmlCdnEnvResolver),global:{hostUrl:r,apiBase:urlJoin(r,[l]),virtualPath:join(P,i),moduleName:m.replace(i,"").replace(/^\//,""),...S,...p?.serveGlobalData||{}},styles:[urlJoin(u,[join("public",y,"bundle.css")])],scripts:[urlJoin(u,[join("public",y,"bundle.js")])]};if(e[m]&&shouldEnableReactFastRefresh(!0,[y,c],a)){const e=[devReactFastRefresh.runtime,devReactFastRefresh.reactRefreshSetup];b.headBeforeHtmlTags?b.headBeforeHtmlTags=b.headBeforeHtmlTags.slice(0):b.headBeforeHtmlTags=[],e.forEach((e=>{const t=urlJoin(u,[join("public",join(y,e),"bundle.js")]);b.headBeforeHtmlTags?.push(`<script src="${t}"><\/script>`)}))}return _.template(h)(b)};export const createPageMiddleware=(e,t,r,l)=>(l.devServer?.middlewares||[]).concat((async(a,o)=>{let i;i="/"===a.path?await getPageMainHtml(r,e,l):await getPageModuleHtml(r,a,e,t,l),o.send(i)}));
1
+ import{existsSync,readFileSync}from"node:fs";import{isAbsolute,join}from"node:path";import{ensureSlash,urlJoin}from"@flatjs/common";import _ from"lodash";import{devReactFastRefresh}from"../../constants.js";import{allowPx2remForModule}from"../../helpers/allow-px2rem-for-module.js";import{getHtmlPluginConfig}from"../../helpers/get-html-plugin-config.js";import{getPackageDir}from"../../helpers/get-pacakge-dir.js";import{normalizeEvolveEntryName}from"../../helpers/normalize-entry-map.js";import{injectFederationScripts}from"../../helpers/script-injects.js";import{shouldEnableReactFastRefresh}from"../../helpers/should-enable-react-fast-refresh.js";const getPageMainHtml=async(e,t,r)=>{const l=getPackageDir(),a=readFileSync(join(l,"./templates/main.html"),"utf-8"),o=[],i=r.projectVirtualPath.replace(/^\//,"");for(const[l,a]of Object.entries(r.entryMap)){const s=a.options?.servePageMainLinkFn||(e=>e),n=Object.keys(e).includes(l),m=normalizeEvolveEntryName(l,i),c=r.devServer?.pageProxy||"/pages",p=urlJoin(t,[c,m],{env:"me"});o.push({name:l.replace(i,"").replace(/^\//,""),link:s(p,{hostUri:t,entryName:l,virtualPath:i}),flagText:n?"serve":"static",isServed:n?1:0})}const s={title:"@flatjs/evolve",modules:o.sort(((e,t)=>t.isServed-e.isServed))};return _.template(a)(s)},getPageModuleHtml=async(e,t,r,l,a)=>{const{entryMap:o,projectVirtualPath:i}=a,s=Object.keys(o).sort(((e,t)=>t.length-e.length)),n=getPackageDir(),m=s.find((e=>{const r=normalizeEvolveEntryName(e,i);return ensureSlash(t.path.replace(/^\//,""),!0).startsWith(ensureSlash(r,!0))}));if(!m){const e=readFileSync(join(n,"./templates/module-404.html"),"utf-8");return _.template(e)({title:"404 Not Found",errorMeta:[{name:"@flatjs/evolve workspace",value:n},{name:"served entry names",value:JSON.stringify(s)},{name:"module path",value:`${t.path}`}]})}const c=o[m],p=c.options,d=e[m],u=d?.devServerHostUri||r,f=p?.serveModuleTemplate||"./templates/module.html",g=join(a.projectCwd,"./templates/module.html"),h=readFileSync(isAbsolute(f)?f:existsSync(g)?g:join(n,"./templates/module.html"),"utf-8"),v=a.devServer,S=v?.defaultServeGlobalData?await v.defaultServeGlobalData(c,r):{},j={mode:"development",envCdn:urlJoin(r,["public"])};let y=normalizeEvolveEntryName(m,i);if(v?.bundleDirResolver){const t=Object.keys(e).includes(m);y=v?.bundleDirResolver(y,{currEntry:m,projectVirtualPath:i,isServedModule:t})}const P=v?.pageProxy||"/pages",b={title:getHtmlPluginConfig("title",j,p?.title),favicon:getHtmlPluginConfig("favicon",j,p?.favicon),viewport:allowPx2remForModule([m,c],a)?getHtmlPluginConfig("viewport",j,p?.viewport):"",headBeforeHtmlTags:getHtmlPluginConfig("headBeforeHtmlTags",j,p?.headBeforeHtmlTags),inlineScripts:getHtmlPluginConfig("inlineScripts",j,p?.inlineScripts),headBeforeStyles:getHtmlPluginConfig("headBeforeStyles",j,p?.headBeforeStyles),headBeforeScripts:getHtmlPluginConfig("headBeforeScripts",j,p?.headBeforeScripts),bodyAfterScripts:getHtmlPluginConfig("bodyAfterScripts",j,p?.bodyAfterScripts),moduleFederationScripts:injectFederationScripts({me:[urlJoin(r,["public"])]},a.multiHtmlCdnEnvResolver),global:{hostUrl:r,apiBase:urlJoin(r,[l]),virtualPath:join(P,i),moduleName:m.replace(i,"").replace(/^\//,""),...S,...p?.serveGlobalData||{}},styles:[urlJoin(u,[join("public",y,"bundle.css")])],scripts:[urlJoin(u,[join("public",y,"bundle.js")])]};if(e[m]&&shouldEnableReactFastRefresh(!0,[y,c],a)){const e=[devReactFastRefresh.runtime,devReactFastRefresh.reactRefreshSetup];b.headBeforeHtmlTags?b.headBeforeHtmlTags=b.headBeforeHtmlTags.slice(0):b.headBeforeHtmlTags=[],e.forEach((e=>{const t=urlJoin(u,[join("public",join(y,e),"bundle.js")]);b.headBeforeHtmlTags?.push(`<script src="${t}"><\/script>`)}))}return _.template(h)(b)};export const createPageMiddleware=(e,t,r,l)=>(l.devServer?.middlewares||[]).concat((async(a,o)=>{let i;i="/"===a.path?await getPageMainHtml(r,e,l):await getPageModuleHtml(r,a,e,t,l),o.send(i)}));
@@ -46,9 +46,28 @@ export type FlatEvolveDevServerOptions = {
46
46
  * @default `(dir)=>dir`
47
47
  * @returns
48
48
  */
49
- bundleDirResolver?: (dir: string, data: {
49
+ bundleDirResolver?: (
50
+ /**
51
+ * The default normalized dir path
52
+ */
53
+ dir: string,
54
+ /**
55
+ * The resolver parameters
56
+ * @param data.currEntry ddd
57
+ */
58
+ data: {
59
+ /**
60
+ * The current entry
61
+ */
50
62
  currEntry: string;
63
+ /**
64
+ * The project virtual path
65
+ */
51
66
  projectVirtualPath: string;
67
+ /**
68
+ * The value indicates if current module is `static` or `served` module.
69
+ */
70
+ isServedModule: boolean;
52
71
  }) => string;
53
72
  /**
54
73
  * The value indicates if we will auto open broswers page for `serve`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flatjs/evolve",
3
- "version": "2.0.13",
3
+ "version": "2.0.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {