@moneko/core 4.8.6-beta.12 → 4.8.6-beta.13
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/lib/dev/server.d.mts +2 -1
- package/lib/dev/server.mjs +1 -1
- package/lib/plugin/html-plugin.mjs +1 -1
- package/package.json +1 -1
package/lib/dev/server.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { createReadStream, existsSync } from 'node:fs';
|
|
1
|
+
import type { createReadStream, existsSync, statSync } from 'node:fs';
|
|
2
2
|
import { type IncomingMessage, type Server as HttpServer, type ServerResponse } from 'node:http';
|
|
3
3
|
import { type Server as HttpsServer } from 'node:https';
|
|
4
4
|
import type { OutputFileSystem } from 'webpack';
|
|
@@ -6,6 +6,7 @@ import { type CertOption } from '../dev/get-cert.mjs';
|
|
|
6
6
|
export type FileSystem = OutputFileSystem & {
|
|
7
7
|
existsSync: typeof existsSync;
|
|
8
8
|
createReadStream: typeof createReadStream;
|
|
9
|
+
statSync: typeof statSync;
|
|
9
10
|
};
|
|
10
11
|
type HttpServerResponse = ServerResponse<IncomingMessage> & {
|
|
11
12
|
req: IncomingMessage;
|
package/lib/dev/server.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createServer as e}from"node:http";import{createServer as t}from"node:https";import{platform as o}from"node:os";import{extname as r,join as n,sep as i}from"node:path";import{env as s}from"node:process";import{ink as a,println as
|
|
1
|
+
import{createServer as e}from"node:http";import{createServer as t}from"node:https";import{platform as o}from"node:os";import{extname as r,join as n,sep as i}from"node:path";import{env as s}from"node:process";import{ink as a,println as l}from"@moneko/utils";import{CA_ORGANIZATION as m,isCertificateInstalled as p}from"../commom/ca.mjs";import c from"../commom/mime.mjs";import d from"../commom/open.mjs";import{CONFIG as f}from"../config.mjs";import C from"../dev/get-cert.mjs";import{setupProxy as h}from"../dev/proxy.mjs";import u,{cacheBody as y}from"./mock.mjs";let A={"Access-Control-Allow-Credentials":"true","Access-Control-Allow-Headers":"Authorization, Content-Type, Accept, X-Requested-With, Origin, DNT, User-Agent, Cache-Control, X-CSRF-Token, X-API-Key, X-Client-Version, X-Device-ID, Content-Language, Accept-Language, X-Custom-Header, X-Mx-ReqToken, Keep-Alive, If-Modified-Since","Access-Control-Allow-Methods":"GET, DELETE, HEAD, OPTIONS, POST, PUT, PATCH, PURGE, LINK, UNLINK, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, REPORT, SEARCH, CONNECT, TRACE","Access-Control-Allow-Origin":"*","Cache-Control":"no-store, no-cache, must-revalidate, proxy-revalidate",Pragma:"no-cache",Expires:"0"};async function E(E){let S=n(E.root_dir,"index.html"),T=u();function g(e,t,o){let n=e.headers["accept-encoding"],i=n?.includes("br"),s=`${o}.${i?"br":"gz"}`,a=E.fileSystem.existsSync(s);a&&t.setHeader("Content-Encoding",i?"br":"gzip");let l=E.fileSystem.createReadStream(a?s:o,{autoClose:!0});l.on("open",()=>{t.writeHead(200,Object.assign({},A,E.headers,{"Access-Control-Allow-Origin":e.headers.origin??"*","Content-Type":c[r(o)]||"application/octet-stream"}))}),l.on("error",e=>{switch(l.readable=!0,e.code){case"ENOENT":t.writeHead(301,{Location:"/index.html"}),t.end();break;case"EACCES":t.writeHead(403,{"Content-Type":"text/plain"}),t.end(`Forbidden: ${e.message}`);break;default:t.writeHead(500,{"Content-Type":"text/plain"}),t.end(`Internal Server Error: ${e.message}`)}}),l.pipe(t)}async function w(e,t){if(y(e),!(E.onRequest&&E.onRequest(e,t))){if(!(f.proxy&&await h(e,t,f.proxy)||t.writableEnded||T&&T(e,t))&&!t.writableEnded){if("GET"===e.method){let o=("/"===e.url?S:decodeURIComponent(n(E.root_dir,e.url.replace(E.basename??"/","/")))).split("?")[0];if(E.fileSystem.existsSync(o))if(!E.fileSystem.statSync(o).isDirectory())return g(e,t,o);else{let r=n(o,"index.html");if(E.fileSystem.existsSync(r))return g(e,t,r)}}if(e.headers.accept?.includes("text/html"))return g(e,t,S.split("?")[0]);t.writeHead(404,{"Content-Type":"text/plain"}),t.end("File Not Found")}}}let x=E.https?t(await C(E.host,!0===E.https?{install:!0}:E.https),w):e(w);if(x.listen(E.port),E.https&&!await p(m)&&"darwin"===o()){let e=s.npm_execpath?.split(i).pop()?.split(".").shift()||"npm",t=s.npm_lifecycle_event??"start",o=`> sudo ${e} ${t}`;l(a("⚠️ 未检测到本地 SSL 证书,请通过以下命令重启项目以安装证书: ","yellow")),l(a(o,"yellow")),l("")}return E.open&&d(`${E.https?"https:":"http:"}//${E.host}:${E.port}${"/"===E.basename?"":E.basename}`),x}export default E;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{existsSync as t,readFileSync as e}from"node:fs";import{basename as s,resolve as i,sep as o}from"node:path";import n from"webpack";import{isNetworkUrl as a,join as h}from"../commom/posix.mjs";export class HtmlPlugin{constructor(t={}){this.AddEntry=/(?:.*\/)?main\.[a-zA-Z0-9]+\.entry\.js$/,this.faviconPath="",this.name="HtmlPlugin",this.cachedMetaTags=null,this.cachedCustomTags=null,this.options={title:"WebApp",filename:"index.html",inject:"head",...t}}getPublicPath(t){if("string"==typeof this.options.publicPath)return this.options.publicPath;let e=t.getAssetPath(t.outputOptions.publicPath,{hash:t.hash}),s="auto"===e?"
|
|
1
|
+
import{existsSync as t,readFileSync as e}from"node:fs";import{basename as s,resolve as i,sep as o}from"node:path";import n from"webpack";import{isNetworkUrl as a,join as h}from"../commom/posix.mjs";export class HtmlPlugin{constructor(t={}){this.AddEntry=/(?:.*\/)?main\.[a-zA-Z0-9]+\.entry\.js$/,this.faviconPath="",this.name="HtmlPlugin",this.cachedMetaTags=null,this.cachedCustomTags=null,this.options={title:"WebApp",filename:"index.html",inject:"head",...t}}getPublicPath(t){if("string"==typeof this.options.publicPath)return this.options.publicPath;let e=t.getAssetPath(t.outputOptions.publicPath,{hash:t.hash}),s="auto"===e?"":e;return s.length&&!s.endsWith("/")&&(s+="/"),s}apply(l){l.hooks.compilation.tap(this.name,r=>{r.hooks.processAssets.tap({name:this.name,stage:n.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL},()=>{let c;if(this.options.favicon)if(a(this.options.favicon))this.faviconPath=this.options.favicon;else{let a=i(l.context,this.options.favicon);if(t(a)){let t=s(this.options.favicon),i=e(a);r.emitAsset(t,new n.sources.RawSource(i),{javascriptModule:!1});let l=this.getPublicPath(r);this.faviconPath=h(l,t).replaceAll(o,"/")}else r.warnings.push(new n.WebpackError(`Favicon file ${a} does not exist`))}if(c=this.options.templateContent?this.options.templateContent(this.options):this.options.template?e(i(l.context,this.options.template),{encoding:"utf-8"}):this.generateDefaultTemplate(),this.options.inject){let t=this.getAssets(r);c=this.injectAssets(c,t,this.options.inject)}let p="function"==typeof this.options.filename?this.options.filename("index"):this.options.filename||"index.html";r.emitAsset(p,new n.sources.RawSource(c),{javascriptModule:!1})})})}generateDefaultTemplate(){let t=this.generateMetaTags();return`<!DOCTYPE html><html><head><meta charset="utf-8">${t}<title>${this.options.title}</title><link rel="icon" type="image/x-icon" href="${this.faviconPath}"></head><body><div id="root"></div></body></html>`}generateMetaTags(){if(null!==this.cachedMetaTags)return this.cachedMetaTags;if(!this.options.meta)return this.cachedMetaTags="","";let t=Object.entries(this.options.meta).map(([t,e])=>{if("string"==typeof e)return`<meta name="${t}" content="${e}">`;if(void 0===e)return"";let s=Object.entries(e).map(([t,e])=>`${t}="${e}"`).join(" ");return`<meta ${s}>`}).join("");return this.cachedMetaTags=t,t}generateCustomTags(){if(null!==this.cachedCustomTags)return this.cachedCustomTags;let t={head:"",body:""};if(!this.options.tags)return this.cachedCustomTags=t,t;let e=[],s=[];return this.options.tags.forEach(t=>{let{tag:i="script",textContent:o="",inject:n=this.options.inject,...a}=t,h=Object.entries(a).filter(([,t])=>null!=t).map(([t,e])=>!0===e?t:`${t}="${e}"`).join(" "),l=`<${i} ${h}>${o}</${i}>`;"head"===n?e.push(l):s.push(l)}),t.head=e.join(""),t.body=s.join(""),this.cachedCustomTags=t,t}getAssets(t){let e={js:[],css:[]},s=["import-scripts","require","async-node"],i=this.getPublicPath(t);for(let n of t.entrypoints.values())for(let t of n.chunks){let n=t.getEntryOptions(),a=n?.chunkLoading;if(!(n?.asyncChunks||a&&s.includes(a)))for(let s of t.files)s.endsWith(".js")?e.js.push(h(i,s).replaceAll(o,"/")):s.endsWith(".css")&&e.css.push(h(i,s).replaceAll(o,"/"))}return e}injectAssets(t,e,s){let i=e.css.length?e.css.map(t=>`<link href="${t}" rel="stylesheet" />`).join(""):"",o=e.js.length?e.js.map(t=>`<script defer ${this.AddEntry.test(t)?'entry="true" ':""}src="${t}"></script>`).join(""):"",n=this.generateCustomTags(),a=t.split("</head>");if(2===a.length){let[t,e]=a,h=e.split("</body>");return"head"===s?`${t}${i}${o}${n.head}</head>${h[0]}${n.body}</body>${h[1]||""}`:`${t}${n.head}</head>${h[0]}${i}${o}${n.body}</body>${h[1]||""}`}return t}}
|