Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: File not found: commonjsHelpers.js #36

Closed
skimhugo opened this issue Oct 25, 2021 · 6 comments
Closed

Error: File not found: commonjsHelpers.js #36

skimhugo opened this issue Oct 25, 2021 · 6 comments

Comments

@skimhugo
Copy link

at DirectoryCoordinator.addSourceFileAtPath (./node_modules/ts-morph/dist/ts-morph.js:18117:19)
at Project.addSourceFileAtPath (./node_modules/ts-morph/dist/ts-morph.js:19567:51)
at Object.transform (./node_modules/vite-plugin-dts/dist/index.js:138846:17)
at ./node_modules/rollup/dist/shared/rollup.js:22620:25

@qmhc
Copy link
Owner

qmhc commented Oct 26, 2021

Would you provide a test project to reproduce this issue?

@skimhugo
Copy link
Author

`import { encrypt, decrypt } from 'crypto-js/aes';
import UTF8, { parse } from 'crypto-js/enc-utf8';
import pkcs7 from 'crypto-js/pad-pkcs7';
import ECB from 'crypto-js/mode-ecb';
import md5 from 'crypto-js/md5';
// import UTF8 from 'crypto-js/enc-utf8';
import Base64 from 'crypto-js/enc-base64';
import type WordArray from 'crypto-js/lib-typedarrays';
import type crypto from 'crypto-js';
type CipherParams = crypto.lib.CipherParams;

// type WordArray = lib.WordArray;
// aes encryption key
export const cacheCipher = {
key: '11111000001111@',
iv: '@11111000001111
',
};

// 加密参数
export interface EncryptionParams {
key: string;
iv: string;
}

interface Format {
stringify: (cipherParams: CipherParams) => string;
parse: (str: string) => CipherParams;
}

interface CipherOption {
iv?: WordArray | undefined;
format?: Format | undefined;
[key: string]: any;
}
// interface CipherOptionCopy {
// /**
// * The IV to use for this operation.
// */
// iv?: WordArray | undefined;
// format?: Format | undefined;
// [key: string]: any;
// }

// export interface CipherOptions {
// mode: typeof ECB;
// padding: typeof pkcs7;
// key: WordArray | null;
// iv: WordArray | null;
// }

// JSON.stringify()
// JSON.parse()
// usage const encryption = new AesEncryption({ key, iv });
// const stringifyValue = this.hasEncrypt
// ? this.encryption.encryptByAES(stringData)
// : stringData;
// const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val;
// const data = JSON.parse(decVal);
//

// aes加密器
export class AesEncryption {
private readonly key: WordArray | undefined = undefined;
private readonly iv: WordArray | undefined = undefined;

public constructor(opt: Partial = {}) {
const { key, iv } = opt;
if (!key || !iv) {
throw new Error('the key or iv can not be empty!');
} else if ([key.length, iv.length].some((item) => item !== 16)) {
throw new Error('the key or iv must be 16 bits!');
}
if (key) {
this.key = parse(key);
}
if (iv) {
this.iv = parse(iv);
}
}

public get getOptions(): CipherOption {
return {
mode: ECB,
padding: pkcs7,
key: this.key,
iv: this.iv,
};
}

// 加密
public encryptByAES(cipherText: string) {
return encrypt(cipherText, this.key as WordArray, this.getOptions).toString();
}

// 解密
public decryptByAES(cipherText: string) {
return decrypt(cipherText, this.key as WordArray, this.getOptions).toString(UTF8);
}
}

export function encryptByBase64(cipherText: string) {
return UTF8.parse(cipherText).toString(Base64);
}

export function decodeByBase64(cipherText: string) {
return Base64.parse(cipherText).toString(UTF8);
}

export function encryptByMd5(password: string) {
return md5(password).toString();
}
`

index.ts:
export * from '@/cipher';
dependency
"crypto-js": "^4.1.1",

@skimhugo
Copy link
Author

@qmhc I think this problem is because crypto-js is a umd package.
I'm trying rewrite crypto-js to ts project.
but I think if there are common solution is good

@luozhihua
Copy link

commonjsHelpers.js 是 Vite 中生成的一个虚拟文件名,本身就不存在,看有没有办法直接判断一下这个文件名,将它忽略掉

@skimhugo
Copy link
Author

@luozhihua 没有继续深挖这个问题,目前用 tsup 和 tsc 进行 type 的打包。由于 type 实际只存在于运行态,目前我计划后续研究下 esbuild 在这方面的问题。依赖 rollup 进行对 ts 的类型打包有一些问题,尤其因为我还用了 pnpm,尤其麻烦。只好先作罢了。

@CaiWenlie
Copy link

commonjsHelpers.js 是 Vite 中生成的一个虚拟文件名,本身就不存在,看有没有办法直接判断一下这个文件名,将它忽略掉

额,打包之后还是有这个文件是咋回事
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants