@meng-xi/vite-plugin 0.0.8 → 0.0.9
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/README-en.md +790 -540
- package/README.md +788 -540
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/plugins/index.cjs +1 -1
- package/dist/plugins/index.d.cts +73 -8
- package/dist/plugins/index.d.mts +73 -8
- package/dist/plugins/index.d.ts +73 -8
- package/dist/plugins/index.mjs +1 -1
- package/dist/shared/vite-plugin.BI4kA-bR.mjs +526 -0
- package/dist/shared/{vite-plugin.CIQOGuWb.cjs → vite-plugin.CsdNNQ-4.cjs} +194 -152
- package/package.json +72 -72
- package/dist/shared/vite-plugin.N63Ts2dO.mjs +0 -484
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";const index=require("./vite-plugin.CawoITTT.cjs"),format=require("./vite-plugin.Ba9646wL.cjs");require("crypto");const u$1=require("path"),o$1=require("fs"),validation=require("./vite-plugin.IGZeStMa.cjs"),SPINNER_FRAMES=process.platform==="win32"?["|","/","-","\\"]:["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],ANSI={reset:"\x1B[0G",clearLine:"\x1B[2K",hideCursor:"\x1B[?25l",showCursor:"\x1B[?25h",green:
|
|
2
|
-
`)},"\u8F93\u51FA\u5B8C\u6210\u8FDB\u5EA6")}this.safeExecuteSync(()=>{process.stdout.write(ANSI.showCursor)},"\u6062\u590D\u5149\u6807\u663E\u793A")}addPluginHooks(e){e.config=(t,{command:i})=>(this.isDev=i==="serve",null),e.configResolved=()=>{this.options.enabled&&(this.phase="config",process.stdout.isTTY&&this.safeExecuteSync(()=>{process.stdout.write(ANSI.hideCursor)},"\u9690\u85CF\u5149\u6807"),this.startSpinner())},e.buildStart=()=>{this.options.enabled&&(this.phase="resolve",this.totalModules=0,this.transformedModules=0)},e.resolveId={handler:t=>{this.options.enabled&&(t.includes("node_modules")||t.includes(".virtual")||this.totalModules++)}},e.transform={handler:(t,i)=>{this.options.enabled&&(i.includes("node_modules")||i.includes(".virtual")||(this.phase="transform",this.transformedModules++,this.currentModule=i))}},e.writeBundle=()=>{this.options.enabled&&(this.phase="write",this.update())},e.closeBundle=()=>{this.options.enabled&&this.complete()},e.buildEnd=()=>{this.options.enabled&&(this.isDev||(this.phase="bundle",this.update()))},e.configureServer=()=>{this.options.enabled&&this.isDev&&this.complete()}}destroy(){super.destroy(),this.stopSpinner(),process.stdout.isTTY&&this.safeExecuteSync(()=>{process.stdout.write(ANSI.showCursor)},"\u6062\u590D\u5149\u6807\u663E\u793A")}}const buildProgress=index.createPluginFactory(o);class p extends index.BasePlugin{getDefaultOptions(){return{overwrite:!0,recursive:!0,incremental:!0}}validateOptions(){this.validator.field("sourceDir").required().string().custom(e=>e.trim()!=="","sourceDir \u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32").field("targetDir").required().string().custom(e=>e.trim()!=="","targetDir \u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32").field("overwrite").boolean().field("recursive").boolean().field("incremental").boolean().validate()}getPluginName(){return"copy-file"}getEnforce(){return"post"}async copyFiles(){const{sourceDir:e,targetDir:t,overwrite:i=!0,recursive:
|
|
1
|
+
"use strict";const index=require("./vite-plugin.CawoITTT.cjs"),format=require("./vite-plugin.Ba9646wL.cjs");require("crypto");const u$1=require("path"),o$1=require("fs"),validation=require("./vite-plugin.IGZeStMa.cjs"),SPINNER_FRAMES=process.platform==="win32"?["|","/","-","\\"]:["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],ANSI={reset:"\x1B[0G",clearLine:"\x1B[2K",hideCursor:"\x1B[?25l",showCursor:"\x1B[?25h",green:n=>`\x1B[32m${n}\x1B[39m`,cyan:n=>`\x1B[36m${n}\x1B[39m`,gray:n=>`\x1B[90m${n}\x1B[39m`,bold:n=>`\x1B[1m${n}\x1B[22m`},DEFAULT_THEME={completeColor:ANSI.green,incompleteColor:ANSI.gray,percentageColor:ANSI.bold,phaseColor:ANSI.cyan,moduleColor:ANSI.gray},PHASE_LABELS={idle:"\u7B49\u5F85\u4E2D",config:"\u8BFB\u53D6\u914D\u7F6E",resolve:"\u89E3\u6790\u6A21\u5757",transform:"\u8F6C\u6362\u6A21\u5757",bundle:"\u6253\u5305\u4E2D",write:"\u5199\u5165\u6587\u4EF6",done:"\u6784\u5EFA\u5B8C\u6210"};class o extends index.BasePlugin{totalModules=0;transformedModules=0;currentModule="";phase="idle";spinnerIndex=0;spinnerTimer=null;isDev=!1;theme=DEFAULT_THEME;lastPercentage=0;static ANSI_REGEX=/\x1b\[[0-9;]*m/g;static stripAnsi(e){return e.replace(o.ANSI_REGEX,"")}getDefaultOptions(){return{width:30,format:"bar",completeChar:"\u2588",incompleteChar:"\u2591",clearOnComplete:!0,showModuleName:!0}}validateOptions(){this.validator.field("width").number().custom(e=>!e||e>0,"width \u5FC5\u987B\u5927\u4E8E 0").field("format").custom(e=>!e||["bar","spinner","minimal"].includes(e),"format \u5FC5\u987B\u662F bar, spinner \u6216 minimal").field("completeChar").string().field("incompleteChar").string().field("clearOnComplete").boolean().field("showModuleName").boolean().validate()}getPluginName(){return"build-progress"}onConfigResolved(e){super.onConfigResolved(e),this.theme=this.options.theme||DEFAULT_THEME}getPercentage(){if(this.phase==="done")return this.lastPercentage=100;if(this.phase==="config")return this.lastPercentage=5;if(this.phase==="resolve")return this.lastPercentage=10;if(this.totalModules===0)return this.lastPercentage=15;const e=Math.min(this.transformedModules/this.totalModules*70,70),t=this.phase==="bundle"?10:0,i=this.phase==="write"?5:0,r=Math.min(Math.floor(15+e+t+i),99);return this.lastPercentage=Math.max(r,this.lastPercentage),this.lastPercentage}renderBar(e){const t=this.options.width||30,i=this.options.completeChar||"\u2588",r=this.options.incompleteChar||"\u2591",s=Math.round(e/100*t),a=t-s;return this.theme.completeColor(i.repeat(s))+this.theme.incompleteColor(r.repeat(a))}renderSpinner(){const e=SPINNER_FRAMES[this.spinnerIndex%SPINNER_FRAMES.length];return this.spinnerIndex++,this.theme.phaseColor(e)}render(e){const t=this.options.format||"bar",i=this.theme.phaseColor(PHASE_LABELS[this.phase]),r=this.theme.percentageColor(`${e}%`);let s="";if(t==="bar"?s=`${this.renderSpinner()} ${i} ${this.renderBar(e)} ${r}`:t==="spinner"?s=`${this.renderSpinner()} ${i} ${r}`:s=`${i} ${r}`,this.options.showModuleName&&this.currentModule&&this.phase==="transform"){const a=o.stripAnsi(s).length,l=Math.max((process.stdout.columns||80)-a-3,20),d=this.currentModule.length>l?"..."+this.currentModule.slice(-l+3):this.currentModule;s+=` ${this.theme.moduleColor(d)}`}return s}update(){if(!process.stdout.isTTY)return;const e=this.getPercentage(),t=this.render(e);this.safeExecuteSync(()=>{process.stdout.write(ANSI.clearLine+ANSI.reset+t)},"\u66F4\u65B0\u8FDB\u5EA6\u663E\u793A")}startSpinner(){this.spinnerTimer||process.stdout.isTTY&&(this.spinnerTimer=setInterval(()=>this.update(),80))}stopSpinner(){this.spinnerTimer&&(clearInterval(this.spinnerTimer),this.spinnerTimer=null)}complete(){if(this.phase="done",this.stopSpinner(),!process.stdout.isTTY){this.logger.success("\u6784\u5EFA\u5B8C\u6210");return}if(this.options.clearOnComplete)this.safeExecuteSync(()=>{process.stdout.write(ANSI.clearLine+ANSI.reset)},"\u6E05\u9664\u8FDB\u5EA6\u884C");else{const e=this.render(100);this.safeExecuteSync(()=>{process.stdout.write(ANSI.clearLine+ANSI.reset+e+`
|
|
2
|
+
`)},"\u8F93\u51FA\u5B8C\u6210\u8FDB\u5EA6")}this.safeExecuteSync(()=>{process.stdout.write(ANSI.showCursor)},"\u6062\u590D\u5149\u6807\u663E\u793A")}addPluginHooks(e){e.config=(t,{command:i})=>(this.isDev=i==="serve",null),e.configResolved=()=>{this.options.enabled&&(this.phase="config",process.stdout.isTTY&&this.safeExecuteSync(()=>{process.stdout.write(ANSI.hideCursor)},"\u9690\u85CF\u5149\u6807"),this.startSpinner())},e.buildStart=()=>{this.options.enabled&&(this.phase="resolve",this.totalModules=0,this.transformedModules=0)},e.resolveId={handler:t=>{this.options.enabled&&(t.includes("node_modules")||t.includes(".virtual")||this.totalModules++)}},e.transform={handler:(t,i)=>{this.options.enabled&&(i.includes("node_modules")||i.includes(".virtual")||(this.phase="transform",this.transformedModules++,this.currentModule=i))}},e.writeBundle=()=>{this.options.enabled&&(this.phase="write",this.update())},e.closeBundle=()=>{this.options.enabled&&this.complete()},e.buildEnd=()=>{this.options.enabled&&(this.isDev||(this.phase="bundle",this.update()))},e.configureServer=()=>{this.options.enabled&&this.isDev&&this.complete()}}destroy(){super.destroy(),this.stopSpinner(),process.stdout.isTTY&&this.safeExecuteSync(()=>{process.stdout.write(ANSI.showCursor)},"\u6062\u590D\u5149\u6807\u663E\u793A")}}const buildProgress=index.createPluginFactory(o);class p extends index.BasePlugin{getDefaultOptions(){return{overwrite:!0,recursive:!0,incremental:!0}}validateOptions(){this.validator.field("sourceDir").required().string().custom(e=>e.trim()!=="","sourceDir \u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32").field("targetDir").required().string().custom(e=>e.trim()!=="","targetDir \u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32").field("overwrite").boolean().field("recursive").boolean().field("incremental").boolean().validate()}getPluginName(){return"copy-file"}getEnforce(){return"post"}async copyFiles(){const{sourceDir:e,targetDir:t,overwrite:i=!0,recursive:r=!0,incremental:s=!0,enabled:a=!0}=this.options;if(!a){this.logger.info(`\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6267\u884C\uFF1A\u4ECE ${e} \u590D\u5236\u5230 ${t}`);return}await format.checkSourceExists(e);const l=await format.copySourceToTarget(e,t,{recursive:r,overwrite:i,incremental:s});this.logger.success(`\u590D\u5236\u6587\u4EF6\u6210\u529F\uFF1A\u4ECE ${e} \u5230 ${t}`,`\u590D\u5236\u4E86 ${l.copiedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8DF3\u8FC7\u4E86 ${l.skippedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8017\u65F6 ${l.executionTime}ms`)}addPluginHooks(e){e.writeBundle=async()=>{await this.safeExecute(()=>this.copyFiles(),"\u590D\u5236\u6587\u4EF6")}}}const copyFile=index.createPluginFactory(p);class R extends index.BasePlugin{projectRoot=process.cwd();tabBarPages=new Set;watcher=null;getDefaultOptions(){return{pagesJsonPath:"src/pages.json",outputPath:"src/router.config.ts",outputFormat:"ts",nameStrategy:"camelCase",includeSubPackages:!0,watch:!0,exportTypes:!0,preserveRouteChanges:!0,metaMapping:{navigationBarTitleText:"title",requireAuth:"requireAuth"}}}validateOptions(){if(this.validator.field("pagesJsonPath").string().field("outputPath").string().field("outputFormat").custom(e=>!e||["ts","js"].includes(e),"outputFormat \u5FC5\u987B\u662F ts \u6216 js").field("nameStrategy").custom(e=>!e||["path","camelCase","pascalCase","custom"].includes(e),"nameStrategy \u5FC5\u987B\u662F path, camelCase, pascalCase \u6216 custom").validate(),this.options.nameStrategy==="custom"&&!this.options.customNameGenerator)throw new Error("\u5F53 nameStrategy \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customNameGenerator")}getPluginName(){return"generate-router"}generateRouteName(e){switch(this.options.nameStrategy){case"path":return e.replace(/\//g,"_").replace(/^_/,"");case"camelCase":return format.toCamelCase(e);case"pascalCase":return format.toPascalCase(e);case"custom":return this.options.customNameGenerator(e);default:return format.toCamelCase(e)}}extractMeta(e,t){const i={},r=e.style||{},s=this.options.metaMapping||{};for(const[a,l]of Object.entries(s))r[a]!==void 0&&(i[l]=r[a]);return this.tabBarPages.has(t)&&(i.isTab=!0),i}parsePageToRoute(e,t=""){const i=t?`/${t}/${e.path}`:`/${e.path}`,r=this.generateRouteName(i),s=this.extractMeta(e,i.replace(/^\//,"")),a={path:i,name:r};return Object.keys(s).length>0&&(a.meta=s),a}parsePagesJson(e){const t=[];if(!e.pages||!Array.isArray(e.pages)||e.pages.length===0)return this.logger.warn("pages.json \u4E2D\u6CA1\u6709\u6709\u6548\u7684\u9875\u9762\u914D\u7F6E"),t;if(this.tabBarPages.clear(),e.tabBar?.list)for(const i of e.tabBar.list)this.tabBarPages.add(i.pagePath);for(const i of e.pages)t.push(this.parsePageToRoute(i));if(this.options.includeSubPackages&&e.subPackages){for(const i of e.subPackages)if(i.pages&&Array.isArray(i.pages))for(const r of i.pages)t.push(this.parsePageToRoute(r,i.root))}return t}generateTypeDefinitions(){return!this.options.exportTypes||this.options.outputFormat==="js"?"":`
|
|
3
3
|
/**
|
|
4
4
|
* \u8DEF\u7531\u5143\u4FE1\u606F
|
|
5
5
|
*/
|
|
@@ -25,16 +25,16 @@ export interface RouteConfig {
|
|
|
25
25
|
/** \u8DEF\u7531\u5143\u4FE1\u606F */
|
|
26
26
|
meta?: RouteMeta
|
|
27
27
|
}
|
|
28
|
-
`}generateFileContent(e){const t=this.generateTypeDefinitions(),i=this.options.outputFormat==="ts",
|
|
28
|
+
`}generateFileContent(e){const t=this.generateTypeDefinitions(),i=this.options.outputFormat==="ts",r=JSON.stringify(e,null," ").replace(/"(\w+)":/g,"$1:").replace(/: "([^"]+)"/g,": '$1'");return`${t}
|
|
29
29
|
/**
|
|
30
30
|
* \u8DEF\u7531\u914D\u7F6E\u5217\u8868
|
|
31
31
|
* @description \u7531 pages.json \u81EA\u52A8\u751F\u6210
|
|
32
32
|
*/
|
|
33
|
-
export const routes${i?": RouteConfig[]":""} = ${
|
|
33
|
+
export const routes${i?": RouteConfig[]":""} = ${r}
|
|
34
34
|
|
|
35
35
|
export default routes
|
|
36
|
-
`}async readPagesJson(){const e=u$1.resolve(this.projectRoot,this.options.pagesJsonPath);if(!o$1.existsSync(e))return this.logger.warn(`pages.json \u6587\u4EF6\u4E0D\u5B58\u5728: ${e}`),null;try{const t=await format.readFileContent(e),i=format.stripJsonComments(t);return JSON.parse(i)}catch(t){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${t.message}`),null}}extractExistingRoutes(e){const t=new Map,i=e.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!i)return t;try{let
|
|
37
|
-
`+e.slice(
|
|
36
|
+
`}async readPagesJson(){const e=u$1.resolve(this.projectRoot,this.options.pagesJsonPath);if(!o$1.existsSync(e))return this.logger.warn(`pages.json \u6587\u4EF6\u4E0D\u5B58\u5728: ${e}`),null;try{const t=await format.readFileContent(e),i=format.stripJsonComments(t);return JSON.parse(i)}catch(t){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${t.message}`),null}}extractExistingRoutes(e){const t=new Map,i=e.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!i)return t;try{let r=i[1].replace(/(\w+)(?=\s*:)/g,'"$1"').replace(/'([^']*)'/g,'"$1"').replace(/,\s*([\]\}])/g,"$1");const s=JSON.parse(r);for(const a of s)a.path&&t.set(a.path,a)}catch{this.logger.warn("\u89E3\u6790\u73B0\u6709 routes \u914D\u7F6E\u5931\u8D25\uFF0C\u5C06\u5B8C\u5168\u91CD\u65B0\u751F\u6210")}return t}mergeRoutes(e,t){return e.map(i=>{const r=t.get(i.path);if(!r)return i;const s={};return i.meta&&Object.assign(s,i.meta),r.meta&&Object.assign(s,r.meta),{...r,path:i.path,meta:Object.keys(s).length>0?s:void 0}})}async generateRouterConfig(){const e=await this.readPagesJson();if(!e)return;let t=this.parsePagesJson(e);const i=u$1.resolve(this.projectRoot,this.options.outputPath);if(this.options.preserveRouteChanges&&o$1.existsSync(i))try{const s=await format.readFileContent(i),a=this.extractExistingRoutes(s);a.size>0&&(t=this.mergeRoutes(t,a),this.logger.info("\u5DF2\u5408\u5E76\u7528\u6237\u5BF9\u8DEF\u7531\u914D\u7F6E\u7684\u4FEE\u6539"))}catch{}const r=this.generateFileContent(t);await format.writeFileContent(i,r),this.logger.success(`\u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u5DF2\u751F\u6210: ${i}`),this.logger.info(`\u5171\u751F\u6210 ${t.length} \u6761\u8DEF\u7531\u914D\u7F6E`)}startWatching(){if(!this.options.watch)return;const e=u$1.resolve(this.projectRoot,this.options.pagesJsonPath);o$1.existsSync(e)&&(this.watcher=o$1.watch(e,async t=>{t==="change"&&(this.logger.info("\u68C0\u6D4B\u5230 pages.json \u53D8\u5316\uFF0C\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E..."),await this.safeExecute(()=>this.generateRouterConfig(),"\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E"))}),this.logger.info(`\u6B63\u5728\u76D1\u542C pages.json \u53D8\u5316: ${e}`))}stopWatching(){this.watcher&&(this.watcher.close(),this.watcher=null)}addPluginHooks(e){e.configResolved=async t=>{this.projectRoot=t.root,await this.safeExecute(()=>this.generateRouterConfig(),"\u751F\u6210\u8DEF\u7531\u914D\u7F6E"),t.command==="serve"&&this.startWatching()}}destroy(){super.destroy(),this.stopWatching()}}const generateRouter=index.createPluginFactory(R);class f extends index.BasePlugin{version="";buildTime=new Date;getDefaultOptions(){return{format:"timestamp",semverBase:"1.0.0",outputType:"file",outputFile:"version.json",defineName:"__APP_VERSION__",hashLength:8,prefix:"",suffix:""}}validateOptions(){if(this.validator.field("format").custom(e=>!e||["timestamp","date","datetime","semver","hash","custom"].includes(e),"format \u5FC5\u987B\u662F timestamp, date, datetime, semver, hash \u6216 custom").field("outputType").custom(e=>!e||["file","define","both"].includes(e),"outputType \u5FC5\u987B\u662F file, define \u6216 both").field("hashLength").number().custom(e=>!e||e>0&&e<=32,"hashLength \u5FC5\u987B\u5728 1-32 \u4E4B\u95F4").validate(),this.options.format==="custom"&&!this.options.customFormat)throw new Error("\u5F53 format \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customFormat")}getPluginName(){return"generate-version"}generateVersion(){const e=format.getDateFormatParams(this.buildTime),t=format.generateRandomHash(this.options.hashLength);let i;switch(this.options.format){case"timestamp":i=`${e.YYYY}${e.MM}${e.DD}${e.HH}${e.mm}${e.ss}`;break;case"date":i=`${e.YYYY}.${e.MM}.${e.DD}`;break;case"datetime":i=`${e.YYYY}.${e.MM}.${e.DD}.${e.HH}${e.mm}${e.ss}`;break;case"semver":i=this.options.semverBase||"1.0.0";break;case"hash":i=t;break;case"custom":i=this.parseCustomFormat({...e,hash:t});break;default:i=e.timestamp}const r=this.options.prefix||"",s=this.options.suffix||"";return`${r}${i}${s}`}parseCustomFormat(e){const t={...e};if(this.options.semverBase){const[i,r,s]=this.options.semverBase.split(".");t.major=i||"1",t.minor=r||"0",t.patch=s||"0"}return format.parseTemplate(this.options.customFormat||"",t)}generateVersionInfo(){return{version:this.version,buildTime:this.buildTime.toISOString(),timestamp:this.buildTime.getTime(),format:this.options.format,...this.options.extra}}async writeVersionFile(e){const t=u$1.join(e,this.options.outputFile||"version.json"),i=this.generateVersionInfo();await format.writeFileContent(t,JSON.stringify(i,null,2)),this.logger.success(`\u7248\u672C\u6587\u4EF6\u5DF2\u751F\u6210: ${t}`)}addPluginHooks(e){e.configResolved=()=>{this.buildTime=new Date,this.version=this.generateVersion(),this.logger.info(`\u751F\u6210\u7248\u672C\u53F7: ${this.version}`)},(this.options.outputType==="define"||this.options.outputType==="both")&&(e.config=()=>{this.version||(this.buildTime=new Date,this.version=this.generateVersion());const t=this.options.defineName||"__APP_VERSION__";return this.logger.info(`\u6CE8\u5165\u5168\u5C40\u53D8\u91CF: ${t} = "${this.version}"`),{define:{[t]:JSON.stringify(this.version),[`${t}_INFO`]:JSON.stringify(this.generateVersionInfo())}}}),(this.options.outputType==="file"||this.options.outputType==="both")&&(e.writeBundle=async()=>{this.viteConfig&&await this.safeExecute(async()=>{const t=this.viteConfig.build.outDir;await this.writeVersionFile(t)},"\u5199\u5165\u7248\u672C\u6587\u4EF6")})}}const generateVersion=index.createPluginFactory(f);function generateIconTagDescriptors(n){const e=[];if(n.link)return[];if(n.icons&&n.icons.length>0)for(const t of n.icons){const i={rel:t.rel,href:t.href};t.sizes&&(i.sizes=t.sizes),t.type&&(i.type=t.type),e.push({tag:"link",attrs:i,injectTo:"head"})}else if(n.url)e.push({tag:"link",attrs:{rel:"icon",href:n.url},injectTo:"head"});else{const t=n.base||"/",i=t.endsWith("/")?`${t}favicon.ico`:`${t}/favicon.ico`;e.push({tag:"link",attrs:{rel:"icon",href:i},injectTo:"head"})}return e}class u extends index.BasePlugin{getDefaultOptions(){return{base:"/"}}validateOptions(){this.validator.field("base").string().field("url").string().field("link").string().field("icons").array(),this.options?.copyOptions&&(this.validator.field("copyOptions").object(),new validation.Validator(this.options.copyOptions).field("sourceDir").required().string().field("targetDir").required().string().field("overwrite").boolean().field("recursive").boolean().validate()),this.validator.validate()}getPluginName(){return"inject-ico"}getIconTagDescriptors(){if(!this.options.enabled)return this.logger.info("\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165"),[];const e=generateIconTagDescriptors(this.options);return e.length>0&&this.logger.success(`\u6210\u529F\u6CE8\u5165 ${e.length} \u4E2A\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6`),e}injectCustomLinkTag(e){if(!this.options.enabled||!this.options.link)return e;const t=this.options.link,i=/<\/head>/i,r=e.match(i);if(r&&r.index!==void 0){const s=e.slice(0,r.index)+t+`
|
|
37
|
+
`+e.slice(r.index);return this.logger.success("\u6210\u529F\u6CE8\u5165\u81EA\u5B9A\u4E49\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6"),this.logger.info(` - ${t}`),s}return this.logger.warn("\u672A\u627E\u5230 </head> \u6807\u7B7E\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165"),e}async copyFiles(){if(!this.options.enabled){this.logger.info("\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6587\u4EF6\u590D\u5236");return}const{copyOptions:e}=this.options;if(!e)return;const{sourceDir:t,targetDir:i,overwrite:r=!0,recursive:s=!0}=e;await format.checkSourceExists(t);const a=await format.copySourceToTarget(t,i,{recursive:s,overwrite:r,incremental:!0});this.logger.success(`\u56FE\u6807\u6587\u4EF6\u590D\u5236\u6210\u529F\uFF1A\u4ECE ${t} \u5230 ${i}`,`\u590D\u5236\u4E86 ${a.copiedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8DF3\u8FC7\u4E86 ${a.skippedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8017\u65F6 ${a.executionTime}ms`)}addPluginHooks(e){e.transformIndexHtml={order:"pre",handler:t=>{if(this.options.link)return this.injectCustomLinkTag(t);const i=this.getIconTagDescriptors();return i.length>0?{html:t,tags:i}:t}},e.writeBundle=async()=>{await this.safeExecute(()=>this.copyFiles(),"\u56FE\u6807\u6587\u4EF6\u590D\u5236")}}}const injectIco=index.createPluginFactory(u,n=>typeof n=="string"?{base:n}:n||{}),CLS_OVERLAY="__loading-overlay__",CLS_HIDDEN="__loading-hidden__",CLS_VISIBLE="__loading-visible__",CLS_TOP="__loading-top__",CLS_CENTER="__loading-center__",CLS_BOTTOM="__loading-bottom__",CLS_SPINNER="__loading-spinner__",CLS_TEXT="__loading-text__",CLS_DOT="__loading-dot__",ID_ROOT="__loading-root__",ATTR_TEXT="data-loading-text",ANIM_SPIN="__loading-spin__",ANIM_DOTS="__loading-dots__",ANIM_PULSE="__loading-pulse__",ANIM_BAR="__loading-bar__",POSITION_CLASS_MAP={center:CLS_CENTER,top:CLS_TOP,bottom:CLS_BOTTOM};function P(n){return n.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function generateCSS(n,e="spinner",t){const{overlayColor:i="rgba(255, 255, 255, 0.7)",spinnerColor:r="#4361ee",spinnerSize:s="40px",textColor:a="#333",textSize:l="14px",zIndex:d=9999,pointerEvents:h=!0,backdropBlur:c=!1,backdropBlurAmount:g=4}=n,E=h?"":"pointer-events: none;",y=c?`backdrop-filter: blur(${g}px);-webkit-backdrop-filter: blur(${g}px);`:"",C=getSpinnerCSS(e,r,s),b=t?.enabled!==!1,m=t?.duration??200,_=t?.easing??"ease-out",F=b?`transition: opacity ${m}ms ${_}, visibility ${m}ms ${_};`:"";return`
|
|
38
38
|
.${CLS_OVERLAY} {
|
|
39
39
|
position: fixed;
|
|
40
40
|
top: 0;
|
|
@@ -46,9 +46,9 @@ export default routes
|
|
|
46
46
|
flex-direction: column;
|
|
47
47
|
align-items: center;
|
|
48
48
|
z-index: ${d};
|
|
49
|
+
${E}
|
|
49
50
|
${y}
|
|
50
|
-
|
|
51
|
-
contain: layout style paint;
|
|
51
|
+
contain: content;
|
|
52
52
|
will-change: opacity;
|
|
53
53
|
}
|
|
54
54
|
.${CLS_OVERLAY}.${CLS_TOP} {
|
|
@@ -62,10 +62,10 @@ export default routes
|
|
|
62
62
|
justify-content: flex-end;
|
|
63
63
|
padding-bottom: 60px;
|
|
64
64
|
}
|
|
65
|
-
${
|
|
65
|
+
${C}
|
|
66
66
|
.${CLS_TEXT} {
|
|
67
67
|
margin-top: 12px;
|
|
68
|
-
color: ${
|
|
68
|
+
color: ${a};
|
|
69
69
|
font-size: ${l};
|
|
70
70
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
71
71
|
user-select: none;
|
|
@@ -73,13 +73,23 @@ ${v}
|
|
|
73
73
|
.${CLS_OVERLAY}.${CLS_HIDDEN} {
|
|
74
74
|
opacity: 0;
|
|
75
75
|
visibility: hidden;
|
|
76
|
-
${
|
|
76
|
+
${F}
|
|
77
|
+
}
|
|
78
|
+
.${CLS_OVERLAY}.${CLS_HIDDEN} .${CLS_SPINNER},
|
|
79
|
+
.${CLS_OVERLAY}.${CLS_HIDDEN} .${CLS_DOT},
|
|
80
|
+
.${CLS_OVERLAY}.${CLS_HIDDEN} .${CLS_SPINNER}::after {
|
|
81
|
+
animation-play-state: paused;
|
|
77
82
|
}
|
|
78
83
|
.${CLS_OVERLAY}.${CLS_VISIBLE} {
|
|
79
84
|
opacity: 1;
|
|
80
85
|
visibility: visible;
|
|
81
|
-
${
|
|
82
|
-
}
|
|
86
|
+
${F}
|
|
87
|
+
}
|
|
88
|
+
.${CLS_OVERLAY}.${CLS_VISIBLE} .${CLS_SPINNER},
|
|
89
|
+
.${CLS_OVERLAY}.${CLS_VISIBLE} .${CLS_DOT},
|
|
90
|
+
.${CLS_OVERLAY}.${CLS_VISIBLE} .${CLS_SPINNER}::after {
|
|
91
|
+
animation-play-state: running;
|
|
92
|
+
}`}function getSpinnerCSS(n,e,t){switch(n){case"dots":return`
|
|
83
93
|
.${CLS_SPINNER} {
|
|
84
94
|
display: flex;
|
|
85
95
|
gap: 6px;
|
|
@@ -143,16 +153,10 @@ ${v}
|
|
|
143
153
|
}
|
|
144
154
|
@keyframes ${ANIM_SPIN} {
|
|
145
155
|
to { transform: rotate(360deg); }
|
|
146
|
-
}`}}function generateHTMLTemplate(
|
|
156
|
+
}`}}function generateHTMLTemplate(n){const e=n.position||"center",t=n.defaultText||"\u52A0\u8F7D\u4E2D...",i=n.spinnerType||"spinner",r=POSITION_CLASS_MAP[e],s=n.style?.customClass?` ${n.style.customClass}`:"",a=n.style?.customStyle?` style="${P(n.style.customStyle)}"`:"",l=n.defaultVisible?CLS_VISIBLE:CLS_HIDDEN;if(n.customTemplate)return`<div class="${CLS_OVERLAY} ${r} ${l}${s}" id="${ID_ROOT}"${a}>${n.customTemplate}</div>`;const d=getSpinnerHTML(i);return`<div class="${CLS_OVERLAY} ${r} ${l}${s}" id="${ID_ROOT}"${a}>
|
|
147
157
|
${d}
|
|
148
|
-
<div class="${CLS_TEXT}" ${ATTR_TEXT}
|
|
149
|
-
</div>`}function getSpinnerHTML(
|
|
150
|
-
'use strict';
|
|
151
|
-
|
|
152
|
-
// SSR \u73AF\u5883\u68C0\u6D4B
|
|
153
|
-
if (typeof window === 'undefined' || typeof document === 'undefined') return;
|
|
154
|
-
|
|
155
|
-
var _loadingEl = null;
|
|
158
|
+
<div class="${CLS_TEXT}" ${ATTR_TEXT}>${t}</div>
|
|
159
|
+
</div>`}function getSpinnerHTML(n){switch(n){case"dots":return`<div class="${CLS_SPINNER}"><div class="${CLS_DOT}"></div><div class="${CLS_DOT}"></div><div class="${CLS_DOT}"></div></div>`;default:return`<div class="${CLS_SPINNER}"></div>`}}function makeCallback(n){return n?`function() { try { ${n} } catch(e) { console.error('[injectLoading] callback error:', e); } }`:"function() {}"}function generateVarsCode(n){return` var _loadingEl = null;
|
|
156
160
|
var _textEl = null;
|
|
157
161
|
var _styleEl = null;
|
|
158
162
|
var _visible = false;
|
|
@@ -160,41 +164,41 @@ ${v}
|
|
|
160
164
|
var _showTimer = null;
|
|
161
165
|
var _hideTimer = null;
|
|
162
166
|
var _debounceTimer = null;
|
|
167
|
+
var _retryTimer = null;
|
|
163
168
|
var _showTime = 0;
|
|
164
169
|
var _pendingCount = 0;
|
|
170
|
+
var _showRetryCount = 0;
|
|
171
|
+
var _maxShowRetries = 20;
|
|
165
172
|
|
|
166
173
|
// \u914D\u7F6E
|
|
167
|
-
var _minDisplayTime = ${JSON.stringify(
|
|
168
|
-
var _delayShow = ${JSON.stringify(n)};
|
|
169
|
-
var _debounceHide = ${JSON.stringify(
|
|
170
|
-
var _transition = ${JSON.stringify(
|
|
171
|
-
var _excludeUrls = ${JSON.stringify(
|
|
172
|
-
var _includeUrls = ${JSON.stringify(
|
|
173
|
-
var _excludeMethods = ${JSON.stringify(
|
|
174
|
-
var _excludeUrlPrefixes = ${JSON.stringify(
|
|
174
|
+
var _minDisplayTime = ${JSON.stringify(n.minDisplayTime)};
|
|
175
|
+
var _delayShow = ${JSON.stringify(n.delayShow)};
|
|
176
|
+
var _debounceHide = ${JSON.stringify(n.debounceHide)};
|
|
177
|
+
var _transition = ${JSON.stringify(n.transition)};
|
|
178
|
+
var _excludeUrls = ${JSON.stringify(n.excludeUrls.map(e=>({source:e.source,flags:e.flags})))}.map(function(s) { return new RegExp(s.source, s.flags); });
|
|
179
|
+
var _includeUrls = ${JSON.stringify(n.includeUrls.map(e=>({source:e.source,flags:e.flags})))}.map(function(s) { return new RegExp(s.source, s.flags); });
|
|
180
|
+
var _excludeMethods = ${JSON.stringify(n.excludeMethods)};
|
|
181
|
+
var _excludeUrlPrefixes = ${JSON.stringify(n.excludeUrlPrefixes)};
|
|
175
182
|
|
|
176
183
|
// \u56DE\u8C03
|
|
177
|
-
var _onBeforeShow = ${
|
|
178
|
-
var _onShow = ${
|
|
179
|
-
var _onBeforeHide = ${
|
|
180
|
-
var _onHide = ${
|
|
181
|
-
var _onDestroy = ${
|
|
184
|
+
var _onBeforeShow = ${n.cbBeforeShow};
|
|
185
|
+
var _onShow = ${n.cbShow};
|
|
186
|
+
var _onBeforeHide = ${n.cbBeforeHide};
|
|
187
|
+
var _onHide = ${n.cbHide};
|
|
188
|
+
var _onDestroy = ${n.cbDestroy};
|
|
182
189
|
|
|
183
190
|
// \u4FDD\u5B58\u539F\u59CB\u65B9\u6CD5\u5F15\u7528\uFF0C\u7528\u4E8E destroy \u65F6\u6062\u590D
|
|
184
191
|
var _originalFetch = null;
|
|
185
192
|
var _originalXHROpen = null;
|
|
186
|
-
var _originalXHRSend = null
|
|
187
|
-
|
|
188
|
-
function _findEl() {
|
|
193
|
+
var _originalXHRSend = null;`}function generateHelpersCode(n){return` function _findEl() {
|
|
189
194
|
if (!_loadingEl) {
|
|
190
195
|
_loadingEl = document.getElementById('${ID_ROOT}');
|
|
191
196
|
}
|
|
192
197
|
if (!_textEl && _loadingEl) {
|
|
193
198
|
_textEl = _loadingEl.querySelector('[${ATTR_TEXT}]');
|
|
194
199
|
}
|
|
195
|
-
// \u67E5\u627E\u5E76\u7F13\u5B58\u6CE8\u5165\u7684 style \u5143\u7D20\uFF0C\u786E\u4FDD destroy \u65F6\u80FD\u6B63\u786E\u6E05\u7406
|
|
196
200
|
if (!_styleEl) {
|
|
197
|
-
_styleEl = document.querySelector('style[data-loading-id="${
|
|
201
|
+
_styleEl = document.querySelector('style[data-loading-id="${n}"]');
|
|
198
202
|
}
|
|
199
203
|
}
|
|
200
204
|
|
|
@@ -210,11 +214,13 @@ ${v}
|
|
|
210
214
|
if (_includeUrls.length > 0) {
|
|
211
215
|
var included = false;
|
|
212
216
|
for (var i = 0; i < _includeUrls.length; i++) {
|
|
217
|
+
_includeUrls[i].lastIndex = 0;
|
|
213
218
|
if (_includeUrls[i].test(url)) { included = true; break; }
|
|
214
219
|
}
|
|
215
220
|
if (!included) return true;
|
|
216
221
|
}
|
|
217
222
|
for (var i = 0; i < _excludeUrls.length; i++) {
|
|
223
|
+
_excludeUrls[i].lastIndex = 0;
|
|
218
224
|
if (_excludeUrls[i].test(url)) return true;
|
|
219
225
|
}
|
|
220
226
|
return false;
|
|
@@ -224,6 +230,7 @@ ${v}
|
|
|
224
230
|
if (_showTimer) { clearTimeout(_showTimer); _showTimer = null; }
|
|
225
231
|
if (_hideTimer) { clearTimeout(_hideTimer); _hideTimer = null; }
|
|
226
232
|
if (_debounceTimer) { clearTimeout(_debounceTimer); _debounceTimer = null; }
|
|
233
|
+
if (_retryTimer) { clearTimeout(_retryTimer); _retryTimer = null; }
|
|
227
234
|
}
|
|
228
235
|
|
|
229
236
|
function _applyTransition(show) {
|
|
@@ -231,14 +238,26 @@ ${v}
|
|
|
231
238
|
var d = _transition.duration || 200;
|
|
232
239
|
var e = _transition.easing || 'ease-out';
|
|
233
240
|
_loadingEl.style.transition = 'opacity ' + d + 'ms ' + e + ', visibility ' + d + 'ms ' + e;
|
|
241
|
+
}`}function generateCoreLogicCode(){return` function _applyHide() {
|
|
242
|
+
if (!_loadingEl) return;
|
|
243
|
+
_loadingEl.classList.remove('${CLS_VISIBLE}');
|
|
244
|
+
_loadingEl.classList.add('${CLS_HIDDEN}');
|
|
245
|
+
_applyTransition(false);
|
|
246
|
+
_visible = false;
|
|
247
|
+
_showTime = 0;
|
|
248
|
+
_onHide();
|
|
234
249
|
}
|
|
235
250
|
|
|
236
251
|
function _doShow(text) {
|
|
237
252
|
if (_destroyed) return;
|
|
238
253
|
_findEl();
|
|
239
|
-
if (!_loadingEl)
|
|
254
|
+
if (!_loadingEl) {
|
|
255
|
+
if (++_showRetryCount > _maxShowRetries) return;
|
|
256
|
+
_retryTimer = setTimeout(function() { _retryTimer = null; _doShow(text); }, 50);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
_showRetryCount = 0;
|
|
240
260
|
|
|
241
|
-
// onBeforeShow \u56DE\u8C03\uFF0C\u8FD4\u56DE false \u53EF\u963B\u6B62\u663E\u793A\uFF08makeCallback \u5DF2\u63D0\u4F9B try-catch \u4FDD\u62A4\uFF09
|
|
242
261
|
var result = _onBeforeShow();
|
|
243
262
|
if (result === false) return;
|
|
244
263
|
|
|
@@ -253,11 +272,11 @@ ${v}
|
|
|
253
272
|
|
|
254
273
|
function _doHide(force) {
|
|
255
274
|
if (_destroyed) return;
|
|
275
|
+
if (!_visible && !force) return;
|
|
256
276
|
_findEl();
|
|
257
277
|
if (!_loadingEl) return;
|
|
258
278
|
|
|
259
279
|
if (!force) {
|
|
260
|
-
// onBeforeHide \u56DE\u8C03\uFF0C\u8FD4\u56DE false \u53EF\u963B\u6B62\u9690\u85CF\uFF08makeCallback \u5DF2\u63D0\u4F9B try-catch \u4FDD\u62A4\uFF09
|
|
261
280
|
var result = _onBeforeHide();
|
|
262
281
|
if (result === false) return;
|
|
263
282
|
}
|
|
@@ -266,29 +285,19 @@ ${v}
|
|
|
266
285
|
var elapsed = Date.now() - _showTime;
|
|
267
286
|
var remaining = _minDisplayTime.duration - elapsed;
|
|
268
287
|
if (remaining > 0) {
|
|
269
|
-
|
|
288
|
+
if (_hideTimer) clearTimeout(_hideTimer);
|
|
289
|
+
_hideTimer = setTimeout(function() { _hideTimer = null; _doHide(true); }, remaining);
|
|
270
290
|
return;
|
|
271
291
|
}
|
|
272
292
|
}
|
|
273
293
|
|
|
274
294
|
if (_debounceHide.enabled && !force) {
|
|
275
|
-
_debounceTimer
|
|
276
|
-
|
|
277
|
-
_loadingEl.classList.add('${CLS_HIDDEN}');
|
|
278
|
-
_applyTransition(false);
|
|
279
|
-
_visible = false;
|
|
280
|
-
_showTime = 0;
|
|
281
|
-
_onHide();
|
|
282
|
-
}, _debounceHide.duration);
|
|
295
|
+
if (_debounceTimer) clearTimeout(_debounceTimer);
|
|
296
|
+
_debounceTimer = setTimeout(function() { _debounceTimer = null; _applyHide(); }, _debounceHide.duration);
|
|
283
297
|
return;
|
|
284
298
|
}
|
|
285
299
|
|
|
286
|
-
|
|
287
|
-
_loadingEl.classList.add('${CLS_HIDDEN}');
|
|
288
|
-
_applyTransition(false);
|
|
289
|
-
_visible = false;
|
|
290
|
-
_showTime = 0;
|
|
291
|
-
_onHide();
|
|
300
|
+
_applyHide();
|
|
292
301
|
}
|
|
293
302
|
|
|
294
303
|
function _restoreInterceptors() {
|
|
@@ -302,16 +311,13 @@ ${v}
|
|
|
302
311
|
_originalXHROpen = null;
|
|
303
312
|
_originalXHRSend = null;
|
|
304
313
|
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
var manager = {
|
|
308
|
-
show: function(text) {
|
|
314
|
+
}`}function generateManagerObjectCode(){return` show: function(text) {
|
|
309
315
|
if (_destroyed) return;
|
|
310
316
|
_clearTimers();
|
|
311
|
-
// \u91CD\u7F6E _showTime\uFF0C\u786E\u4FDD\u91CD\u65B0 show \u65F6\u6700\u5C0F\u663E\u793A\u65F6\u95F4\u4ECE\u65B0\u663E\u793A\u65F6\u523B\u5F00\u59CB\u8BA1\u7B97
|
|
312
317
|
_showTime = 0;
|
|
318
|
+
_showRetryCount = 0;
|
|
313
319
|
if (_delayShow.enabled && _delayShow.duration > 0) {
|
|
314
|
-
_showTimer = setTimeout(function() { _doShow(text); }, _delayShow.duration);
|
|
320
|
+
_showTimer = setTimeout(function() { _showTimer = null; _doShow(text); }, _delayShow.duration);
|
|
315
321
|
} else {
|
|
316
322
|
_doShow(text);
|
|
317
323
|
}
|
|
@@ -326,6 +332,51 @@ ${v}
|
|
|
326
332
|
_clearTimers();
|
|
327
333
|
_doHide(true);
|
|
328
334
|
},
|
|
335
|
+
toggle: function(text) {
|
|
336
|
+
if (_destroyed) return;
|
|
337
|
+
if (_visible) {
|
|
338
|
+
this.hide();
|
|
339
|
+
} else {
|
|
340
|
+
this.show(text);
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
enablePointerEvents: function() {
|
|
344
|
+
if (_destroyed) return;
|
|
345
|
+
_findEl();
|
|
346
|
+
if (_loadingEl) _loadingEl.style.pointerEvents = 'auto';
|
|
347
|
+
},
|
|
348
|
+
disablePointerEvents: function() {
|
|
349
|
+
if (_destroyed) return;
|
|
350
|
+
_findEl();
|
|
351
|
+
if (_loadingEl) _loadingEl.style.pointerEvents = 'none';
|
|
352
|
+
},
|
|
353
|
+
togglePointerEvents: function() {
|
|
354
|
+
if (_destroyed) return;
|
|
355
|
+
_findEl();
|
|
356
|
+
if (!_loadingEl) return;
|
|
357
|
+
if (_loadingEl.style.pointerEvents === 'none') {
|
|
358
|
+
this.enablePointerEvents();
|
|
359
|
+
} else {
|
|
360
|
+
this.disablePointerEvents();
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
updateText: function(text) {
|
|
364
|
+
if (_destroyed) return;
|
|
365
|
+
_findEl();
|
|
366
|
+
if (_textEl) _textEl.textContent = text;
|
|
367
|
+
},
|
|
368
|
+
isVisible: function() {
|
|
369
|
+
return _visible && !_destroyed;
|
|
370
|
+
},
|
|
371
|
+
isPointerEventsEnabled: function() {
|
|
372
|
+
if (_destroyed) return false;
|
|
373
|
+
_findEl();
|
|
374
|
+
if (!_loadingEl) return false;
|
|
375
|
+
return _loadingEl.style.pointerEvents !== 'none';
|
|
376
|
+
},
|
|
377
|
+
getPendingCount: function() {
|
|
378
|
+
return _pendingCount;
|
|
379
|
+
},
|
|
329
380
|
destroy: function() {
|
|
330
381
|
if (_destroyed) return;
|
|
331
382
|
_destroyed = true;
|
|
@@ -346,17 +397,6 @@ ${v}
|
|
|
346
397
|
_restoreInterceptors();
|
|
347
398
|
_onDestroy();
|
|
348
399
|
},
|
|
349
|
-
updateText: function(text) {
|
|
350
|
-
if (_destroyed) return;
|
|
351
|
-
_findEl();
|
|
352
|
-
if (_textEl) _textEl.textContent = text;
|
|
353
|
-
},
|
|
354
|
-
isVisible: function() {
|
|
355
|
-
return _visible && !_destroyed;
|
|
356
|
-
},
|
|
357
|
-
getPendingCount: function() {
|
|
358
|
-
return _pendingCount;
|
|
359
|
-
},
|
|
360
400
|
_requestStart: function(url, method) {
|
|
361
401
|
if (_destroyed) return;
|
|
362
402
|
if (_shouldFilter(url, method)) return;
|
|
@@ -368,89 +408,90 @@ ${v}
|
|
|
368
408
|
if (_shouldFilter(url, method)) return;
|
|
369
409
|
_pendingCount = Math.max(0, _pendingCount - 1);
|
|
370
410
|
if (_pendingCount === 0) this.hide();
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
_originalFetch
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
manager._requestStart(url, method);
|
|
384
|
-
return _originalFetch.apply(this, arguments).then(
|
|
385
|
-
function(response) { manager._requestEnd(url, method); return response; },
|
|
386
|
-
function(error) { manager._requestEnd(url, method); throw error; }
|
|
387
|
-
);
|
|
388
|
-
};
|
|
389
|
-
}
|
|
411
|
+
}`}function generateInterceptorsCode(n){let e="";return(n==="fetch"||n==="all")&&(e+=` // \u81EA\u52A8\u62E6\u622A fetch
|
|
412
|
+
if (typeof window !== 'undefined' && window.fetch) {
|
|
413
|
+
_originalFetch = window.fetch;
|
|
414
|
+
window.fetch = function(input, init) {
|
|
415
|
+
var url = typeof input === 'string' ? input : (input instanceof URL ? input.href : (input && input.url ? input.url : ''));
|
|
416
|
+
var method = (init && init.method) || (input && input.method) || 'GET';
|
|
417
|
+
manager._requestStart(url, method);
|
|
418
|
+
return _originalFetch.apply(this, arguments).then(
|
|
419
|
+
function(response) { manager._requestEnd(url, method); return response; },
|
|
420
|
+
function(error) { manager._requestEnd(url, method); throw error; }
|
|
421
|
+
);
|
|
422
|
+
};
|
|
390
423
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
manager.
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
self.onloadend = function() {
|
|
409
|
-
manager._requestEnd(self.__loadingUrl, self.__loadingMethod);
|
|
410
|
-
if (_origLoadEnd) _origLoadEnd.apply(this, arguments);
|
|
411
|
-
};
|
|
412
|
-
return _originalXHRSend.apply(this, arguments);
|
|
413
|
-
};
|
|
414
|
-
}
|
|
424
|
+
`),(n==="xhr"||n==="all")&&(e+=` // \u81EA\u52A8\u62E6\u622A XMLHttpRequest
|
|
425
|
+
if (typeof window !== 'undefined' && window.XMLHttpRequest) {
|
|
426
|
+
_originalXHROpen = XMLHttpRequest.prototype.open;
|
|
427
|
+
_originalXHRSend = XMLHttpRequest.prototype.send;
|
|
428
|
+
XMLHttpRequest.prototype.open = function(method, url) {
|
|
429
|
+
this.__loadingUrl = url || '';
|
|
430
|
+
this.__loadingMethod = method || 'GET';
|
|
431
|
+
return _originalXHROpen.apply(this, arguments);
|
|
432
|
+
};
|
|
433
|
+
XMLHttpRequest.prototype.send = function() {
|
|
434
|
+
var self = this;
|
|
435
|
+
manager._requestStart(self.__loadingUrl, self.__loadingMethod);
|
|
436
|
+
self.addEventListener('loadend', function() {
|
|
437
|
+
manager._requestEnd(self.__loadingUrl, self.__loadingMethod);
|
|
438
|
+
});
|
|
439
|
+
return _originalXHRSend.apply(this, arguments);
|
|
440
|
+
};
|
|
415
441
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
var _defaultVisible = ${JSON.stringify(m)};
|
|
442
|
+
`),e.trimEnd()}function generateInitCode(n,e,t){let i="";return i+=` // defaultVisible: \u540C\u6B65\u521D\u59CB\u53EF\u89C1\u72B6\u6001
|
|
443
|
+
var _defaultVisible = ${JSON.stringify(n)};
|
|
419
444
|
if (_defaultVisible) {
|
|
420
445
|
_visible = true;
|
|
421
446
|
_showTime = Date.now();
|
|
422
447
|
}
|
|
423
|
-
|
|
448
|
+
`,n&&e!=="manual"&&(i+=`
|
|
424
449
|
// autoHideOn: \u81EA\u52A8\u9690\u85CF\u65F6\u673A
|
|
425
|
-
var _autoHideOn = '${
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
450
|
+
var _autoHideOn = '${e}';
|
|
451
|
+
function _autoHideHandler() {
|
|
452
|
+
manager.hide();
|
|
453
|
+
}
|
|
454
|
+
if (_autoHideOn === 'load') {
|
|
455
|
+
if (document.readyState === 'complete') {
|
|
456
|
+
_autoHideHandler();
|
|
457
|
+
} else {
|
|
458
|
+
window.addEventListener('load', _autoHideHandler);
|
|
429
459
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
} else {
|
|
434
|
-
window.addEventListener('load', _autoHideHandler);
|
|
435
|
-
}
|
|
460
|
+
} else {
|
|
461
|
+
if (document.readyState !== 'loading') {
|
|
462
|
+
_autoHideHandler();
|
|
436
463
|
} else {
|
|
437
|
-
|
|
438
|
-
if (document.readyState !== 'loading') {
|
|
439
|
-
_autoHideHandler();
|
|
440
|
-
} else {
|
|
441
|
-
document.addEventListener('DOMContentLoaded', _autoHideHandler);
|
|
442
|
-
}
|
|
464
|
+
document.addEventListener('DOMContentLoaded', _autoHideHandler);
|
|
443
465
|
}
|
|
444
466
|
}
|
|
445
|
-
|
|
467
|
+
`),i+=`
|
|
446
468
|
// \u66B4\u9732\u5230\u5168\u5C40
|
|
447
|
-
window['${t}'] = manager;
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
469
|
+
window['${t}'] = manager;`,i}function generateLoadingManagerCode(n){const e=n.globalName||"__LOADING_MANAGER__",t=n.minDisplayTime||{enabled:!0,duration:300},i=n.delayShow||{enabled:!0,duration:200},r=n.debounceHide||{enabled:!1,duration:100},s=n.transition||{enabled:!0,duration:200,easing:"ease-out"},a=n.autoBind||"none",l=n.requestFilter||{},d=n.defaultVisible||!1,h=n.autoHideOn||"DOMContentLoaded",c=n.callbacks||{},g=l.excludeUrls||[],E=l.includeUrls||[],y=l.excludeMethods||[],C=l.excludeUrlPrefixes||[],b=makeCallback(c.onBeforeShow),m=makeCallback(c.onShow),_=makeCallback(c.onBeforeHide),F=makeCallback(c.onHide),$=makeCallback(c.onDestroy),v=generateVarsCode({minDisplayTime:t,delayShow:i,debounceHide:r,transition:s,excludeUrls:g,includeUrls:E,excludeMethods:y,excludeUrlPrefixes:C,cbBeforeShow:b,cbShow:m,cbBeforeHide:_,cbHide:F,cbDestroy:$}),w=generateHelpersCode(e),D=generateCoreLogicCode(),B=generateManagerObjectCode(),x=generateInterceptorsCode(a),S=generateInitCode(d,h,e);return`(function() {
|
|
470
|
+
'use strict';
|
|
471
|
+
|
|
472
|
+
// SSR \u73AF\u5883\u68C0\u6D4B
|
|
473
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') return;
|
|
474
|
+
|
|
475
|
+
${v}
|
|
476
|
+
|
|
477
|
+
${w}
|
|
478
|
+
|
|
479
|
+
${D}
|
|
480
|
+
|
|
481
|
+
var manager = {
|
|
482
|
+
${B}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
${x}
|
|
486
|
+
|
|
487
|
+
${S}
|
|
488
|
+
})();`}function validateStyle(n){if(!n)return;const{zIndex:e,pointerEvents:t,backdropBlurAmount:i}=n;if(e!==void 0&&(typeof e!="number"||e<0))throw new Error("style.zIndex \u5FC5\u987B\u662F\u975E\u8D1F\u6570");if(t!==void 0&&typeof t!="boolean")throw new Error("style.pointerEvents \u5FC5\u987B\u662F\u5E03\u5C14\u503C");if(i!==void 0&&(typeof i!="number"||i<0))throw new Error("style.backdropBlurAmount \u5FC5\u987B\u662F\u975E\u8D1F\u6570")}function validateNestedConfig(n,e){if(n?.duration!==void 0&&(typeof n.duration!="number"||n.duration<0))throw new Error(e)}function validateTransition(n){if(!n)return;const{duration:e,easing:t}=n;if(e!==void 0&&(typeof e!="number"||e<0))throw new Error("transition.duration \u5FC5\u987B\u662F\u975E\u8D1F\u6570");if(t!==void 0&&typeof t!="string")throw new Error("transition.easing \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u7C7B\u578B")}function validateCallbacks(n){if(!n)return;const e=["onBeforeShow","onShow","onBeforeHide","onHide","onDestroy"];for(const t of e){if(n[t]!==void 0&&typeof n[t]!="string")throw new Error(`callbacks.${t} \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`);if(n[t]&&/<script\b/i.test(n[t]))throw new Error(`callbacks.${t} \u4E0D\u5141\u8BB8\u5305\u542B <script> \u6807\u7B7E`)}}function validateCustomTemplate(n){if(n&&/<script\b/i.test(n))throw new Error("customTemplate \u4E0D\u5141\u8BB8\u5305\u542B <script> \u6807\u7B7E\uFF0C\u8BF7\u4F7F\u7528 callbacks \u914D\u7F6E\u56DE\u8C03\u903B\u8F91")}function validateGlobalName(n){if(n){if(!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(n))throw new Error(`globalName "${n}" \u4E0D\u662F\u5408\u6CD5\u7684 JavaScript \u6807\u8BC6\u7B26\uFF0C\u5FC5\u987B\u4EE5\u5B57\u6BCD\u3001\u4E0B\u5212\u7EBF\u6216\u7F8E\u5143\u7B26\u5F00\u5934\uFF0C\u4EC5\u5305\u542B\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u548C\u7F8E\u5143\u7B26`);if(["__proto__","constructor","prototype"].includes(n))throw new Error(`globalName "${n}" \u662F JavaScript \u5185\u7F6E\u5C5E\u6027\uFF0C\u53EF\u80FD\u5BFC\u81F4\u539F\u578B\u6C61\u67D3\uFF0C\u8BF7\u4F7F\u7528\u5176\u4ED6\u540D\u79F0`)}}function validateDefaultText(n){return n===""?"defaultText \u4E3A\u7A7A\u5B57\u7B26\u4E32\uFF0Cloading \u5C06\u4E0D\u663E\u793A\u6587\u672C\u5185\u5BB9":null}function validateAutoHideOn(n,e){return!n&&e?"autoHideOn \u4EC5\u5728 defaultVisible \u4E3A true \u65F6\u751F\u6548\uFF0C\u5F53\u524D defaultVisible \u4E3A false\uFF0CautoHideOn \u914D\u7F6E\u5C06\u88AB\u5FFD\u7565":null}class T extends index.BasePlugin{getDefaultOptions(){return{position:"center",defaultText:"\u52A0\u8F7D\u4E2D...",spinnerType:"spinner",autoBind:"none",globalName:"__LOADING_MANAGER__",defaultVisible:!1,autoHideOn:"DOMContentLoaded",style:{overlayColor:"rgba(255, 255, 255, 0.7)",spinnerColor:"#4361ee",spinnerSize:"40px",textColor:"#333",textSize:"14px",zIndex:9999,pointerEvents:!0,backdropBlur:!1,backdropBlurAmount:4},transition:{enabled:!0,duration:200,easing:"ease-out"},minDisplayTime:{enabled:!0,duration:300},delayShow:{enabled:!0,duration:200},debounceHide:{enabled:!1,duration:100}}}validateOptions(){this.validator.field("position").custom(i=>!i||["center","top","bottom"].includes(i),"position \u5FC5\u987B\u662F center, top \u6216 bottom").field("defaultText").string().field("spinnerType").custom(i=>!i||["spinner","dots","pulse","bar"].includes(i),"spinnerType \u5FC5\u987B\u662F spinner, dots, pulse \u6216 bar").field("autoBind").custom(i=>!i||["fetch","xhr","all","none"].includes(i),"autoBind \u5FC5\u987B\u662F fetch, xhr, all \u6216 none").field("globalName").string().field("customTemplate").string().field("defaultVisible").boolean().field("autoHideOn").custom(i=>!i||["DOMContentLoaded","load","manual"].includes(i),"autoHideOn \u5FC5\u987B\u662F DOMContentLoaded, load \u6216 manual").validate(),validateCustomTemplate(this.options.customTemplate);const e=validateDefaultText(this.options.defaultText);e&&this.logger.warn(e),validateGlobalName(this.options.globalName),validateStyle(this.options.style),validateNestedConfig(this.options.minDisplayTime,"minDisplayTime.duration \u5FC5\u987B\u662F\u975E\u8D1F\u6570"),validateNestedConfig(this.options.delayShow,"delayShow.duration \u5FC5\u987B\u662F\u975E\u8D1F\u6570"),validateNestedConfig(this.options.debounceHide,"debounceHide.duration \u5FC5\u987B\u662F\u975E\u8D1F\u6570"),validateTransition(this.options.transition),validateCallbacks(this.options.callbacks);const t=validateAutoHideOn(this.options.defaultVisible,this.options.autoHideOn);t&&this.logger.warn(t)}getPluginName(){return"inject-loading"}generateLoadingManager(e){return generateLoadingManagerCode(e)}generateHeadInjectCode(){const{css:e,html:t}=this.getCachedAssets();return`<!-- inject-loading: head start -->
|
|
489
|
+
<style data-loading-style data-loading-id="${this.options.globalName||"__LOADING_MANAGER__"}">${e}</style>
|
|
490
|
+
${t}
|
|
491
|
+
<!-- inject-loading: head end -->`}generateBodyInjectCode(e){const t=this.generateLoadingManager(this.options);if(e)return`<!-- inject-loading: body start -->
|
|
492
|
+
<script>${t}<\/script>
|
|
493
|
+
<!-- inject-loading: body end -->`;const{css:i,html:r}=this.getCachedAssets();return`<!-- inject-loading: start -->
|
|
494
|
+
<script>
|
|
454
495
|
(function() {
|
|
455
496
|
// SSR \u73AF\u5883\u68C0\u6D4B
|
|
456
497
|
if (typeof window === 'undefined' || typeof document === 'undefined') return;
|
|
@@ -459,7 +500,7 @@ ${l}
|
|
|
459
500
|
var style = document.createElement('style');
|
|
460
501
|
style.setAttribute('data-loading-style', '');
|
|
461
502
|
style.setAttribute('data-loading-id', '${this.options.globalName||"__LOADING_MANAGER__"}');
|
|
462
|
-
style.textContent = ${JSON.stringify(
|
|
503
|
+
style.textContent = ${JSON.stringify(i)};
|
|
463
504
|
document.head.appendChild(style);
|
|
464
505
|
|
|
465
506
|
// \u6CE8\u5165 HTML\uFF08\u7B49\u5F85 body \u53EF\u7528\u65F6\u6267\u884C\uFF09
|
|
@@ -477,8 +518,9 @@ ${l}
|
|
|
477
518
|
document.addEventListener('DOMContentLoaded', injectHTML);
|
|
478
519
|
}
|
|
479
520
|
})();
|
|
480
|
-
${
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
</
|
|
484
|
-
</
|
|
521
|
+
${t}
|
|
522
|
+
<\/script>
|
|
523
|
+
<!-- inject-loading: end -->`}_cachedAssets=null;getCachedAssets(){if(!this._cachedAssets){const e=this.options.style||{},t=this.options.spinnerType||"spinner",i=this.options.transition;this._cachedAssets={css:generateCSS(e,t,i),html:generateHTMLTemplate(this.options)}}return this._cachedAssets}addPluginHooks(e){const t=this.options.defaultVisible||!1,i=t?this.generateHeadInjectCode():"",r=this.generateBodyInjectCode(t);e.transformIndexHtml={order:"post",handler:s=>{let a=s;if(i){const h=/<\/head>/i;h.test(a)?a=a.replace(h,`${i}
|
|
524
|
+
</head>`):this.logger.warn("\u672A\u627E\u5230 </head> \u6807\u7B7E\uFF0CdefaultVisible \u7684\u767D\u5C4F loading \u5C06\u65E0\u6CD5\u751F\u6548")}const l=/<\/body>/i;if(l.test(a))return a=a.replace(l,`${r}
|
|
525
|
+
</body>`),this.logger.success("\u6210\u529F\u6CE8\u5165\u5168\u5C40 Loading \u72B6\u6001\u7BA1\u7406\u4EE3\u7801\u5230 HTML \u6587\u4EF6"),a;const d=/<\/html>/i;return d.test(a)?(a=a.replace(d,`${r}
|
|
526
|
+
</html>`),this.logger.success("\u6210\u529F\u6CE8\u5165\u5168\u5C40 Loading \u72B6\u6001\u7BA1\u7406\u4EE3\u7801\u5230 HTML \u6587\u4EF6"),a):(this.logger.warn("\u672A\u627E\u5230 </body> \u6216 </html> \u6807\u7B7E\uFF0CLoading \u4EE3\u7801\u8FFD\u52A0\u5230\u6587\u4EF6\u672B\u5C3E"),a+r)}}}}const injectLoading=index.createPluginFactory(T);exports.buildProgress=buildProgress,exports.copyFile=copyFile,exports.generateRouter=generateRouter,exports.generateVersion=generateVersion,exports.injectIco=injectIco,exports.injectLoading=injectLoading;
|