Skip to content

Commit

Permalink
feat: allow loading .js config file with ESM syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 21, 2021
1 parent bb0030a commit 410c3ba
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/loadConfig.js
Expand Up @@ -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)
Expand Down Expand Up @@ -29,6 +31,9 @@ export const loadConfig = (configPath) => {
'lint-staged.config.cjs',
],
loaders: {
'.cjs': dynamicImport,
'.js': dynamicImport,
'.json': jsonParse,
'.mjs': dynamicImport,
},
})
Expand Down
3 changes: 3 additions & 0 deletions test/__mocks__/esm-config-in-js.js
@@ -0,0 +1,3 @@
export default {
'*': 'mytask',
}
3 changes: 3 additions & 0 deletions test/__mocks__/my-config.cjs
@@ -0,0 +1,3 @@
module.exports = {
'*': 'mytask',
}
50 changes: 46 additions & 4 deletions test/index.spec.js
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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)

Expand Down

0 comments on commit 410c3ba

Please sign in to comment.