Skip to content

Commit f35a120

Browse files
authored
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
1 parent 20cbbf5 commit f35a120

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Module } from 'module';
33
import * as util from 'util';
44
import { fileURLToPath } from 'url';
55

6-
import sourceMapSupport = require('@cspotcode/source-map-support');
6+
import type * as _sourceMapSupport from '@cspotcode/source-map-support';
77
import { BaseError } from 'make-error';
88
import type * as _ts from 'typescript';
99

@@ -793,6 +793,8 @@ export function create(rawOptions: CreateOptions = {}): Service {
793793
// Install source map support and read from memory cache.
794794
installSourceMapSupport();
795795
function installSourceMapSupport() {
796+
const sourceMapSupport =
797+
require('@cspotcode/source-map-support') as typeof _sourceMapSupport;
796798
sourceMapSupport.install({
797799
environment: 'node',
798800
retrieveFile(pathOrUrl: string) {

src/repl.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { diffLines } from 'diff';
1+
import type * as _diff from 'diff';
22
import { homedir } from 'os';
33
import { join } from 'path';
44
import {
@@ -26,6 +26,13 @@ function getProcessTopLevelAwait() {
2626
}
2727
return _processTopLevelAwait;
2828
}
29+
let diff: typeof _diff;
30+
function getDiffLines() {
31+
if (diff === undefined) {
32+
diff = require('diff');
33+
}
34+
return diff.diffLines;
35+
}
2936

3037
/** @internal */
3138
export const EVAL_FILENAME = `[eval].ts`;
@@ -544,7 +551,7 @@ function appendCompileAndEvalInput(options: {
544551
);
545552

546553
// Use `diff` to check for new JavaScript to execute.
547-
const changes = diffLines(
554+
const changes = getDiffLines()(
548555
oldOutputWithoutSourcemapComment,
549556
outputWithoutSourcemapComment
550557
);

0 commit comments

Comments
 (0)