Skip to content

Doesn't appear to work in an ESM browser environment #496

@kaiyoma

Description

@kaiyoma

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/[email protected]/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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions