Jamie Jamier
Jamie Jamier

Reputation: 539

monorepo Yarn workspaces - shared esLint and Prettier configs

How someone could share esLint/prettier config across Yarn workspaces(no NPM, Lerna and etc).

I try to keep things pretty high level but I have tried to create a separate package for esLint config and consume it in project-packages(by installing it and extending it), however I ran into two issues:

fictional-project
├── config-packages
│   ├── eslint-config
│   │   └── package.json
│   └── settings
│       └── package.json
├── package.json
├── project-packages
│   └── book
│       ├── book-function-as-a-service.js
│       └── package.json
└── src
    └── index.js

Upvotes: 12

Views: 10403

Answers (1)

user5602665
user5602665

Reputation:

You do indeed need eslint installed in the package which is using your shared config. It may work without it in some instances but you should be explicit with yarn so you can ensure the binary will always be present/symlinked. I am not certain about why eslint is not picking up your jest environment settings. Are you defining your eslint config in your package.json? I have a similar setup in my monorepo (Yarn v3.x). It may be that the your config package needs to be explicitly exporting the config in a module. Hopefully this helps you, assuming you haven't figured it out already.

.
├── libs
│   └── eslint-config-react
│       ├── index.js
│       └── package.json
│
└── packages
    └── some-package
        ├── .eslintrc.js
        └── package.json

./libs/eslint-config-react

The index.js is a typical eslint config and the package.json looks like this:

{
    "name": "@my-workspace/eslint-config-react",
    "main": "index.js",
    "dependencies": {
        "eslint": "7.32.0",
        "eslint-config-next": "11.1.3",
        "eslint-config-prettier": "8.3.0",
        "next": "11.1.3",
        "typescript": "4.4.4"
    }
}

./packages/some-package

package.json

{
    "name": "some-package",
    "scripts": {
        "lint": "eslint .",
    },
    "devDependencies": {
        "@my-workspace/eslint-config-react": "workspace:*",
        "eslint": "7.32.0",
    }
}

.eslintrc.js

module.exports = {
    extends: ['@my-workspace/eslint-config-react'],
};

Upvotes: 7

Related Questions