nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
22.58k stars 2.25k forks source link

Support @nx/eslint:convert-to-flat-config with ESM syntax #19927

Open vneogi199 opened 8 months ago

vneogi199 commented 8 months ago

Description

Currently, @nx/eslint:convert-to-flat-config creates an eslint.config.js with CJS syntax (require). It should also allow user to create same file with ESM syntax (import/export).

Motivation

When source code is in ESM, it is better to have eslint.config.js also to be in ESM. It will help to use "type": "module" in package.json

Suggested Implementation

Convert require to import and module.exports to export default.

Alternate Implementations

meeroslav commented 7 months ago

Thank you @vneogi199 for the suggestion.

The early versions on flat config parser were failing with ESM syntax so we implemented it with CJS. We can look into this again, since there were several versions of eslint released in the meantime.

Balvajs commented 6 months ago

🔝 This. And I also noticed that if I migrate the eslint.config.js to ESM, the generators for new libraries stop working, because it expects module.exports to be present. https://github.com/nrwl/nx/blob/8f9ea6a8c55423f28d6a2159a572d0d90d0a4e13/packages/eslint/src/generators/utils/flat-config/ast-utils.ts#L352

TypeError: Cannot read properties of undefined (reading 'length')

The workaround is to set linter to none for generators, and then generate eslint-related code manually. That, or having to deal with renaming eslint.config.js to eslint.config.cjs.

johntimothybailey commented 2 weeks ago

Any updates on this? Seems like the support for Flat Config isn't really there yet and the announcement was a bit premature? Maybe? Would like to know if it is more of a "beta" feature support

yjaaidi commented 1 week ago

Once ESM is supported, it would be nice to have a CJS=>ESM migration. Should I open a distinct issue for that?