npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

concat-source-text

v2.0.0

Published

Concatenates text source files and generates a source map for the text output.

Readme

concat-source-text

Latest version Dependency status Code coverage

Concatenates text source files and generates a source map for the text output. Can be used to concatenate stylesheets and show the original source stylesheets in the debugger. Supports nested source maps and merges them to the final output.

Synopsis

Let us concatenate text content of two source files:

import { SourceTextConcatenator } from 'concat-source-text'

const concatenator = new SourceTextConcatenator
concatenator.append('first content', 'first.txt')
concatenator.append('second content', 'second.txt')
const { text } = concatenator.join({ sourceMap: true })
console.log(text)

The console output will be:

first content
second content
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpcnN0LnR4dCIsInNlY29uZC50eHQiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7QUNBQSJ9

And the decoded source map content:

{"version":3,"sources":["first.txt","second.txt"],"names":[],"mappings":"AAAA;ACAA"}

Installation

This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 12 or newer.

npm i concat-source-text
pnpm i concat-source-text
yarn add concat-source-text

API

constructor()

Creates a new empty instance of the concatenator.

append(contents: string, source: string): void

Appends a text file to the output.

  • contents: the contents of the file
  • source: the name of the file

join(options?: Options): Output

Concatenates the appended files and returns the complete text with the source map. A line break will be appended after each source text, if the source text does not already end with a line break.

Options

  • separator?: inserts a string between two source files (an empty string ('') by default)
  • sourceMap?: enables generating the source map (false by default)

If sourceMap is set to true or to {}, it will be converted to { inline: true }. Otherwise an object is expected with the following properties:

SourceMap

  • inline?: appends the source map as a comment to the output text (true by default, if external is not true)
  • external?: appends a comment to the output text pointing to an external file with the source map (false by default)
  • separate?: disables source map generation if set to false together with inline
  • outputFile?: sets a name of the output file to the source map
  • mapFile?: set the name of the external source map file in the comment
  • sourcesContent?: includes the content of the source files in the source map
  • multilineComment?: uses the multi-line comment (/**# ... */) instead of the single-line one (//# ...), which is the default
  • readSourceMaps?: checks existence and reads source maps from input sources if detected
  • mapDir?: directory to read a source map file from
  • locateSourceMap?: returns directory to read a source map file from

If both inline and external are set to true, external will be chosen and inline considered unset. If inline is set to false and external unset or set to false, a separate source map will be returned, but no comment will be appended to the output text.

If mapFile is not provided and it is needed for the comment, it will be computed from outputFile by appending ".map" to it.

Setting separate can be used to strip source maps from the source files, if they are expected there: { inline: false, separate: false, readSourceMaps: true }.

If the source map file is lot located on the path stored in the comment, usually resolved from the current directory, you can supply the directory by mapDir, or by a callback locateSourceMap(mapFile: string, source: string): string | undefined:

  • mapFile: the name of the source map file parsed from the comment
  • source: the name of the source, which the source map belongs to

Output

  • text: the concatenated text
  • map?: the source map, if sourceMap was set and was not false

Example - CSS

Let us concatenate two stylesheets:

base.css:

body { padding: 1rem }

widgets.css:

.container { display: flex }

By the following script:

import { readFile, writeFile } from 'fs/promises'
import { SourceTextConcatenator } from 'concat-source-text'

const concatenator = new SourceTextConcatenator
concatenator.append(await readFile('base.css', 'utf8'), 'base.css')
concatenator.append(await readFile('widgets.css', 'utf8'), 'widgets.css')
const { text, map } = concatenator.join({
  separator: '\n',
  sourceMap: {
    external: true,
    mapFile: 'main.css.map',
    sourcesContent: true,
    multilineComment: true
  }
})
await writeFile('main.css', text)
await writeFile('main.css.map', JSON.stringify(map))

Then the result will be:

main.css:

body { padding: 1rem }

.container { display: flex }
/*# sourceMappingURL=main.css.map */

main.css.map:

{
  "version": 3,
  "sources": ["base.css", "widgets.css"],
  "names": [],
  "mappings": "AAAA;AACA;;ACDA",
  "sourcesContent": ["body { padding: 1rem }\n", ".container { display: flex }"]
}

Example - LESS

Let us concatenate two stylesheets:

base.css:

body { padding: 1rem }

widgets.css:

.widget {
  background-color: #ddd;
}
.widget-active {
  background-color: #888;
}
/*# sourceMappingURL=widget.css.map */

widgets.css.map:

{
  "version": 3,
  "sources": ["widget.less"],
  "names": [],
  "mappings": "AAGA;EACE,sBAAA;;AAEA,OAAC;EACC,sBAAA",
  "file": "widget.css",
  "sourcesContent": ["@color-normal: #ddd;\n@color-active: #888;\n\n.widget {\n  background-color: @color-normal;\n\n  &-active {\n    background-color: @color-active;\n  }\n}\n"]
}

By the following script:

import { readFile, writeFile } from 'fs/promises'
import { SourceTextConcatenator } from 'concat-source-text'

const concatenator = new SourceTextConcatenator
concatenator.append(await readFile('base.css', 'utf8'), 'base.css')
concatenator.append(await readFile('widgets.css', 'utf8'), 'widgets.css')
const { text, map } = concatenator.join({
  separator: '\n',
  sourceMap: {
    external: true,
    outputFile: 'main.css',
    sourcesContent: true,
    multilineComment: true,
    readSourceMaps: true
  }
})
await writeFile('main.css', text)
await writeFile('main.css.map', JSON.stringify(map))

Then the result will be:

main.css:

body { padding: 1rem }

.widget {
  background-color: #ddd;
}
.widget-active {
  background-color: #888;
}
/*# sourceMappingURL=main.css.map */

main.css.map:

{
  "version": 3,
  "sources": ["base.css", "widget.less"],
  "names": [],
  "mappings": "AAAA;;ACGA;EACE,sBAAA;;AAEA,OAAC;EACC,sBAAA",
  "file": "main.css",
  "sourcesContent": ["body { padding: 1rem }","@color-normal: #ddd;\n@color-active: #888;\n\n.widget {\n  background-color: @color-normal;\n\n  &-active {\n    background-color: @color-active;\n  }\n}\n"]
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.

License

Copyright (c) 2022 Ferdinand Prantl

Licensed under the MIT license.