Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ Configuration base name. The default is `config`.

Configuration file name without extension. Default is generated from `name` (f.e., if `name` is `foo`, the config file will be => `foo.config`).

Set to `false` to avoid loading the config file.

### `rcFile`

RC Config file name. Default is generated from `name` (name=foo => `.foorc`).
Expand Down
54 changes: 37 additions & 17 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,24 @@
const _merger = options.merger || defu;

// Create jiti instance
options.jiti =
options.jiti ||
createJiti(join(options.cwd, options.configFile), {
interopDefault: true,
moduleCache: false,
extensions: [...SUPPORTED_EXTENSIONS],
...options.jitiOptions,
});
if (options.configFile) {
options.jiti =
options.jiti ||
createJiti(join(options.cwd, options.configFile), {
interopDefault: true,
moduleCache: false,
extensions: [...SUPPORTED_EXTENSIONS],
...options.jitiOptions,
});
}

// Create context
const r: ResolvedConfig<T, MT> = {
config: {} as any,
cwd: options.cwd,
configFile: resolve(options.cwd, options.configFile),
configFile: options.configFile
? resolve(options.cwd, options.configFile)
: options.configFile,
layers: [],
};

Expand All @@ -109,14 +113,16 @@
}

// Load main config file
const _mainConfig = await resolveConfig(".", options);
if (_mainConfig.configFile) {
_configs.main = _mainConfig.config;
r.configFile = _mainConfig.configFile;
}
if (options.configFile) {
const _mainConfig = await resolveConfig(".", options);
if (_mainConfig.configFile) {
_configs.main = _mainConfig.config;
r.configFile = _mainConfig.configFile;
}

if (_mainConfig.meta) {
r.meta = _mainConfig.meta;
if (_mainConfig.meta) {
r.meta = _mainConfig.meta;
}
}

// Load rc files
Expand Down Expand Up @@ -185,7 +191,11 @@
configFile: undefined,
cwd: undefined,
},
{ config: configs.main, configFile: options.configFile, cwd: options.cwd },
configs.main && {
config: configs.main,
configFile: options.configFile,
cwd: options.cwd,
},
configs.rc && { config: configs.rc, configFile: options.rcFile },
configs.packageJson && {
config: configs.packageJson,
Expand Down Expand Up @@ -303,6 +313,16 @@
}
}

if (options.configFile === false) {
return {
config: undefined as unknown as T,
configFile: undefined,
cwd: options.cwd,
source,
sourceOptions,
};

Check warning on line 323 in src/loader.ts

View check run for this annotation

Codecov / codecov/patch

src/loader.ts#L317-L323

Added lines #L317 - L323 were not covered by tests
}

// Custom merger
const _merger = options.merger || defu;

Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ConfigLayer<
sourceOptions?: SourceOptions<T, MT>;
meta?: MT;
cwd?: string;
configFile?: string;
configFile?: string | false;
}

export interface ResolvedConfig<
Expand Down Expand Up @@ -101,9 +101,9 @@ export interface LoadConfigOptions<
name?: string;
cwd?: string;

configFile?: string;
configFile?: string | false;

rcFile?: false | string;
rcFile?: string | false;
globalRc?: boolean;

dotenv?: boolean | DotenvOptions;
Expand Down
20 changes: 11 additions & 9 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@
(config.layers || [])
.filter((l) => l.cwd)
.flatMap((l) => [
...SUPPORTED_EXTENSIONS.flatMap((ext) => [
resolve(l.cwd!, configFileName + ext),
resolve(l.cwd!, ".config", configFileName + ext),
resolve(
l.cwd!,
".config",
configFileName.replace(/\.config$/, "") + ext,
),
]),
...(configFileName
? SUPPORTED_EXTENSIONS.flatMap((ext) => [
resolve(l.cwd!, configFileName + ext),
resolve(l.cwd!, ".config", configFileName + ext),
resolve(
l.cwd!,
".config",
configFileName.replace(/\.config$/, "") + ext,
),
])
: []),

Check warning on line 79 in src/watch.ts

View check run for this annotation

Codecov / codecov/patch

src/watch.ts#L69-L79

Added lines #L69 - L79 were not covered by tests
l.source && resolve(l.cwd!, l.source),
// TODO: Support watching rc from home and workspace
options.rcFile &&
Expand Down
76 changes: 76 additions & 0 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,82 @@ describe("loader", () => {
`);
});

it("load fixture config with `configFile` disabled", async () => {
type UserConfig = Partial<{
virtual: boolean;
overridden: boolean;
enableDefault: boolean;
defaultConfig: boolean;
extends: string[];
}>;
const { config, layers } = await loadConfig<UserConfig>({
cwd: r("./fixture"),
name: "test",
dotenv: true,
packageJson: ["c12", "c12-alt"],
globalRc: true,
envName: "test",
configFile: false,
extend: {
extendKey: ["theme", "extends"],
},
resolve: (id) => {
if (id === "virtual") {
return { config: { virtual: true } };
}
},
overrides: {
overridden: true,
},
defaults: {
defaultConfig: true,
},
defaultConfig: ({ configs }) => {
if (configs?.main?.enableDefault) {
return Promise.resolve({
extends: ["virtual"],
});
}
return {};
},
});

expect(transformPaths(config!)).toMatchInlineSnapshot(`
{
"defaultConfig": true,
"overridden": true,
"packageJSON": true,
"packageJSON2": true,
"rcFile": true,
"testConfig": true,
}
`);

expect(transformPaths(layers!)).toMatchInlineSnapshot(`
[
{
"config": {
"overridden": true,
},
},
{
"config": {
"rcFile": true,
"testConfig": true,
},
"configFile": ".testrc",
},
{
"config": {
"packageJSON": true,
"packageJSON2": true,
},
"configFile": "package.json",
},
]
`);
});

it("extend from git repo", async () => {
const { config } = await loadConfig({
name: "test",
Expand Down