Skip to content

Commit

Permalink
fix: remapping sourcemap from transform hook (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Apr 5, 2024
1 parent 97ea4b6 commit 43242f4
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
30 changes: 22 additions & 8 deletions packages/rolldown/src/plugin/bindingify-build-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BindingPluginOptions } from '../binding'

import type { Plugin } from './index'
import { RolldownNormalizedInputOptions } from '../options/input-options'
import { isEmptySourcemapFiled, transformSourcemap } from '../utils'

export function bindingifyBuildStart(
options: RolldownNormalizedInputOptions,
Expand Down Expand Up @@ -77,12 +78,24 @@ export function bindingifyTransform(
return
}

const retCode = typeof ret === 'string' ? ret : ret.code
const retMap = typeof ret === 'string' ? undefined : ret.map
if (typeof ret === 'string') {
return { code: ret }
}

// TODO(underfin) move the logic to rust
// If sourcemap hasn't `sourcesContent` and `sources`, using original code to fill it.
if (ret.map && typeof ret.map === 'object') {
if (isEmptySourcemapFiled(ret.map.sourcesContent)) {
ret.map.sourcesContent = [code]
}
if (isEmptySourcemapFiled(ret.map.sources)) {
ret.map.sources = [id]
}
}

return {
code: retCode,
map: retMap ?? undefined,
code: ret.code,
map: transformSourcemap(ret.map),
}
}
}
Expand All @@ -102,12 +115,13 @@ export function bindingifyLoad(
return
}

const retCode = typeof ret === 'string' ? ret : ret.code
const retMap = typeof ret === 'string' ? undefined : ret.map
if (typeof ret === 'string') {
return { code: ret }
}

return {
code: retCode,
map: retMap ?? undefined,
code: ret.code,
map: transformSourcemap(ret.map),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rolldown/src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SourceMap } from 'src/types/rolldown-output'
import {
BindingHookResolveIdExtraOptions,
BindingPluginContext,
Expand Down Expand Up @@ -68,7 +69,7 @@ export interface Plugin {
| string
| {
code: string
map?: string | null
map?: string | null | SourceMap
}
>
>
Expand Down
4 changes: 2 additions & 2 deletions packages/rolldown/src/types/rolldown-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ function _assertRolldownOutputAsset() {
}

export interface SourceMap {
file: string
file?: string
mappings: string
names: string[]
sources: string[]
sourcesContent: (string | null)[]
sourcesContent?: string[]
version: number
// toString(): string
// toUrl(): string
Expand Down
10 changes: 10 additions & 0 deletions packages/rolldown/src/utils/transform-sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ export function transformSourcemap(value?: SourceMapInput): string | undefined {
}
return value
}

export function isEmptySourcemapFiled(array: undefined | string[]): boolean {
if (!array) {
return true
}
if (array.length === 0 || array[0] === '') {
return true
}
return false
}
3 changes: 1 addition & 2 deletions packages/rollup-tests/src/failed-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,5 @@
"rollup@sourcemaps@transform-full-source-paths: provides the full source map path when transforming source maps@generates es",
"rollup@sourcemaps@transform-low-resolution: handles combining low-resolution and high-resolution source-maps when transforming@generates es",
"rollup@sourcemaps@transform-source-paths: transform sourcemap paths (#2168)@generates es",
"rollup@sourcemaps@transforms: preserves sourcemap chains when transforming@generates es",
"rollup@sourcemaps@untransformed-modules: allows sourcemap chains with some untransformed modules (#404)@generates es"
"rollup@sourcemaps@transforms: preserves sourcemap chains when transforming@generates es"
]
4 changes: 2 additions & 2 deletions packages/rollup-tests/src/status.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"failed": 0,
"skipFailed": 680,
"skipFailed": 679,
"skipped": 0,
"passed": 239
"passed": 240
}
4 changes: 2 additions & 2 deletions packages/rollup-tests/src/status.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| | number |
|----| ---- |
| failed | 0|
| skipFailed | 680|
| skipFailed | 679|
| skipped | 0|
| passed | 239|
| passed | 240|

1 comment on commit 43242f4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 43242f4 Previous: 97ea4b6 Ratio
threejs10x 344.99 ms / ops 380.34 ms / ops 0.91
threejs10x-sourcemap 466.03 ms / ops 430.97 ms / ops 1.08

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.