Skip to content

Commit

Permalink
feat: support loading .mjs config
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 21, 2021
1 parent c4c2cfd commit 8d3b176
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Expand Up @@ -33,7 +33,10 @@
],
"no-console": "off",
"node/no-unsupported-features/es-builtins": "error",
"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],
"node/no-unsupported-features/es-syntax": [
"error",
{ "ignores": ["dynamicImport", "modules"] }
],
"node/no-unsupported-features/node-builtins": "error",
"prettier/prettier": "error"
}
Expand Down
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -106,7 +106,11 @@ Starting with v3.1 you can now use different ways of configuring lint-staged:
- `.lintstagedrc.json`
- `.lintstagedrc.yaml`
- `.lintstagedrc.yml`
- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in JS format
- `.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
- 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
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -7,6 +7,7 @@ const config = {
testEnvironment: 'node',
transform: {
'\\.[jt]sx?$': 'babel-jest',
'\\.mjs$': 'babel-jest',
},
/** Also transform ESM packages in `node_modules` */
transformIgnorePatterns: [],
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Expand Up @@ -32,11 +32,16 @@ const loadConfig = (configPath) => {
'.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()
Expand Down
3 changes: 3 additions & 0 deletions test/__mocks__/esm-config.mjs
@@ -0,0 +1,3 @@
export default {
'*': 'mytask',
}
21 changes: 21 additions & 0 deletions test/index.spec.js
Expand Up @@ -193,6 +193,27 @@ describe('lintStaged', () => {
`)
})

it('should read config from relative ESM file', async () => {
expect.assertions(1)

await lintStaged(
{
configPath: path.join('test', '__mocks__', 'esm-config.mjs'),
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 8d3b176

Please sign in to comment.