Convert require calls to imports

Although the existing require calls would work fine, it's more
consistent to make them actual imports and let them be converted at
compile/bundle time. It also lets us emit ESM if needed.

There are still conditional requires present for things like the node
System, but that's a more difficult problem to solve.
This commit is contained in:
Jake Bailey
2022-05-26 17:00:27 -07:00
parent d12116d8da
commit 01e5200096
8 changed files with 23 additions and 33 deletions

View File

@@ -1,6 +1,6 @@
/// <reference types="node"/>
import fs = require("fs");
import * as fs from "fs";
interface ServerCancellationToken {
isCancellationRequested(): boolean;

View File

@@ -1,5 +1,5 @@
const { join, resolve, dirname } = require("path") as typeof import("path");
const { existsSync } = require("fs") as typeof import("fs");
import { existsSync } from "fs";
import { dirname, join, resolve } from "path";
// search directories upward to avoid hard-wired paths based on the
// build tree (same as scripts/build/findUpDir.js)

View File

@@ -1,19 +1,17 @@
import * as ts from "./_namespaces/ts";
import * as chai from "chai";
// Block scoped definitions work poorly for global variables, temporarily enable var
/* eslint-disable no-var */
import * as ts from "./_namespaces/ts";
// this will work in the browser via browserify
declare global {
// Module transform: converted from ambient declaration
var assert: typeof _chai.assert;
var assert: typeof chai.assert; // eslint-disable-line no-var
}
declare global {
// Module transform: converted from ambient declaration
var expect: typeof _chai.expect;
var expect: typeof chai.expect; // eslint-disable-line no-var
}
var _chai: typeof import("chai") = require("chai");
globalThis.assert = _chai.assert;
globalThis.assert = chai.assert;
{
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
@@ -39,7 +37,7 @@ globalThis.assert = _chai.assert;
}
};
}
globalThis.expect = _chai.expect;
/* eslint-enable no-var */
globalThis.expect = chai.expect;
// empty ts namespace so this file is included in the `ts.ts` namespace file generated by the module swapover
// This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation
// This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation

View File

@@ -1,10 +1,10 @@
import * as del from "del";
import * as fs from "fs";
import * as path from "path";
import * as ts from "./_namespaces/ts";
import { Baseline, IO, isWorker, RunnerBase, TestRunnerKind } from "./_namespaces/Harness";
const fs: typeof import("fs") = require("fs");
const path: typeof import("path") = require("path");
const del: typeof import("del") = require("del");
interface ExecResult {
stdout: Buffer;
stderr: Buffer;

View File

@@ -1,7 +1,7 @@
import { expect } from "chai";
import * as ts from "../../_namespaces/ts";
const _chai: typeof import("chai") = require("chai");
const expect: typeof _chai.expect = _chai.expect;
describe("unittests:: services:: languageService", () => {
const files: {[index: string]: string} = {
"foo.ts": `import Vue from "./vue";

View File

@@ -1,9 +1,9 @@
import { expect } from "chai";
import * as ts from "../../_namespaces/ts";
import * as Harness from "../../_namespaces/Harness";
import * as Utils from "../../_namespaces/Utils";
const _chai: typeof import("chai") = require("chai");
const expect: typeof _chai.expect = _chai.expect;
let lastWrittenToHost: string;
const noopFileWatcher: ts.FileWatcher = { close: ts.noop };
const mockHost: ts.server.ServerHost = {

View File

@@ -1,3 +1,6 @@
import * as fs from "fs";
import * as path from "path";
import {
installNpmPackages, Log, RequestCompletedAction, TypingsInstaller,
} from "./_namespaces/ts.server.typingsInstaller";
@@ -11,16 +14,6 @@ import {
MapLike, normalizeSlashes, stringContains, sys, toPath, version,
} from "./_namespaces/ts";
const fs: {
appendFileSync(file: string, content: string): void
} = require("fs");
const path: {
join(...parts: string[]): string;
dirname(path: string): string;
basename(path: string, extension?: string): string;
} = require("path");
class FileLog implements Log {
constructor(private logFile: string | undefined) {
}

View File

@@ -1,10 +1,9 @@
/// <reference types="node" />
import * as fs from "fs";
if (process.argv.length < 3) {
process.exit(1);
}
const directoryName = process.argv[2];
const fs: { watch(directoryName: string, options: any, callback: () => {}): any } = require("fs");
// main reason why we need separate process to check if it is safe to watch some path
// is to guard against crashes that cannot be intercepted with protected blocks and
// code in tsserver already can handle normal cases, like non-existing folders.