Skip to content

Commit

Permalink
lazy-load dependencies to improve responsiveness when they aren't used (
Browse files Browse the repository at this point in the history
#1676)

* lazy-load diff and @cspotcode/source-map-support to improve responsiveness for use-cases that do not require them

* lint fix
  • Loading branch information
cspotcode committed Mar 5, 2022
1 parent 20cbbf5 commit f35a120
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions 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 {
Expand Down Expand Up @@ -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`;
Expand Down Expand Up @@ -544,7 +551,7 @@ function appendCompileAndEvalInput(options: {
);

// Use `diff` to check for new JavaScript to execute.
const changes = diffLines(
const changes = getDiffLines()(
oldOutputWithoutSourcemapComment,
outputWithoutSourcemapComment
);
Expand Down

0 comments on commit f35a120

Please sign in to comment.