From d25da73ed9e7e32feaaa98950db2362411856e00 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 9 Nov 2022 20:39:49 -0800 Subject: [PATCH 1/5] Add an export default to our libraries --- Herebyfile.mjs | 4 ++-- scripts/dtsBundler.mjs | 4 ++++ src/tsserverlibrary/tsserverlibrary.ts | 2 ++ src/typescript/typescript.ts | 2 ++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 9a4fff3df2756..123ac4b43be3e 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -389,7 +389,7 @@ export const dtsServices = task({ description: "Bundles typescript.d.ts", dependencies: [buildServices], run: async () => { - if (needsUpdate("./built/local/typescript/tsconfig.tsbuildinfo", ["./built/local/typescript.d.ts", "./built/local/typescript.internal.d.ts"])) { + if (needsUpdate(["./built/local/typescript/tsconfig.tsbuildinfo", "./scripts/dtsBundler.mjs"], ["./built/local/typescript.d.ts", "./built/local/typescript.internal.d.ts"])) { await runDtsBundler("./built/local/typescript/typescript.d.ts", "./built/local/typescript.d.ts"); } }, @@ -452,7 +452,7 @@ export const dtsLssl = task({ description: "Bundles tsserverlibrary.d.ts", dependencies: [buildLssl], run: async () => { - if (needsUpdate("./built/local/tsserverlibrary/tsconfig.tsbuildinfo", ["./built/local/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.internal.d.ts"])) { + if (needsUpdate(["./built/local/tsserverlibrary/tsconfig.tsbuildinfo", "./scripts/dtsBundler.mjs"], ["./built/local/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.internal.d.ts"])) { await runDtsBundler("./built/local/tsserverlibrary/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.d.ts"); } } diff --git a/scripts/dtsBundler.mjs b/scripts/dtsBundler.mjs index fc17bbaebd09b..d3d0085d00288 100644 --- a/scripts/dtsBundler.mjs +++ b/scripts/dtsBundler.mjs @@ -331,6 +331,10 @@ function verifyMatchingSymbols(decl) { * @param {ts.Symbol} moduleSymbol */ function emitAsNamespace(name, moduleSymbol) { + if (name === "default") { + return; + } + assert(moduleSymbol.flags & ts.SymbolFlags.ValueModule, "moduleSymbol is not a module"); scopeStack.push(new Map()); diff --git a/src/tsserverlibrary/tsserverlibrary.ts b/src/tsserverlibrary/tsserverlibrary.ts index caa2e6b1dba33..8d7e4a9d70630 100644 --- a/src/tsserverlibrary/tsserverlibrary.ts +++ b/src/tsserverlibrary/tsserverlibrary.ts @@ -1 +1,3 @@ +import * as ts from "./_namespaces/ts"; export * from "./_namespaces/ts"; +export default ts; diff --git a/src/typescript/typescript.ts b/src/typescript/typescript.ts index 8a5136a1eb337..cee44bbd2d584 100644 --- a/src/typescript/typescript.ts +++ b/src/typescript/typescript.ts @@ -15,4 +15,6 @@ if (typeof console !== "undefined") { }; } +import * as ts from "./_namespaces/ts"; export * from "./_namespaces/ts"; +export default ts; From 2a99bd649e0c9f0ba81c54a80e92c8a2a59f1a35 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 10 Nov 2022 12:24:16 -0800 Subject: [PATCH 2/5] Remove use of __toCommonJS for as workaround --- Herebyfile.mjs | 18 ++++++++++++++++-- src/tsserverlibrary/tsserverlibrary.ts | 2 -- src/typescript/typescript.ts | 2 -- src/typingsInstaller/nodeTypingsInstaller.ts | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 123ac4b43be3e..ddd29abecaba9 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -237,9 +237,10 @@ function createBundler(entrypoint, outfile, taskOptions = {}) { } }, { - name: "fix-require", + name: "output-compat-fixups", setup: (build) => { build.onEnd(async () => { + let contents = await fs.promises.readFile(outfile, "utf-8"); // 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 @@ -250,8 +251,21 @@ function createBundler(entrypoint, outfile, taskOptions = {}) { // source maps working (though this only really matters for the line the require is on). // // See: https://github.com/evanw/esbuild/issues/1905 - let contents = await fs.promises.readFile(outfile, "utf-8"); contents = contents.replace(/__require\(/g, " require("); + + // Pre-modules, our CJS export looked like this: + // + // var ts; + // (function(ts) { ... })(ts || (ts = {})); + // module.exports = ts; + // + // This means that __esModule was not set in our code. But, esbuild will create this + // property on our new output object. This causes runtime behavior differences when + // paired with __importDefault. + // + // For compatibility, just skip this step. + contents = contents.replace(/__toCommonJS\((\w+)\)/g, "$1"); + await fs.promises.writeFile(outfile, contents); }); }, diff --git a/src/tsserverlibrary/tsserverlibrary.ts b/src/tsserverlibrary/tsserverlibrary.ts index 8d7e4a9d70630..caa2e6b1dba33 100644 --- a/src/tsserverlibrary/tsserverlibrary.ts +++ b/src/tsserverlibrary/tsserverlibrary.ts @@ -1,3 +1 @@ -import * as ts from "./_namespaces/ts"; export * from "./_namespaces/ts"; -export default ts; diff --git a/src/typescript/typescript.ts b/src/typescript/typescript.ts index cee44bbd2d584..8a5136a1eb337 100644 --- a/src/typescript/typescript.ts +++ b/src/typescript/typescript.ts @@ -15,6 +15,4 @@ if (typeof console !== "undefined") { }; } -import * as ts from "./_namespaces/ts"; export * from "./_namespaces/ts"; -export default ts; diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 6b996af1fdafd..780c826accb19 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -81,7 +81,7 @@ interface ExecSyncOptions { } type ExecSync = (command: string, options: ExecSyncOptions) => string; -export class NodeTypingsInstaller extends TypingsInstaller { +class NodeTypingsInstaller extends TypingsInstaller { private readonly nodeExecSync: ExecSync; private readonly npmPath: string; readonly typesRegistry: Map>; From fe4ecf517b3a80cbe33b4c3e48242fd2ab3c1813 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 10 Nov 2022 12:37:25 -0800 Subject: [PATCH 3/5] Use export = --- Herebyfile.mjs | 18 ++---------------- src/tsserverlibrary/tsserverlibrary.ts | 3 ++- src/typescript/typescript.ts | 3 ++- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index ddd29abecaba9..123ac4b43be3e 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -237,10 +237,9 @@ function createBundler(entrypoint, outfile, taskOptions = {}) { } }, { - name: "output-compat-fixups", + name: "fix-require", setup: (build) => { build.onEnd(async () => { - let contents = await fs.promises.readFile(outfile, "utf-8"); // 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 @@ -251,21 +250,8 @@ function createBundler(entrypoint, outfile, taskOptions = {}) { // source maps working (though this only really matters for the line the require is on). // // See: https://github.com/evanw/esbuild/issues/1905 + let contents = await fs.promises.readFile(outfile, "utf-8"); contents = contents.replace(/__require\(/g, " require("); - - // Pre-modules, our CJS export looked like this: - // - // var ts; - // (function(ts) { ... })(ts || (ts = {})); - // module.exports = ts; - // - // This means that __esModule was not set in our code. But, esbuild will create this - // property on our new output object. This causes runtime behavior differences when - // paired with __importDefault. - // - // For compatibility, just skip this step. - contents = contents.replace(/__toCommonJS\((\w+)\)/g, "$1"); - await fs.promises.writeFile(outfile, contents); }); }, diff --git a/src/tsserverlibrary/tsserverlibrary.ts b/src/tsserverlibrary/tsserverlibrary.ts index caa2e6b1dba33..6d2ea4f2b682b 100644 --- a/src/tsserverlibrary/tsserverlibrary.ts +++ b/src/tsserverlibrary/tsserverlibrary.ts @@ -1 +1,2 @@ -export * from "./_namespaces/ts"; +import * as ts from "./_namespaces/ts"; +export = ts; diff --git a/src/typescript/typescript.ts b/src/typescript/typescript.ts index 8a5136a1eb337..2b6f76c71d418 100644 --- a/src/typescript/typescript.ts +++ b/src/typescript/typescript.ts @@ -15,4 +15,5 @@ if (typeof console !== "undefined") { }; } -export * from "./_namespaces/ts"; +import * as ts from "./_namespaces/ts"; +export = ts; From 3fc3890af0fd448ac8038240c06269ea02c952d5 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 10 Nov 2022 12:40:03 -0800 Subject: [PATCH 4/5] Revert other changes --- Herebyfile.mjs | 4 ++-- scripts/dtsBundler.mjs | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 123ac4b43be3e..9a4fff3df2756 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -389,7 +389,7 @@ export const dtsServices = task({ description: "Bundles typescript.d.ts", dependencies: [buildServices], run: async () => { - if (needsUpdate(["./built/local/typescript/tsconfig.tsbuildinfo", "./scripts/dtsBundler.mjs"], ["./built/local/typescript.d.ts", "./built/local/typescript.internal.d.ts"])) { + if (needsUpdate("./built/local/typescript/tsconfig.tsbuildinfo", ["./built/local/typescript.d.ts", "./built/local/typescript.internal.d.ts"])) { await runDtsBundler("./built/local/typescript/typescript.d.ts", "./built/local/typescript.d.ts"); } }, @@ -452,7 +452,7 @@ export const dtsLssl = task({ description: "Bundles tsserverlibrary.d.ts", dependencies: [buildLssl], run: async () => { - if (needsUpdate(["./built/local/tsserverlibrary/tsconfig.tsbuildinfo", "./scripts/dtsBundler.mjs"], ["./built/local/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.internal.d.ts"])) { + if (needsUpdate("./built/local/tsserverlibrary/tsconfig.tsbuildinfo", ["./built/local/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.internal.d.ts"])) { await runDtsBundler("./built/local/tsserverlibrary/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.d.ts"); } } diff --git a/scripts/dtsBundler.mjs b/scripts/dtsBundler.mjs index d3d0085d00288..fc17bbaebd09b 100644 --- a/scripts/dtsBundler.mjs +++ b/scripts/dtsBundler.mjs @@ -331,10 +331,6 @@ function verifyMatchingSymbols(decl) { * @param {ts.Symbol} moduleSymbol */ function emitAsNamespace(name, moduleSymbol) { - if (name === "default") { - return; - } - assert(moduleSymbol.flags & ts.SymbolFlags.ValueModule, "moduleSymbol is not a module"); scopeStack.push(new Map()); From ac08bc07ee6a515a3950f1f5ff9540c50c5a79ce Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 10 Nov 2022 12:40:46 -0800 Subject: [PATCH 5/5] Revert other changes --- src/typingsInstaller/nodeTypingsInstaller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 780c826accb19..6b996af1fdafd 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -81,7 +81,7 @@ interface ExecSyncOptions { } type ExecSync = (command: string, options: ExecSyncOptions) => string; -class NodeTypingsInstaller extends TypingsInstaller { +export class NodeTypingsInstaller extends TypingsInstaller { private readonly nodeExecSync: ExecSync; private readonly npmPath: string; readonly typesRegistry: Map>;