Skip to content

Commit

Permalink
fix: add cjs override for esm projects (fixes #59)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Jun 2, 2023
1 parent 6c32b32 commit 8c285e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ function processAnswers(answers) {
config.parserOptions.ecmaVersion = "latest";
config.env.es2021 = true;

if (answers.moduleType === "esm" && answers.format === "JavaScript") {
config.overrides.push({
files: ["**/*.cjs"],
parserOptions: { sourceType: "script" },
env: {
node: true
}
});
}

// set the module type
if (answers.moduleType === "esm") {
config.parserOptions.sourceType = "module";
Expand Down
12 changes: 12 additions & 0 deletions tests/init/config-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ describe("configInitializer", () => {

assert.isTrue(config.env.commonjs);
});

it("should set commonjs config for `.eslintrc.cjs` in esm projects", () => {
answers.moduleType = "esm";
answers.format = "JavaScript";
const config = init.processAnswers(answers);

assert.isArray(config.overrides, "should have overrides config");
assert.strictEqual(config.overrides.length, 1);
assert.deepStrictEqual(config.overrides[0].parserOptions, { sourceType: "script" });
assert.deepStrictEqual(config.overrides[0].env, { node: true });
});

});

describe("guide", () => {
Expand Down

0 comments on commit 8c285e1

Please sign in to comment.