Repository navigation
Unwrap tsc's interop helpers around builtins; lower re-exports of empty and entry namespaces #1584
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d60d1f2
Unwrap tsc's interop helpers around builtin modules in packages
cramforce 24963c8
Lower star re-exports of empty namespaces and reads of a bundle's ent…
cramforce 1b6aa95
Merge branch 'opt/npm-cjs-var-export' into opt/npm-tsc-helpers
cramforce 278f6ca
Format and record the new fixtures' order baselines
cramforce 8d392b8
Merge branch 'opt/npm-cjs-var-export' into opt/npm-tsc-helpers
cramforce 8d98630
Merge branch 'opt/npm-cjs-var-export' into opt/npm-tsc-helpers
cramforce d5a93a2
Merge branch 'opt/npm-cjs-var-export' into opt/npm-tsc-helpers
cramforce 8ac501b
Refuse namespace reads of inherited names and of names an entry star …
ctate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/compiler/src/frontend/npm-static-tsc-helpers.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { spawnSync } from "node:child_process"; | ||
| import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { afterAll, expect, test } from "vitest"; | ||
| import { nodeFrontendServices } from "./services-node.js"; | ||
|
|
||
| const dirs: string[] = []; | ||
| afterAll(() => { | ||
| for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function rewrite(source: string): string | null { | ||
| return nodeFrontendServices().tscBuiltinImports(source, "/pkg/index.js"); | ||
| } | ||
|
|
||
| const helpers = `"use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| var __importStar = (this && this.__importStar) || function (mod) { | ||
| if (mod && mod.__esModule) return mod; | ||
| var result = {}; | ||
| if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
| result["default"] = mod; | ||
| return result; | ||
| }; | ||
| `; | ||
|
|
||
| function expectSameBehavior(source: string): string { | ||
| const result = rewrite(source); | ||
| expect(typeof result).toBe("string"); | ||
| const dir = mkdtempSync(join(tmpdir(), "scriptc-tsc-builtin-imports-")); | ||
| dirs.push(dir); | ||
| writeFileSync(join(dir, "original.cjs"), source); | ||
| writeFileSync(join(dir, "rewritten.cjs"), result!); | ||
| const run = (file: string) => | ||
| spawnSync(process.execPath, [join(dir, file)], { encoding: "utf8" }); | ||
| const oracle = run("original.cjs"); | ||
| expect(oracle.status).toBe(0); | ||
| expect(run("rewritten.cjs").stdout).toBe(oracle.stdout); | ||
| return result!; | ||
| } | ||
|
|
||
| test("builtin modules behind tsc's interop helpers become plain requires", () => { | ||
| const out = expectSameBehavior(`${helpers}const events_1 = __importDefault(require("events")); | ||
| const path = __importStar(require("node:path")); | ||
| class Bus extends events_1.default.EventEmitter {} | ||
| console.log(new Bus() instanceof events_1.default, path.basename("/a/b.txt"), typeof path.join); | ||
| `); | ||
| expect(out).toContain('const events_1 = require("events");'); | ||
| expect(out).toContain("class Bus extends events_1.EventEmitter {}"); | ||
| expect(out).toContain('const path = require("node:path");'); | ||
| }); | ||
|
|
||
| test("other references, program modules and default reads of a namespace keep their text", () => { | ||
| // The binding itself as a value. | ||
| expect( | ||
| rewrite( | ||
| `${helpers}const events_1 = __importDefault(require("events"));\nconsole.log(events_1);\n`, | ||
| ), | ||
| ).toBeNull(); | ||
| // A program module (cjs-tsc-interop.ts owns it). | ||
| expect( | ||
| rewrite(`${helpers}const m = __importDefault(require("./m"));\nm.default();\n`), | ||
| ).toBeNull(); | ||
| // `ns.default` reads the namespace copy's own default. | ||
| expect( | ||
| rewrite(`${helpers}const p = __importStar(require("path"));\nconsole.log(p.default === p);\n`), | ||
| ).toBeNull(); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.