website/node_modules/source-map-resolve/readme.md

Overview Build Status

Resolve the source map and/or sources for a generated file.

var sourceMapResolve = require("source-map-resolve")
var sourceMap        = require("source-map")

var code = [
  "!function(){...}();",
  "/*# sourceMappingURL=foo.js.map */"
].join("\n")

sourceMapResolve.resolveSourceMap(code, "/js/foo.js", fs.readFile, function(error, result) {
  if (error) {
    return notifyFailure(error)
  }
  result
  // {
  //   map: {file: "foo.js", mappings: "...", sources: ["/coffee/foo.coffee"], names: []},
  //   url: "/js/foo.js.map",
  //   sourcesRelativeTo: "/js/foo.js.map",
  //   sourceMappingURL: "foo.js.map"
  // }

  sourceMapResolve.resolveSources(result.map, result.sourcesRelativeTo, fs.readFile, function(error, result) {
    if (error) {
      return notifyFailure(error)
    }
    result
    // {
    //   sourcesResolved: ["/coffee/foo.coffee"],
    //   sourcesContent: ["<contents of /coffee/foo.coffee>"]
    // }
  })
})

sourceMapResolve.resolve(code, "/js/foo.js", fs.readFile, function(error, result) {
  if (error) {
    return notifyFailure(error)
  }
  result
  // {
  //   map: {file: "foo.js", mappings: "...", sources: ["/coffee/foo.coffee"], names: []},
  //   url: "/js/foo.js.map",
  //   sourcesRelativeTo: "/js/foo.js.map",
  //   sourceMappingURL: "foo.js.map",
  //   sourcesResolved: ["/coffee/foo.coffee"],
  //   sourcesContent: ["<contents of /coffee/foo.coffee>"]
  // }
  result.map.sourcesContent = result.sourcesContent
  var map = new sourceMap.sourceMapConsumer(result.map)
  map.sourceContentFor("/coffee/foo.coffee")
  // "<contents of /coffee/foo.coffee>"
})

Installation

Works with CommonJS, AMD and browser globals, through UMD.

Note: This module requires setImmediate and atob. Use polyfills if needed, such as:

Usage

sourceMapResolve.resolveSourceMap(code, codeUrl, read, callback)

The result is an object with the following properties:

If code contains no sourceMappingURL, the result is null.

sourceMapResolve.resolveSources(map, mapUrl, read, [options], callback)

The result is an object with the following properties:

sourceMapResolve.resolve(code, codeUrl, read, [options], callback)

The arguments are identical to sourceMapResolve.resolveSourceMap, except that you may also provide the same options as in sourceMapResolve.resolveSources.

This is a convenience method that first resolves the source map and then its sources. You could also do this by first calling sourceMapResolve.resolveSourceMap and then sourceMapResolve.resolveSources.

The result is identical to sourceMapResolve.resolveSourceMap, with the properties from sourceMapResolve.resolveSources merged into it.

There is one extra feature available, though. If code is null, codeUrl is treated as a url to the source map instead of to code, and will be read. This is handy if you sometimes get the source map url from the SourceMap: <url> header (see the Notes section). In this case, the sourceMappingURL property of the result is null.

sourceMapResolve.*Sync()

There are also sync versions of the three previous functions. They are identical to the async versions, except:

sourceMapResolve.resolveSourcesSync also accepts null as the read parameter. The result is the same as when passing a function as the read parameter, except that the sourcesContent property of the result will be an empty array. In other words, the sources aren’t read. You only get the sourcesResolved property. (This only supported in the synchronus version, since there is no point doing it asynchronusly.)

sourceMapResolve.parseMapToJSON(string, [data])

The spec says that if a source map (as a string) starts with )]}', it should be stripped off. This is to prevent XSSI attacks. This function does that and returns the result of JSON.parseing what’s left.

If this function throws error, error.sourceMapData === data.

Errors

All errors passed to callbacks or thrown by this module have a sourceMapData property that contain as much as possible of the intended result of the function up until the error occurred.

Note that while the map property of result objects always is an object, error.sourceMapData.map will be a string if parsing that string fails.

Note

This module resolves the source map for a given generated file by looking for a sourceMappingURL comment. The spec defines yet a way to provide the URL to the source map: By sending the SourceMap: <url> header along with the generated file. Since this module doesn’t retrive the generated code for you (instead you give the generated code to the module), it’s up to you to look for such a header when you retrieve the file (should the need arise).

Development

Tests

First off, run npm install to install testing modules and browser polyfills.

npm test lints the code and runs the test suite in Node.js.

x-package.json5

package.json, component.json and bower.json are all generated from x-package.json5 by using [xpkg]. Only edit x-package.json5, and remember to run xpkg before commiting!

Generating the browser version

source-map-resolve.js is generated from source-map-resolve-node.js and source-map-resolve-template.js. Only edit the two latter files, not source-map-resolve.js! To generate it, run npm run build.

License

The X11 (“MIT”) License.



JohnCoene/chirp documentation built on May 25, 2021, 6:33 p.m.