Skip to content

Commit

Permalink
fix: too long file path error from mfsu (#10984)
Browse files Browse the repository at this point in the history
* fix: 🐛 dep 文件路径过长 生产 hash ,保留 200 个字符方便查看按照文件名查找文件

* fix: 🐛 移除 cwd,路径尽量短

---------

Co-authored-by: pshu <pishu.spf@antfin.com>
  • Loading branch information
stormslowly and stormslowly committed Apr 19, 2023
1 parent 1ef3c33 commit 8df3299
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/mfsu/src/dep/dep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chalk, logger, pkgUp, winPath } from '@umijs/utils';
import assert from 'assert';
import { createHash } from 'crypto';
import { readFileSync } from 'fs';
import { dirname, isAbsolute, join } from 'path';
import { MF_VA_PREFIX } from '../constants';
Expand Down Expand Up @@ -29,12 +30,30 @@ export class Dep {
this.version = opts.version;
this.cwd = opts.cwd;
this.shortFile = this.file;
this.normalizedFile = this.shortFile.replace(/\//g, '_').replace(/:/g, '_');
this.normalizedFile = this.normalizePath(this.shortFile);
this.filePath = `${MF_VA_PREFIX}${this.normalizedFile}.js`;
this.excludeNodeNatives = opts.excludeNodeNatives!;
this.importer = opts.importer;
}

private normalizePath(p: string): string {
let longPath = p;

if (longPath.startsWith(this.cwd)) {
longPath = longPath.slice(this.cwd.length);
}
longPath = longPath.replace(/\//g, '_').replace(/:/g, '_');

if (longPath.length <= 200) {
return longPath;
}

const hash = createHash('md5').update(longPath).digest('hex').slice(0, 16);
const post = longPath.slice(-200);

return `${hash}_${post}`;
}

async buildExposeContent() {
// node natives
// @ts-ignore
Expand Down

0 comments on commit 8df3299

Please sign in to comment.