Skip to content

Commit a88f8cb

Browse files
authored
Support ESLint plugins with "type": "module" and main in package.json (#598)
* feat: add support for projects with "type": "module" in package.json * test: add test in package-json
1 parent c2a0ce1 commit a88f8cb

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/package-json.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export async function loadPlugin(path: string): Promise<Plugin> {
6262
}
6363
}
6464

65+
if (pluginPackageJson.type === 'module' && !exports) {
66+
pluginEntryPoint = pluginPackageJson.main;
67+
}
68+
6569
// If the ESM export doesn't exist, fall back to throwing the CJS error
6670
// (if the ESM export does exist, we'll validate it next)
6771
if (!pluginEntryPoint) {

test/lib/generate/package-json-test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,44 @@ describe('generate (package.json)', function () {
355355
});
356356
});
357357

358+
describe("plugin entry point with type: 'module' and main field specified", function () {
359+
beforeEach(function () {
360+
mockFs({
361+
'package.json': JSON.stringify({
362+
name: 'eslint-plugin-test',
363+
main: 'index.js',
364+
type: 'module',
365+
}),
366+
367+
'index.js': `export default {
368+
rules: {
369+
'no-foo': {
370+
meta: { docs: { description: 'disallow foo.' }, },
371+
create(context) {}
372+
},
373+
},
374+
};`,
375+
376+
'README.md':
377+
'<!-- begin auto-generated rules list --><!-- end auto-generated rules list -->',
378+
379+
'docs/rules/no-foo.md': '',
380+
381+
// Needed for some of the test infrastructure to work.
382+
node_modules: mockFs.load(PATH_NODE_MODULES),
383+
});
384+
});
385+
386+
afterEach(function () {
387+
mockFs.restore();
388+
jest.resetModules();
389+
});
390+
391+
it('finds the entry point', async function () {
392+
await expect(generate('.')).resolves.toBeUndefined();
393+
});
394+
});
395+
358396
describe('passing absolute path for plugin root', function () {
359397
beforeEach(function () {
360398
mockFs({

0 commit comments

Comments
 (0)