Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy-load dependencies to improve responsiveness when they aren't used #1676

Merged
merged 2 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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