Doesn't appear to work in an ESM browser environment

Open
#496 10 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, wasm
Domain
frontend, web-dev

Research direction

Start by tracing read-wasm.js and wasm.js from SourceMapConsumer.initialize and SourceMapConsumer.with, using the reported browser stack to inspect where __dirname is referenced. Verify the ESM browser path can initialize lib/mappings.wasm and complete originalPositionFor without the type error or ReferenceError.

Written by the indexing model from the issue text.

Description

I'm trying to use this library for the first time, and it doesn't appear to work in a browser correctly (in an ESM webapp). My init code is very simple and taken from the docs:

import { SourceMapConsumer } from 'source-map';

SourceMapConsumer.initialize({
  'lib/mappings.wasm': 'https://unpkg.com/source-map@0.7.3/lib/mappings.wasm',
});

But this has a type error:

image

Then I'm trying to apply a source map, basically like this:

await SourceMapConsumer.with(result.contents, null, (consumer) => {
  positions.forEach((position) => {
    console.log(consumer.originalPositionFor(position));
  });
});

And I'm getting this in the browser console:

Uncaught (in promise) ReferenceError: __dirname is not defined
    at read-wasm.js:34:34
    at new Promise (<anonymous>)
    at readWasm (read-wasm.js:33:12)
    at wasm (wasm.js:25:16)
    at source-map-consumer.js:247:14
    at async _SourceMapConsumer.with (source-map-consumer.js:68:22)
    at async Object.handleAddFiles [as onAddFiles] (App.tsx?t=1692066785254:100:9)

Looking at the code, I can see that this library thinks it's running in a Node environment, but it's clearly not, so maybe the environment detection code is not right?

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

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.

More from mozilla/source-map

All issues in mozilla/source-map

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.