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 14, 2021
1 parent 37f4e75 commit 978aa9e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
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 978aa9e

Please sign in to comment.