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

Support nested config splitting in .config/ dir #137

Open
pi0 opened this issue Feb 15, 2024 · 3 comments · May be fixed by #211
Open

Support nested config splitting in .config/ dir #137

pi0 opened this issue Feb 15, 2024 · 3 comments · May be fixed by #211

Comments

@pi0
Copy link
Member

pi0 commented Feb 15, 2024

As a followup to .config/ dir supports #136 for config-dir proposal.

When configuration file size increases (and there is no way to keep it minimal!!), users might prefer to split the configuration into smaller files.

One way is that we just let this task to the users to use manual imports:

// .config/name.ts
import longConf from './name/key.ts'

export default {
 key: longConf
}

This method is clear and also can leave the details up to the end-users on how to split.

Another way is that c12 can allow a conventional way to allow splitting with nested directories:

export default { /* long config */ }

It can be in:
a. .config/name/key.ts
b. .config/name.key.ts

This gives a conventional way of splitting longer configs into smaller files. The main downside is perf. We need at least one readdir call in the case of (b) or one stat in the case of (a).

types support for sub-types

it is probably more something that authors have to support but still worth to mention complexities in this dicussion.

@ineshbose
Copy link

We need at least one readdir call in the case of (b) or one stat in the case of (a).

How about using globby to see all paths starting with name, and based on that we may already have the tree, and then load accordingly?

@pi0
Copy link
Member Author

pi0 commented Dec 13, 2024

globby/glob i'm afraid is little more expensive... readdir (with limited length) maps to one system call.

@ineshbose
Copy link

ineshbose commented Dec 13, 2024

Forgive me! I meant to do glob at the start of load here:

c12/src/loader.ts

Lines 378 to 392 in 0542960

if (!existsSync(res.configFile!)) {
return res;
}
const configFileExt = extname(res.configFile!) || "";
if (configFileExt in ASYNC_LOADERS) {
const asyncLoader =
await ASYNC_LOADERS[configFileExt as keyof typeof ASYNC_LOADERS]();
const contents = await readFile(res.configFile!, "utf8");
res.config = asyncLoader(contents);
} else {
res.config = (await options.jiti!.import(res.configFile!, {
default: true,
})) as T;
}

(we're doing a few resolves, but maybe that is still less expensive)

Let me try and raise a draft PR as POC to what I mean!

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