@liujitcn/kratos-taro-app-cli 0.0.21 → 0.0.24

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.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/package.json +1 -1
  3. package/src/index.mjs +58 -12
package/README.md CHANGED
@@ -15,4 +15,6 @@ pnpm dlx @liujitcn/kratos-taro-app-cli create customer-app --with @acme/customer
15
15
 
16
16
  本地模块包含运行时入口、页面清单和构建期入口。模块页面由 core runner 在构建期间装配到私有宿主,不需要提交生成的页面包装器。
17
17
 
18
+ 脚手架会在 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` 变量。
19
+
18
20
  JavaScript 调用方也可以直接使用 `scaffoldKratosTaroApp(target, options)`。
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "type": "git",
8
8
  "url": "git@github.com:liujitcn/kratos-admin.git"
9
9
  },
10
- "version": "0.0.21",
10
+ "version": "0.0.24",
11
11
  "type": "module",
12
12
  "bin": {
13
13
  "kratos-taro-app": "./bin/kratos-taro-app.mjs"
package/src/index.mjs CHANGED
@@ -93,6 +93,7 @@ export function scaffoldKratosTaroApp(targetPath, options = {}) {
93
93
  }),
94
94
  )
95
95
  writeWorkspaceReadme(target, projectName)
96
+ writeEnvironmentFiles(target)
96
97
  writeHost(target, projectName, modules, packages)
97
98
  modules.forEach((name) => writeLocalModule(target, projectName, name))
98
99
  return target
@@ -148,10 +149,35 @@ pnpm tsc
148
149
  \`\`\`
149
150
 
150
151
  模块装配入口是 \`apps/taro-app/src/module-manifest.ts\`。模块顺序决定静态视图覆盖优先级;新增页面时同步维护模块自己的 \`src/pages.ts\` 和视图映射。
152
+
153
+ 环境文件位于 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\` 文件。
151
154
  `,
152
155
  )
153
156
  }
154
157
 
158
+ function writeEnvironmentFiles(target) {
159
+ write(
160
+ target,
161
+ '.env.development',
162
+ 'VITE_APP_BASE_API=/api\nVITE_APP_API_URL=http://localhost:7001\nVITE_APP_STATIC_API=\nVITE_APP_STATIC_URL=http://localhost:7001\n',
163
+ )
164
+ write(
165
+ target,
166
+ '.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',
168
+ )
169
+ write(
170
+ target,
171
+ '.env.production',
172
+ 'VITE_APP_BASE_API=/api\nVITE_APP_API_URL=http://localhost:7001\nVITE_APP_STATIC_API=\nVITE_APP_STATIC_URL=http://localhost:7001\n',
173
+ )
174
+ write(
175
+ target,
176
+ '.env.production-h5',
177
+ 'VITE_APP_BASE_PATH=/app/\nVITE_APP_BASE_API=/api\nVITE_APP_API_URL=\nVITE_APP_STATIC_API=\nVITE_APP_STATIC_URL=\n',
178
+ )
179
+ }
180
+
155
181
  function writeHost(target, projectName, modules, packages) {
156
182
  const localDependencies = Object.fromEntries(modules.map((name) => [`@local/${name}`, 'workspace:*']))
157
183
  const publishedDependencies = Object.fromEntries(packages.map((name) => [name, 'latest']))
@@ -169,7 +195,7 @@ function writeHost(target, projectName, modules, packages) {
169
195
  'dev:mp-weixin':
170
196
  'cross-env NODE_ENV=development node scripts/run-taro.mjs --type weapp --watch --mode development',
171
197
  'build:h5':
172
- 'cross-env KRATOS_TARO_OUTPUT_ROOT=dist/build/h5 KRATOS_TARO_PUBLIC_PATH=/ node scripts/run-taro.mjs --type h5 --mode production',
198
+ 'cross-env KRATOS_TARO_OUTPUT_ROOT=dist/build/h5 node scripts/run-taro.mjs --type h5 --mode production',
173
199
  'build:mp-weixin':
174
200
  'cross-env KRATOS_TARO_OUTPUT_ROOT=dist/build/mp-weixin node scripts/run-taro.mjs --type weapp --mode production',
175
201
  tsc: 'tsc --noEmit -p tsconfig.json',
@@ -468,23 +494,41 @@ function hostConfig(projectName, packageNames) {
468
494
  return `import { createRequire } from 'node:module'
469
495
  import { dirname, resolve } from 'node:path'
470
496
  import { defineConfig, type UserConfigExport } from '@tarojs/cli'
497
+ import { dotenvParse } from '@tarojs/helper'
471
498
  import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'
472
499
  import devConfig from './dev'
473
500
  import prodConfig from './prod'
474
501
 
475
502
  const packageNames = ${JSON.stringify(packageNames, null, 2)}
476
503
  const hostRequire = createRequire(resolve(__dirname, '../package.json'))
504
+ const workspaceRoot = resolve(__dirname, '../../..')
505
+
506
+ function resolveEnv(mode: string, platform: string): Record<string, string> {
507
+ const baseEnv = dotenvParse(workspaceRoot, 'VITE_APP_', mode)
508
+ const platformEnv =
509
+ platform === 'h5' ? dotenvParse(workspaceRoot, 'VITE_APP_', \`\${mode}-\${platform}\`) : {}
510
+ const shellEnv = Object.fromEntries(
511
+ Object.entries(process.env).filter(([key]) => key.startsWith('VITE_APP_')),
512
+ ) as Record<string, string>
513
+ return { ...baseEnv, ...platformEnv, ...shellEnv }
514
+ }
477
515
 
478
516
  export default defineConfig<'webpack5'>(async (merge) => {
517
+ const mode = process.env.NODE_ENV || 'production'
518
+ const platform = process.env.TARO_ENV || ''
519
+ const env = resolveEnv(mode, platform)
479
520
  const outputRoot =
480
521
  process.env.KRATOS_TARO_OUTPUT_ROOT ||
481
- (process.env.TARO_ENV === 'h5'
522
+ (platform === 'h5'
482
523
  ? 'dist/h5'
483
- : process.env.TARO_ENV === 'weapp'
524
+ : platform === 'weapp'
484
525
  ? 'dist/mp-weixin'
485
526
  : 'dist')
486
- const publicPath = process.env.KRATOS_TARO_PUBLIC_PATH || '/'
487
- const apiTargetUrl = process.env.KRATOS_TARO_API_URL || 'http://localhost:7001'
527
+ const publicPath = env.VITE_APP_BASE_PATH ?? '/'
528
+ const apiBasePath = env.VITE_APP_BASE_API ?? '/api'
529
+ const apiTargetUrl = env.VITE_APP_API_URL ?? 'http://localhost:7001'
530
+ const staticApi = env.VITE_APP_STATIC_API ?? ''
531
+ const staticUrl = env.VITE_APP_STATIC_URL ?? apiTargetUrl
488
532
  const packageRoots = Object.fromEntries(
489
533
  packageNames.map((name) => [name, dirname(hostRequire.resolve(\`\${name}/package.json\`))]),
490
534
  )
@@ -522,10 +566,12 @@ export default defineConfig<'webpack5'>(async (merge) => {
522
566
  options: {},
523
567
  },
524
568
  defineConstants: {
525
- 'process.env.KRATOS_TARO_API_BASE': JSON.stringify(process.env.KRATOS_TARO_API_BASE || '/api'),
526
- 'process.env.KRATOS_TARO_API_URL': JSON.stringify(apiTargetUrl),
527
- 'process.env.KRATOS_TARO_PUBLIC_PATH': JSON.stringify(publicPath),
528
- 'process.env.KRATOS_TARO_STATIC_URL': JSON.stringify(process.env.KRATOS_TARO_STATIC_URL || apiTargetUrl),
569
+ 'process.env.VITE_APP_PORT': JSON.stringify(env.VITE_APP_PORT ?? ''),
570
+ 'process.env.VITE_APP_BASE_PATH': JSON.stringify(publicPath),
571
+ 'process.env.VITE_APP_BASE_API': JSON.stringify(apiBasePath),
572
+ 'process.env.VITE_APP_API_URL': JSON.stringify(apiTargetUrl),
573
+ 'process.env.VITE_APP_STATIC_API': JSON.stringify(staticApi),
574
+ 'process.env.VITE_APP_STATIC_URL': JSON.stringify(staticUrl),
529
575
  },
530
576
  alias: aliases,
531
577
  mini: {
@@ -537,11 +583,11 @@ export default defineConfig<'webpack5'>(async (merge) => {
537
583
  staticDirectory: 'static',
538
584
  router: { mode: 'hash' },
539
585
  devServer: {
540
- port: 5002,
586
+ port: Number(env.VITE_APP_PORT || 5002),
541
587
  host: '0.0.0.0',
542
588
  proxy: {
543
- '/api': { target: apiTargetUrl, changeOrigin: true },
544
- '/events': { target: apiTargetUrl, changeOrigin: true },
589
+ [apiBasePath || '/api']: { target: apiTargetUrl || 'http://localhost:7001', changeOrigin: true },
590
+ '/events': { target: apiTargetUrl || 'http://localhost:7001', changeOrigin: true },
545
591
  },
546
592
  },
547
593
  output: {