Skip to content

Commit 8d3b176

Browse files
committed
feat: support loading .mjs config
1 parent c4c2cfd commit 8d3b176

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
],
3434
"no-console": "off",
3535
"node/no-unsupported-features/es-builtins": "error",
36-
"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],
36+
"node/no-unsupported-features/es-syntax": [
37+
"error",
38+
{ "ignores": ["dynamicImport", "modules"] }
39+
],
3740
"node/no-unsupported-features/node-builtins": "error",
3841
"prettier/prettier": "error"
3942
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ Starting with v3.1 you can now use different ways of configuring lint-staged:
106106
- `.lintstagedrc.json`
107107
- `.lintstagedrc.yaml`
108108
- `.lintstagedrc.yml`
109-
- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in JS format
109+
- `.lintstagedrc.mjs`
110+
- `.lintstagedrc.js`
111+
- `.lintstagedrc.cjs`
112+
- `lint-staged.config.mjs` file in ESM format
113+
- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in CommonJS format
110114
- Pass a configuration file using the `--config` or `-c` flag
111115
112116
See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config = {
77
testEnvironment: 'node',
88
transform: {
99
'\\.[jt]sx?$': 'babel-jest',
10+
'\\.mjs$': 'babel-jest',
1011
},
1112
/** Also transform ESM packages in `node_modules` */
1213
transformIgnorePatterns: [],

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ const loadConfig = (configPath) => {
3232
'.lintstagedrc.json',
3333
'.lintstagedrc.yaml',
3434
'.lintstagedrc.yml',
35+
'.lintstagedrc.mjs',
3536
'.lintstagedrc.js',
3637
'.lintstagedrc.cjs',
38+
'lint-staged.config.mjs',
3739
'lint-staged.config.js',
3840
'lint-staged.config.cjs',
3941
],
42+
loaders: {
43+
'.mjs': (path) => import(path).then((module) => module.default),
44+
},
4045
})
4146

4247
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()

test/__mocks__/esm-config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
'*': 'mytask',
3+
}

test/index.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ describe('lintStaged', () => {
193193
`)
194194
})
195195

196+
it('should read config from relative ESM file', async () => {
197+
expect.assertions(1)
198+
199+
await lintStaged(
200+
{
201+
configPath: path.join('test', '__mocks__', 'esm-config.mjs'),
202+
debug: true,
203+
quiet: true,
204+
},
205+
logger
206+
)
207+
208+
expect(logger.printHistory()).toMatchInlineSnapshot(`
209+
"
210+
LOG Running lint-staged with the following config:
211+
LOG {
212+
'*': 'mytask'
213+
}"
214+
`)
215+
})
216+
196217
it('should use config object', async () => {
197218
expect.assertions(1)
198219

0 commit comments

Comments
 (0)