From 1b17d2f9fefb53329838a16ff72436bd9bdcf220 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 4 Jan 2022 01:19:39 +0100 Subject: feat: Source mapping for stacktraces in dev mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have to cheat again and use require() to lazy load a dev dependency. Signed-off-by: Kristóf Marussy --- packages/main/esbuild.config.js | 2 +- packages/main/package.json | 4 +++- packages/main/src/devTools.ts | 20 ++++++++++++++++++-- packages/main/src/index.ts | 8 ++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) (limited to 'packages') diff --git a/packages/main/esbuild.config.js b/packages/main/esbuild.config.js index ef0aa71..996ec5a 100644 --- a/packages/main/esbuild.config.js +++ b/packages/main/esbuild.config.js @@ -7,7 +7,7 @@ import getEsbuildConfig from '../../config/getEsbuildConfig.js'; const externalPackages = ['electron']; if (process.env.MODE !== 'development') { - externalPackages.push('electron-devtools-installer'); + externalPackages.push('electron-devtools-installer', 'source-map-support'); } const gitInfo = getRepoInfo(); diff --git a/packages/main/package.json b/packages/main/package.json index c03e81e..9b87835 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -33,10 +33,12 @@ "@types/ms": "^0.7.31", "@types/node": "^17.0.12", "@types/slug": "^5", + "@types/source-map-support": "^0", "electron-devtools-installer": "^3.2.0", "esbuild": "^0.14.14", "git-repo-info": "^2.1.1", "jest": "^27.4.7", - "jest-mock": "^27.4.6" + "jest-mock": "^27.4.6", + "source-map-support": "^0.5.21" } } diff --git a/packages/main/src/devTools.ts b/packages/main/src/devTools.ts index 4ca1bf3..10f4545 100644 --- a/packages/main/src/devTools.ts +++ b/packages/main/src/devTools.ts @@ -34,6 +34,22 @@ export const DEVMODE_ALLOWED_URL_PREFIXES = [ 'https://clients2.googleusercontent.com/crx', ]; +/** + * Enables using source maps for node stack traces. + */ +export function enableStacktraceSourceMaps(): void { + const sourceMapSupport = + /* eslint-disable-next-line + import/no-extraneous-dependencies, + global-require, + @typescript-eslint/no-var-requires, + unicorn/prefer-module -- + Hack to lazily require a CJS module from an ES module transpiled into a CJS module. + */ + require('source-map-support') as typeof import('source-map-support'); + sourceMapSupport.install(); +} + /** * Installs the react and redux developer tools extensions. * @@ -41,7 +57,6 @@ export const DEVMODE_ALLOWED_URL_PREFIXES = [ * because the mobx-state-tree devtools are currently unmaintained. */ export async function installDevToolsExtensions(): Promise { - // Hack to lazily require a CJS module from an ES module transpiled into a CJS module. const { default: installExtension, REACT_DEVELOPER_TOOLS, @@ -50,7 +65,8 @@ export async function installDevToolsExtensions(): Promise { import/no-extraneous-dependencies, global-require, @typescript-eslint/no-var-requires, - unicorn/prefer-module + unicorn/prefer-module -- + Hack to lazily require a CJS module from an ES module transpiled into a CJS module. */ } = require('electron-devtools-installer') as typeof import('electron-devtools-installer'); await installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS], { diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index 2830fa7..ee89f49 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts @@ -41,6 +41,7 @@ import osName from 'os-name'; import { DEVMODE_ALLOWED_URL_PREFIXES, + enableStacktraceSourceMaps, installDevToolsExtensions, openDevToolsWhenReady, } from './devTools'; @@ -55,11 +56,14 @@ const log = getLogger('index'); // Always enable sandboxing. app.enableSandbox(); -// Use alternative directory when debugging to avoid clobbering the main installation. if (isDevelopment) { + // Use alternative directory when debugging to avoid clobbering the main installation. app.setPath('userData', `${app.getPath('userData')}-dev`); + ensureDirSync(app.getPath('userData')); + + // Use source maps in stack traces. + enableStacktraceSourceMaps(); } -ensureDirSync(app.getPath('userData')); // Only allow a single instance at a time. const isSingleInstance = app.requestSingleInstanceLock(); -- cgit v1.2.3-54-g00ecf