Files
next.js/turbopack/crates/turbopack-ecmascript/src/single_file_ecmascript_output.rs
Luke SandbergandGitHub 7c40cde272 [turbopack] Populate the names field of sourcemaps (#87911)
Ensure that turbopack produces sourcemaps with the `names` field populated.

SWC has support for this but because we are using lower level APIs we need to collect identifiers ourself.  This follows the pattern that SWC uses internally in APIs like `Compiler::optimize`.

Partially addresses #87896, to actually use these when reporting errors we need to modify the deobfuscation logic in next.


<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
2026-01-05 19:21:31 -08:00

107 lines
2.9 KiB
Rust

use std::sync::Arc;
use anyhow::Result;
use swc_core::common::{BytePos, FileName, LineCol, SourceMap};
use tokio::io::AsyncReadExt;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath, rope::Rope};
use turbopack_core::{
asset::{Asset, AssetContent},
output::{OutputAsset, OutputAssetsReference},
source::Source,
source_map::GenerateSourceMap,
};
use crate::parse::generate_js_source_map;
/// An EcmaScript OutputAsset composed of one file, no parsing and no references. Includes a source
/// map to the original file.
#[turbo_tasks::value]
pub struct SingleFileEcmascriptOutput {
output_path: FileSystemPath,
source_path: FileSystemPath,
source: ResolvedVc<Box<dyn Source>>,
}
#[turbo_tasks::value_impl]
impl OutputAsset for SingleFileEcmascriptOutput {
#[turbo_tasks::function]
fn path(&self) -> Vc<FileSystemPath> {
self.output_path.clone().cell()
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for SingleFileEcmascriptOutput {}
#[turbo_tasks::value_impl]
impl Asset for SingleFileEcmascriptOutput {
#[turbo_tasks::function]
fn content(&self) -> Vc<AssetContent> {
self.source.content()
}
}
#[turbo_tasks::value_impl]
impl SingleFileEcmascriptOutput {
#[turbo_tasks::function]
pub fn new(
output_path: FileSystemPath,
source_path: FileSystemPath,
source: ResolvedVc<Box<dyn Source>>,
) -> Vc<SingleFileEcmascriptOutput> {
SingleFileEcmascriptOutput {
output_path,
source_path,
source,
}
.cell()
}
}
#[turbo_tasks::value_impl]
impl GenerateSourceMap for SingleFileEcmascriptOutput {
#[turbo_tasks::function]
pub async fn generate_source_map(&self) -> Result<Vc<FileContent>> {
let FileContent::Content(file) = &*self.source.content().file_content().await? else {
return Ok(FileContent::NotFound.cell());
};
let file_source = {
let mut s = String::new();
file.read().read_to_string(&mut s).await?;
s
};
let mut mappings = vec![];
// Start from 1 because 0 is reserved for dummy spans in SWC.
let mut pos: u32 = 1;
for (index, line) in file_source.split_inclusive('\n').enumerate() {
mappings.push((
BytePos(pos),
LineCol {
line: index as u32,
col: 0,
},
));
pos += line.len() as u32;
}
let sm: Arc<SourceMap> = Default::default();
sm.new_source_file(
FileName::Custom(self.source_path.to_string()).into(),
file_source,
);
let map = generate_js_source_map(
&*sm,
mappings,
None::<&Rope>,
true,
true,
Default::default(),
)?;
Ok(FileContent::Content(File::from(map)).cell())
}
}