From 8d3b176a7af75790efbcd1f63f73e7ef51f6b377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Sun, 14 Nov 2021 09:30:46 +0200 Subject: [PATCH] feat: support loading `.mjs` config --- .eslintrc.json | 5 ++++- README.md | 6 +++++- jest.config.js | 1 + lib/index.js | 5 +++++ test/__mocks__/esm-config.mjs | 3 +++ test/index.spec.js | 21 +++++++++++++++++++++ 6 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test/__mocks__/esm-config.mjs diff --git a/.eslintrc.json b/.eslintrc.json index 9b4f9aa7b..f04582226 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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" } diff --git a/README.md b/README.md index 9cca38044..d4452b61b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/jest.config.js b/jest.config.js index d05ef8a0a..b7568c554 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,6 +7,7 @@ const config = { testEnvironment: 'node', transform: { '\\.[jt]sx?$': 'babel-jest', + '\\.mjs$': 'babel-jest', }, /** Also transform ESM packages in `node_modules` */ transformIgnorePatterns: [], diff --git a/lib/index.js b/lib/index.js index 2241554d4..368d2ad80 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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() diff --git a/test/__mocks__/esm-config.mjs b/test/__mocks__/esm-config.mjs new file mode 100644 index 000000000..df7963d08 --- /dev/null +++ b/test/__mocks__/esm-config.mjs @@ -0,0 +1,3 @@ +export default { + '*': 'mytask', +} diff --git a/test/index.spec.js b/test/index.spec.js index f323826c4..12e1262dc 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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)