Add source maps to downlevel and downlevel-jsx modes #7

Closed natemoo.re
targeting main from sourcemaps
Closed opened by natemoo.re
targeting main from sourcemaps

Adds an optional source map to downlevel and downlevel-jsx modes, and makes the JSX output easier to map.

Source maps #

transform(src, { mode: "downlevel", sourceMap: true }) returns { code, map }. The chunked generator returns the map as its done value. Erase mode returns map: null, since it keeps every offset. The option also takes { source, file, includeContent }.

Every generated line gets an identity segment at column 0, because Chrome and Node floor-search across lines and Mozilla's library returns null for a line with no segment. Rewritten lines get a segment at each edit boundary and at each copied token after the first shift. No names, no Scopes: nothing is renamed.

In the engine, the three output primitives log records (generated line and column, source line and column) into a new MAP region with mapCount, mapPtr, mapLine, and mapSrcLine exports. Records under an anchor are held and adjusted by the two remaining insert-into-earlier-output paths. The wrapper fills identity segments and encodes VLQ with a ten-line function. The erase engine carries none of it.

JSX output #

  • The callee and children:[ are fixed-width slots filled in place at the element's close. Only _createElement( still inserts.
  • The automatic runtime's imports go before the trailing trivia, so a file ending in a // comment keeps its line count.
  • A moved key carries its value's source position into its record.

Docs and tests #

docs/sourcemaps.md explains the no-map common case, the line-preserving approach, the consumer rule, a worked example checked against real output, and the implementation. test/sourcemap.mjs round-trips every downlevel and JSX corpus case through @jridgewell/trace-mapping, with random chunking.

Sizes (gzip) #

Engine Before After
erase.wasm 13.7 kB 13.7 kB
downlevel.wasm 19.0 kB 20.1 kB
downlevel-jsx.wasm 23.6 kB 25.0 kB
wrapper 3.3 kB 4.1 kB

Throughput on the OSS samples is unchanged within noise in all three modes. Parameter-property fields stay at the class {: moving them after the constructor changes own-key order, which the downlevel corpus pins.

Commits 10

Match the source map guide to the engine's records, and update the engine sizes
Map a moved JSX key to its value's source position, and drop the map exports from the erase engine
Document how source mapping works, and when downlevel mode needs a map
Document source maps and the records behind them
Add the sourceMap option
Record source map positions in downlevel mode
Update the downlevel-jsx engine size and add changesets
Keep each saved JSX key's source offset for a source map record
Write the automatic runtime's imports before the trailing trivia
Fill the automatic runtime's callee and children slots in place
Sign up or Sign in to add to the discussion.