diff --git a/lib/index.js b/lib/index.js index 368d2ad80..84f5c168f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ -import { cosmiconfig } from 'cosmiconfig' import debug from 'debug' import inspect from 'object-inspect' +import { loadConfig } from './loadConfig.js' import { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } from './messages.js' import { printTaskOutput } from './printTaskOutput.js' import { runAll } from './runAll.js' @@ -16,37 +16,6 @@ import { validateOptions } from './validateOptions.js' const debugLog = debug('lint-staged') -const resolveConfig = (configPath) => { - try { - return require.resolve(configPath) - } catch { - return configPath - } -} - -const loadConfig = (configPath) => { - const explorer = cosmiconfig('lint-staged', { - searchPlaces: [ - 'package.json', - '.lintstagedrc', - '.lintstagedrc.json', - '.lintstagedrc.yaml', - '.lintstagedrc.yml', - '.lintstagedrc.mjs', - '.lintstagedrc.js', - '.lintstagedrc.cjs', - 'lint-staged.config.mjs', - 'lint-staged.config.js', - 'lint-staged.config.cjs', - ], - loaders: { - '.mjs': (path) => import(path).then((module) => module.default), - }, - }) - - return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search() -} - /** * @typedef {(...any) => void} LogFunction * @typedef {{ error: LogFunction, log: LogFunction, warn: LogFunction }} Logger diff --git a/lib/loadConfig.js b/lib/loadConfig.js new file mode 100644 index 000000000..a2762851b --- /dev/null +++ b/lib/loadConfig.js @@ -0,0 +1,37 @@ +import { cosmiconfig } from 'cosmiconfig' + +const dynamicImport = (path) => import(path).then((module) => module.default) + +const resolveConfig = (configPath) => { + try { + return require.resolve(configPath) + } catch { + return configPath + } +} + +/** + * @param {string} [configPath] + */ +export const loadConfig = (configPath) => { + const explorer = cosmiconfig('lint-staged', { + searchPlaces: [ + 'package.json', + '.lintstagedrc', + '.lintstagedrc.json', + '.lintstagedrc.yaml', + '.lintstagedrc.yml', + '.lintstagedrc.mjs', + '.lintstagedrc.js', + '.lintstagedrc.cjs', + 'lint-staged.config.mjs', + 'lint-staged.config.js', + 'lint-staged.config.cjs', + ], + loaders: { + '.mjs': dynamicImport, + }, + }) + + return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search() +}