Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

source-map-concat

lydell615MITdeprecated1.0.1

Unsupported package

Concatenate files with source maps.

source map, source-map, source, map, dummy, concatenate, concat, cat, mapcat

readme

Overview Build Status

Concatenate files with source maps.

var fs     = require("fs")
var path   = require("path")
var concat = require("source-map-concat")

var resolveSourceMapSync = require("source-map-resolve").resolveSourceMapSync
var createDummySourceMap = require("source-map-dummy")

var jsFiles = ["foo.js", "subdir/bar.js", "../baz.js"]

jsFiles = jsFiles.map(function(file) {
  return {
    source:  file,
    code: fs.readFileSync(file).toString()
  }
})
jsFiles.forEach(function(file) {
  var previousMap = resolveSourceMapSync(file.code, file.source, fs.readFileSync)
  if (previousMap) {
    file.map = previousMap.map
    file.sourcesRelativeTo = previousMap.sourcesRelativeTo
  } else {
    file.map = createDummySourceMap(file.code, {source: file.source, type: "js"})
  }
})

function wrap(node, file) {
  node.prepend("void function(){\n// File: " + file.source + "\n")
  node.add("}();")
}

var output = "subdir/bundle.js"

var concatenated = concat(jsFiles, {
  delimiter: "\n",
  process: wrap,
  mapPath: output + ".map"
})

concatenated.prepend("/* Bruce Banner */\n")
concatenated.add("\n/* Footer */")

var result = concatenated.toStringWithSourceMap({
  file: path.basename(output)
})

fs.writeFileSync(output, result.code)
fs.writeFileSync(output + ".map", result.map.toString())

Installation

npm install source-map-concat

var concat = require("source-map-concat")

Usage

concat(files, options)

files is an array of objects with the following properties:

  • code: The contents of the file, as a string.
  • map: The source map of the file, if any, as an object, a string or anything with a .toJSON() method (such as a SourceMapGenerator). It could be taken straight from a compiler, be resolved using source-map-resolve or created using source-map-dummy.
  • sourcesRelativeTo: A path that file.map.sources are relative to. Defaults to ..

options:

  • delimiter: A string to insert between each file.
  • process(node, file, index): A function to call on each file in files. node is a SourceNode. You could use this to wrap JavaScript files in IIFEs, for example.
  • mapPath: The path to where you intend to write the source map of the produced concatenated file. Defaults to ..

The files in files will be concatenated into a SourceNode which is returned. You may then modify this source node if you wish (node.add(...) for example). When you’re done, call node.toStringWithSourceMap(), which returns an object with a code property containing the concatenated code, and a map property containing the source map.

License

The X11 (“MIT”) License.

changelog

Version 1.0.1 (2016-03-07)

  • Update the source-map dependency.
  • Reduce the npm package size by only including needed files.

Version 1.0.0 (2015-02-26)

  • Update the source-map dependency.

Version 0.4.0 (2014-08-16)

  • Updated the source-map dependency from a fork to 0.1.38. The fork supported ancient \r newlines, while the official package does not. (Backwards-incompatible change (but I doubt anyone will notice).)

Version 0.3.0 (2014-06-19)

  • Updated source-map-dummy to 0.3.0, which means slightly different mappings. (Backwards-incompatible change.)

Version 0.2.0 (2014-06-05)

  • Allow passing a source map as a string and anything with a .toJSON() method (such as a SourceMapGenerator) as well as an object.
  • Rename file.content to file.code, to be consistent with SourceNode.toStringWithSourceMap() and rework (css.stringify). After all, in reality the content is going to be code, so we might just as well call it that. “code” is also shorter than “content”. (Backwards-incompatible change.)

Version 0.1.0 (2014-03-22)

  • Initial release.