mirror of
https://github.com/hanzoai/azure-sdk-for-js.git
synced 2026-10-30 19:51:51 +00:00
## Problem
Warp currently emits CommonJS output by setting `module: "CommonJS"` and
`moduleResolution: "Node10"` in CJS tsconfigs. This creates several
problems:
1. **IDE and tooling mismatch**: The tsconfig on disk says Node10, but
warp silently upgrades to Node16 at compile time. This means VS Code,
eslint, api-extractor, and any other tsc-based tool sees a different
module resolution than warp uses, causing phantom errors and
inconsistent behavior.
2. **Implicit virtual package.json**: Warp had an `inferModuleType`
function that detected `module: "CommonJS"` in the tsconfig and
implicitly injected a virtual `{"type":"commonjs"}` package.json —
behavior that was invisible and hard to debug.
3. **Blocks TypeScript v6/v7 migration**: TypeScript is deprecating
`moduleResolution: "Node10"` and `module: "CommonJS"`. Future TypeScript
versions will only support `Node16`/`NodeNext` module resolution. Our
current CJS tsconfigs would fail to parse.
## Solution
### Explicit `moduleType` in warp config
Instead of inferring CJS from the tsconfig, each warp target now
declares `moduleType: "commonjs"` explicitly. The tsconfig on disk uses
`module: "Node16"` / `moduleResolution: "Node16"` — the same resolution
that every tool sees. Warp injects the virtual `{"type":"commonjs"}`
package.json only when `moduleType` is set, making the behavior explicit
and auditable.
### esbuild CJS from TypeScript sources
When a CJS target has the same source graph as an already-emitted ESM
target (same files, same resolved imports, same emit options — differing
only in `moduleType`), warp skips tsc entirely and uses esbuild to build
CJS directly from the original `.ts` source files. For each source file,
esbuild strips types and emits CommonJS with `format: "cjs"`, `loader:
"ts"`. Declaration files (`.d.ts`, `.d.ts.map`) are copied from the ESM
output since they are identical between targets.
This approach has three key advantages over the previous ESM `.js` → CJS
`.js` transform:
- **Correct source maps**: Maps point directly to `.ts` sources, not
`.js` intermediaries
- **Preserves directory paths**: Subdirectory files get full relative
paths in source maps (e.g. `../../src/internal/helper.ts` instead of
`helper.js`)
- **Structured error handling**: Build failures are caught and returned
as `CompileResult` instead of propagating raw exceptions
## Changes
### Compiler
- `buildCjsFromSources`: New function replaces `transformEsmToCjs`.
Reads `.ts` source files from `rootDir`, runs `esbuild.transform` with
`format: "cjs"`, `loader: "ts"`, writes `.js` + `.js.map` to CJS
`outDir`, copies `.d.ts` from ESM output.
- `programIdentity` includes `moduleType` to prevent ESM/CJS dedup bugs
- CJS fast-path errors are caught and returned as structured
`CompileResult` failures
- Removed `inferModuleType` — no implicit behavior, no Node10 fallback
- Removed dead `json` option from `BuildOptions` (JSON output handled in
CLI only)
### Config
- All ~440 CJS `warp.config.yml` targets now have `moduleType: commonjs`
- `tsconfig.src.cjs.json` extends `tsconfig.src.esm.json` (only
overrides `outDir`)
- `warp.base.config.yml` sets `moduleType: commonjs` on the CJS target
(inherited by 340+ packages)
- Backward compatibility: `condition: "require"` infers `moduleType:
"commonjs"` when not set
### Worker & error handling
- `try/catch` around `runCompilation` in worker to prevent silent worker
death
- Worker crash results report `packageRoot` as `outDir` fallback (not
tsconfig path)
### Tests
- Source map tests verify CJS maps point to `.ts` sources
- Subdirectory source map path preservation (regression test for
basename bug)
- `sourceMappingURL` comment presence in CJS output
- Polyfill + dedup + CJS triple interaction test
## Related PRs
- Codegen (autorest.typescript):
https://github.com/Azure/autorest.typescript/pull/3872
---------
Co-authored-by: Copilot <[email protected]>
7 lines
114 B
JSON
7 lines
114 B
JSON
{
|
|
"extends": "./tsconfig.src.esm.json",
|
|
"compilerOptions": {
|
|
"outDir": "${configDir}/dist/commonjs"
|
|
}
|
|
}
|