Skip to content

Commit

Permalink
fix: normalize original sourcemap sources (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Apr 6, 2024
1 parent f69b004 commit 679a9ac
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
43 changes: 33 additions & 10 deletions packages/rolldown/src/plugin/bindingify-build-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { BindingPluginOptions } from '../binding'

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

export function bindingifyBuildStart(
options: RolldownNormalizedInputOptions,
Expand Down Expand Up @@ -82,20 +84,23 @@ export function bindingifyTransform(
return { code: ret }
}

if (!ret.map) {
return { code: ret.code }
}

// 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]
}
let map = typeof ret.map === 'object' ? ret.map : JSON.parse(ret.map)
if (isEmptySourcemapFiled(map.sourcesContent)) {
map.sourcesContent = [code]
}
if (isEmptySourcemapFiled(map.sources)) {
map.sources = [id]
}

return {
code: ret.code,
map: transformSourcemap(ret.map),
map: JSON.stringify(map),
}
}
}
Expand All @@ -119,9 +124,27 @@ export function bindingifyLoad(
return { code: ret }
}

if (!ret.map) {
return { code: ret.code }
}

let map =
typeof ret.map === 'object'
? ret.map
: (JSON.parse(ret.map) as SourceMapInputObject)
if (!isEmptySourcemapFiled(map.sources)) {
// normalize original sourcemap sources
// Port form https://github.com/rollup/rollup/blob/master/src/utils/collapseSourcemaps.ts#L180-L188.
const directory = path.dirname(id) || '.'
const sourceRoot = map.sourceRoot || '.'
map.sources = map.sources!.map((source) =>
path.resolve(directory, sourceRoot, source!),
)
}

return {
code: ret.code,
map: transformSourcemap(ret.map),
map: JSON.stringify(map),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/types/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface SourceMapInputObject {
names?: string[]
sources?: (string | null)[]
sourcesContent?: (string | null)[]
sourceRoot?: string
version: number
}

Expand Down
7 changes: 0 additions & 7 deletions packages/rolldown/src/utils/transform-sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { SourceMapInput } from '../types/sourcemap'

export function transformSourcemap(value?: SourceMapInput): string | undefined {
if (typeof value === 'object') {
return JSON.stringify(value)
}
return value
}

export function isEmptySourcemapFiled(
array: undefined | (string | null)[],
): boolean {
Expand Down
1 change: 0 additions & 1 deletion packages/rollup-tests/src/failed-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@
"rollup@sourcemaps@ignore-list-false: accepts false for `sourcemapIgnoreList` to disable the default ignore-listing of node_modules@generates es",
"rollup@sourcemaps@ignore-list-source-files: populates ignore list@generates es",
"rollup@sourcemaps@ignore-list-sourcemap-path: correctly passes source map path@generates es",
"rollup@sourcemaps@loaders: preserves sourcemap chains when transforming@generates es",
"rollup@sourcemaps@names-transformed-render-chunk: names are recovered if transforms are used@generates es",
"rollup@sourcemaps@names: names are recovered (https://github.com/rollup/rollup/issues/101)@generates es",
"rollup@sourcemaps@reified-namespace: generates correct sourcemap with reified namespace (#668)@generates es",
Expand Down
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": 677,
"skipFailed": 676,
"skipped": 0,
"passed": 242
"passed": 243
}
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 | 677|
| skipFailed | 676|
| skipped | 0|
| passed | 242|
| passed | 243|

0 comments on commit 679a9ac

Please sign in to comment.