diff --git a/README.md b/README.md index ef4b702..47d9589 100644 --- a/README.md +++ b/README.md @@ -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`). diff --git a/src/loader.ts b/src/loader.ts index 6c36b3a..b87f9b7 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -72,20 +72,24 @@ export async function loadConfig< 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 = { config: {} as any, cwd: options.cwd, - configFile: resolve(options.cwd, options.configFile), + configFile: options.configFile + ? resolve(options.cwd, options.configFile) + : options.configFile, layers: [], }; @@ -109,14 +113,16 @@ export async function loadConfig< } // 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 @@ -185,7 +191,11 @@ export async function loadConfig< 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, @@ -303,6 +313,16 @@ async function resolveConfig< } } + if (options.configFile === false) { + return { + config: undefined as unknown as T, + configFile: undefined, + cwd: options.cwd, + source, + sourceOptions, + }; + } + // Custom merger const _merger = options.merger || defu; diff --git a/src/types.ts b/src/types.ts index 636f7bb..d9b35a1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -68,7 +68,7 @@ export interface ConfigLayer< sourceOptions?: SourceOptions; meta?: MT; cwd?: string; - configFile?: string; + configFile?: string | false; } export interface ResolvedConfig< @@ -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; diff --git a/src/watch.ts b/src/watch.ts index 689f4ab..c1bc236 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -66,15 +66,17 @@ export async function watchConfig< (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, + ), + ]) + : []), l.source && resolve(l.cwd!, l.source), // TODO: Support watching rc from home and workspace options.rcFile && diff --git a/test/loader.test.ts b/test/loader.test.ts index 1831941..4fd813d 100644 --- a/test/loader.test.ts +++ b/test/loader.test.ts @@ -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({ + 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",