From f35a1201445b004723bff6c1445e012861bde2a3 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 4 Mar 2022 21:09:44 -0500 Subject: [PATCH] lazy-load dependencies to improve responsiveness when they aren't used (#1676) * lazy-load diff and @cspotcode/source-map-support to improve responsiveness for use-cases that do not require them * lint fix --- src/index.ts | 4 +++- src/repl.ts | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index ffb858101..719a3e88d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { Module } from 'module'; import * as util from 'util'; import { fileURLToPath } from 'url'; -import sourceMapSupport = require('@cspotcode/source-map-support'); +import type * as _sourceMapSupport from '@cspotcode/source-map-support'; import { BaseError } from 'make-error'; import type * as _ts from 'typescript'; @@ -793,6 +793,8 @@ export function create(rawOptions: CreateOptions = {}): Service { // Install source map support and read from memory cache. installSourceMapSupport(); function installSourceMapSupport() { + const sourceMapSupport = + require('@cspotcode/source-map-support') as typeof _sourceMapSupport; sourceMapSupport.install({ environment: 'node', retrieveFile(pathOrUrl: string) { diff --git a/src/repl.ts b/src/repl.ts index 41776e12e..c6371bdbb 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -1,4 +1,4 @@ -import { diffLines } from 'diff'; +import type * as _diff from 'diff'; import { homedir } from 'os'; import { join } from 'path'; import { @@ -26,6 +26,13 @@ function getProcessTopLevelAwait() { } return _processTopLevelAwait; } +let diff: typeof _diff; +function getDiffLines() { + if (diff === undefined) { + diff = require('diff'); + } + return diff.diffLines; +} /** @internal */ export const EVAL_FILENAME = `[eval].ts`; @@ -544,7 +551,7 @@ function appendCompileAndEvalInput(options: { ); // Use `diff` to check for new JavaScript to execute. - const changes = diffLines( + const changes = getDiffLines()( oldOutputWithoutSourcemapComment, outputWithoutSourcemapComment );