Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
floridoo committed Jul 8, 2014
0 parents commit 2f6b1e5
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules
reports
*.tgz
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"node": true,
"strict": true
}
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
node_modules
reports
*.tgz
.git*
test
.jshintrc
.npmignore
.coveralls.yml
.travis.yml
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- 0.10
after_script:
- npm run coveralls
branches:
except:
- /^v[0-9]/
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Concat with source maps

NPM module for concatenating text files and generating source maps.

### Usage example
```js
var concat = new Concat(true, 'all.js', '\n');
concat.add('file1.js', file1Content);
concat.add('file2.js', file2Content, file2SourceMap);

var concatenatedContent = concat.content;
var sourceMapForContent = concat.sourceMap;
```

### API

#### new Concat(generateSourceMap, outFileName, separator)
Initialize a new concat object.

Parameters:
- generateSourceMap: whether or not to generate a source map (default: false)
- outFileName: the file name/path of the output file (for the source map)
- separator: the string that should separate files (default: \n)

#### concat.add(fileName, content, sourceMap)
Add a file to the output file.

Parameters:
- fileName: file name of the input file
- content: content (string) of the input file
- sourceMap: optional source map of the input file. Will be merged into the output source map.

#### concat.content
The resulting concatenated file content.

#### concat.sourceMap
The resulting source map of the concatenated files.
94 changes: 94 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var SourceMapConsumer = require('source-map').SourceMapConsumer;

function Concat(generateSourceMap, fileName, separator) {
this.separator = separator;
this.lineOffset = 0;
this.columnOffset = 0;
this.sourceMapping = generateSourceMap;
this.content = '';

if (separator === undefined) {
this.separator = '\n';
}

if (this.sourceMapping) {
this._sourceMap = new SourceMapGenerator({file: fileName});
this.separatorLineOffset = 0;
this.separatorColumnOffset = 0;
for (var i = 0; i < this.separator.length; i++) {
this.separatorColumnOffset++;
if (this.separator[i] === '\n') {
this.separatorLineOffset++;
this.separatorColumnOffset = 0;
}
}
}
}

Concat.prototype.add = function(filePath, content, sourceMap) {
if (this.content !== '') {
this.content += this.separator;
}
this.content += content;

if (this.sourceMapping) {
var lines = content.split('\n').length;

if (Object.prototype.toString.call(sourceMap) === '[object String]')
sourceMap = JSON.parse(sourceMap);

if (sourceMap && sourceMap.mappings && sourceMap.mappings.length > 0) {
var upstreamSM = new SourceMapConsumer(sourceMap);
var _this = this;
upstreamSM.eachMapping(function(mapping) {
_this._sourceMap.addMapping({
generated: {
line: _this.lineOffset + mapping.generatedLine,
column: (mapping.generatedLine === 1 ? _this.columnOffset : 0) + mapping.generatedColumn
},
original: {
line: mapping.originalLine,
column: mapping.originalColumn
},
source: mapping.source,
name: mapping.name
});
});
if (upstreamSM.sourcesContent) {
upstreamSM.sourcesContent.forEach(function(sourceContent, i) {
_this._sourceMap.setSourceContent(upstreamSM.sources[i], sourceContent);
});
}
} else {
for (var i = 1; i <= lines; i++) {
this._sourceMap.addMapping({
generated: {
line: this.lineOffset + i,
column: (i === 1 ? this.columnOffset : 0)
},
original: {
line: i,
column: 0
},
source: filePath
});
}
}
if (lines > 1)
this.columnOffset = 0;
if (this.separatorLineOffset === 0)
this.columnOffset += content.length - Math.max(0, content.lastIndexOf('\n')+1);
this.columnOffset += this.separatorColumnOffset;
this.lineOffset += lines - 1 + this.separatorLineOffset;
}
};

Object.defineProperty(Concat.prototype, 'sourceMap', {
get: function sourceMap() {
return this._sourceMap ? this._sourceMap.toString() : undefined;
}
});

module.exports = Concat;
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "concat-with-sourcemaps",
"version": "0.1.0",
"description": "Concatenate file contents with a custom separator and generate a source map",
"homepage": "http://github.com/floridoo/concat-with-sourcemaps",
"repository": "git://github.com/floridoo/concat-with-sourcemaps.git",
"main": "index.js",
"scripts": {
"test": "jshint *.js test/*.js && faucet test/*.js",
"tap": "node node_modules/argg test/*.js",
"cover": "istanbul cover --dir reports/coverage node_modules/argg test/*.js",
"coveralls": "istanbul cover node_modules/argg test/*.js --report lcovonly && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
"keywords": [
"concat",
"source map"
],
"author": "Florian Reiterer <[email protected]>",
"license": "ISC",
"dependencies": {
"source-map": "^0.1.34"
},
"devDependencies": {
"jshint": "^2.5.0",
"tape": "^2.12.3",
"argg": "0.0.1",
"istanbul": "^0.2.8",
"faucet": "0.0.1",
"coveralls": "^2.10.0"
}
}
Loading

0 comments on commit 2f6b1e5

Please sign in to comment.