1 3
import * as path from 'path';
2 3
import { slash } from '@remax/shared';
3
import { Options } from '@remax/types';
4 3
import crypto from 'crypto';
5

6 3
const hash = (name: string) => crypto.createHash('md5').update(name).digest('hex');
7

8 3
export function replaceExtension(file: string, ext: string) {
9 3
  const oldExt = path.extname(file);
10 3
  return file.replace(new RegExp(`${oldExt}$`), ext);
11
}
12

13 3
export function getNativeAssetOutputPath(sourcePath: string, options: Options) {
14 3
  let output = slash(sourcePath)
15
    .replace(slash(options.cwd) + '/', '')
16
    .replace(slash(options.rootDir) + '/', '')
17
    .replace(/@/g, '_')
18
    .replace(/node_modules/g, 'npm');
19

20
  // native组件支持yarn pnp模式
21
  // 常见的文件路径是 ${cwd}/.yarn/cache/xxx.zip/ or ${home}/.yarn/berry/xxx.zip
22
  /** @todo 测试用例依赖pnp环境下的pnp-api,得让测试项目使用pnp安装依赖 */
23
  /* istanbul ignore next */
24
  if (process.versions.pnp) {
25
    output = output.replace(/.+\.zip\//, '');
26
  }
27

28 3
  if (path.isAbsolute(output)) {
29
    // 通过 config.resolve.modules 设置非 cwd 的 sourcePath, 会进入此分支
30
    // 将这种路径改成相对路径
31 0
    const dirname = path.dirname(output);
32 0
    const basename = path.basename(output);
33

34 0
    output = '_external/' + hash(dirname) + basename;
35
  }
36

37 3
  return output;
38
}

Read our documentation on viewing source code .

Loading