aliases.mjs 5.28 KB
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const HERE = path.dirname(fileURLToPath(import.meta.url));
/** 仓库根:packages/vite-config/src -> ../../.. */
export const ROOT = path.resolve(HERE, '../../..');

/**
 * 单一真源:老项目 `src/**` 里的 `@/xxx` import 不动一个字,靠 alias 把不同职责
 * 映射到不同包。这是 wujie 改造能「零 import 重写」的关键。
 *
 * 两类应用的差异只有前 4 条:
 *   - shell   -> 指向 @uj/platform-core(真实 axios 实例 / React Context / zustand)
 *   - subapp  -> 指向 @uj/platform-sdk/compat(props.request 代理 / 纯 localStorage 权限)
 *
 * ⚠️ 顺序敏感:具体前缀必须排在 `@/api`、`@/theme` 与 `@/(.*)` 兜底之前。
 * Vite alias 走 @rollup/plugin-alias,正则捕获组用 `$1` 回填;组未参与匹配时
 * 替换为空串,因此 `dir + '$1'` 同时覆盖 `@/x` 与 `@/x/y` 两种写法。
 *
 * @param {{ appDir: string, kind: 'shell' | 'subapp' }} options
 */
export function createAliases({ appDir, kind }) {
  const isShell = kind === 'shell';
  const pkg = (...p) => path.join(ROOT, 'packages', ...p);
  const at = (...p) => path.join(appDir, 'src', ...p);

  const apiCore = isShell
    ? pkg('platform-core', 'src', 'api', 'core')
    : pkg('platform-sdk', 'src', 'compat', 'api-core');
  const access = isShell
    ? pkg('platform-core', 'src', 'access')
    : pkg('platform-sdk', 'src', 'compat', 'access');
  const store = isShell
    ? pkg('platform-core', 'src', 'store')
    : pkg('platform-sdk', 'src', 'compat', 'store');
  const queryClient = isShell
    ? pkg('platform-core', 'src', 'queryClient.ts')
    : pkg('platform-sdk', 'src', 'compat', 'queryClient.ts');

  return [
    // ---- host 独占运行时 vs 子应用兼容层 ----
    { find: /^@\/api\/core(\/.*)?$/, replacement: apiCore + '$1' },
    { find: /^@\/api\/core$/, replacement: apiCore },
    { find: /^@\/access(\/.*)?$/, replacement: access + '$1' },
    { find: /^@\/store(\/.*)?$/, replacement: store + '$1' },
    { find: /^@\/queryClient$/, replacement: queryClient },

    // ---- 跨域共享(两边一致)----
    { find: /^@\/theme\/themeConfig$/, replacement: pkg('design-tokens', 'src', 'themeConfig.ts') },
    { find: /^@\/components(\/.*)?$/, replacement: pkg('biz', 'src', 'components') + '$1' },
    { find: /^@\/constants(\/.*)?$/, replacement: pkg('biz', 'src', 'constants') + '$1' },
    { find: /^@\/config(\/.*)?$/, replacement: pkg('biz', 'src', 'config') + '$1' },
    { find: /^@\/utils(\/.*)?$/, replacement: pkg('biz', 'src', 'utils') + '$1' },

    // ---- 应用自身 ----
    { find: /^@\/api(\/.*)?$/, replacement: at('api') + '$1' },
    { find: /^@\/theme(\/.*)?$/, replacement: at('theme') + '$1' },
    { find: /^@uj\/api-core$/, replacement: pkg('api-core', 'src', 'index.ts') },
    { find: /^@uj\/ui$/, replacement: pkg('ui', 'src', 'index.ts') },
    { find: /^@\/(.*)$/, replacement: at('$1') },
  ];
}

/**
 * 与 createAliases 同源的 tsconfig paths。编译器与打包器必须看到同一张表,
 * 否则会出现「dev 能跑、tsc 报找不到模块」。
 * 返回的路径相对 appDir,且一律以 ./ 或 ../ 开头:TS 4.1+ 的 paths 相对于声明它的
 * tsconfig 解析,不需要 baseUrl(baseUrl 自 TS 6 起已弃用,非相对路径会报 TS5090)。
 */
export function createTsconfigPaths({ appDir, kind }) {
  const isShell = kind === 'shell';
  const rel = (...p) =>
    path
      .relative(appDir, path.join(ROOT, 'packages', ...p))
      .split(path.sep)
      .join('/');

  const apiCore = isShell
    ? rel('platform-core', 'src', 'api', 'core')
    : rel('platform-sdk', 'src', 'compat', 'api-core');
  const access = isShell
    ? rel('platform-core', 'src', 'access')
    : rel('platform-sdk', 'src', 'compat', 'access');
  const store = isShell
    ? rel('platform-core', 'src', 'store')
    : rel('platform-sdk', 'src', 'compat', 'store');
  const queryClient = isShell
    ? rel('platform-core', 'src', 'queryClient.ts')
    : rel('platform-sdk', 'src', 'compat', 'queryClient.ts');

  return {
    '@/api/core': [`${apiCore}/index.ts`],
    '@/api/core/*': [`${apiCore}/*`],
    '@/access': [`${access}/index.ts`],
    '@/access/*': [`${access}/*`],
    '@/store/*': [`${store}/*`],
    '@/queryClient': [queryClient],
    '@/theme/themeConfig': [`${rel('design-tokens', 'src', 'themeConfig.ts')}`],
    '@/components': [`${rel('biz', 'src', 'components', 'index.ts')}`],
    '@/components/*': [`${rel('biz', 'src', 'components')}/*`],
    '@/constants/*': [`${rel('biz', 'src', 'constants')}/*`],
    '@/config/*': [`${rel('biz', 'src', 'config')}/*`],
    '@/utils': [`${rel('biz', 'src', 'utils', 'index.ts')}`],
    '@/utils/*': [`${rel('biz', 'src', 'utils')}/*`],
    '@/api': ['./src/api/index.ts'],
    '@/api/*': ['./src/api/*'],
    '@/theme/*': ['./src/theme/*'],
    '@uj/ui': [`${rel('ui', 'src', 'index.ts')}`],
    '@uj/contracts': [`${rel('contracts', 'src', 'index.ts')}`],
    '@uj/platform-core': [`${rel('platform-core', 'src', 'index.ts')}`],
    '@uj/platform-sdk': [`${rel('platform-sdk', 'src', 'index.ts')}`],
    '@uj/biz': [`${rel('biz', 'src', 'index.ts')}`],
    '@uj/design-tokens': [`${rel('design-tokens', 'src', 'index.ts')}`],
    '@uj/api-core': [`${rel('api-core', 'src', 'index.ts')}`],
    '@/*': ['./src/*'],
  };
}