diff --git a/README.md b/README.md index d4452b61b..dd40853e4 100644 --- a/README.md +++ b/README.md @@ -106,11 +106,12 @@ Starting with v3.1 you can now use different ways of configuring lint-staged: - `.lintstagedrc.json` - `.lintstagedrc.yaml` - `.lintstagedrc.yml` - - `.lintstagedrc.mjs` - - `.lintstagedrc.js` - - `.lintstagedrc.cjs` -- `lint-staged.config.mjs` file in ESM format -- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in CommonJS format +- `.lintstagedrc.mjs` or `lint-staged.config.mjs` file in ESM format + - the default export value should be a configuration: `export default { ... }` +- `.lintstagedrc.cjs` or `lint-staged.config.cjs` file in CommonJS format + - the exports value should be a configuration: `module.exports = { ... }` +- `lint-staged.config.js` or `.lintstagedrc.js` in either ESM or CommonJS format, depending on + whether your project's _package.json_ contains the `"type": "module"` option or not. - Pass a configuration file using the `--config` or `-c` flag See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported. diff --git a/lib/loadConfig.js b/lib/loadConfig.js index a2762851b..e79c4b339 100644 --- a/lib/loadConfig.js +++ b/lib/loadConfig.js @@ -2,6 +2,8 @@ import { cosmiconfig } from 'cosmiconfig' const dynamicImport = (path) => import(path).then((module) => module.default) +const jsonParse = (path, content) => JSON.parse(content) + const resolveConfig = (configPath) => { try { return require.resolve(configPath) @@ -29,6 +31,9 @@ export const loadConfig = (configPath) => { 'lint-staged.config.cjs', ], loaders: { + '.cjs': dynamicImport, + '.js': dynamicImport, + '.json': jsonParse, '.mjs': dynamicImport, }, }) diff --git a/test/__mocks__/esm-config-in-js.js b/test/__mocks__/esm-config-in-js.js new file mode 100644 index 000000000..df7963d08 --- /dev/null +++ b/test/__mocks__/esm-config-in-js.js @@ -0,0 +1,3 @@ +export default { + '*': 'mytask', +} diff --git a/test/__mocks__/my-config.cjs b/test/__mocks__/my-config.cjs new file mode 100644 index 000000000..c718f9bae --- /dev/null +++ b/test/__mocks__/my-config.cjs @@ -0,0 +1,3 @@ +module.exports = { + '*': 'mytask', +} diff --git a/test/index.spec.js b/test/index.spec.js index 12e1262dc..19979233c 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -128,7 +128,7 @@ describe('lintStaged', () => { expect(logger.printHistory()).toMatchInlineSnapshot(`""`) }) - it('should load config file when specified', async () => { + it('should load JSON config file', async () => { expect.assertions(1) await lintStaged( @@ -149,7 +149,7 @@ describe('lintStaged', () => { `) }) - it('should parse function linter from absolute CJS config', async () => { + it('should load CommonJS config file from absolute path', async () => { expect.assertions(1) await lintStaged( @@ -171,7 +171,7 @@ describe('lintStaged', () => { `) }) - it('should parse function linter from relative CJS config', async () => { + it('should load CommonJS config file from relative path', async () => { expect.assertions(1) await lintStaged( @@ -193,7 +193,28 @@ describe('lintStaged', () => { `) }) - it('should read config from relative ESM file', async () => { + it('should load CommonJS config file from .cjs file', async () => { + expect.assertions(1) + + await lintStaged( + { + configPath: path.join('test', '__mocks__', 'my-config.cjs'), + debug: true, + quiet: true, + }, + logger + ) + + expect(logger.printHistory()).toMatchInlineSnapshot(` + " + LOG Running lint-staged with the following config: + LOG { + '*': 'mytask' + }" + `) + }) + + it('should load EMS config file from .mjs file', async () => { expect.assertions(1) await lintStaged( @@ -214,6 +235,27 @@ describe('lintStaged', () => { `) }) + it('should load EMS config file from .js file', async () => { + expect.assertions(1) + + await lintStaged( + { + configPath: path.join('test', '__mocks__', 'esm-config-in-js.js'), + debug: true, + quiet: true, + }, + logger + ) + + expect(logger.printHistory()).toMatchInlineSnapshot(` + " + LOG Running lint-staged with the following config: + LOG { + '*': 'mytask' + }" + `) + }) + it('should use config object', async () => { expect.assertions(1)