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

Option to skip file content reading #41

Open
fisker opened this issue Nov 14, 2023 · 3 comments
Open

Option to skip file content reading #41

fisker opened this issue Nov 14, 2023 · 3 comments

Comments

@fisker
Copy link

fisker commented Nov 14, 2023

I think the loaders can read the file themself, it's not necessary to provide the content to loader.

const content = String(await fsReadFileAsync(absPath));

@antonk52
Copy link
Owner

Hello and thank you for suggestion.

While I agree this makes since in a subset of scenarios. This was a conscious decision to be compatible with cosmiconfig

https://github.com/cosmiconfig/cosmiconfig/blob/42ca3fab6d6ae593a895c0fe4e2a5f6b297e6361/src/types.ts#L24-L31

@antonk52
Copy link
Owner

I've given this more thought and I think this is a good idea. I will release a new minor version where a new option like skipFileContent will be added. Thank you for a suggestion.

@iiroj
Copy link

iiroj commented Dec 2, 2023

When using custom loaders, a simple approach could be to detect the number of arguments of the loader using Function.length. If Function.length === 1, it only uses the filepath and if Function.length === 2 it also uses the content argument. This way the file reading could be automatically skipped when the loader doesn't care. For example:

https://github.com/antonk52/lilconfig/blob/8deeb3b8e4c4f073e329b29a53781d4f8fc79183/src/index.ts#L211-L213C55

const loaderKey = path.extname(searchPlace) || 'noExt';
const loader = loaders[loaderKey];
const content = loader.length === 1 ? undefined : String(await fsReadFileAsync(filepath));

EDIT: For reference, I implented something like this in lint-staged naive config loading: lint-staged/lint-staged@dc0fe71#diff-bff4b732db5b89cc001efe07fb282c50542546d6ccd1f7ee2053f7ff5afdd89dR102

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

3 participants