@lytjs/cli 3.1.0 → 3.2.1
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/cli.js +0 -0
- package/dist/index.js +97 -108
- package/dist/index.mjs +29 -40
- package/package.json +7 -2
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var
|
|
2
|
+
"use strict";var re=Object.create;var E=Object.defineProperty;var se=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,le=Object.prototype.hasOwnProperty;var ce=(e,o)=>{for(var n in o)E(e,n,{get:o[n],enumerable:!0})},Y=(e,o,n,r)=>{if(o&&typeof o=="object"||typeof o=="function")for(let s of ie(o))!le.call(e,s)&&s!==n&&E(e,s,{get:()=>o[s],enumerable:!(r=se(o,s))||r.enumerable});return e};var m=(e,o,n)=>(n=e!=null?re(ae(e)):{},Y(o||!e||!e.__esModule?E(n,"default",{value:e,enumerable:!0}):n,e)),pe=e=>Y(E({},"__esModule",{value:!0}),e);var Qe={};ce(Qe,{createHMREndpoint:()=>V,createHMRServer:()=>G,createProject:()=>L,getHMRClientScript:()=>I});module.exports=pe(Qe);var te=m(require("fs")),oe=m(require("path"));var y=m(require("fs")),U=m(require("path")),ue="\x1B[0m",de={black:"\x1B[30m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",white:"\x1B[37m",brightRed:"\x1B[91m",brightGreen:"\x1B[92m",brightYellow:"\x1B[93m",brightBlue:"\x1B[94m",brightMagenta:"\x1B[95m",brightCyan:"\x1B[96m",brightWhite:"\x1B[97m",bgBlack:"\x1B[40m",bgRed:"\x1B[41m",bgGreen:"\x1B[42m",bgYellow:"\x1B[43m",bgBlue:"\x1B[44m",bgMagenta:"\x1B[45m",bgCyan:"\x1B[46m",bgWhite:"\x1B[47m",bold:"\x1B[1m",dim:"\x1B[2m",italic:"\x1B[3m",underline:"\x1B[4m",strikethrough:"\x1B[9m"};function t(e,o){let n=de[o];return n?`${n}${e}${ue}`:e}function z(e){let o=e.slice(2),n={command:"",args:[],options:{},raw:o},r=0;for(;r<o.length;){let s=o[r];if(s==="--help"||s==="-h")n.options.help=!0,r++;else if(s==="--version"||s==="-v")n.options.version=!0,r++;else if(s.startsWith("--")){let a=s.indexOf("=");if(a!==-1){let u=s.slice(2,a),l=s.slice(a+1);n.options[u]=l}else{let u=s.slice(2),l=o[r+1];l&&!l.startsWith("-")?(n.options[u]=l,r++):n.options[u]=!0}r++}else if(s.startsWith("-")&&s.length>1){let a=s.slice(1),u=o[r+1];u&&!u.startsWith("-")?(n.options[a]=u,r++):n.options[a]=!0,r++}else n.command?(n.args.push(s),r++):(n.command=s,r++)}return n}function $(e){y.existsSync(e)||y.mkdirSync(e,{recursive:!0})}function F(e){return y.existsSync(e)}function k(e){return y.readFileSync(e,"utf-8")}function S(e,o){let n=U.dirname(e);$(n),y.writeFileSync(e,o,"utf-8")}var i={info(e){console.log(`${t("[INFO]","blue")} ${e}`)},warn(e){console.log(`${t("[WARN]","yellow")} ${e}`)},error(e){console.error(`${t("[ERROR]","red")} ${e}`)},success(e){console.log(`${t("[SUCCESS]","green")} ${e}`)}};var T=m(require("path"));function fe(){return`<!DOCTYPE html>
|
|
3
3
|
<html lang="zh-CN">
|
|
4
4
|
<head>
|
|
5
5
|
<meta charset="UTF-8" />
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
<script type="module" src="/src/main.ts"></script>
|
|
12
12
|
</body>
|
|
13
13
|
</html>
|
|
14
|
-
`}function
|
|
14
|
+
`}function ge(){return`import { createApp } from 'lyt';
|
|
15
15
|
import App from './App';
|
|
16
16
|
|
|
17
17
|
// \u521B\u5EFA\u5E94\u7528\u5B9E\u4F8B
|
|
@@ -19,7 +19,7 @@ const app = createApp(App);
|
|
|
19
19
|
|
|
20
20
|
// \u5C06\u5E94\u7528\u6302\u8F7D\u5230 #app \u5143\u7D20
|
|
21
21
|
app.mount('#app');
|
|
22
|
-
`}function
|
|
22
|
+
`}function me(){return`import { defineComponent } from 'lyt';
|
|
23
23
|
|
|
24
24
|
// \u5B9A\u4E49\u6839\u7EC4\u4EF6
|
|
25
25
|
const App = defineComponent({
|
|
@@ -44,7 +44,7 @@ const App = defineComponent({
|
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
export default App;
|
|
47
|
-
`}function
|
|
47
|
+
`}function he(){return`/* Lyt \u5168\u5C40\u6837\u5F0F */
|
|
48
48
|
|
|
49
49
|
* {
|
|
50
50
|
margin: 0;
|
|
@@ -75,9 +75,9 @@ body {
|
|
|
75
75
|
font-size: 1.2rem;
|
|
76
76
|
color: #666;
|
|
77
77
|
}
|
|
78
|
-
`}function
|
|
79
|
-
`}function
|
|
80
|
-
`}function
|
|
78
|
+
`}function ye(e){return JSON.stringify({name:e,version:"0.1.0",private:!0,type:"module",scripts:{dev:"lyt dev",build:"lyt build",preview:"lyt preview"},dependencies:{lyt:"latest"},devDependencies:{typescript:"^5.0.0"}},null,2)+`
|
|
79
|
+
`}function be(){return JSON.stringify({compilerOptions:{target:"ES2020",module:"ESNext",moduleResolution:"bundler",strict:!0,jsx:"preserve",resolveJsonModule:!0,isolatedModules:!0,esModuleInterop:!0,lib:["ES2020","DOM","DOM.Iterable"],skipLibCheck:!0,noEmit:!0,paths:{"@/*":["./src/*"]}},include:["src/**/*.ts","src/**/*.tsx"],exclude:["node_modules","dist"]},null,2)+`
|
|
80
|
+
`}function xe(){return`# \u4F9D\u8D56
|
|
81
81
|
node_modules/
|
|
82
82
|
|
|
83
83
|
# \u6784\u5EFA\u8F93\u51FA
|
|
@@ -100,9 +100,9 @@ Thumbs.db
|
|
|
100
100
|
# \u65E5\u5FD7
|
|
101
101
|
*.log
|
|
102
102
|
npm-debug.log*
|
|
103
|
-
`}async function _(e,
|
|
104
|
-
`}function $e(e){let
|
|
105
|
-
`}function
|
|
103
|
+
`}async function _(e,o={}){let n=o.template||"spa",r=T.resolve(process.cwd(),e);i.info(`\u6B63\u5728\u521B\u5EFA Lyt \u9879\u76EE: ${t(e,"brightCyan")}`),i.info(`\u4F7F\u7528\u6A21\u677F: ${t(n,"brightCyan")}`),F(r)&&(i.error(`\u76EE\u5F55 "${e}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u9009\u62E9\u5176\u4ED6\u540D\u79F0\u6216\u5220\u9664\u5DF2\u6709\u76EE\u5F55`),process.exit(1)),$(r);let s=[{filePath:"index.html",content:fe()},{filePath:"src/main.ts",content:ge()},{filePath:"src/App.ts",content:me()},{filePath:"src/style.css",content:he()},{filePath:"package.json",content:ye(e)},{filePath:"tsconfig.json",content:be()},{filePath:".gitignore",content:xe()}];for(let a of s){let u=T.join(r,a.filePath);S(u,a.content),i.success(` \u521B\u5EFA ${a.filePath}`)}console.log(""),i.success(`\u9879\u76EE ${t(e,"brightCyan")} \u521B\u5EFA\u6210\u529F\uFF01`),console.log(""),console.log(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u542F\u52A8\u9879\u76EE\uFF1A"),console.log(""),console.log(` ${t("cd","brightGreen")} ${e}`),console.log(` ${t("npm install","brightGreen")}`),console.log(` ${t("npm run dev","brightGreen")}`),console.log("")}var D=m(require("path"));function ve(e){let o={name:e.name,version:"0.1.0",private:!0,type:"module",scripts:{dev:"lytx dev",build:"lytx build",preview:"lytx preview"},dependencies:{lyt:"^2.0.0"}};return e.ts&&(o.devDependencies={typescript:"^5.0.0"}),e.eslint&&(o.devDependencies=o.devDependencies||{},o.devDependencies.eslint="^8.0.0",o.scripts=o.scripts||{},o.scripts.lint="eslint src --ext .ts,.js"),e.router&&(o.dependencies["@lytjs/router"]="^2.0.0"),e.store&&(o.dependencies["@lytjs/store"]="^2.0.0"),JSON.stringify(o,null,2)+`
|
|
104
|
+
`}function $e(e){let o={compilerOptions:{target:"ES2020",module:"ESNext",moduleResolution:"bundler",strict:!0,jsx:"preserve",resolveJsonModule:!0,isolatedModules:!0,esModuleInterop:!0,lib:["ES2020","DOM","DOM.Iterable"],skipLibCheck:!0,noEmit:!0,paths:{"@/*":["./src/*"]}},include:["src/**/*.ts","src/**/*.tsx"],exclude:["node_modules","dist"]};return e.template==="ssr"&&(o.compilerOptions.types=["node"]),JSON.stringify(o,null,2)+`
|
|
105
|
+
`}function we(e){let o=e.ts?".ts":".js";return`<!DOCTYPE html>
|
|
106
106
|
<html lang="zh-CN">
|
|
107
107
|
<head>
|
|
108
108
|
<meta charset="UTF-8" />
|
|
@@ -112,11 +112,11 @@ npm-debug.log*
|
|
|
112
112
|
</head>
|
|
113
113
|
<body>
|
|
114
114
|
<div id="app"></div>
|
|
115
|
-
<script type="module" src="/src/main${
|
|
115
|
+
<script type="module" src="/src/main${o}"></script>
|
|
116
116
|
</body>
|
|
117
117
|
</html>
|
|
118
|
-
`}function Se(e){let
|
|
119
|
-
`)}function
|
|
118
|
+
`}function Se(e){let o=["// Lytx \u914D\u7F6E\u6587\u4EF6","import { defineConfig } from 'lyt'","","export default defineConfig({"," // \u6784\u5EFA\u6A21\u5F0F",` mode: '${e.template}',`];return e.router&&(o.push(" // \u8DEF\u7531\u914D\u7F6E"),o.push(" router: {"),o.push(" historyMode: true,"),o.push(" },")),e.store&&(o.push(" // \u72B6\u6001\u7BA1\u7406\u914D\u7F6E"),o.push(" store: {"),o.push(" strict: true,"),o.push(" },")),o.push("})"),o.push(""),o.join(`
|
|
119
|
+
`)}function Ce(e){let o=e.ts?".ts":".js",n=["import { createApp } from 'lyt'","import App from './App.lyt'","import './styles/main.css'"];return e.router&&n.push("import { router } from './router'"),e.store&&n.push("import { store } from './store'"),n.push(""),n.push("// \u521B\u5EFA\u5E94\u7528\u5B9E\u4F8B"),n.push("const app = createApp(App)"),e.router&&n.push("app.use(router)"),e.store&&n.push("app.use(store)"),n.push(""),n.push("// \u5C06\u5E94\u7528\u6302\u8F7D\u5230 #app \u5143\u7D20"),n.push("app.mount('#app')"),n.join(`
|
|
120
120
|
`)+`
|
|
121
121
|
`}function Me(){return`<template>
|
|
122
122
|
<div class="app">
|
|
@@ -146,7 +146,7 @@ export default defineComponent({
|
|
|
146
146
|
padding: 20px;
|
|
147
147
|
}
|
|
148
148
|
</style>
|
|
149
|
-
`}function
|
|
149
|
+
`}function Pe(){return`import { defineComponent } from 'lyt'
|
|
150
150
|
|
|
151
151
|
export default defineComponent({
|
|
152
152
|
name: 'HomePage',
|
|
@@ -158,7 +158,7 @@ export default defineComponent({
|
|
|
158
158
|
</div>
|
|
159
159
|
\`,
|
|
160
160
|
})
|
|
161
|
-
`}function
|
|
161
|
+
`}function Re(){return`import { defineComponent } from 'lyt'
|
|
162
162
|
|
|
163
163
|
export default defineComponent({
|
|
164
164
|
name: 'AboutPage',
|
|
@@ -170,7 +170,7 @@ export default defineComponent({
|
|
|
170
170
|
</div>
|
|
171
171
|
\`,
|
|
172
172
|
})
|
|
173
|
-
`}function
|
|
173
|
+
`}function je(){return`import { defineComponent } from 'lyt'
|
|
174
174
|
|
|
175
175
|
export default defineComponent({
|
|
176
176
|
name: 'Header',
|
|
@@ -184,7 +184,7 @@ export default defineComponent({
|
|
|
184
184
|
</header>
|
|
185
185
|
\`,
|
|
186
186
|
})
|
|
187
|
-
`}function
|
|
187
|
+
`}function He(){return`import { createRouter, createWebHistory } from '@lytjs/router'
|
|
188
188
|
import HomePage from '../pages/index'
|
|
189
189
|
import AboutPage from '../pages/about'
|
|
190
190
|
|
|
@@ -201,7 +201,7 @@ export const router = createRouter({
|
|
|
201
201
|
},
|
|
202
202
|
],
|
|
203
203
|
})
|
|
204
|
-
`}function
|
|
204
|
+
`}function Ae(){return`import { createStore } from '@lytjs/store'
|
|
205
205
|
|
|
206
206
|
export const store = createStore({
|
|
207
207
|
state: {
|
|
@@ -229,7 +229,7 @@ export const store = createStore({
|
|
|
229
229
|
doubleCount: (state: any) => state.count * 2,
|
|
230
230
|
},
|
|
231
231
|
})
|
|
232
|
-
`}function
|
|
232
|
+
`}function Ee(){return`/* \u5168\u5C40\u6837\u5F0F */
|
|
233
233
|
|
|
234
234
|
* {
|
|
235
235
|
margin: 0;
|
|
@@ -253,29 +253,17 @@ a {
|
|
|
253
253
|
a:hover {
|
|
254
254
|
text-decoration: underline;
|
|
255
255
|
}
|
|
256
|
-
`}function
|
|
256
|
+
`}function Fe(){return`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
|
|
257
257
|
<rect width="32" height="32" rx="6" fill="#42b883"/>
|
|
258
258
|
<text x="16" y="22" text-anchor="middle" fill="white" font-size="18" font-weight="bold">L</text>
|
|
259
259
|
</svg>
|
|
260
|
-
`}function
|
|
261
|
-
`}async function
|
|
262
|
-
Upgrade: websocket\r
|
|
263
|
-
Connection: Upgrade\r
|
|
264
|
-
Sec-WebSocket-Accept: ${i}\r
|
|
265
|
-
\r
|
|
266
|
-
`);let a={socket:n,isAlive:!0};n.on("close",()=>{this.clients=this.clients.filter(c=>c!==a)}),n.on("error",()=>{this.clients=this.clients.filter(c=>c!==a)}),this.clients.push(a),l.info("WebSocket \u5BA2\u6237\u7AEF\u5DF2\u8FDE\u63A5")}broadcast(t){let n=[];for(let r of this.clients)try{if(!r.isAlive){n.push(r);continue}let s=Buffer.from(t,"utf-8"),i=this.createFrame(129,s);r.socket.write(i)}catch(s){n.push(r)}for(let r of n)this.clients=this.clients.filter(s=>s!==r),r.socket.destroy()}createFrame(t,n){let s=n.length,i;s<126?i=2:s<65536?i=4:i=10;let a=Buffer.alloc(i+s);return a[0]=t,s<126?a[1]=s:s<65536?(a[1]=126,a.writeUInt16BE(s,2)):(a[1]=127,a.writeUInt32BE(0,2),a.writeUInt32BE(s,6)),n.copy(a,i),a}getClientCount(){return this.clients.length}};function Be(e,t){let n=new Set([".ts",".tsx",".js",".jsx",".css",".html",".json"]);function r(s){try{let i=C.readdirSync(s,{withFileTypes:!0});for(let a of i){let c=g.join(s,a.name);if(a.isDirectory()){if(a.name==="node_modules"||a.name===".git"||a.name==="dist")continue;r(c)}else if(a.isFile()){let p=g.extname(a.name);n.has(p)&&C.watch(c,{persistent:!1},u=>{if(u==="change"){let f=g.relative(e,c);l.info(`\u6587\u4EF6\u53D8\u5316: ${o(f,"brightYellow")}`);let d=JSON.stringify({type:"hmr-update",path:`/${f}`,timestamp:Date.now()});t.broadcast(d)}})}}}catch(i){}}r(e),l.info(`\u6B63\u5728\u76D1\u542C ${o(e,"brightCyan")} \u76EE\u5F55\u7684\u6587\u4EF6\u53D8\u5316...`)}function Le(e,t){let n=e.url||"/";if(n==="/favicon.ico")return{statusCode:204,headers:{},body:""};let r=n.split("?")[0],s=g.normalize(r).replace(/^(\.\.[\/\\])+/,""),i=g.join(t,s);try{C.statSync(i).isDirectory()&&(i=g.join(i,"index.html"))}catch(a){}try{let a=C.readFileSync(i),c=g.extname(i);if(c===".ts"||c===".tsx"){let u=a.toString("utf-8"),f=Oe(u,i);return{statusCode:200,headers:{"Content-Type":"application/javascript; charset=utf-8","Cache-Control":"no-cache, no-store, must-revalidate","Access-Control-Allow-Origin":"*"},body:f}}return{statusCode:200,headers:{"Content-Type":z(i),"Cache-Control":c===".html"?"no-cache":"public, max-age=3600","Access-Control-Allow-Origin":"*"},body:a}}catch(a){let c=a.code;return c==="ENOENT"?{statusCode:404,headers:{"Content-Type":"text/html; charset=utf-8"},body:`<!DOCTYPE html>
|
|
267
|
-
<html>
|
|
268
|
-
<head><title>404 Not Found</title></head>
|
|
269
|
-
<body>
|
|
270
|
-
<h1>404 Not Found</h1>
|
|
271
|
-
<p>\u627E\u4E0D\u5230\u6587\u4EF6: ${r}</p>
|
|
272
|
-
</body>
|
|
273
|
-
</html>`}:c==="EACCES"?{statusCode:403,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"403 Forbidden"}:{statusCode:500,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"500 Internal Server Error"}}}function U(e={}){let t=e.port||3e3,n=g.resolve(e.root||process.cwd()),r=e.hmr!==!1,s=new I,i=J.createServer((c,p)=>{let u=Le(c,n);p.writeHead(u.statusCode,u.headers),p.end(u.body)});r&&i.on("upgrade",(c,p,u)=>{s.handleUpgrade(c,p,u)}),i.listen(t,()=>{console.log(""),console.log(o(" \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557","brightCyan")),console.log(o(" \u2551","brightCyan")+o(" Lyt \u5F00\u53D1\u670D\u52A1\u5668\u5DF2\u542F\u52A8 ","brightWhite")+o("\u2551","brightCyan")),console.log(o(" \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D","brightCyan")),console.log(""),console.log(` ${o("\u279C","brightGreen")} \u672C\u5730\u8BBF\u95EE: ${o(`http://localhost:${t}`,"brightBlue")}`),console.log(` ${o("\u279C","brightGreen")} \u7F51\u7EDC\u8BBF\u95EE: ${o(`http://127.0.0.1:${t}`,"brightBlue")}`),console.log(` ${o("\u279C","brightGreen")} \u9879\u76EE\u76EE\u5F55: ${o(n,"brightBlue")}`),console.log(` ${o("\u279C","brightGreen")} \u70ED\u66F4\u65B0: ${o(r?"\u5DF2\u5F00\u542F":"\u5DF2\u5173\u95ED",r?"brightGreen":"brightRed")}`),console.log(""),console.log(` ${o("\u6309 Ctrl+C \u505C\u6B62\u670D\u52A1\u5668","dim")}`),console.log("")}),i.on("error",c=>{c.code==="EADDRINUSE"?(l.error(`\u7AEF\u53E3 ${t} \u5DF2\u88AB\u5360\u7528\uFF0C\u8BF7\u4F7F\u7528 --port \u6307\u5B9A\u5176\u4ED6\u7AEF\u53E3`),process.exit(1)):(l.error(`\u670D\u52A1\u5668\u542F\u52A8\u5931\u8D25: ${c.message}`),process.exit(1))}),r&&Be(n,s);let a=()=>{l.info("\u6B63\u5728\u5173\u95ED\u5F00\u53D1\u670D\u52A1\u5668..."),i.close(()=>{l.success("\u670D\u52A1\u5668\u5DF2\u5173\u95ED"),process.exit(0)}),setTimeout(()=>{l.warn("\u670D\u52A1\u5668\u5173\u95ED\u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(1)},5e3)};process.on("SIGINT",a),process.on("SIGTERM",a)}var y=m(require("fs")),h=m(require("path"));var Ie=require("esbuild");async function V(e={}){let t=Date.now(),n=h.resolve(e.root||process.cwd()),r=h.resolve(n,e.outDir||"dist"),s=e.entry||"index.html",i=e.minify||!1;l.info("\u5F00\u59CB\u6784\u5EFA\u9879\u76EE..."),l.info(` \u6839\u76EE\u5F55: ${o(n,"brightCyan")}`),l.info(` \u8F93\u51FA\u76EE\u5F55: ${o(r,"brightCyan")}`),l.info(` \u538B\u7F29: ${o(i?"\u5F00\u542F":"\u5173\u95ED",i?"brightGreen":"brightYellow")}`);let a=h.join(n,s);y.existsSync(a)||(l.error(`\u5165\u53E3\u6587\u4EF6\u4E0D\u5B58\u5728: ${a}`),process.exit(1));let c=Y(a),p=/<script\s+type="module"\s+src="([^"]+)"\s*><\/script>/g,u,f=[];for(;(u=p.exec(c))!==null;)f.push(u[1]);f.length===0&&l.warn("\u672A\u5728\u5165\u53E3 HTML \u4E2D\u627E\u5230\u6A21\u5757\u811A\u672C\u5F15\u7528");let d={inputFiles:0,outputFiles:0,totalSize:0,buildTime:0};$(r),$(h.join(r,"assets"));for(let v of f){let E=h.join(n,v);if(!y.existsSync(E)){l.warn(`\u811A\u672C\u6587\u4EF6\u4E0D\u5B58\u5728: ${v}`);continue}l.info(`\u6B63\u5728\u6253\u5305: ${o(v,"brightYellow")}`);let R=`${h.basename(v,h.extname(v))}.bundle.js`,M=h.join(r,"assets",R);try{await Ie.build({entryPoints:[E],bundle:!0,minify:i,target:"es2018",format:"esm",outfile:M,sourcemap:!0,external:k=>k.startsWith("@lytjs/"),drop:i?["console"]:[],metafile:!0});let w=y.readFileSync(M,"utf-8");d.totalSize+=Buffer.byteLength(w,"utf-8"),d.outputFiles++;let S=M+".map";if(y.existsSync(S)){let k=y.readFileSync(S,"utf-8");d.totalSize+=Buffer.byteLength(k,"utf-8"),d.outputFiles++}c=c.replace(`<script type="module" src="${v}"></script>`,`<script src="/assets/${R}"></script>`),d.inputFiles++}catch(w){let S=w instanceof Error?w.message:String(w);l.error(`\u6253\u5305\u5931\u8D25 ${v}: ${S}`),process.exit(1)}}Ue(n,r,d);let j=h.join(r,"index.html");P(j,c),d.outputFiles++,d.totalSize+=Buffer.byteLength(c,"utf-8"),d.buildTime=Date.now()-t,console.log(""),l.success("\u6784\u5EFA\u5B8C\u6210\uFF01"),console.log(""),console.log(` ${o("\u8F93\u5165\u6587\u4EF6:","brightWhite")} ${d.inputFiles} \u4E2A`),console.log(` ${o("\u8F93\u51FA\u6587\u4EF6:","brightWhite")} ${d.outputFiles} \u4E2A`),console.log(` ${o("\u603B\u5927\u5C0F:","brightWhite")} ${Ge(d.totalSize)}`),console.log(` ${o("\u6784\u5EFA\u8017\u65F6:","brightWhite")} ${d.buildTime}ms`),console.log(` ${o("\u8F93\u51FA\u76EE\u5F55:","brightWhite")} ${o(r,"brightCyan")}`),console.log("")}function Ue(e,t,n){let r=h.join(e,"src");if(!y.existsSync(r))return;function s(i,a){let c=y.readdirSync(i,{withFileTypes:!0});for(let p of c){let u=h.join(i,p.name);if(p.isDirectory()){if(p.name==="node_modules")continue;s(u,a)}else if(p.isFile()){let f=h.extname(p.name);if([".ts",".tsx",".js",".jsx"].includes(f))continue;let d=h.relative(a,u),j=h.join(t,d);P(j,y.readFileSync(u).toString("utf-8")),n.outputFiles++,n.totalSize+=y.statSync(u).size}}}s(r,r)}function Ge(e){if(e===0)return"0 B";let t=["B","KB","MB","GB"],n=1024,r=Math.floor(Math.log(e)/Math.log(n));return`${(e/Math.pow(n,r)).toFixed(2)} ${t[r]}`}var K=m(require("http")),A=m(require("fs")),x=m(require("path")),X=m(require("crypto")),L=class{constructor(){this.clients=[]}handleUpgrade(t,n,r){let s=t.headers["sec-websocket-key"];if(!s){n.destroy();return}let i=X.createHash("sha1").update(s+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");n.write(`HTTP/1.1 101 Switching Protocols\r
|
|
260
|
+
`}function ke(){return JSON.stringify({root:!0,env:{browser:!0,es2021:!0,node:!0},extends:["eslint:recommended"],parserOptions:{ecmaVersion:"latest",sourceType:"module"},rules:{"no-unused-vars":"warn","no-console":"warn"}},null,2)+`
|
|
261
|
+
`}async function L(e){let{name:o,template:n,ts:r,router:s,store:a,eslint:u}=e,l=D.resolve(process.cwd(),o);if(i.info(`\u6B63\u5728\u521B\u5EFA Lyt \u9879\u76EE: ${t(o,"brightCyan")}`),i.info(`\u4F7F\u7528\u6A21\u677F: ${t(n,"brightCyan")}`),F(l))throw i.error(`\u76EE\u5F55 "${o}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u9009\u62E9\u5176\u4ED6\u540D\u79F0\u6216\u5220\u9664\u5DF2\u6709\u76EE\u5F55`),new Error(`Directory "${o}" already exists`);$(l);let c=[{filePath:"package.json",content:ve(e)},{filePath:"index.html",content:we(e)},{filePath:"lytx.config.ts",content:Se(e)},{filePath:"src/main.ts",content:Ce(e)},{filePath:"src/App.lyt",content:Me()},{filePath:"src/pages/index.ts",content:Pe()},{filePath:"src/pages/about.ts",content:Re()},{filePath:"src/components/Header.ts",content:je()},{filePath:"src/styles/main.css",content:Ee()},{filePath:"public/favicon.svg",content:Fe()}];r&&c.push({filePath:"tsconfig.json",content:$e(e)}),s&&c.push({filePath:"src/router/index.ts",content:He()}),a&&c.push({filePath:"src/store/index.ts",content:Ae()}),u&&c.push({filePath:".eslintrc.json",content:ke()});for(let p of c){let d=D.join(l,p.filePath);S(d,p.content),i.success(` \u521B\u5EFA ${p.filePath}`)}console.log(""),i.success(`\u9879\u76EE ${t(o,"brightCyan")} \u521B\u5EFA\u6210\u529F\uFF01`),console.log(""),console.log(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u542F\u52A8\u9879\u76EE\uFF1A"),console.log(""),console.log(` ${t("cd","brightGreen")} ${o}`),console.log(` ${t("npm install","brightGreen")}`),console.log(` ${t("npm run dev","brightGreen")}`),console.log("")}var K=m(require("fs")),B=m(require("fs")),P=m(require("path"));var J=m(require("http")),M=m(require("fs")),b=m(require("path")),q=m(require("crypto")),O=class{constructor(){this.clients=[]}handleUpgrade(o,n,r){let s=o.headers["sec-websocket-key"];if(!s){n.destroy();return}let a=q.createHash("sha1").update(s+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");n.write(`HTTP/1.1 101 Switching Protocols\r
|
|
274
262
|
Upgrade: websocket\r
|
|
275
263
|
Connection: Upgrade\r
|
|
276
|
-
Sec-WebSocket-Accept: ${
|
|
264
|
+
Sec-WebSocket-Accept: ${a}\r
|
|
277
265
|
\r
|
|
278
|
-
`);let
|
|
266
|
+
`);let u={socket:n,isAlive:!0};n.on("close",()=>{this.clients=this.clients.filter(l=>l!==u)}),n.on("error",()=>{this.clients=this.clients.filter(l=>l!==u)}),this.clients.push(u)}broadcast(o){let n=[];for(let r of this.clients)try{if(!r.isAlive){n.push(r);continue}let s=Buffer.from(o,"utf-8"),a=this.createFrame(129,s);r.socket.write(a)}catch(s){n.push(r)}for(let r of n){this.clients=this.clients.filter(s=>s!==r);try{r.socket.destroy()}catch(s){}}}createFrame(o,n){let r=n.length,s;r<126?s=2:r<65536?s=4:s=10;let a=Buffer.alloc(s+r);return a[0]=o,r<126?a[1]=r:r<65536?(a[1]=126,a.writeUInt16BE(r,2)):(a[1]=127,a.writeUInt32BE(0,2),a.writeUInt32BE(r,6)),n.copy(a,s),a}getClientCount(){return this.clients.length}closeAll(){for(let o of this.clients)try{o.socket.destroy()}catch(n){}this.clients=[]}};function G(e){let o=new O,n=[],r=[],s=null,a=!1;function u(c){let p=b.extname(c).toLowerCase();if(p===".css")return"css";let d=b.basename(c);return d.startsWith("lytx.config")||d==="tsconfig.json"||d==="package.json"?"reload":p===".ts"||p===".tsx"||p===".lyt"||p===".js"||p===".jsx"?"update":"reload"}function l(c){try{let p=M.readdirSync(c,{withFileTypes:!0});for(let d of p){let f=b.join(c,d.name);if(d.isDirectory()){if(d.name==="node_modules"||d.name===".git"||d.name==="dist")continue;l(f)}else if(d.isFile()){let R=b.extname(d.name).toLowerCase();if(new Set([".ts",".tsx",".js",".jsx",".css",".html",".json",".lyt"]).has(R))try{let j=M.watch(f,{persistent:!1},W=>{if(W==="change"){let H=b.relative(e,f);for(let w of n)try{w(H)}catch(A){}let C=u(f),v={type:C,path:`/${H}`};if(C==="css")try{v.content=M.readFileSync(f,"utf-8")}catch(w){}o.broadcast(JSON.stringify(v))}});r.push(j)}catch(j){}}}}catch(p){}}return{start(c){a||(a=!0,s=J.createServer((p,d)=>{d.writeHead(426,{"Content-Type":"text/plain"}),d.end("Upgrade Required")}),s.on("upgrade",(p,d,f)=>{o.handleUpgrade(p,d,f)}),s.listen(c,()=>{}),l(e))},stop(){a=!1;for(let c of r)try{c.close()}catch(p){}if(r.length=0,o.closeAll(),s){try{s.close()}catch(c){}s=null}},onFileChange(c){n.push(c)},notifyClient(c){o.broadcast(JSON.stringify(c))}}}function V(e){let o=new O;return e.on("upgrade",(n,r,s)=>{o.handleUpgrade(n,r,s)}),{broadcast(n){o.broadcast(n)},getClientCount(){return o.getClientCount()}}}function I(){return`(function() {
|
|
279
267
|
'use strict';
|
|
280
268
|
|
|
281
269
|
var ws = null;
|
|
@@ -373,111 +361,112 @@ Sec-WebSocket-Accept: ${i}\r
|
|
|
373
361
|
|
|
374
362
|
// \u542F\u52A8\u8FDE\u63A5
|
|
375
363
|
connect();
|
|
376
|
-
})();`}var
|
|
377
|
-
${o(
|
|
364
|
+
})();`}var X;try{X=require("esbuild")}catch(e){i.error("\u7F3A\u5C11\u4F9D\u8D56: esbuild"),i.error(""),i.error(" Lyt CLI \u7684\u5F00\u53D1\u670D\u52A1\u5668\u9700\u8981 esbuild \u6765\u5B9E\u65F6\u7F16\u8BD1 TypeScript\u3002"),i.error(""),i.error(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u5B89\u88C5:"),i.error(` ${t("npm install esbuild --save-dev","brightGreen")}`),i.error(""),i.error(" \u5982\u679C\u60A8\u4F7F\u7528 pnpm:"),i.error(` ${t("pnpm add esbuild -D","brightGreen")}`),i.error(""),process.exit(1)}function Te(e,o){try{return X.transformSync(e,{loader:"ts",target:"es2018",format:"esm",sourcemap:"inline"}).code}catch(n){let r=n instanceof Error?n.message:String(n);return console.error(`[Lyt CLI] \u7F16\u8BD1\u9519\u8BEF ${o||""}:`,r),e}}function De(e,o){var c;let n=((c=e.url)==null?void 0:c.split("?")[0])||"/";n==="/"&&(n="/index.html");let r=P.join(o,n);if(!r.startsWith(o))return{statusCode:403,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"403 Forbidden"};if(!B.existsSync(r)||!B.statSync(r).isFile())return{statusCode:404,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"404 Not Found"};let s=P.extname(r).toLowerCase(),u={".html":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"application/javascript; charset=utf-8",".mjs":"application/javascript; charset=utf-8",".ts":"application/typescript; charset=utf-8",".json":"application/json; charset=utf-8",".svg":"image/svg+xml",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".ico":"image/x-icon",".woff":"font/woff",".woff2":"font/woff2",".txt":"text/plain; charset=utf-8"}[s]||"application/octet-stream",l=k(r);return(s===".ts"||s===".tsx")&&(l=Te(l,n)),s===".html"&&(l=l.replace("</head>",`<script>${I()}</script>
|
|
365
|
+
</head>`)),{statusCode:200,headers:{"Content-Type":u},body:l}}function N(e={}){let o=e.port||3e3,n=P.resolve(e.root||process.cwd()),r=e.hmr!==!1,s=G(n),a=K.createServer((l,c)=>{let p=De(l,n);c.writeHead(p.statusCode,p.headers),c.end(p.body)});r&&a.on("upgrade",(l,c,p)=>{s.handleUpgrade(l,c,p)}),a.listen(o,()=>{console.log(""),console.log(t(" \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557","brightCyan")),console.log(t(" \u2551","brightCyan")+t(" Lyt \u5F00\u53D1\u670D\u52A1\u5668\u5DF2\u542F\u52A8 ","brightWhite")+t("\u2551","brightCyan")),console.log(t(" \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D","brightCyan")),console.log(""),console.log(` ${t("\u279C","brightGreen")} \u672C\u5730\u8BBF\u95EE: ${t(`http://localhost:${o}`,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u7F51\u7EDC\u8BBF\u95EE: ${t(`http://127.0.0.1:${o}`,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u9879\u76EE\u76EE\u5F55: ${t(n,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u70ED\u66F4\u65B0: ${t(r?"\u5DF2\u5F00\u542F":"\u5DF2\u5173\u95ED",r?"brightGreen":"brightRed")}`),console.log(""),console.log(` ${t("\u6309 Ctrl+C \u505C\u6B62\u670D\u52A1\u5668","dim")}`),console.log("")}),a.on("error",l=>{l.code==="EADDRINUSE"?(i.error(`\u7AEF\u53E3 ${o} \u5DF2\u88AB\u5360\u7528\uFF0C\u8BF7\u4F7F\u7528 --port \u6307\u5B9A\u5176\u4ED6\u7AEF\u53E3`),process.exit(1)):(i.error(`\u670D\u52A1\u5668\u542F\u52A8\u5931\u8D25: ${l.message}`),process.exit(1))});let u=()=>{i.info("\u6B63\u5728\u5173\u95ED\u5F00\u53D1\u670D\u52A1\u5668..."),a.close(()=>{i.success("\u670D\u52A1\u5668\u5DF2\u5173\u95ED"),process.exit(0)}),setTimeout(()=>{i.warn("\u670D\u52A1\u5668\u5173\u95ED\u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(1)},5e3)};process.on("SIGINT",u),process.on("SIGTERM",u)}var h=m(require("fs")),g=m(require("path"));var Q;try{Q=require("esbuild")}catch(e){i.error("\u7F3A\u5C11\u4F9D\u8D56: esbuild"),i.error(""),i.error(" Lyt CLI \u7684\u6784\u5EFA\u529F\u80FD\u9700\u8981 esbuild\u3002"),i.error(""),i.error(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u5B89\u88C5:"),i.error(` ${t("npm install esbuild --save-dev","brightGreen")}`),i.error(""),i.error(" \u5982\u679C\u60A8\u4F7F\u7528 pnpm:"),i.error(` ${t("pnpm add esbuild -D","brightGreen")}`),i.error(""),process.exit(1)}async function Z(e={}){let o=Date.now(),n=g.resolve(e.root||process.cwd()),r=g.resolve(n,e.outDir||"dist"),s=e.entry||"index.html",a=e.minify||!1;i.info("\u5F00\u59CB\u6784\u5EFA\u9879\u76EE..."),i.info(` \u6839\u76EE\u5F55: ${t(n,"brightCyan")}`),i.info(` \u8F93\u51FA\u76EE\u5F55: ${t(r,"brightCyan")}`),i.info(` \u538B\u7F29: ${t(a?"\u5F00\u542F":"\u5173\u95ED",a?"brightGreen":"brightYellow")}`);let u=g.join(n,s);h.existsSync(u)||(i.error(`\u5165\u53E3\u6587\u4EF6\u4E0D\u5B58\u5728: ${u}`),process.exit(1));let l=k(u),c=/<script\s+type="module"\s+src="([^"]+)"\s*><\/script>/g,p,d=[];for(;(p=c.exec(l))!==null;)d.push(p[1]);d.length===0&&i.warn("\u672A\u5728\u5165\u53E3 HTML \u4E2D\u627E\u5230\u6A21\u5757\u811A\u672C\u5F15\u7528");let f={inputFiles:0,outputFiles:0,totalSize:0,buildTime:0};$(r),$(g.join(r,"assets"));for(let x of d){let j=g.join(n,x);if(!h.existsSync(j)){i.warn(`\u811A\u672C\u6587\u4EF6\u4E0D\u5B58\u5728: ${x}`);continue}i.info(`\u6B63\u5728\u6253\u5305: ${t(x,"brightYellow")}`);let H=`${g.basename(x,g.extname(x))}.bundle.js`,C=g.join(r,"assets",H);try{await Q.build({entryPoints:[j],bundle:!0,minify:a,target:"es2018",format:"esm",outfile:C,sourcemap:!0,external:A=>A.startsWith("@lytjs/"),drop:a?["console"]:[],metafile:!0});let v=h.readFileSync(C,"utf-8");f.totalSize+=Buffer.byteLength(v,"utf-8"),f.outputFiles++;let w=C+".map";if(h.existsSync(w)){let A=h.readFileSync(w,"utf-8");f.totalSize+=Buffer.byteLength(A,"utf-8"),f.outputFiles++}l=l.replace(`<script type="module" src="${x}"></script>`,`<script src="/assets/${H}"></script>`),f.inputFiles++}catch(v){let w=v instanceof Error?v.message:String(v);i.error(`\u6253\u5305\u5931\u8D25 ${x}: ${w}`),process.exit(1)}}Le(n,r,f);let R=g.join(r,"index.html");S(R,l),f.outputFiles++,f.totalSize+=Buffer.byteLength(l,"utf-8"),f.buildTime=Date.now()-o,console.log(""),i.success("\u6784\u5EFA\u5B8C\u6210\uFF01"),console.log(""),console.log(` ${t("\u8F93\u5165\u6587\u4EF6:","brightWhite")} ${f.inputFiles} \u4E2A`),console.log(` ${t("\u8F93\u51FA\u6587\u4EF6:","brightWhite")} ${f.outputFiles} \u4E2A`),console.log(` ${t("\u603B\u5927\u5C0F:","brightWhite")} ${Oe(f.totalSize)}`),console.log(` ${t("\u6784\u5EFA\u8017\u65F6:","brightWhite")} ${f.buildTime}ms`),console.log(` ${t("\u8F93\u51FA\u76EE\u5F55:","brightWhite")} ${t(r,"brightCyan")}`),console.log("")}function Le(e,o,n){let r=g.join(e,"src");if(!h.existsSync(r))return;function s(a,u){let l=h.readdirSync(a,{withFileTypes:!0});for(let c of l){let p=g.join(a,c.name);if(c.isDirectory()){if(c.name==="node_modules")continue;s(p,u)}else if(c.isFile()){let d=g.extname(c.name);if([".ts",".tsx",".js",".jsx"].includes(d))continue;let f=g.relative(u,p),R=g.join(o,f);S(R,h.readFileSync(p).toString("utf-8")),n.outputFiles++,n.totalSize+=h.statSync(p).size}}}s(r,r)}function Oe(e){if(e===0)return"0 B";let o=["B","KB","MB","GB"],n=1024,r=Math.floor(Math.log(e)/Math.log(n));return`${(e/Math.pow(n,r)).toFixed(2)} ${o[r]}`}var Ge="0.2.0",ne="lytx",Ie="Lyt.js \u6846\u67B6\u547D\u4EE4\u884C\u5DE5\u5177\uFF08\u589E\u5F3A\u7248\uFF09",ee=`
|
|
366
|
+
${t(ne,"brightCyan")} - ${Ie}
|
|
378
367
|
|
|
379
|
-
${
|
|
368
|
+
${t("\u7528\u6CD5:","brightGreen")}
|
|
380
369
|
lytx <command> [options] [args]
|
|
381
370
|
|
|
382
|
-
${
|
|
383
|
-
${
|
|
384
|
-
${
|
|
385
|
-
${
|
|
386
|
-
${
|
|
387
|
-
|
|
388
|
-
${
|
|
389
|
-
${
|
|
390
|
-
${
|
|
391
|
-
|
|
392
|
-
${
|
|
393
|
-
${
|
|
394
|
-
${
|
|
395
|
-
${
|
|
396
|
-
${
|
|
397
|
-
${
|
|
398
|
-
${
|
|
399
|
-
${
|
|
400
|
-
|
|
401
|
-
`,
|
|
402
|
-
${
|
|
403
|
-
|
|
404
|
-
${
|
|
371
|
+
${t("\u547D\u4EE4:","brightGreen")}
|
|
372
|
+
${t("create","brightYellow")} <name> \u521B\u5EFA\u4E00\u4E2A\u65B0\u7684 Lyt \u9879\u76EE
|
|
373
|
+
${t("dev","brightYellow")} \u542F\u52A8\u672C\u5730\u5F00\u53D1\u670D\u52A1\u5668
|
|
374
|
+
${t("build","brightYellow")} \u6784\u5EFA\u751F\u4EA7\u7248\u672C
|
|
375
|
+
${t("preview","brightYellow")} \u9884\u89C8\u6784\u5EFA\u7ED3\u679C
|
|
376
|
+
|
|
377
|
+
${t("\u5168\u5C40\u9009\u9879:","brightGreen")}
|
|
378
|
+
${t("-h, --help","brightYellow")} \u663E\u793A\u5E2E\u52A9\u4FE1\u606F
|
|
379
|
+
${t("-v, --version","brightYellow")} \u663E\u793A\u7248\u672C\u53F7
|
|
380
|
+
|
|
381
|
+
${t("\u793A\u4F8B:","brightGreen")}
|
|
382
|
+
${t("$","dim")} lytx create my-app
|
|
383
|
+
${t("$","dim")} lytx create my-app --template spa --ts --router --store
|
|
384
|
+
${t("$","dim")} lytx dev
|
|
385
|
+
${t("$","dim")} lytx dev --port 8080 --hmr
|
|
386
|
+
${t("$","dim")} lytx build
|
|
387
|
+
${t("$","dim")} lytx build --mode ssr
|
|
388
|
+
${t("$","dim")} lytx preview --port 4173
|
|
389
|
+
|
|
390
|
+
`,Be=`
|
|
391
|
+
${t("lytx create","brightCyan")} - \u521B\u5EFA\u65B0\u7684 Lyt \u9879\u76EE
|
|
392
|
+
|
|
393
|
+
${t("\u7528\u6CD5:","brightGreen")}
|
|
405
394
|
lytx create <name> [options]
|
|
406
395
|
|
|
407
|
-
${
|
|
408
|
-
${
|
|
396
|
+
${t("\u53C2\u6570:","brightGreen")}
|
|
397
|
+
${t("<name>","brightYellow")} \u9879\u76EE\u540D\u79F0\uFF08\u540C\u65F6\u4F5C\u4E3A\u76EE\u5F55\u540D\uFF09
|
|
409
398
|
|
|
410
|
-
${
|
|
411
|
-
${
|
|
399
|
+
${t("\u9009\u9879:","brightGreen")}
|
|
400
|
+
${t("--template <tpl>","brightYellow")} \u9879\u76EE\u6A21\u677F\uFF08\u9ED8\u8BA4: spa\uFF09
|
|
412
401
|
\u53EF\u9009\u503C: spa, ssr, ssg
|
|
413
|
-
${
|
|
414
|
-
${
|
|
415
|
-
${
|
|
416
|
-
${
|
|
402
|
+
${t("--ts","brightYellow")} \u4F7F\u7528 TypeScript
|
|
403
|
+
${t("--router","brightYellow")} \u5305\u542B\u8DEF\u7531
|
|
404
|
+
${t("--store","brightYellow")} \u5305\u542B\u72B6\u6001\u7BA1\u7406
|
|
405
|
+
${t("--eslint","brightYellow")} \u5305\u542B ESLint \u914D\u7F6E
|
|
417
406
|
|
|
418
|
-
${
|
|
419
|
-
${
|
|
420
|
-
${
|
|
421
|
-
${
|
|
407
|
+
${t("\u793A\u4F8B:","brightGreen")}
|
|
408
|
+
${t("$","dim")} lytx create my-app
|
|
409
|
+
${t("$","dim")} lytx create my-app --template spa --ts --router --store
|
|
410
|
+
${t("$","dim")} lytx create my-app --template ssr --ts
|
|
422
411
|
|
|
423
|
-
`,
|
|
424
|
-
${
|
|
412
|
+
`,Ue=`
|
|
413
|
+
${t("lytx dev","brightCyan")} - \u542F\u52A8\u672C\u5730\u5F00\u53D1\u670D\u52A1\u5668
|
|
425
414
|
|
|
426
|
-
${
|
|
415
|
+
${t("\u7528\u6CD5:","brightGreen")}
|
|
427
416
|
lytx dev [options]
|
|
428
417
|
|
|
429
|
-
${
|
|
430
|
-
${
|
|
431
|
-
${
|
|
432
|
-
${
|
|
418
|
+
${t("\u9009\u9879:","brightGreen")}
|
|
419
|
+
${t("-p, --port <port>","brightYellow")} \u670D\u52A1\u7AEF\u53E3\uFF08\u9ED8\u8BA4: 3000\uFF09
|
|
420
|
+
${t("--hmr","brightYellow")} \u5F00\u542F\u70ED\u66F4\u65B0\uFF08\u9ED8\u8BA4: \u5F00\u542F\uFF09
|
|
421
|
+
${t("--no-hmr","brightYellow")} \u5173\u95ED\u70ED\u66F4\u65B0
|
|
433
422
|
|
|
434
|
-
${
|
|
423
|
+
${t("\u529F\u80FD:","brightGreen")}
|
|
435
424
|
- \u9759\u6001\u6587\u4EF6\u670D\u52A1
|
|
436
425
|
- TypeScript \u5373\u65F6\u7F16\u8BD1
|
|
437
426
|
- \u70ED\u6A21\u5757\u66FF\u6362\uFF08HMR\uFF09
|
|
438
427
|
- WebSocket \u5B9E\u65F6\u901A\u4FE1
|
|
439
428
|
|
|
440
|
-
${
|
|
441
|
-
${
|
|
442
|
-
${
|
|
443
|
-
${
|
|
429
|
+
${t("\u793A\u4F8B:","brightGreen")}
|
|
430
|
+
${t("$","dim")} lytx dev
|
|
431
|
+
${t("$","dim")} lytx dev --port 8080
|
|
432
|
+
${t("$","dim")} lytx dev --no-hmr
|
|
444
433
|
|
|
445
|
-
`,
|
|
446
|
-
${
|
|
434
|
+
`,Ne=`
|
|
435
|
+
${t("lytx build","brightCyan")} - \u6784\u5EFA\u751F\u4EA7\u7248\u672C
|
|
447
436
|
|
|
448
|
-
${
|
|
437
|
+
${t("\u7528\u6CD5:","brightGreen")}
|
|
449
438
|
lytx build [options]
|
|
450
439
|
|
|
451
|
-
${
|
|
452
|
-
${
|
|
440
|
+
${t("\u9009\u9879:","brightGreen")}
|
|
441
|
+
${t("--mode <mode>","brightYellow")} \u6784\u5EFA\u6A21\u5F0F\uFF08\u9ED8\u8BA4: spa\uFF09
|
|
453
442
|
\u53EF\u9009\u503C: spa, ssr, ssg
|
|
454
|
-
${
|
|
455
|
-
${
|
|
456
|
-
${
|
|
443
|
+
${t("--minify","brightYellow")} \u538B\u7F29\u4EE3\u7801\uFF08\u53BB\u9664\u7A7A\u767D\u548C\u6CE8\u91CA\uFF09
|
|
444
|
+
${t("-o, --outDir <dir>","brightYellow")} \u8F93\u51FA\u76EE\u5F55\uFF08\u9ED8\u8BA4: dist\uFF09
|
|
445
|
+
${t("--entry <file>","brightYellow")} \u5165\u53E3\u6587\u4EF6\uFF08\u9ED8\u8BA4: index.html\uFF09
|
|
457
446
|
|
|
458
|
-
${
|
|
447
|
+
${t("\u529F\u80FD:","brightGreen")}
|
|
459
448
|
- TypeScript \u7F16\u8BD1
|
|
460
449
|
- \u6A21\u5757\u6253\u5305\uFF08\u5185\u8054\u4F9D\u8D56\uFF09
|
|
461
450
|
- \u53BB\u9664 console.log
|
|
462
451
|
- Source Map \u751F\u6210
|
|
463
452
|
- \u9759\u6001\u8D44\u6E90\u590D\u5236
|
|
464
453
|
|
|
465
|
-
${
|
|
466
|
-
${
|
|
467
|
-
${
|
|
468
|
-
${
|
|
454
|
+
${t("\u793A\u4F8B:","brightGreen")}
|
|
455
|
+
${t("$","dim")} lytx build
|
|
456
|
+
${t("$","dim")} lytx build --mode ssr
|
|
457
|
+
${t("$","dim")} lytx build --minify --outDir ./output
|
|
469
458
|
|
|
470
|
-
`,
|
|
471
|
-
${
|
|
459
|
+
`,We=`
|
|
460
|
+
${t("lytx preview","brightCyan")} - \u9884\u89C8\u6784\u5EFA\u7ED3\u679C
|
|
472
461
|
|
|
473
|
-
${
|
|
462
|
+
${t("\u7528\u6CD5:","brightGreen")}
|
|
474
463
|
lytx preview [options]
|
|
475
464
|
|
|
476
|
-
${
|
|
477
|
-
${
|
|
465
|
+
${t("\u9009\u9879:","brightGreen")}
|
|
466
|
+
${t("-p, --port <port>","brightYellow")} \u670D\u52A1\u7AEF\u53E3\uFF08\u9ED8\u8BA4: 4173\uFF09
|
|
478
467
|
|
|
479
|
-
${
|
|
480
|
-
${
|
|
481
|
-
${
|
|
468
|
+
${t("\u793A\u4F8B:","brightGreen")}
|
|
469
|
+
${t("$","dim")} lytx preview
|
|
470
|
+
${t("$","dim")} lytx preview --port 5000
|
|
482
471
|
|
|
483
|
-
`;function
|
|
472
|
+
`;function Ye(){console.log(""),console.log(` ${t(ne,"brightCyan")} v${t(Ge,"brightWhite")}`),console.log("")}function ze(e){i.error(`\u672A\u77E5\u547D\u4EE4: ${t(e,"brightRed")}`),console.log(""),console.log(` \u8FD0\u884C ${t("lytx --help","brightCyan")} \u67E5\u770B\u53EF\u7528\u547D\u4EE4`),console.log("")}function _e(e){return typeof e=="string"&&["spa","ssr","ssg"].includes(e)?e:"spa"}async function Je(e){if(e.options.help){console.log(Be);return}e.args.length===0&&(i.error("\u8BF7\u63D0\u4F9B\u9879\u76EE\u540D\u79F0"),console.log(""),console.log(` \u7528\u6CD5: ${t("lytx create <name>","brightCyan")}`),console.log(""),console.log(` \u8FD0\u884C ${t("lytx create --help","brightCyan")} \u67E5\u770B\u66F4\u591A\u9009\u9879`),console.log(""),process.exit(1));let o=e.args[0];if(e.options.ts===!0||e.options.router===!0||e.options.store===!0||e.options.eslint===!0||typeof e.options.template=="string"&&["ssr","ssg"].includes(e.options.template)){let r={name:o,template:_e(e.options.template),ts:e.options.ts===!0,router:e.options.router===!0,store:e.options.store===!0,eslint:e.options.eslint===!0};await L(r)}else{let r={template:typeof e.options.template=="string"?e.options.template:"spa"};await _(o,r)}}function qe(e){if(e.options.help){console.log(Ue);return}let o={port:typeof e.options.port=="string"?parseInt(e.options.port,10):typeof e.options.p=="string"?parseInt(e.options.p,10):3e3,hmr:e.options["no-hmr"]!==!0};(isNaN(o.port)||o.port<1||o.port>65535)&&(i.error(`\u65E0\u6548\u7684\u7AEF\u53E3\u53F7: ${e.options.port||e.options.p}`),process.exit(1)),N(o)}async function Ve(e){if(e.options.help){console.log(Ne);return}let o={minify:e.options.minify===!0,outDir:typeof e.options.outDir=="string"?e.options.outDir:typeof e.options.o=="string"?e.options.o:"dist",entry:typeof e.options.entry=="string"?e.options.entry:"index.html"};await Z(o)}function Ke(e){if(e.options.help){console.log(We);return}let o=typeof e.options.port=="string"?parseInt(e.options.port,10):typeof e.options.p=="string"?parseInt(e.options.p,10):4173;(isNaN(o)||o<1||o>65535)&&(i.error(`\u65E0\u6548\u7684\u7AEF\u53E3\u53F7: ${e.options.port||e.options.p}`),process.exit(1));let n=oe.resolve(process.cwd(),"dist");te.existsSync(n)||(i.error("\u672A\u627E\u5230\u6784\u5EFA\u8F93\u51FA\u76EE\u5F55 dist/\uFF0C\u8BF7\u5148\u8FD0\u884C lytx build"),process.exit(1)),N({port:o,root:n,hmr:!1})}async function Xe(){let e=z(process.argv);if(e.options.version){Ye();return}if(e.options.help&&!e.command){console.log(ee);return}e.command||(console.log(ee),process.exit(1));try{switch(e.command){case"create":await Je(e);break;case"dev":qe(e);break;case"build":await Ve(e);break;case"preview":Ke(e);break;default:ze(e.command),process.exit(1)}}catch(o){let n=o instanceof Error?o.message:String(o);i.error(`\u6267\u884C\u5931\u8D25: ${n}`),console.log(""),console.log(` ${t("\u63D0\u793A:","brightYellow")} \u8BF7\u68C0\u67E5\u8F93\u5165\u53C2\u6570\u6216\u8FD0\u884C ${t("lytx --help","brightCyan")} \u67E5\u770B\u5E2E\u52A9`),console.log(""),process.exit(1)}}Xe();0&&(module.exports={createHMREndpoint,createHMRServer,createProject,getHMRClientScript});
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var
|
|
2
|
+
var N=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(o,n)=>(typeof require!="undefined"?require:o)[n]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});import*as Q from"fs";import*as Z from"path";import*as h from"fs";import*as L from"path";var te="\x1B[0m",oe={black:"\x1B[30m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",white:"\x1B[37m",brightRed:"\x1B[91m",brightGreen:"\x1B[92m",brightYellow:"\x1B[93m",brightBlue:"\x1B[94m",brightMagenta:"\x1B[95m",brightCyan:"\x1B[96m",brightWhite:"\x1B[97m",bgBlack:"\x1B[40m",bgRed:"\x1B[41m",bgGreen:"\x1B[42m",bgYellow:"\x1B[43m",bgBlue:"\x1B[44m",bgMagenta:"\x1B[45m",bgCyan:"\x1B[46m",bgWhite:"\x1B[47m",bold:"\x1B[1m",dim:"\x1B[2m",italic:"\x1B[3m",underline:"\x1B[4m",strikethrough:"\x1B[9m"};function t(e,o){let n=oe[o];return n?`${n}${e}${te}`:e}function W(e){let o=e.slice(2),n={command:"",args:[],options:{},raw:o},r=0;for(;r<o.length;){let i=o[r];if(i==="--help"||i==="-h")n.options.help=!0,r++;else if(i==="--version"||i==="-v")n.options.version=!0,r++;else if(i.startsWith("--")){let a=i.indexOf("=");if(a!==-1){let u=i.slice(2,a),l=i.slice(a+1);n.options[u]=l}else{let u=i.slice(2),l=o[r+1];l&&!l.startsWith("-")?(n.options[u]=l,r++):n.options[u]=!0}r++}else if(i.startsWith("-")&&i.length>1){let a=i.slice(1),u=o[r+1];u&&!u.startsWith("-")?(n.options[a]=u,r++):n.options[a]=!0,r++}else n.command?(n.args.push(i),r++):(n.command=i,r++)}return n}function v(e){h.existsSync(e)||h.mkdirSync(e,{recursive:!0})}function A(e){return h.existsSync(e)}function E(e){return h.readFileSync(e,"utf-8")}function w(e,o){let n=L.dirname(e);v(n),h.writeFileSync(e,o,"utf-8")}var s={info(e){console.log(`${t("[INFO]","blue")} ${e}`)},warn(e){console.log(`${t("[WARN]","yellow")} ${e}`)},error(e){console.error(`${t("[ERROR]","red")} ${e}`)},success(e){console.log(`${t("[SUCCESS]","green")} ${e}`)}};import*as F from"path";function ne(){return`<!DOCTYPE html>
|
|
3
3
|
<html lang="zh-CN">
|
|
4
4
|
<head>
|
|
5
5
|
<meta charset="UTF-8" />
|
|
@@ -11,7 +11,7 @@ var U=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Prox
|
|
|
11
11
|
<script type="module" src="/src/main.ts"></script>
|
|
12
12
|
</body>
|
|
13
13
|
</html>
|
|
14
|
-
`}function
|
|
14
|
+
`}function re(){return`import { createApp } from 'lyt';
|
|
15
15
|
import App from './App';
|
|
16
16
|
|
|
17
17
|
// \u521B\u5EFA\u5E94\u7528\u5B9E\u4F8B
|
|
@@ -19,7 +19,7 @@ const app = createApp(App);
|
|
|
19
19
|
|
|
20
20
|
// \u5C06\u5E94\u7528\u6302\u8F7D\u5230 #app \u5143\u7D20
|
|
21
21
|
app.mount('#app');
|
|
22
|
-
`}function
|
|
22
|
+
`}function se(){return`import { defineComponent } from 'lyt';
|
|
23
23
|
|
|
24
24
|
// \u5B9A\u4E49\u6839\u7EC4\u4EF6
|
|
25
25
|
const App = defineComponent({
|
|
@@ -44,7 +44,7 @@ const App = defineComponent({
|
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
export default App;
|
|
47
|
-
`}function
|
|
47
|
+
`}function ie(){return`/* Lyt \u5168\u5C40\u6837\u5F0F */
|
|
48
48
|
|
|
49
49
|
* {
|
|
50
50
|
margin: 0;
|
|
@@ -75,8 +75,8 @@ body {
|
|
|
75
75
|
font-size: 1.2rem;
|
|
76
76
|
color: #666;
|
|
77
77
|
}
|
|
78
|
-
`}function
|
|
79
|
-
`}function
|
|
78
|
+
`}function ae(e){return JSON.stringify({name:e,version:"0.1.0",private:!0,type:"module",scripts:{dev:"lyt dev",build:"lyt build",preview:"lyt preview"},dependencies:{lyt:"latest"},devDependencies:{typescript:"^5.0.0"}},null,2)+`
|
|
79
|
+
`}function le(){return JSON.stringify({compilerOptions:{target:"ES2020",module:"ESNext",moduleResolution:"bundler",strict:!0,jsx:"preserve",resolveJsonModule:!0,isolatedModules:!0,esModuleInterop:!0,lib:["ES2020","DOM","DOM.Iterable"],skipLibCheck:!0,noEmit:!0,paths:{"@/*":["./src/*"]}},include:["src/**/*.ts","src/**/*.tsx"],exclude:["node_modules","dist"]},null,2)+`
|
|
80
80
|
`}function ce(){return`# \u4F9D\u8D56
|
|
81
81
|
node_modules/
|
|
82
82
|
|
|
@@ -100,9 +100,9 @@ Thumbs.db
|
|
|
100
100
|
# \u65E5\u5FD7
|
|
101
101
|
*.log
|
|
102
102
|
npm-debug.log*
|
|
103
|
-
`}async function Y(e,o={}){let n=o.template||"spa",r=
|
|
104
|
-
`}function
|
|
105
|
-
`}function
|
|
103
|
+
`}async function Y(e,o={}){let n=o.template||"spa",r=F.resolve(process.cwd(),e);s.info(`\u6B63\u5728\u521B\u5EFA Lyt \u9879\u76EE: ${t(e,"brightCyan")}`),s.info(`\u4F7F\u7528\u6A21\u677F: ${t(n,"brightCyan")}`),A(r)&&(s.error(`\u76EE\u5F55 "${e}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u9009\u62E9\u5176\u4ED6\u540D\u79F0\u6216\u5220\u9664\u5DF2\u6709\u76EE\u5F55`),process.exit(1)),v(r);let i=[{filePath:"index.html",content:ne()},{filePath:"src/main.ts",content:re()},{filePath:"src/App.ts",content:se()},{filePath:"src/style.css",content:ie()},{filePath:"package.json",content:ae(e)},{filePath:"tsconfig.json",content:le()},{filePath:".gitignore",content:ce()}];for(let a of i){let u=F.join(r,a.filePath);w(u,a.content),s.success(` \u521B\u5EFA ${a.filePath}`)}console.log(""),s.success(`\u9879\u76EE ${t(e,"brightCyan")} \u521B\u5EFA\u6210\u529F\uFF01`),console.log(""),console.log(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u542F\u52A8\u9879\u76EE\uFF1A"),console.log(""),console.log(` ${t("cd","brightGreen")} ${e}`),console.log(` ${t("npm install","brightGreen")}`),console.log(` ${t("npm run dev","brightGreen")}`),console.log("")}import*as k from"path";function pe(e){let o={name:e.name,version:"0.1.0",private:!0,type:"module",scripts:{dev:"lytx dev",build:"lytx build",preview:"lytx preview"},dependencies:{lyt:"^2.0.0"}};return e.ts&&(o.devDependencies={typescript:"^5.0.0"}),e.eslint&&(o.devDependencies=o.devDependencies||{},o.devDependencies.eslint="^8.0.0",o.scripts=o.scripts||{},o.scripts.lint="eslint src --ext .ts,.js"),e.router&&(o.dependencies["@lytjs/router"]="^2.0.0"),e.store&&(o.dependencies["@lytjs/store"]="^2.0.0"),JSON.stringify(o,null,2)+`
|
|
104
|
+
`}function ue(e){let o={compilerOptions:{target:"ES2020",module:"ESNext",moduleResolution:"bundler",strict:!0,jsx:"preserve",resolveJsonModule:!0,isolatedModules:!0,esModuleInterop:!0,lib:["ES2020","DOM","DOM.Iterable"],skipLibCheck:!0,noEmit:!0,paths:{"@/*":["./src/*"]}},include:["src/**/*.ts","src/**/*.tsx"],exclude:["node_modules","dist"]};return e.template==="ssr"&&(o.compilerOptions.types=["node"]),JSON.stringify(o,null,2)+`
|
|
105
|
+
`}function de(e){let o=e.ts?".ts":".js";return`<!DOCTYPE html>
|
|
106
106
|
<html lang="zh-CN">
|
|
107
107
|
<head>
|
|
108
108
|
<meta charset="UTF-8" />
|
|
@@ -116,9 +116,9 @@ npm-debug.log*
|
|
|
116
116
|
</body>
|
|
117
117
|
</html>
|
|
118
118
|
`}function fe(e){let o=["// Lytx \u914D\u7F6E\u6587\u4EF6","import { defineConfig } from 'lyt'","","export default defineConfig({"," // \u6784\u5EFA\u6A21\u5F0F",` mode: '${e.template}',`];return e.router&&(o.push(" // \u8DEF\u7531\u914D\u7F6E"),o.push(" router: {"),o.push(" historyMode: true,"),o.push(" },")),e.store&&(o.push(" // \u72B6\u6001\u7BA1\u7406\u914D\u7F6E"),o.push(" store: {"),o.push(" strict: true,"),o.push(" },")),o.push("})"),o.push(""),o.join(`
|
|
119
|
-
`)}function
|
|
119
|
+
`)}function ge(e){let o=e.ts?".ts":".js",n=["import { createApp } from 'lyt'","import App from './App.lyt'","import './styles/main.css'"];return e.router&&n.push("import { router } from './router'"),e.store&&n.push("import { store } from './store'"),n.push(""),n.push("// \u521B\u5EFA\u5E94\u7528\u5B9E\u4F8B"),n.push("const app = createApp(App)"),e.router&&n.push("app.use(router)"),e.store&&n.push("app.use(store)"),n.push(""),n.push("// \u5C06\u5E94\u7528\u6302\u8F7D\u5230 #app \u5143\u7D20"),n.push("app.mount('#app')"),n.join(`
|
|
120
120
|
`)+`
|
|
121
|
-
`}function
|
|
121
|
+
`}function me(){return`<template>
|
|
122
122
|
<div class="app">
|
|
123
123
|
<Header />
|
|
124
124
|
<main>
|
|
@@ -146,7 +146,7 @@ export default defineComponent({
|
|
|
146
146
|
padding: 20px;
|
|
147
147
|
}
|
|
148
148
|
</style>
|
|
149
|
-
`}function
|
|
149
|
+
`}function he(){return`import { defineComponent } from 'lyt'
|
|
150
150
|
|
|
151
151
|
export default defineComponent({
|
|
152
152
|
name: 'HomePage',
|
|
@@ -158,7 +158,7 @@ export default defineComponent({
|
|
|
158
158
|
</div>
|
|
159
159
|
\`,
|
|
160
160
|
})
|
|
161
|
-
`}function
|
|
161
|
+
`}function ye(){return`import { defineComponent } from 'lyt'
|
|
162
162
|
|
|
163
163
|
export default defineComponent({
|
|
164
164
|
name: 'AboutPage',
|
|
@@ -170,7 +170,7 @@ export default defineComponent({
|
|
|
170
170
|
</div>
|
|
171
171
|
\`,
|
|
172
172
|
})
|
|
173
|
-
`}function
|
|
173
|
+
`}function be(){return`import { defineComponent } from 'lyt'
|
|
174
174
|
|
|
175
175
|
export default defineComponent({
|
|
176
176
|
name: 'Header',
|
|
@@ -184,7 +184,7 @@ export default defineComponent({
|
|
|
184
184
|
</header>
|
|
185
185
|
\`,
|
|
186
186
|
})
|
|
187
|
-
`}function
|
|
187
|
+
`}function xe(){return`import { createRouter, createWebHistory } from '@lytjs/router'
|
|
188
188
|
import HomePage from '../pages/index'
|
|
189
189
|
import AboutPage from '../pages/about'
|
|
190
190
|
|
|
@@ -201,7 +201,7 @@ export const router = createRouter({
|
|
|
201
201
|
},
|
|
202
202
|
],
|
|
203
203
|
})
|
|
204
|
-
`}function
|
|
204
|
+
`}function ve(){return`import { createStore } from '@lytjs/store'
|
|
205
205
|
|
|
206
206
|
export const store = createStore({
|
|
207
207
|
state: {
|
|
@@ -229,7 +229,7 @@ export const store = createStore({
|
|
|
229
229
|
doubleCount: (state: any) => state.count * 2,
|
|
230
230
|
},
|
|
231
231
|
})
|
|
232
|
-
`}function
|
|
232
|
+
`}function $e(){return`/* \u5168\u5C40\u6837\u5F0F */
|
|
233
233
|
|
|
234
234
|
* {
|
|
235
235
|
margin: 0;
|
|
@@ -257,25 +257,13 @@ a:hover {
|
|
|
257
257
|
<rect width="32" height="32" rx="6" fill="#42b883"/>
|
|
258
258
|
<text x="16" y="22" text-anchor="middle" fill="white" font-size="18" font-weight="bold">L</text>
|
|
259
259
|
</svg>
|
|
260
|
-
`}function
|
|
261
|
-
`}async function O(e){let{name:o,template:n,ts:r,router:
|
|
262
|
-
Upgrade: websocket\r
|
|
263
|
-
Connection: Upgrade\r
|
|
264
|
-
Sec-WebSocket-Accept: ${i}\r
|
|
265
|
-
\r
|
|
266
|
-
`);let a={socket:n,isAlive:!0};n.on("close",()=>{this.clients=this.clients.filter(c=>c!==a)}),n.on("error",()=>{this.clients=this.clients.filter(c=>c!==a)}),this.clients.push(a),l.info("WebSocket \u5BA2\u6237\u7AEF\u5DF2\u8FDE\u63A5")}broadcast(o){let n=[];for(let r of this.clients)try{if(!r.isAlive){n.push(r);continue}let s=Buffer.from(o,"utf-8"),i=this.createFrame(129,s);r.socket.write(i)}catch(s){n.push(r)}for(let r of n)this.clients=this.clients.filter(s=>s!==r),r.socket.destroy()}createFrame(o,n){let s=n.length,i;s<126?i=2:s<65536?i=4:i=10;let a=Buffer.alloc(i+s);return a[0]=o,s<126?a[1]=s:s<65536?(a[1]=126,a.writeUInt16BE(s,2)):(a[1]=127,a.writeUInt32BE(0,2),a.writeUInt32BE(s,6)),n.copy(a,i),a}getClientCount(){return this.clients.length}};function Pe(e,o){let n=new Set([".ts",".tsx",".js",".jsx",".css",".html",".json"]);function r(s){try{let i=$.readdirSync(s,{withFileTypes:!0});for(let a of i){let c=m.join(s,a.name);if(a.isDirectory()){if(a.name==="node_modules"||a.name===".git"||a.name==="dist")continue;r(c)}else if(a.isFile()){let p=m.extname(a.name);n.has(p)&&$.watch(c,{persistent:!1},u=>{if(u==="change"){let f=m.relative(e,c);l.info(`\u6587\u4EF6\u53D8\u5316: ${t(f,"brightYellow")}`);let d=JSON.stringify({type:"hmr-update",path:`/${f}`,timestamp:Date.now()});o.broadcast(d)}})}}}catch(i){}}r(e),l.info(`\u6B63\u5728\u76D1\u542C ${t(e,"brightCyan")} \u76EE\u5F55\u7684\u6587\u4EF6\u53D8\u5316...`)}function Me(e,o){let n=e.url||"/";if(n==="/favicon.ico")return{statusCode:204,headers:{},body:""};let r=n.split("?")[0],s=m.normalize(r).replace(/^(\.\.[\/\\])+/,""),i=m.join(o,s);try{$.statSync(i).isDirectory()&&(i=m.join(i,"index.html"))}catch(a){}try{let a=$.readFileSync(i),c=m.extname(i);if(c===".ts"||c===".tsx"){let u=a.toString("utf-8"),f=Se(u,i);return{statusCode:200,headers:{"Content-Type":"application/javascript; charset=utf-8","Cache-Control":"no-cache, no-store, must-revalidate","Access-Control-Allow-Origin":"*"},body:f}}return{statusCode:200,headers:{"Content-Type":W(i),"Cache-Control":c===".html"?"no-cache":"public, max-age=3600","Access-Control-Allow-Origin":"*"},body:a}}catch(a){let c=a.code;return c==="ENOENT"?{statusCode:404,headers:{"Content-Type":"text/html; charset=utf-8"},body:`<!DOCTYPE html>
|
|
267
|
-
<html>
|
|
268
|
-
<head><title>404 Not Found</title></head>
|
|
269
|
-
<body>
|
|
270
|
-
<h1>404 Not Found</h1>
|
|
271
|
-
<p>\u627E\u4E0D\u5230\u6587\u4EF6: ${r}</p>
|
|
272
|
-
</body>
|
|
273
|
-
</html>`}:c==="EACCES"?{statusCode:403,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"403 Forbidden"}:{statusCode:500,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"500 Internal Server Error"}}}function L(e={}){let o=e.port||3e3,n=m.resolve(e.root||process.cwd()),r=e.hmr!==!1,s=new B,i=z.createServer((c,p)=>{let u=Me(c,n);p.writeHead(u.statusCode,u.headers),p.end(u.body)});r&&i.on("upgrade",(c,p,u)=>{s.handleUpgrade(c,p,u)}),i.listen(o,()=>{console.log(""),console.log(t(" \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557","brightCyan")),console.log(t(" \u2551","brightCyan")+t(" Lyt \u5F00\u53D1\u670D\u52A1\u5668\u5DF2\u542F\u52A8 ","brightWhite")+t("\u2551","brightCyan")),console.log(t(" \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D","brightCyan")),console.log(""),console.log(` ${t("\u279C","brightGreen")} \u672C\u5730\u8BBF\u95EE: ${t(`http://localhost:${o}`,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u7F51\u7EDC\u8BBF\u95EE: ${t(`http://127.0.0.1:${o}`,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u9879\u76EE\u76EE\u5F55: ${t(n,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u70ED\u66F4\u65B0: ${t(r?"\u5DF2\u5F00\u542F":"\u5DF2\u5173\u95ED",r?"brightGreen":"brightRed")}`),console.log(""),console.log(` ${t("\u6309 Ctrl+C \u505C\u6B62\u670D\u52A1\u5668","dim")}`),console.log("")}),i.on("error",c=>{c.code==="EADDRINUSE"?(l.error(`\u7AEF\u53E3 ${o} \u5DF2\u88AB\u5360\u7528\uFF0C\u8BF7\u4F7F\u7528 --port \u6307\u5B9A\u5176\u4ED6\u7AEF\u53E3`),process.exit(1)):(l.error(`\u670D\u52A1\u5668\u542F\u52A8\u5931\u8D25: ${c.message}`),process.exit(1))}),r&&Pe(n,s);let a=()=>{l.info("\u6B63\u5728\u5173\u95ED\u5F00\u53D1\u670D\u52A1\u5668..."),i.close(()=>{l.success("\u670D\u52A1\u5668\u5DF2\u5173\u95ED"),process.exit(0)}),setTimeout(()=>{l.warn("\u670D\u52A1\u5668\u5173\u95ED\u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(1)},5e3)};process.on("SIGINT",a),process.on("SIGTERM",a)}import*as g from"fs";import*as h from"path";var Ae=U("esbuild");async function J(e={}){let o=Date.now(),n=h.resolve(e.root||process.cwd()),r=h.resolve(n,e.outDir||"dist"),s=e.entry||"index.html",i=e.minify||!1;l.info("\u5F00\u59CB\u6784\u5EFA\u9879\u76EE..."),l.info(` \u6839\u76EE\u5F55: ${t(n,"brightCyan")}`),l.info(` \u8F93\u51FA\u76EE\u5F55: ${t(r,"brightCyan")}`),l.info(` \u538B\u7F29: ${t(i?"\u5F00\u542F":"\u5173\u95ED",i?"brightGreen":"brightYellow")}`);let a=h.join(n,s);g.existsSync(a)||(l.error(`\u5165\u53E3\u6587\u4EF6\u4E0D\u5B58\u5728: ${a}`),process.exit(1));let c=N(a),p=/<script\s+type="module"\s+src="([^"]+)"\s*><\/script>/g,u,f=[];for(;(u=p.exec(c))!==null;)f.push(u[1]);f.length===0&&l.warn("\u672A\u5728\u5165\u53E3 HTML \u4E2D\u627E\u5230\u6A21\u5757\u811A\u672C\u5F15\u7528");let d={inputFiles:0,outputFiles:0,totalSize:0,buildTime:0};w(r),w(h.join(r,"assets"));for(let x of f){let j=h.join(n,x);if(!g.existsSync(j)){l.warn(`\u811A\u672C\u6587\u4EF6\u4E0D\u5B58\u5728: ${x}`);continue}l.info(`\u6B63\u5728\u6253\u5305: ${t(x,"brightYellow")}`);let E=`${h.basename(x,h.extname(x))}.bundle.js`,P=h.join(r,"assets",E);try{await Ae.build({entryPoints:[j],bundle:!0,minify:i,target:"es2018",format:"esm",outfile:P,sourcemap:!0,external:R=>R.startsWith("@lytjs/"),drop:i?["console"]:[],metafile:!0});let v=g.readFileSync(P,"utf-8");d.totalSize+=Buffer.byteLength(v,"utf-8"),d.outputFiles++;let C=P+".map";if(g.existsSync(C)){let R=g.readFileSync(C,"utf-8");d.totalSize+=Buffer.byteLength(R,"utf-8"),d.outputFiles++}c=c.replace(`<script type="module" src="${x}"></script>`,`<script src="/assets/${E}"></script>`),d.inputFiles++}catch(v){let C=v instanceof Error?v.message:String(v);l.error(`\u6253\u5305\u5931\u8D25 ${x}: ${C}`),process.exit(1)}}je(n,r,d);let A=h.join(r,"index.html");S(A,c),d.outputFiles++,d.totalSize+=Buffer.byteLength(c,"utf-8"),d.buildTime=Date.now()-o,console.log(""),l.success("\u6784\u5EFA\u5B8C\u6210\uFF01"),console.log(""),console.log(` ${t("\u8F93\u5165\u6587\u4EF6:","brightWhite")} ${d.inputFiles} \u4E2A`),console.log(` ${t("\u8F93\u51FA\u6587\u4EF6:","brightWhite")} ${d.outputFiles} \u4E2A`),console.log(` ${t("\u603B\u5927\u5C0F:","brightWhite")} ${Ee(d.totalSize)}`),console.log(` ${t("\u6784\u5EFA\u8017\u65F6:","brightWhite")} ${d.buildTime}ms`),console.log(` ${t("\u8F93\u51FA\u76EE\u5F55:","brightWhite")} ${t(r,"brightCyan")}`),console.log("")}function je(e,o,n){let r=h.join(e,"src");if(!g.existsSync(r))return;function s(i,a){let c=g.readdirSync(i,{withFileTypes:!0});for(let p of c){let u=h.join(i,p.name);if(p.isDirectory()){if(p.name==="node_modules")continue;s(u,a)}else if(p.isFile()){let f=h.extname(p.name);if([".ts",".tsx",".js",".jsx"].includes(f))continue;let d=h.relative(a,u),A=h.join(o,d);S(A,g.readFileSync(u).toString("utf-8")),n.outputFiles++,n.totalSize+=g.statSync(u).size}}}s(r,r)}function Ee(e){if(e===0)return"0 B";let o=["B","KB","MB","GB"],n=1024,r=Math.floor(Math.log(e)/Math.log(n));return`${(e/Math.pow(n,r)).toFixed(2)} ${o[r]}`}import*as q from"http";import*as M from"fs";import*as b from"path";import*as V from"crypto";var D=class{constructor(){this.clients=[]}handleUpgrade(o,n,r){let s=o.headers["sec-websocket-key"];if(!s){n.destroy();return}let i=V.createHash("sha1").update(s+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");n.write(`HTTP/1.1 101 Switching Protocols\r
|
|
260
|
+
`}function Se(){return JSON.stringify({root:!0,env:{browser:!0,es2021:!0,node:!0},extends:["eslint:recommended"],parserOptions:{ecmaVersion:"latest",sourceType:"module"},rules:{"no-unused-vars":"warn","no-console":"warn"}},null,2)+`
|
|
261
|
+
`}async function O(e){let{name:o,template:n,ts:r,router:i,store:a,eslint:u}=e,l=k.resolve(process.cwd(),o);if(s.info(`\u6B63\u5728\u521B\u5EFA Lyt \u9879\u76EE: ${t(o,"brightCyan")}`),s.info(`\u4F7F\u7528\u6A21\u677F: ${t(n,"brightCyan")}`),A(l))throw s.error(`\u76EE\u5F55 "${o}" \u5DF2\u5B58\u5728\uFF0C\u8BF7\u9009\u62E9\u5176\u4ED6\u540D\u79F0\u6216\u5220\u9664\u5DF2\u6709\u76EE\u5F55`),new Error(`Directory "${o}" already exists`);v(l);let c=[{filePath:"package.json",content:pe(e)},{filePath:"index.html",content:de(e)},{filePath:"lytx.config.ts",content:fe(e)},{filePath:"src/main.ts",content:ge(e)},{filePath:"src/App.lyt",content:me()},{filePath:"src/pages/index.ts",content:he()},{filePath:"src/pages/about.ts",content:ye()},{filePath:"src/components/Header.ts",content:be()},{filePath:"src/styles/main.css",content:$e()},{filePath:"public/favicon.svg",content:we()}];r&&c.push({filePath:"tsconfig.json",content:ue(e)}),i&&c.push({filePath:"src/router/index.ts",content:xe()}),a&&c.push({filePath:"src/store/index.ts",content:ve()}),u&&c.push({filePath:".eslintrc.json",content:Se()});for(let p of c){let d=k.join(l,p.filePath);w(d,p.content),s.success(` \u521B\u5EFA ${p.filePath}`)}console.log(""),s.success(`\u9879\u76EE ${t(o,"brightCyan")} \u521B\u5EFA\u6210\u529F\uFF01`),console.log(""),console.log(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u542F\u52A8\u9879\u76EE\uFF1A"),console.log(""),console.log(` ${t("cd","brightGreen")} ${o}`),console.log(` ${t("npm install","brightGreen")}`),console.log(` ${t("npm run dev","brightGreen")}`),console.log("")}import*as J from"fs";import*as D from"fs";import*as M from"path";import*as z from"http";import*as C from"fs";import*as y from"path";import*as _ from"crypto";var T=class{constructor(){this.clients=[]}handleUpgrade(o,n,r){let i=o.headers["sec-websocket-key"];if(!i){n.destroy();return}let a=_.createHash("sha1").update(i+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");n.write(`HTTP/1.1 101 Switching Protocols\r
|
|
274
262
|
Upgrade: websocket\r
|
|
275
263
|
Connection: Upgrade\r
|
|
276
|
-
Sec-WebSocket-Accept: ${
|
|
264
|
+
Sec-WebSocket-Accept: ${a}\r
|
|
277
265
|
\r
|
|
278
|
-
`);let
|
|
266
|
+
`);let u={socket:n,isAlive:!0};n.on("close",()=>{this.clients=this.clients.filter(l=>l!==u)}),n.on("error",()=>{this.clients=this.clients.filter(l=>l!==u)}),this.clients.push(u)}broadcast(o){let n=[];for(let r of this.clients)try{if(!r.isAlive){n.push(r);continue}let i=Buffer.from(o,"utf-8"),a=this.createFrame(129,i);r.socket.write(a)}catch(i){n.push(r)}for(let r of n){this.clients=this.clients.filter(i=>i!==r);try{r.socket.destroy()}catch(i){}}}createFrame(o,n){let r=n.length,i;r<126?i=2:r<65536?i=4:i=10;let a=Buffer.alloc(i+r);return a[0]=o,r<126?a[1]=r:r<65536?(a[1]=126,a.writeUInt16BE(r,2)):(a[1]=127,a.writeUInt32BE(0,2),a.writeUInt32BE(r,6)),n.copy(a,i),a}getClientCount(){return this.clients.length}closeAll(){for(let o of this.clients)try{o.socket.destroy()}catch(n){}this.clients=[]}};function G(e){let o=new T,n=[],r=[],i=null,a=!1;function u(c){let p=y.extname(c).toLowerCase();if(p===".css")return"css";let d=y.basename(c);return d.startsWith("lytx.config")||d==="tsconfig.json"||d==="package.json"?"reload":p===".ts"||p===".tsx"||p===".lyt"||p===".js"||p===".jsx"?"update":"reload"}function l(c){try{let p=C.readdirSync(c,{withFileTypes:!0});for(let d of p){let f=y.join(c,d.name);if(d.isDirectory()){if(d.name==="node_modules"||d.name===".git"||d.name==="dist")continue;l(f)}else if(d.isFile()){let P=y.extname(d.name).toLowerCase();if(new Set([".ts",".tsx",".js",".jsx",".css",".html",".json",".lyt"]).has(P))try{let R=C.watch(f,{persistent:!1},U=>{if(U==="change"){let j=y.relative(e,f);for(let $ of n)try{$(j)}catch(H){}let S=u(f),x={type:S,path:`/${j}`};if(S==="css")try{x.content=C.readFileSync(f,"utf-8")}catch($){}o.broadcast(JSON.stringify(x))}});r.push(R)}catch(R){}}}}catch(p){}}return{start(c){a||(a=!0,i=z.createServer((p,d)=>{d.writeHead(426,{"Content-Type":"text/plain"}),d.end("Upgrade Required")}),i.on("upgrade",(p,d,f)=>{o.handleUpgrade(p,d,f)}),i.listen(c,()=>{}),l(e))},stop(){a=!1;for(let c of r)try{c.close()}catch(p){}if(r.length=0,o.closeAll(),i){try{i.close()}catch(c){}i=null}},onFileChange(c){n.push(c)},notifyClient(c){o.broadcast(JSON.stringify(c))}}}function Ce(e){let o=new T;return e.on("upgrade",(n,r,i)=>{o.handleUpgrade(n,r,i)}),{broadcast(n){o.broadcast(n)},getClientCount(){return o.getClientCount()}}}function I(){return`(function() {
|
|
279
267
|
'use strict';
|
|
280
268
|
|
|
281
269
|
var ws = null;
|
|
@@ -373,8 +361,9 @@ Sec-WebSocket-Accept: ${i}\r
|
|
|
373
361
|
|
|
374
362
|
// \u542F\u52A8\u8FDE\u63A5
|
|
375
363
|
connect();
|
|
376
|
-
})();`}var
|
|
377
|
-
${t(
|
|
364
|
+
})();`}var q;try{q=N("esbuild")}catch(e){s.error("\u7F3A\u5C11\u4F9D\u8D56: esbuild"),s.error(""),s.error(" Lyt CLI \u7684\u5F00\u53D1\u670D\u52A1\u5668\u9700\u8981 esbuild \u6765\u5B9E\u65F6\u7F16\u8BD1 TypeScript\u3002"),s.error(""),s.error(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u5B89\u88C5:"),s.error(` ${t("npm install esbuild --save-dev","brightGreen")}`),s.error(""),s.error(" \u5982\u679C\u60A8\u4F7F\u7528 pnpm:"),s.error(` ${t("pnpm add esbuild -D","brightGreen")}`),s.error(""),process.exit(1)}function Me(e,o){try{return q.transformSync(e,{loader:"ts",target:"es2018",format:"esm",sourcemap:"inline"}).code}catch(n){let r=n instanceof Error?n.message:String(n);return console.error(`[Lyt CLI] \u7F16\u8BD1\u9519\u8BEF ${o||""}:`,r),e}}function Pe(e,o){var c;let n=((c=e.url)==null?void 0:c.split("?")[0])||"/";n==="/"&&(n="/index.html");let r=M.join(o,n);if(!r.startsWith(o))return{statusCode:403,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"403 Forbidden"};if(!D.existsSync(r)||!D.statSync(r).isFile())return{statusCode:404,headers:{"Content-Type":"text/plain; charset=utf-8"},body:"404 Not Found"};let i=M.extname(r).toLowerCase(),u={".html":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"application/javascript; charset=utf-8",".mjs":"application/javascript; charset=utf-8",".ts":"application/typescript; charset=utf-8",".json":"application/json; charset=utf-8",".svg":"image/svg+xml",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".ico":"image/x-icon",".woff":"font/woff",".woff2":"font/woff2",".txt":"text/plain; charset=utf-8"}[i]||"application/octet-stream",l=E(r);return(i===".ts"||i===".tsx")&&(l=Me(l,n)),i===".html"&&(l=l.replace("</head>",`<script>${I()}</script>
|
|
365
|
+
</head>`)),{statusCode:200,headers:{"Content-Type":u},body:l}}function B(e={}){let o=e.port||3e3,n=M.resolve(e.root||process.cwd()),r=e.hmr!==!1,i=G(n),a=J.createServer((l,c)=>{let p=Pe(l,n);c.writeHead(p.statusCode,p.headers),c.end(p.body)});r&&a.on("upgrade",(l,c,p)=>{i.handleUpgrade(l,c,p)}),a.listen(o,()=>{console.log(""),console.log(t(" \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557","brightCyan")),console.log(t(" \u2551","brightCyan")+t(" Lyt \u5F00\u53D1\u670D\u52A1\u5668\u5DF2\u542F\u52A8 ","brightWhite")+t("\u2551","brightCyan")),console.log(t(" \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D","brightCyan")),console.log(""),console.log(` ${t("\u279C","brightGreen")} \u672C\u5730\u8BBF\u95EE: ${t(`http://localhost:${o}`,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u7F51\u7EDC\u8BBF\u95EE: ${t(`http://127.0.0.1:${o}`,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u9879\u76EE\u76EE\u5F55: ${t(n,"brightBlue")}`),console.log(` ${t("\u279C","brightGreen")} \u70ED\u66F4\u65B0: ${t(r?"\u5DF2\u5F00\u542F":"\u5DF2\u5173\u95ED",r?"brightGreen":"brightRed")}`),console.log(""),console.log(` ${t("\u6309 Ctrl+C \u505C\u6B62\u670D\u52A1\u5668","dim")}`),console.log("")}),a.on("error",l=>{l.code==="EADDRINUSE"?(s.error(`\u7AEF\u53E3 ${o} \u5DF2\u88AB\u5360\u7528\uFF0C\u8BF7\u4F7F\u7528 --port \u6307\u5B9A\u5176\u4ED6\u7AEF\u53E3`),process.exit(1)):(s.error(`\u670D\u52A1\u5668\u542F\u52A8\u5931\u8D25: ${l.message}`),process.exit(1))});let u=()=>{s.info("\u6B63\u5728\u5173\u95ED\u5F00\u53D1\u670D\u52A1\u5668..."),a.close(()=>{s.success("\u670D\u52A1\u5668\u5DF2\u5173\u95ED"),process.exit(0)}),setTimeout(()=>{s.warn("\u670D\u52A1\u5668\u5173\u95ED\u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(1)},5e3)};process.on("SIGINT",u),process.on("SIGTERM",u)}import*as m from"fs";import*as g from"path";var V;try{V=N("esbuild")}catch(e){s.error("\u7F3A\u5C11\u4F9D\u8D56: esbuild"),s.error(""),s.error(" Lyt CLI \u7684\u6784\u5EFA\u529F\u80FD\u9700\u8981 esbuild\u3002"),s.error(""),s.error(" \u8BF7\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\u5B89\u88C5:"),s.error(` ${t("npm install esbuild --save-dev","brightGreen")}`),s.error(""),s.error(" \u5982\u679C\u60A8\u4F7F\u7528 pnpm:"),s.error(` ${t("pnpm add esbuild -D","brightGreen")}`),s.error(""),process.exit(1)}async function K(e={}){let o=Date.now(),n=g.resolve(e.root||process.cwd()),r=g.resolve(n,e.outDir||"dist"),i=e.entry||"index.html",a=e.minify||!1;s.info("\u5F00\u59CB\u6784\u5EFA\u9879\u76EE..."),s.info(` \u6839\u76EE\u5F55: ${t(n,"brightCyan")}`),s.info(` \u8F93\u51FA\u76EE\u5F55: ${t(r,"brightCyan")}`),s.info(` \u538B\u7F29: ${t(a?"\u5F00\u542F":"\u5173\u95ED",a?"brightGreen":"brightYellow")}`);let u=g.join(n,i);m.existsSync(u)||(s.error(`\u5165\u53E3\u6587\u4EF6\u4E0D\u5B58\u5728: ${u}`),process.exit(1));let l=E(u),c=/<script\s+type="module"\s+src="([^"]+)"\s*><\/script>/g,p,d=[];for(;(p=c.exec(l))!==null;)d.push(p[1]);d.length===0&&s.warn("\u672A\u5728\u5165\u53E3 HTML \u4E2D\u627E\u5230\u6A21\u5757\u811A\u672C\u5F15\u7528");let f={inputFiles:0,outputFiles:0,totalSize:0,buildTime:0};v(r),v(g.join(r,"assets"));for(let b of d){let R=g.join(n,b);if(!m.existsSync(R)){s.warn(`\u811A\u672C\u6587\u4EF6\u4E0D\u5B58\u5728: ${b}`);continue}s.info(`\u6B63\u5728\u6253\u5305: ${t(b,"brightYellow")}`);let j=`${g.basename(b,g.extname(b))}.bundle.js`,S=g.join(r,"assets",j);try{await V.build({entryPoints:[R],bundle:!0,minify:a,target:"es2018",format:"esm",outfile:S,sourcemap:!0,external:H=>H.startsWith("@lytjs/"),drop:a?["console"]:[],metafile:!0});let x=m.readFileSync(S,"utf-8");f.totalSize+=Buffer.byteLength(x,"utf-8"),f.outputFiles++;let $=S+".map";if(m.existsSync($)){let H=m.readFileSync($,"utf-8");f.totalSize+=Buffer.byteLength(H,"utf-8"),f.outputFiles++}l=l.replace(`<script type="module" src="${b}"></script>`,`<script src="/assets/${j}"></script>`),f.inputFiles++}catch(x){let $=x instanceof Error?x.message:String(x);s.error(`\u6253\u5305\u5931\u8D25 ${b}: ${$}`),process.exit(1)}}Re(n,r,f);let P=g.join(r,"index.html");w(P,l),f.outputFiles++,f.totalSize+=Buffer.byteLength(l,"utf-8"),f.buildTime=Date.now()-o,console.log(""),s.success("\u6784\u5EFA\u5B8C\u6210\uFF01"),console.log(""),console.log(` ${t("\u8F93\u5165\u6587\u4EF6:","brightWhite")} ${f.inputFiles} \u4E2A`),console.log(` ${t("\u8F93\u51FA\u6587\u4EF6:","brightWhite")} ${f.outputFiles} \u4E2A`),console.log(` ${t("\u603B\u5927\u5C0F:","brightWhite")} ${je(f.totalSize)}`),console.log(` ${t("\u6784\u5EFA\u8017\u65F6:","brightWhite")} ${f.buildTime}ms`),console.log(` ${t("\u8F93\u51FA\u76EE\u5F55:","brightWhite")} ${t(r,"brightCyan")}`),console.log("")}function Re(e,o,n){let r=g.join(e,"src");if(!m.existsSync(r))return;function i(a,u){let l=m.readdirSync(a,{withFileTypes:!0});for(let c of l){let p=g.join(a,c.name);if(c.isDirectory()){if(c.name==="node_modules")continue;i(p,u)}else if(c.isFile()){let d=g.extname(c.name);if([".ts",".tsx",".js",".jsx"].includes(d))continue;let f=g.relative(u,p),P=g.join(o,f);w(P,m.readFileSync(p).toString("utf-8")),n.outputFiles++,n.totalSize+=m.statSync(p).size}}}i(r,r)}function je(e){if(e===0)return"0 B";let o=["B","KB","MB","GB"],n=1024,r=Math.floor(Math.log(e)/Math.log(n));return`${(e/Math.pow(n,r)).toFixed(2)} ${o[r]}`}var He="0.2.0",ee="lytx",Ae="Lyt.js \u6846\u67B6\u547D\u4EE4\u884C\u5DE5\u5177\uFF08\u589E\u5F3A\u7248\uFF09",X=`
|
|
366
|
+
${t(ee,"brightCyan")} - ${Ae}
|
|
378
367
|
|
|
379
368
|
${t("\u7528\u6CD5:","brightGreen")}
|
|
380
369
|
lytx <command> [options] [args]
|
|
@@ -398,7 +387,7 @@ ${t("\u793A\u4F8B:","brightGreen")}
|
|
|
398
387
|
${t("$","dim")} lytx build --mode ssr
|
|
399
388
|
${t("$","dim")} lytx preview --port 4173
|
|
400
389
|
|
|
401
|
-
`,
|
|
390
|
+
`,Ee=`
|
|
402
391
|
${t("lytx create","brightCyan")} - \u521B\u5EFA\u65B0\u7684 Lyt \u9879\u76EE
|
|
403
392
|
|
|
404
393
|
${t("\u7528\u6CD5:","brightGreen")}
|
|
@@ -420,7 +409,7 @@ ${t("\u793A\u4F8B:","brightGreen")}
|
|
|
420
409
|
${t("$","dim")} lytx create my-app --template spa --ts --router --store
|
|
421
410
|
${t("$","dim")} lytx create my-app --template ssr --ts
|
|
422
411
|
|
|
423
|
-
`,
|
|
412
|
+
`,Fe=`
|
|
424
413
|
${t("lytx dev","brightCyan")} - \u542F\u52A8\u672C\u5730\u5F00\u53D1\u670D\u52A1\u5668
|
|
425
414
|
|
|
426
415
|
${t("\u7528\u6CD5:","brightGreen")}
|
|
@@ -442,7 +431,7 @@ ${t("\u793A\u4F8B:","brightGreen")}
|
|
|
442
431
|
${t("$","dim")} lytx dev --port 8080
|
|
443
432
|
${t("$","dim")} lytx dev --no-hmr
|
|
444
433
|
|
|
445
|
-
`,
|
|
434
|
+
`,ke=`
|
|
446
435
|
${t("lytx build","brightCyan")} - \u6784\u5EFA\u751F\u4EA7\u7248\u672C
|
|
447
436
|
|
|
448
437
|
${t("\u7528\u6CD5:","brightGreen")}
|
|
@@ -467,7 +456,7 @@ ${t("\u793A\u4F8B:","brightGreen")}
|
|
|
467
456
|
${t("$","dim")} lytx build --mode ssr
|
|
468
457
|
${t("$","dim")} lytx build --minify --outDir ./output
|
|
469
458
|
|
|
470
|
-
`,
|
|
459
|
+
`,Te=`
|
|
471
460
|
${t("lytx preview","brightCyan")} - \u9884\u89C8\u6784\u5EFA\u7ED3\u679C
|
|
472
461
|
|
|
473
462
|
${t("\u7528\u6CD5:","brightGreen")}
|
|
@@ -480,4 +469,4 @@ ${t("\u793A\u4F8B:","brightGreen")}
|
|
|
480
469
|
${t("$","dim")} lytx preview
|
|
481
470
|
${t("$","dim")} lytx preview --port 5000
|
|
482
471
|
|
|
483
|
-
`;function
|
|
472
|
+
`;function De(){console.log(""),console.log(` ${t(ee,"brightCyan")} v${t(He,"brightWhite")}`),console.log("")}function Le(e){s.error(`\u672A\u77E5\u547D\u4EE4: ${t(e,"brightRed")}`),console.log(""),console.log(` \u8FD0\u884C ${t("lytx --help","brightCyan")} \u67E5\u770B\u53EF\u7528\u547D\u4EE4`),console.log("")}function Oe(e){return typeof e=="string"&&["spa","ssr","ssg"].includes(e)?e:"spa"}async function Ge(e){if(e.options.help){console.log(Ee);return}e.args.length===0&&(s.error("\u8BF7\u63D0\u4F9B\u9879\u76EE\u540D\u79F0"),console.log(""),console.log(` \u7528\u6CD5: ${t("lytx create <name>","brightCyan")}`),console.log(""),console.log(` \u8FD0\u884C ${t("lytx create --help","brightCyan")} \u67E5\u770B\u66F4\u591A\u9009\u9879`),console.log(""),process.exit(1));let o=e.args[0];if(e.options.ts===!0||e.options.router===!0||e.options.store===!0||e.options.eslint===!0||typeof e.options.template=="string"&&["ssr","ssg"].includes(e.options.template)){let r={name:o,template:Oe(e.options.template),ts:e.options.ts===!0,router:e.options.router===!0,store:e.options.store===!0,eslint:e.options.eslint===!0};await O(r)}else{let r={template:typeof e.options.template=="string"?e.options.template:"spa"};await Y(o,r)}}function Ie(e){if(e.options.help){console.log(Fe);return}let o={port:typeof e.options.port=="string"?parseInt(e.options.port,10):typeof e.options.p=="string"?parseInt(e.options.p,10):3e3,hmr:e.options["no-hmr"]!==!0};(isNaN(o.port)||o.port<1||o.port>65535)&&(s.error(`\u65E0\u6548\u7684\u7AEF\u53E3\u53F7: ${e.options.port||e.options.p}`),process.exit(1)),B(o)}async function Be(e){if(e.options.help){console.log(ke);return}let o={minify:e.options.minify===!0,outDir:typeof e.options.outDir=="string"?e.options.outDir:typeof e.options.o=="string"?e.options.o:"dist",entry:typeof e.options.entry=="string"?e.options.entry:"index.html"};await K(o)}function Ue(e){if(e.options.help){console.log(Te);return}let o=typeof e.options.port=="string"?parseInt(e.options.port,10):typeof e.options.p=="string"?parseInt(e.options.p,10):4173;(isNaN(o)||o<1||o>65535)&&(s.error(`\u65E0\u6548\u7684\u7AEF\u53E3\u53F7: ${e.options.port||e.options.p}`),process.exit(1));let n=Z.resolve(process.cwd(),"dist");Q.existsSync(n)||(s.error("\u672A\u627E\u5230\u6784\u5EFA\u8F93\u51FA\u76EE\u5F55 dist/\uFF0C\u8BF7\u5148\u8FD0\u884C lytx build"),process.exit(1)),B({port:o,root:n,hmr:!1})}async function Ne(){let e=W(process.argv);if(e.options.version){De();return}if(e.options.help&&!e.command){console.log(X);return}e.command||(console.log(X),process.exit(1));try{switch(e.command){case"create":await Ge(e);break;case"dev":Ie(e);break;case"build":await Be(e);break;case"preview":Ue(e);break;default:Le(e.command),process.exit(1)}}catch(o){let n=o instanceof Error?o.message:String(o);s.error(`\u6267\u884C\u5931\u8D25: ${n}`),console.log(""),console.log(` ${t("\u63D0\u793A:","brightYellow")} \u8BF7\u68C0\u67E5\u8F93\u5165\u53C2\u6570\u6216\u8FD0\u884C ${t("lytx --help","brightCyan")} \u67E5\u770B\u5E2E\u52A9`),console.log(""),process.exit(1)}}Ne();export{Ce as createHMREndpoint,G as createHMRServer,O as createProject,I as getHMRClientScript};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lytjs/cli",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "Lyt.js 命令行工具 - 提供项目创建、开发服务器和构建功能",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"esbuild": "^0.24.0"
|
|
25
|
+
},
|
|
23
26
|
"license": "MIT",
|
|
24
27
|
"author": "lytjs",
|
|
25
28
|
"repository": {
|
|
@@ -35,12 +38,14 @@
|
|
|
35
38
|
"frontend",
|
|
36
39
|
"vue-like",
|
|
37
40
|
"lightweight",
|
|
38
|
-
"zero-dependency",
|
|
39
41
|
"cli",
|
|
40
42
|
"scaffold",
|
|
41
43
|
"dev-server",
|
|
42
44
|
"build"
|
|
43
45
|
],
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.0.0"
|
|
48
|
+
},
|
|
44
49
|
"publishConfig": {
|
|
45
50
|
"access": "public"
|
|
46
51
|
}
|