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

Make TypeScript itself ESM-only, made possible by require(ESM) #58419

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
47 changes: 27 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,13 @@ jobs:
- windows-latest
- macos-14
node-version:
- '22'
- '20'
- '18'
- '16'
- '14'
- '23'
bundle:
- 'true'
include:
- node-version: 'lts/*'
- node-version: '23'
bundle: false
os: ubuntu-latest
exclude:
# No Node 14 on ARM macOS
- node-version: '14'
os: macos-14

runs-on: ${{ matrix.os }}
name: Test Node ${{ matrix.node-version }} on ${{ matrix.os }}${{ (!matrix.bundle && ' with --no-bundle') || '' }}
Expand All @@ -55,6 +47,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
check-latest: true

- run: npm ci

- name: Tests
Expand Down Expand Up @@ -83,7 +76,9 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true

- run: npm ci

- name: Run tests with coverage
Expand All @@ -108,7 +103,8 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true
- run: npm ci

- name: Linter
Expand All @@ -134,7 +130,8 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true
- run: npm ci

- uses: actions/cache@3624ceb22c1c5a301c8db4169662070a689d9ea8 # v4.1.1
Expand All @@ -154,7 +151,8 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true
- run: npm ci

- name: Installing browsers
Expand All @@ -170,7 +168,8 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true
- run: npm ci

- name: Build src
Expand All @@ -184,7 +183,9 @@ jobs:

- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true

- run: |
npm --version
# corepack enable npm
Expand Down Expand Up @@ -232,7 +233,8 @@ jobs:

- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true
- run: |
npm --version
# corepack enable npm
Expand Down Expand Up @@ -265,7 +267,8 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true
- run: npm ci

- name: Build scripts
Expand All @@ -281,7 +284,9 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true

- run: npm ci

- name: Build tsc
Expand All @@ -300,7 +305,9 @@ jobs:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 'lts/*'
node-version: '23'
check-latest: true

- run: npm ci

- name: Remove all baselines
Expand Down
8 changes: 6 additions & 2 deletions .gulp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const cp = require("child_process");
const path = require("path");
import cp from "child_process";
import path from "path";
import url from "url";

const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = path.dirname(__filename);

const argv = process.argv.slice(2);

Expand Down
122 changes: 39 additions & 83 deletions Herebyfile.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-check
import { CancelToken } from "@esfx/canceltoken";
import assert from "assert";
import chalk from "chalk";
import chokidar from "chokidar";
import esbuild from "esbuild";
Expand Down Expand Up @@ -172,25 +171,22 @@ async function runDtsBundler(entrypoint, output) {
* @param {BundlerTaskOptions} [taskOptions]
*
* @typedef BundlerTaskOptions
* @property {boolean} [exportIsTsObject]
* @property {boolean} [treeShaking]
* @property {boolean} [usePublicAPI]
* @property {() => void} [onWatchRebuild]
*/
function createBundler(entrypoint, outfile, taskOptions = {}) {
const getOptions = memoize(async () => {
const copyright = await getCopyrightHeader();
const banner = taskOptions.exportIsTsObject ? "var ts = {}; ((module) => {" : "";

/** @type {esbuild.BuildOptions} */
const options = {
entryPoints: [entrypoint],
banner: { js: copyright + banner },
banner: { js: copyright },
bundle: true,
outfile,
platform: "node",
target: ["es2020", "node14.17"],
format: "cjs",
format: "esm",
sourcemap: "linked",
sourcesContent: false,
treeShaking: taskOptions.treeShaking,
Expand All @@ -200,66 +196,17 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
};

if (taskOptions.usePublicAPI) {
options.external = ["./typescript.js"];
options.plugins = options.plugins || [];
options.plugins.push({
name: "remap-typescript-to-require",
name: "remap-typescript-to-public-api",
setup(build) {
build.onLoad({ filter: /src[\\/]typescript[\\/]typescript\.ts$/ }, () => {
return { contents: `export * from "./typescript.js"` };
build.onResolve({ filter: /^(?:\.\.[\\/])*typescript[\\/]typescript\.js$/ }, () => {
return { path: "./typescript.js", external: true };
});
},
});
}

if (taskOptions.exportIsTsObject) {
// Monaco bundles us as ESM by wrapping our code with something that defines module.exports
// but then does not use it, instead using the `ts` variable. Ensure that if we think we're CJS
// that we still set `ts` to the module.exports object.
options.footer = { js: `})({ get exports() { return ts; }, set exports(v) { ts = v; if (typeof module !== "undefined" && module.exports) { module.exports = v; } } })` };

// esbuild converts calls to "require" to "__require"; this function
// calls the real require if it exists, or throws if it does not (rather than
// throwing an error like "require not defined"). But, since we want typescript
// to be consumable by other bundlers, we need to convert these calls back to
// require so our imports are visible again.
//
// To fix this, we redefine "require" to a name we're unlikely to use with the
// same length as "require", then replace it back to "require" after bundling,
// ensuring that source maps still work.
//
// See: https://github.com/evanw/esbuild/issues/1905
const require = "require";
const fakeName = "Q".repeat(require.length);
const fakeNameRegExp = new RegExp(fakeName, "g");
options.define = { [require]: fakeName };

// For historical reasons, TypeScript does not set __esModule. Hack esbuild's __toCommonJS to be a noop.
// We reference `__copyProps` to ensure the final bundle doesn't have any unreferenced code.
const toCommonJsRegExp = /var __toCommonJS .*/;
const toCommonJsRegExpReplacement = "var __toCommonJS = (mod) => (__copyProps, mod); // Modified helper to skip setting __esModule.";

options.plugins = options.plugins || [];
options.plugins.push(
{
name: "post-process",
setup: build => {
build.onEnd(async () => {
let contents = await fs.promises.readFile(outfile, "utf-8");
contents = contents.replace(fakeNameRegExp, require);
let matches = 0;
contents = contents.replace(toCommonJsRegExp, () => {
matches++;
return toCommonJsRegExpReplacement;
});
assert(matches === 1, "Expected exactly one match for __toCommonJS");
await fs.promises.writeFile(outfile, contents);
});
},
},
);
}

return options;
});

Expand Down Expand Up @@ -305,6 +252,7 @@ let printedWatchWarning = false;
* @param {string} options.output
* @param {boolean} [options.enableCompileCache]
* @param {Task[]} [options.mainDeps]
* @param {boolean} [options.reexportDefault]
* @param {BundlerTaskOptions} [options.bundlerOptions]
*/
function entrypointBuildTask(options) {
Expand All @@ -329,13 +277,13 @@ function entrypointBuildTask(options) {
const moduleSpecifier = path.relative(outDir, output);
const lines = [
`// This file is a shim which defers loading the real module until the compile cache is enabled.`,
`try {`,
` const { enableCompileCache } = require("node:module");`,
` if (enableCompileCache) {`,
` enableCompileCache();`,
` }`,
`} catch {}`,
`module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}");`,
`import mod from "node:module";`,
`if (mod.enableCompileCache) {`,
` mod.enableCompileCache();`,
`}`,
`// Keep this synchronous so downstream people who required this file do not see TLA.`,
`const require = mod.createRequire(import.meta.url);`,
`require("./${moduleSpecifier.replace(/[\\/]/g, "/")}");`,
];
await fs.promises.writeFile(originalOutput, lines.join("\n") + "\n");
},
Expand All @@ -355,22 +303,33 @@ function entrypointBuildTask(options) {
});

/**
* Writes a CJS module that reexports another CJS file. E.g. given
* Writes a module that reexports another file. E.g. given
* `options.builtEntrypoint = "./built/local/tsc/tsc.js"` and
* `options.output = "./built/local/tsc.js"`, this will create a file
* named "./built/local/tsc.js" containing:
*
* ```
* module.exports = require("./tsc/tsc.js")
* export * from "./tsc/tsc.js";
* ```
*/
const shim = task({
name: `shim-${options.name}`,
run: async () => {
const outDir = path.dirname(output);
await fs.promises.mkdir(outDir, { recursive: true });
const moduleSpecifier = path.relative(outDir, options.builtEntrypoint);
await fs.promises.writeFile(output, `module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}")`);
const moduleSpecifier = path.relative(outDir, options.builtEntrypoint).replace(/[\\/]/g, "/");
const lines = [
`export * from "./${moduleSpecifier}";`,
];

if (options.reexportDefault) {
lines.push(
`import _default from "./${moduleSpecifier}";`,
`export default _default;`,
);
}

await fs.promises.writeFile(output, lines.join("\n") + "\n");
},
});

Expand Down Expand Up @@ -435,7 +394,7 @@ const { main: services, build: buildServices, watch: watchServices } = entrypoin
builtEntrypoint: "./built/local/typescript/typescript.js",
output: "./built/local/typescript.js",
mainDeps: [generateLibs],
bundlerOptions: { exportIsTsObject: true },
reexportDefault: true,
});
export { services, watchServices };

Expand Down Expand Up @@ -477,25 +436,22 @@ export const watchMin = task({
dependencies: [watchTsc, watchTsserver],
});

// This is technically not enough to make tsserverlibrary loadable in the
// browser, but it's unlikely that anyone has actually been doing that.
const lsslJs = `
if (typeof module !== "undefined" && module.exports) {
module.exports = require("./typescript.js");
}
else {
throw new Error("tsserverlibrary requires CommonJS; use typescript.js instead");
}
import ts from "./typescript.js";
export * from "./typescript.js";
export default ts;
`;

const lsslDts = `
import ts = require("./typescript.js");
export = ts;
import ts from "./typescript.js";
export * from "./typescript.js";
export default ts;
`;

const lsslDtsInternal = `
import ts = require("./typescript.internal.js");
export = ts;
import ts from "./typescript.internal.js";
export * from "./typescript.internal.js";
export default ts;
`;

/**
Expand Down Expand Up @@ -536,7 +492,7 @@ const { main: tests, watch: watchTests } = entrypointBuildTask({
description: "Builds the test infrastructure",
buildDeps: [generateDiagnostics],
project: "src/testRunner",
srcEntrypoint: "./src/testRunner/_namespaces/Harness.ts",
srcEntrypoint: "./src/testRunner/runner.ts",
builtEntrypoint: "./built/local/testRunner/runner.js",
output: testRunner,
mainDeps: [generateLibs],
Expand Down
2 changes: 1 addition & 1 deletion bin/tsc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../lib/tsc.js')
import '../lib/tsc.js';
2 changes: 1 addition & 1 deletion bin/tsserver
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../lib/tsserver.js')
import '../lib/tsserver.js';
Loading
Loading