electron-eslint-plugin
This ESLint plugin is designed for Electron applications to enforce best practices and prevent common mistakes in Electron development, specifically targeting the separation of JavaScript contexts and window-specific code.
Features
-
Context Separation: Prevents importing renderer-specific code in the main process (
src/renderer/*
insrc/main/*
) and vice versa. -
Window-specific Imports: Restricts imports from other window contexts in
renderer/browsers/${currentBrowserPath}
. - Customizable Common Folders: Allows specifying common folders that can be shared between contexts.
Installation
Install electron-eslint-plugin
as a development dependency:
npm install electron-eslint-plugin --save-dev
Usage
Add electron-eslint-plugin
to the plugins section of your ESLint configuration. Then configure the rules you want to use under the rules section.
{
"plugins": ["electron-eslint-plugin"],
"rules": {
"electron-eslint-plugin/no-import-cross-js-context": "error"
}
}
Configuration
You can customize the common folders in your ESLint configuration:
{
"rules": {
"electron-eslint-plugin/no-import-cross-js-context": [
"error",
{
"commonFolders": [
"src/common",
"src/renderer/bridge",
"src/renderer/constant",
"src/renderer/util",
"src/renderer/assets"
]
}
]
}
}
Default Common Folders
src/common
src/renderer/{bridge,constant,util,assets}
Rules
-
no-cross-context-import: Prevents importing files from
src/renderer/*
insrc/main/*
and vice versa. -
no-external-window-import: Restricts importing code from files meant for other window contexts in
renderer/browsers/${currentBrowserPath}
.
Contributing
Contributions to electron-eslint-plugin
are welcome. Please ensure that your code adheres to the existing style and that all tests pass.
License
This project is licensed under the MIT License.