mozilla / mozilla/source-map

Source maps mapping looks wrong when using Webpack

Open
#498 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
3.7k
Forks
370
PR merge metrics
No merged PRs in 30d

Description

Hello,
I've been experimenting with this repository. I tried to generate a simple project to use source maps using Webpack: https://github.com/eloparco/test-source-maps, where I produce a js bundle starting from Typescript and I want to use source maps for the mapping between the two.

I tried to play around with the generated source maps:

const SourceMapConsumer = require("source-map").SourceMapConsumer;
const fs = require("fs");

const SOURCEMAP_ABSOUTE_PATH = "/path/to/test-source-maps/dist/bundle.js.map";
const ORIGINAL_ABSOLUTE_PATH = "/path/to/test-source-maps/src/index.ts";
const GENERATED_ABSOLUTE_PATH = "/path/to/test-source-maps/dist/bundle.js";
const ORIGINAL_LINE = 11;

let json = JSON.parse(fs.readFileSync(SOURCEMAP_ABSOUTE_PATH).toString());

SourceMapConsumer.with(json, null, (consumer) => {
  console.log(consumer.sources);

  const generated = consumer.generatedPositionFor({
    source: ORIGINAL_ABSOLUTE_PATH,
    line: ORIGINAL_LINE,
    column: 0,
  });
  console.log(generated);

  const original = consumer.originalPositionFor({
    line: generated.line,
    column: generated.column,
  });
  console.log(original);

  assert(original.line === ORIGINAL_LINE); // <------------ This assertion fails!!!
});

Here I use generatedPositionFor() to get the mapping from the original .ts file to the corresponding .js file. Then, I try to do the opposite and feed the line and column previously retrieved to originalPositionFor(), expecting it to return the original line/column that I passed to generatedPositionFor(). But that doesn't happen.

Is my understanding of source maps wrong? Or am I doing something wrong in the Webpack configuration?
Thanks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the external test-source-maps project and inspect dist/bundle.js.map alongside the generatedPositionFor() and originalPositionFor() calls shown in the issue. Compare the returned mappings with the source-map API expectations and Webpack configuration; done means determining whether the round trip is valid or identifying a reproducible mapping/configuration defect.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript, webpack
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.