Skip to content

Commit

Permalink
refactor: handle rapid mode for tnpm and cnpm detection (#11710)
Browse files Browse the repository at this point in the history
* refactor(bundler-webpack): fs cache support tnpm rapid mode

* refactor(utils): handle rapid mode for tnpm/cnpm detection
  • Loading branch information
PeachScript authored Oct 11, 2023
1 parent 8e4db78 commit 0b2daeb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/bundler-webpack/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ export async function getConfig(opts: IOpts): Promise<Configuration> {
// 使用 immutablePaths 避免 node_modules 的内容被写入缓存
// tnpm 安装的依赖路径中同时包含包名和版本号,满足 immutablePaths 使用的条件
// 同时配置 managedPaths 将 tnpm 的软连接结构标记为可信,避免执行快照序列化时 OOM
// ref: smallfish
if (/*isTnpm*/ require('@umijs/utils/package').__npminstall_done) {
// 此处通过软链的目标文件夹名称来判断 node_modules 是否为 tnpm 的 npminstall 模式
// 因为 rapid 模式下 package.json 中没有 __npminstall_done 的标记
// ex. node_modules/_@[email protected]@@umijs/utils/package.json
if (require.resolve('@umijs/utils/package').includes('_@umijs_utils@')) {
const nodeModulesPath =
opts.cache.absNodeModulesPath ||
join(opts.rootDir || opts.cwd, 'node_modules');
Expand Down
23 changes: 20 additions & 3 deletions packages/utils/src/npmClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';

export type NpmClient = 'npm' | 'cnpm' | 'tnpm' | 'yarn' | 'pnpm';
Expand All @@ -11,12 +11,29 @@ export enum NpmClientEnum {
npm = 'npm',
}
export const getNpmClient = (opts: { cwd: string }): NpmClient => {
const tnpmRegistries = ['.alibaba-inc.', '.antgroup-inc.'];
const tcnpmLockPath = join(opts.cwd, 'node_modules', '.package-lock.json');
const chokidarPkg = require('chokidar/package.json');
if (chokidarPkg.__npminstall_done) {
return chokidarPkg._resolved.includes('registry.npm.alibaba-inc.com')

// detect tnpm/cnpm client
// all situations:
// - npminstall mode + native fs => generate _resolved field in package.json
// - npminstall mode + rapid fs => generate .package-lock.json in node_modules
// - npm mode + native fs => generate .package-lock.json in node_modules
// - npm mode + rapid fs => generate .package-lock.json in node_modules
// all conditions:
// - has _resolved field or .package-lock.json means tnpm/cnpm
// - _resolved field or .package-lock.json contains tnpm registry means tnpm
if (chokidarPkg._resolved) {
return tnpmRegistries.some((r) => chokidarPkg._resolved.includes(r))
? 'tnpm'
: 'cnpm';
} else if (existsSync(tcnpmLockPath)) {
const tcnpmLock = readFileSync(tcnpmLockPath, 'utf-8');

return tnpmRegistries.some((r) => tcnpmLock.includes(r)) ? 'tnpm' : 'cnpm';
}

const chokidarPath = require.resolve('chokidar');
if (
chokidarPath.includes('.pnpm') ||
Expand Down

0 comments on commit 0b2daeb

Please sign in to comment.