From c98a3d2913617bab8c52c8b942b2486578643456 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 1 May 2024 14:01:39 +0700 Subject: [PATCH] Require Node.js 18 and move to ESM --- .github/funding.yml | 4 -- .github/workflows/main.yml | 7 ++- index.d.ts | 115 ++++++++++++++++--------------------- index.js | 69 ++++++---------------- index.test-d.ts | 17 ------ package.json | 26 +++++---- preload.js | 1 - readme.md | 29 ++-------- test.js | 12 ++-- 9 files changed, 101 insertions(+), 179 deletions(-) delete mode 100644 .github/funding.yml delete mode 100644 index.test-d.ts delete mode 100644 preload.js diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 15edf6e..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,4 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -patreon: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bafbe5e..346585c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,10 +10,11 @@ jobs: fail-fast: false matrix: node-version: - - 14 + - 20 + - 18 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 78e020c..2cf0524 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,80 +1,67 @@ -import {BrowserWindow} from 'electron'; +import {type BrowserWindow} from 'electron'; -declare namespace electronDebug { - interface Options { - /** - Default: [Only in development](https://github.com/sindresorhus/electron-is-dev) - */ - readonly isEnabled?: boolean; - - /** - Show DevTools on each created `BrowserWindow`. - - @default true - */ - readonly showDevTools?: boolean; - - /** - The dock state to open DevTools in. - - @default 'previous' - */ - readonly devToolsMode?: - | 'undocked' - | 'right' - | 'bottom' - | 'previous' - | 'detach'; - } -} - -declare const electronDebug: { +export type Options = { /** - Install keyboard shortcuts and optionally activate DevTools on each created `BrowserWindow`. - - @example - ``` - import {app, BrowserWindow} from 'electron'; - import debug = require('electron-debug'); - - debug(); - - let mainWindow; - (async () => { - await app.whenReady(); - mainWindow = new BrowserWindow(); - }); - ``` + Default: [Only in development](https://github.com/sindresorhus/electron-is-dev) */ - (options?: electronDebug.Options): void; + readonly isEnabled?: boolean; /** - Reload the specified `BrowserWindow` instance or the focused one. + Show DevTools on each created `BrowserWindow`. - @param window - Default: `BrowserWindow.getFocusedWindow()` + @default true */ - refresh(window?: BrowserWindow): void; + readonly showDevTools?: boolean; /** - Toggle DevTools for the specified `BrowserWindow` instance or the focused one. + The dock state to open DevTools in. - @param window - Default: `BrowserWindow.getFocusedWindow()` + @default 'previous' */ - devTools(window?: BrowserWindow): void; + readonly devToolsMode?: + | 'undocked' + | 'right' + | 'bottom' + | 'previous' + | 'detach'; +}; - /** - Open DevTools for the specified `BrowserWindow` instance or the focused one. +/** +Install keyboard shortcuts and optionally activate DevTools on each created `BrowserWindow`. - @param window - Default: `BrowserWindow.getFocusedWindow()` - */ - openDevTools(window?: BrowserWindow): void; +@example +``` +import {app, BrowserWindow} from 'electron'; +import debug from 'electron-debug'; - /** - The absolute path to a preload script to use in [`session#setPreloads()`](https://www.electronjs.org/docs/api/session#sessetpreloadspreloads). +debug(); - Use it to enable `devtron` even when [`nodeIntegration`](https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions) is turned off. - */ - preloadScriptPath: string; -}; +let mainWindow; +(async () => { + await app.whenReady(); + mainWindow = new BrowserWindow(); +}); +``` +*/ +export default function debug(options?: Options): void; + +/** +Reload the specified `BrowserWindow` instance or the focused one. + +@param window - Default: `BrowserWindow.getFocusedWindow()` +*/ +export function refresh(window?: BrowserWindow): void; + +/** +Toggle DevTools for the specified `BrowserWindow` instance or the focused one. + +@param window - Default: `BrowserWindow.getFocusedWindow()` +*/ +export function developmentTools(window?: BrowserWindow): void; + +/** +Open DevTools for the specified `BrowserWindow` instance or the focused one. -export = electronDebug; +@param window - Default: `BrowserWindow.getFocusedWindow()` +*/ +export function openDevelopmentTools(window?: BrowserWindow): void; diff --git a/index.js b/index.js index 5f4608c..0b4ef3c 100644 --- a/index.js +++ b/index.js @@ -1,37 +1,38 @@ -'use strict'; -const {app, BrowserWindow, session} = require('electron'); -const path = require('path'); -const localShortcut = require('electron-localshortcut'); -const isDev = require('electron-is-dev'); +import process from 'node:process'; +import {app, BrowserWindow} from 'electron'; +import localShortcut from 'electron-localshortcut'; +import isDev from 'electron-is-dev'; const isMacOS = process.platform === 'darwin'; -const devToolsOptions = {}; +const developmentToolsOptions = {}; -function toggleDevTools(win = BrowserWindow.getFocusedWindow()) { +function toggleDevelopmentTools(win = BrowserWindow.getFocusedWindow()) { if (win) { const {webContents} = win; if (webContents.isDevToolsOpened()) { webContents.closeDevTools(); } else { - webContents.openDevTools(devToolsOptions); + webContents.openDevTools(developmentToolsOptions); } } } -function devTools(win = BrowserWindow.getFocusedWindow()) { +// eslint-disable-next-line unicorn/prevent-abbreviations +export function devTools(win = BrowserWindow.getFocusedWindow()) { if (win) { - toggleDevTools(win); + toggleDevelopmentTools(win); } } -function openDevTools(win = BrowserWindow.getFocusedWindow()) { +// eslint-disable-next-line unicorn/prevent-abbreviations +export function openDevTools(win = BrowserWindow.getFocusedWindow()) { if (win) { - win.webContents.openDevTools(devToolsOptions); + win.webContents.openDevTools(developmentToolsOptions); } } -function refresh(win = BrowserWindow.getFocusedWindow()) { +export function refresh(win = BrowserWindow.getFocusedWindow()) { if (win) { win.webContents.reloadIgnoringCache(); } @@ -53,37 +54,12 @@ function inspectElements() { } } -const addExtensionIfInstalled = (name, getPath) => { - const isExtensionInstalled = name => { - // For Electron >=9. - if (session.defaultSession.getAllExtensions) { - return {}.hasOwnProperty.call(session.defaultSession.getAllExtensions(), name); - } - - // TODO: Remove this when targeting Electron >=9. - return BrowserWindow.getDevToolsExtensions && - {}.hasOwnProperty.call(BrowserWindow.getDevToolsExtensions(), name); - }; - - try { - if (!isExtensionInstalled(name)) { - // For Electron >=9. - if (session.defaultSession.loadExtension) { - session.defaultSession.loadExtension(getPath(name)); - } else { - // TODO: Remove this when targeting Electron >=9. - BrowserWindow.addDevToolsExtension(getPath(name)); - } - } - } catch (_) {} -}; - -module.exports = options => { +export default function debug(options) { options = { isEnabled: null, showDevTools: true, devToolsMode: 'previous', - ...options + ...options, }; if (options.isEnabled === false || (options.isEnabled === null && !isDev)) { @@ -91,7 +67,7 @@ module.exports = options => { } if (options.devToolsMode !== 'previous') { - devToolsOptions.mode = options.devToolsMode; + developmentToolsOptions.mode = options.devToolsMode; } app.on('browser-window-created', (event, win) => { @@ -106,19 +82,10 @@ module.exports = options => { (async () => { await app.whenReady(); - addExtensionIfInstalled('devtron', name => require(name).path); - addExtensionIfInstalled('electron-react-devtools', name => require(name).path); - localShortcut.register('CommandOrControl+Shift+C', inspectElements); localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools); localShortcut.register('F12', devTools); - localShortcut.register('CommandOrControl+R', refresh); localShortcut.register('F5', refresh); })(); -}; - -module.exports.refresh = refresh; -module.exports.devTools = devTools; -module.exports.openDevTools = openDevTools; -module.exports.preloadScriptPath = path.join(__dirname, 'preload.js'); +} diff --git a/index.test-d.ts b/index.test-d.ts deleted file mode 100644 index 39d64d5..0000000 --- a/index.test-d.ts +++ /dev/null @@ -1,17 +0,0 @@ -/// -/// -import {expectType} from 'tsd'; -import {BrowserWindow} from 'electron'; -import electronDebug = require('.'); -import {refresh, devTools, openDevTools} from '.'; - -expectType( - electronDebug({ - isEnabled: true, - showDevTools: true - }) -); - -expectType(refresh(new BrowserWindow())); -expectType(devTools()); -expectType(openDevTools()); diff --git a/package.json b/package.json index 8d13ce1..f198413 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,22 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "sideEffects": false, + "engines": { + "node": ">=18" + }, "scripts": { "start": "electron test.js", - "test": "xo && tsd" + "test": "xo" }, "files": [ "index.js", - "index.d.ts", - "preload.js" + "index.d.ts" ], "keywords": [ "electron", @@ -29,16 +37,14 @@ "development" ], "dependencies": { - "electron-is-dev": "^1.1.0", - "electron-localshortcut": "^3.1.0" + "electron-is-dev": "^3.0.1", + "electron-localshortcut": "^3.2.1" }, "devDependencies": { - "@types/node": "^12.0.0", - "devtron": "^1.4.0", - "electron": "^5.0.1", + "@types/node": "^20.12.7", + "electron": "^30.0.1", "electron-react-devtools": "^0.5.3", - "tsd": "^0.7.2", - "xo": "^0.24.0" + "xo": "^0.58.0" }, "xo": { "envs": [ diff --git a/preload.js b/preload.js deleted file mode 100644 index 4e46d9f..0000000 --- a/preload.js +++ /dev/null @@ -1 +0,0 @@ -window.__devtron = {require, process}; diff --git a/readme.md b/readme.md index 89c1206..8e7bc97 100644 --- a/readme.md +++ b/readme.md @@ -28,27 +28,19 @@ Open DevTools and focus the Element Inspector tool. - Linux: Ctrl Shift C - Windows: Ctrl Shift C -### Activates DevTools extensions - -Just install any of these extension and they'll be activated for you: - -- [devtron](https://electronjs.org/devtron) - The official Electron DevTools extension - - You need to use [`preloadScriptPath`](#preloadScriptPath) if the `BrowserWindow`'s `nodeIntegration` is off. -- [electron-react-devtools](https://github.com/firejune/electron-react-devtools) - React DevTools extension for Electron - ## Install -``` -$ npm install electron-debug +```sh +npm install electron-debug ``` -*Requires Electron 5 or later.* +*Requires Electron 30 or later.* ## Usage ```js -const {app, BrowserWindow} = require('electron'); -const debug = require('electron-debug'); +import {app, BrowserWindow} from 'electron'; +import debug from 'electron-debug'; debug(); @@ -117,21 +109,12 @@ Open DevTools for the specified `BrowserWindow` instance or the focused one. Type: `BrowserWindow`\ Default: The focused `BrowserWindow` -### preloadScriptPath - -Type: `string` - -The absolute path to a preload script to use in [`session#setPreloads()`](https://www.electronjs.org/docs/api/session#sessetpreloadspreloads). - -Use it to enable `devtron` even when [`nodeIntegration`](https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions) is turned off. - ## Related - [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules -- [electron-store](https://github.com/sindresorhus/electron-store) - Save and load data like user preferences, app state, cache, etc +- [electron-store](https://github.com/sindresorhus/electron-store) - Save and load data like user settings, app state, cache, etc - [electron-context-menu](https://github.com/sindresorhus/electron-context-menu) - Context menu for your Electron app - [electron-dl](https://github.com/sindresorhus/electron-dl) - Simplified file downloads for your Electron app - [electron-unhandled](https://github.com/sindresorhus/electron-unhandled) - Catch unhandled errors and promise rejections in your Electron app - [electron-reloader](https://github.com/sindresorhus/electron-reloader) - Simple auto-reloading for Electron apps during development - [electron-serve](https://github.com/sindresorhus/electron-serve) - Static file serving for Electron apps -- [debug-menu](https://github.com/parro-it/debug-menu) - Chrome-like debug context-menu for Electron diff --git a/test.js b/test.js index 8526a74..02a6224 100644 --- a/test.js +++ b/test.js @@ -1,17 +1,17 @@ -'use strict'; -const electron = require('electron'); -const debug = require('.'); +import {app, BrowserWindow} from 'electron'; +import debug from './index.js'; debug(); const load = async url => { - const window_ = new electron.BrowserWindow({show: true}); + const window_ = new BrowserWindow({show: true}); await window_.loadURL(url); window_.removeMenu(); }; +// eslint-disable-next-line unicorn/prefer-top-level-await (async () => { - await electron.app.whenReady(); - await load(`file://${__dirname}/fixture.html`); + await app.whenReady(); + await load(`file://${import.meta.dirname}/fixture.html`); await load('https://google.com'); })();