# mold-source-map

> Mold a source map that is almost perfect for you into one that is.

Latest version **0.4.1** (published 2022-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install mold-source-map
pnpm add mold-source-map
yarn add mold-source-map
bun add mold-source-map
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2022-11-14 |
| First published | 2013-03-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 36 |
| Author | Thorsten Lorenz |
| Maintainers | thlorenz |
| Keywords | mold, change, modify, adapt, sourcemap, source, map, browserify |

## Links

- npm: https://www.npmjs.com/package/mold-source-map
- Repository: https://github.com/thlorenz/mold-source-map
- Issues: https://github.com/thlorenz/mold-source-map/issues
- npm.io page: https://npm.io/package/mold-source-map

## Dependencies (2)

- [through](https://npm.io/package/through.md) ~2.2.7
- [convert-source-map](https://npm.io/package/convert-source-map.md) ^1.1.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.4.1 (latest) — 2022-11-14
- 0.4.0 — 2015-05-14
- 0.3.1 — 2015-04-21
- 0.3.0 — 2013-11-21
- 0.2.1 — 2013-11-21
- 0.2.0 — 2013-03-19
- 0.1.0 — 2013-03-18

## README

# mold-source-map [![build status](https://secure.travis-ci.org/thlorenz/mold-source-map.png)](http://travis-ci.org/thlorenz/mold-source-map)

Mold a source map that is almost perfect for you into one that is.

```js
browserify()
  .require(require.resolve('./project/js/main.js'), { entry: true })
  .bundle({ debug: true })

  // will show all source files relative to jsRoot inside devtools
  .pipe(mold.transformSourcesRelativeTo(jsRoot))
  .pipe(fs.createWriteStream(bundlePath));
```
Full example [here](https://github.com/thlorenz/mold-source-map/blob/master/examples/browserify-sources.js).

## Installation

    npm install mold-source-map

## API

### Transforms

Transforms return a duplex stream and are therefore easily threaded into a bundler that streams the generated bundle,
like [browserify](https://github.com/substack/node-browserify).

#### transform(function map(sourcemap[, callback]) {})

This is the most generic and powerfull feature as it allows replacing the entire sourcemap comment with another `String`.

It takes a map function as input whose `sourcemap` argument has all information and lots of functions regarding the existing source map.

The optional `callback` can be used to call back with the final source map comment. If it is given, the transform will
invoke the function asynchronously, otherwise you may just return the final source map comment.

Here is a snippet from [an example](https://github.com/thlorenz/mold-source-map/blob/master/examples/browserify-external-map-file.js) 
showing how to use this in order to write out an external map file and point the browser to it:

```js
function mapFileUrlComment(sourcemap, cb) {
  
  // make source files appear under the following paths:
  // /js
  //    foo.js
  //    main.js
  // /js/wunder
  //    bar.js 
  
  sourcemap.sourceRoot('file://'); 
  sourcemap.mapSources(mold.mapPathRelativeTo(jsRoot));

  // write map file and return a sourceMappingUrl that points to it
  fs.writeFile(mapFilePath, sourcemap.toJSON(2), 'utf-8', function (err) {
    if (err) return console.error(err);
    cb('//@ sourceMappingURL=' + path.basename(mapFilePath));
  });
}

browserify()
  .require(require.resolve('./project/js/main.js'), { entry: true })
  .bundle({ debug: true })
  .pipe(mold.transform(mapFileUrlComment))
  .pipe(fs.createWriteStream(bundlePath));
```

[This example](https://github.com/thlorenz/mold-source-map/blob/master/examples/browserify-external-map-file-sync.js) achieves the same using sync operations.

### Convenience Transforms

The below transforms addressing special use cases. These cases all could be implemented with the generic transform as well.

### transformSourcesRelativeTo(root : String)

```
/**
 * Adjusts all sources paths inside the source map contained in the content that is piped to it.
 *
 * Example: bundleStream.pipe(mold.sourcesRelative(root)).pipe(fs.createWriteStream(bundlePath))
 *
 * @name sourcesRelative
 * @function
 * @param root {String} The path to make sources relative to.
 * @return {Stream} A duplex stream that writes out content with source map that had all sources paths adjusted.
 */
 ```

## Unstable API

A more custom/advanced API will be/is exposed, however it is still in high fluctuation.

Take a look at the `index.js` to get an idea of what's coming/already there.

---
_Source: https://npm.io/package/mold-source-map · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
