@liujitcn/kratos-taro-app-cli 0.0.32 → 0.0.34
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/package.json +1 -1
- package/src/index.mjs +50 -17
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
2
|
import { basename, resolve } from 'node:path'
|
|
3
3
|
|
|
4
4
|
const cliPackage = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
|
|
@@ -9,6 +9,8 @@ if (typeof publicPackageVersion !== 'string' || !publicPackageVersion) {
|
|
|
9
9
|
|
|
10
10
|
const taroVersion = '4.2.1'
|
|
11
11
|
const reactVersion = '18.3.1'
|
|
12
|
+
const h5RootFontSource =
|
|
13
|
+
'!function(n){function f(){var e=n.document.documentElement,w=e.clientWidth||n.innerWidth||375,x=w>960?375:w;e.style.fontSize=20*x/375+"px"}n.addEventListener("resize",function(){f();setTimeout(f,500)}),f()}(window);'
|
|
12
14
|
|
|
13
15
|
/** 创建独立 Kratos Taro React workspace。 */
|
|
14
16
|
export function scaffoldKratosTaroApp(targetPath, options = {}) {
|
|
@@ -30,11 +32,7 @@ export function scaffoldKratosTaroApp(targetPath, options = {}) {
|
|
|
30
32
|
'.gitignore',
|
|
31
33
|
'node_modules\ndist\n.kratos-taro-app-pages-state.json\n.kratos-taro-app-pages-state.json.*\napps/taro-app/src/pages/*\n!apps/taro-app/src/pages/bootstrap/\n!apps/taro-app/src/pages/bootstrap/**\napps/taro-app/src/pages?*/\n',
|
|
32
34
|
)
|
|
33
|
-
write(
|
|
34
|
-
target,
|
|
35
|
-
'pnpm-workspace.yaml',
|
|
36
|
-
'packages:\n - apps/*\n - packages/modules/*\n',
|
|
37
|
-
)
|
|
35
|
+
write(target, 'pnpm-workspace.yaml', 'packages:\n - apps/*\n - packages/modules/*\n')
|
|
38
36
|
write(
|
|
39
37
|
target,
|
|
40
38
|
'package.json',
|
|
@@ -95,6 +93,11 @@ export function scaffoldKratosTaroApp(targetPath, options = {}) {
|
|
|
95
93
|
writeWorkspaceReadme(target, projectName)
|
|
96
94
|
writeEnvironmentFiles(target)
|
|
97
95
|
writeHost(target, projectName, modules, packages)
|
|
96
|
+
write(target, 'apps/taro-app/src/static/h5-root-font.js', `${h5RootFontSource}\n`)
|
|
97
|
+
copyFileSync(
|
|
98
|
+
new URL('../assets/favicon.ico', import.meta.url),
|
|
99
|
+
resolve(target, 'apps/taro-app/src/static/favicon.ico'),
|
|
100
|
+
)
|
|
98
101
|
modules.forEach((name) => writeLocalModule(target, projectName, name))
|
|
99
102
|
return target
|
|
100
103
|
}
|
|
@@ -151,6 +154,8 @@ pnpm tsc
|
|
|
151
154
|
模块装配入口是 \`apps/taro-app/src/module-manifest.ts\`。模块顺序决定静态视图覆盖优先级;新增页面时同步维护模块自己的 \`src/pages.ts\` 和视图映射。
|
|
152
155
|
|
|
153
156
|
环境文件位于 workspace 根目录,变量名与 uni-app 保持一致:\`VITE_APP_PORT\`、\`VITE_APP_BASE_PATH\`、\`VITE_APP_BASE_API\`、\`VITE_APP_API_URL\`、\`VITE_APP_STATIC_API\`、\`VITE_APP_STATIC_URL\`。H5 会在基础模式文件上叠加对应的 \`*-h5\` 文件。
|
|
157
|
+
|
|
158
|
+
H5 通过局域网 IP 访问时,先在仓库根目录运行 \`bash scripts/generate-dev-cert.sh 192.168.1.100\` 生成证书,再在 \`.env.development-h5.local\` 中设置 \`VITE_APP_HTTPS=true\`;后端使用 HTTPS 时同时设置 \`VITE_APP_API_URL=https://localhost:7001\`。
|
|
154
159
|
`,
|
|
155
160
|
)
|
|
156
161
|
}
|
|
@@ -164,7 +169,7 @@ function writeEnvironmentFiles(target) {
|
|
|
164
169
|
write(
|
|
165
170
|
target,
|
|
166
171
|
'.env.development-h5',
|
|
167
|
-
'VITE_APP_PORT=5002\nVITE_APP_BASE_PATH=/\nVITE_APP_BASE_API=/api\nVITE_APP_API_URL=http://localhost:7001\nVITE_APP_STATIC_API=\nVITE_APP_STATIC_URL=http://localhost:7001\n',
|
|
172
|
+
'VITE_APP_PORT=5002\nVITE_APP_HTTPS=false\n# VITE_APP_HTTPS_KEY=../../certs/dev-key.pem\n# VITE_APP_HTTPS_CERT=../../certs/dev-cert.pem\n# 后端使用 HTTPS 时,在 .env.development-h5.local 覆盖 VITE_APP_API_URL=https://localhost:7001\nVITE_APP_BASE_PATH=/\nVITE_APP_BASE_API=/api\nVITE_APP_API_URL=http://localhost:7001\nVITE_APP_STATIC_API=\nVITE_APP_STATIC_URL=http://localhost:7001\n',
|
|
168
173
|
)
|
|
169
174
|
write(
|
|
170
175
|
target,
|
|
@@ -179,7 +184,9 @@ function writeEnvironmentFiles(target) {
|
|
|
179
184
|
}
|
|
180
185
|
|
|
181
186
|
function writeHost(target, projectName, modules, packages) {
|
|
182
|
-
const localDependencies = Object.fromEntries(
|
|
187
|
+
const localDependencies = Object.fromEntries(
|
|
188
|
+
modules.map((name) => [`@local/${name}`, 'workspace:*']),
|
|
189
|
+
)
|
|
183
190
|
const publishedDependencies = Object.fromEntries(packages.map((name) => [name, 'latest']))
|
|
184
191
|
write(
|
|
185
192
|
target,
|
|
@@ -236,9 +243,15 @@ function writeHost(target, projectName, modules, packages) {
|
|
|
236
243
|
\`apps/taro-app\` 是 \`${projectName}\` 的私有 Taro React 宿主,负责装配模块并提供 H5、微信小程序构建入口,不承载可复用业务实现。
|
|
237
244
|
|
|
238
245
|
固定启动页位于 \`src/pages/bootstrap\`。其他模块页面由 core runner 在构建期间临时生成包装器、页面配置与静态资源,构建结束后会自动恢复宿主目录。
|
|
246
|
+
|
|
247
|
+
通过局域网 IP 访问 H5 时,可复用仓库根 certs 下的共享证书,并在 .env.development-h5.local 中设置 VITE_APP_HTTPS=true;后端使用 HTTPS 时同步设置 VITE_APP_API_URL=https://localhost:7001。
|
|
239
248
|
`,
|
|
240
249
|
)
|
|
241
|
-
write(
|
|
250
|
+
write(
|
|
251
|
+
target,
|
|
252
|
+
'apps/taro-app/scripts/run-taro.mjs',
|
|
253
|
+
"import '@liujitcn/kratos-taro-app-core/runner'\n",
|
|
254
|
+
)
|
|
242
255
|
write(
|
|
243
256
|
target,
|
|
244
257
|
'apps/taro-app/babel.config.cjs',
|
|
@@ -298,8 +311,9 @@ function writeHost(target, projectName, modules, packages) {
|
|
|
298
311
|
<meta charset="utf-8" />
|
|
299
312
|
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no,viewport-fit=cover" />
|
|
300
313
|
<meta name="format-detection" content="telephone=no,address=no" />
|
|
314
|
+
<link rel="icon" type="image/x-icon" href="./static/favicon.ico" />
|
|
301
315
|
<title>${projectName}</title>
|
|
302
|
-
<script
|
|
316
|
+
<script src="./static/h5-root-font.js" defer></script>
|
|
303
317
|
</head>
|
|
304
318
|
<body><div id="app"></div></body>
|
|
305
319
|
</html>
|
|
@@ -476,9 +490,7 @@ function moduleManifest(modules, packages) {
|
|
|
476
490
|
const imports = [
|
|
477
491
|
"import { coreModule } from '@liujitcn/kratos-taro-app-core'",
|
|
478
492
|
"import { systemModule } from '@liujitcn/kratos-taro-app-system'",
|
|
479
|
-
...modules.map(
|
|
480
|
-
(name) => `import { ${toCamelCase(name)}Module } from '@local/${name}'`,
|
|
481
|
-
),
|
|
493
|
+
...modules.map((name) => `import { ${toCamelCase(name)}Module } from '@local/${name}'`),
|
|
482
494
|
...packages.map((name, index) => `import packageModule${index} from '${name}'`),
|
|
483
495
|
]
|
|
484
496
|
const members = [
|
|
@@ -492,6 +504,7 @@ function moduleManifest(modules, packages) {
|
|
|
492
504
|
|
|
493
505
|
function hostConfig(projectName, packageNames) {
|
|
494
506
|
return `import { createRequire } from 'node:module'
|
|
507
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
495
508
|
import { dirname, resolve } from 'node:path'
|
|
496
509
|
import { defineConfig, type UserConfigExport } from '@tarojs/cli'
|
|
497
510
|
import { dotenvParse } from '@tarojs/helper'
|
|
@@ -503,6 +516,16 @@ const packageNames = ${JSON.stringify(packageNames, null, 2)}
|
|
|
503
516
|
const hostRequire = createRequire(resolve(__dirname, '../package.json'))
|
|
504
517
|
const workspaceRoot = resolve(__dirname, '../../..')
|
|
505
518
|
|
|
519
|
+
function resolveHttpsOptions(env: Record<string, string>, root: string) {
|
|
520
|
+
if (env.VITE_APP_HTTPS !== 'true') return undefined
|
|
521
|
+
const keyPath = resolve(root, env.VITE_APP_HTTPS_KEY || '../../certs/dev-key.pem')
|
|
522
|
+
const certPath = resolve(root, env.VITE_APP_HTTPS_CERT || '../../certs/dev-cert.pem')
|
|
523
|
+
if (!existsSync(keyPath) || !existsSync(certPath)) {
|
|
524
|
+
throw new Error('VITE_APP_HTTPS 已开启,但未找到证书文件,请先在仓库根目录运行 scripts/generate-dev-cert.sh;期望路径:' + keyPath + ' 和 ' + certPath)
|
|
525
|
+
}
|
|
526
|
+
return { key: readFileSync(keyPath), cert: readFileSync(certPath) }
|
|
527
|
+
}
|
|
528
|
+
|
|
506
529
|
function resolveEnv(mode: string, platform: string): Record<string, string> {
|
|
507
530
|
const baseEnv = dotenvParse(workspaceRoot, 'VITE_APP_', mode)
|
|
508
531
|
const platformEnv =
|
|
@@ -527,8 +550,10 @@ export default defineConfig<'webpack5'>(async (merge) => {
|
|
|
527
550
|
const publicPath = env.VITE_APP_BASE_PATH ?? '/'
|
|
528
551
|
const apiBasePath = env.VITE_APP_BASE_API ?? '/api'
|
|
529
552
|
const apiTargetUrl = env.VITE_APP_API_URL ?? 'http://localhost:7001'
|
|
553
|
+
const apiProxyOptions = apiTargetUrl.startsWith('https://') ? { secure: false } : {}
|
|
530
554
|
const staticApi = env.VITE_APP_STATIC_API ?? ''
|
|
531
555
|
const staticUrl = env.VITE_APP_STATIC_URL ?? apiTargetUrl
|
|
556
|
+
const httpsOptions = resolveHttpsOptions(env, workspaceRoot)
|
|
532
557
|
const packageRoots = Object.fromEntries(
|
|
533
558
|
packageNames.map((name) => [name, dirname(hostRequire.resolve(\`\${name}/package.json\`))]),
|
|
534
559
|
)
|
|
@@ -585,9 +610,10 @@ export default defineConfig<'webpack5'>(async (merge) => {
|
|
|
585
610
|
devServer: {
|
|
586
611
|
port: Number(env.VITE_APP_PORT || 5002),
|
|
587
612
|
host: '0.0.0.0',
|
|
613
|
+
https: httpsOptions,
|
|
588
614
|
proxy: {
|
|
589
|
-
[apiBasePath || '/api']: { target: apiTargetUrl || 'http://localhost:7001', changeOrigin: true },
|
|
590
|
-
'/events': { target: apiTargetUrl || 'http://localhost:7001', changeOrigin: true },
|
|
615
|
+
[apiBasePath || '/api']: { target: apiTargetUrl || 'http://localhost:7001', changeOrigin: true, ...apiProxyOptions },
|
|
616
|
+
'/events': { target: apiTargetUrl || 'http://localhost:7001', changeOrigin: true, ...apiProxyOptions },
|
|
591
617
|
},
|
|
592
618
|
},
|
|
593
619
|
output: {
|
|
@@ -614,7 +640,9 @@ function platformConfig(development) {
|
|
|
614
640
|
return `import type { UserConfigExport } from '@tarojs/cli'
|
|
615
641
|
|
|
616
642
|
export default ${JSON.stringify(
|
|
617
|
-
development
|
|
643
|
+
development
|
|
644
|
+
? { logger: { quiet: false, stats: true }, mini: {}, h5: {} }
|
|
645
|
+
: { mini: {}, h5: {} },
|
|
618
646
|
null,
|
|
619
647
|
2,
|
|
620
648
|
)} satisfies UserConfigExport<'webpack5'>
|
|
@@ -631,7 +659,12 @@ function readOptions(args, option) {
|
|
|
631
659
|
const value = args[++index]
|
|
632
660
|
if (!value || value.startsWith('--')) throw new Error(`选项 ${argument} 缺少值`)
|
|
633
661
|
if (argument === option) {
|
|
634
|
-
values.push(
|
|
662
|
+
values.push(
|
|
663
|
+
...value
|
|
664
|
+
.split(',')
|
|
665
|
+
.map((item) => item.trim())
|
|
666
|
+
.filter(Boolean),
|
|
667
|
+
)
|
|
635
668
|
}
|
|
636
669
|
}
|
|
637
670
|
return [...new Set(values)]
|