Skip to content

Commit

Permalink
Add possibility to add content without file reference (e.g. header co…
Browse files Browse the repository at this point in the history
…mment)
  • Loading branch information
floridoo committed Sep 27, 2015
1 parent cbf82e2 commit 73d8e16
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ NPM module for concatenating files and generating source maps.
### Usage example
```js
var concat = new Concat(true, 'all.js', '\n');
concat.add(null, "// (c) John Doe");
concat.add('file1.js', file1Content);
concat.add('file2.js', file2Content, file2SourceMap);

Expand All @@ -26,7 +27,7 @@ Parameters:
Add a file to the output file.

Parameters:
- fileName: file name of the input file
- fileName: file name of the input file (can be null for content without a file reference, e.g. a license comment)
- content: content (Buffer or string) of the input file
- sourceMap: optional source map of the input file (string). Will be merged into the output source map.

Expand Down
32 changes: 17 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Concat(generateSourceMap, fileName, separator) {
}

Concat.prototype.add = function(filePath, content, sourceMap) {
filePath = unixStylePath(filePath);
filePath = filePath && unixStylePath(filePath);

if (!Buffer.isBuffer(content)) {
content = new Buffer(content);
Expand Down Expand Up @@ -79,21 +79,23 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
} else {
if (sourceMap && sourceMap.sources && sourceMap.sources.length > 0)
filePath = sourceMap.sources[0];
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 (filePath) {
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 (sourceMap && sourceMap.sourcesContent)
this._sourceMap.setSourceContent(filePath, sourceMap.sourcesContent[0]);
}
if (sourceMap && sourceMap.sourcesContent)
this._sourceMap.setSourceContent(filePath, sourceMap.sourcesContent[0]);
}
if (lines > 1)
this.columnOffset = 0;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "concat-with-sourcemaps",
"version": "1.0.3",
"version": "1.0.4",
"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",
Expand Down
22 changes: 20 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function testCase(description, options) {
// content as Buffer
var concat = new Concat(options.sourceMapping, options.outFile, options.separator);
options.input.forEach(function(input, i) {
concat.add(input.fileName || 'test'+(i+1), new Buffer(input.content), input.sourceMap);
concat.add((input.fileName !== undefined ? input.fileName : 'test'+(i+1)), new Buffer(input.content), input.sourceMap);
});
t.equal(concat.content.toString(), options.output.content, 'should produce the right output');
if (options.output.sourceMap)
Expand All @@ -19,7 +19,7 @@ function testCase(description, options) {
// content as string
concat = new Concat(options.sourceMapping, options.outFile, options.separator);
options.input.forEach(function(input, i) {
concat.add(input.fileName || 'test'+(i+1), input.content, input.sourceMap);
concat.add((input.fileName !== undefined ? input.fileName : 'test'+(i+1)), input.content, input.sourceMap);
});
t.equal(concat.content.toString(), options.output.content, 'should produce the right output');
if (options.output.sourceMap)
Expand Down Expand Up @@ -326,3 +326,21 @@ testCase('should not crash with an input source map with no mappings', {
sourceMap: '{"version":3,"file":"out.js","sources":["intermediate.js", "test2", "test3"],"names":[],"mappings":"AAAA;AACA;AACA;ACFA;ACAA"}'
}
});

testCase('should allow content without filename and produce no mapping for it', {
separator: '\n',
sourceMapping: true,
outFile: 'out.js',
input: [
{ content: '// Header', fileName: null },
{ content: 'AA\nA' },
{ content: 'BBB' },
{ content: '// inbetween', fileName: null },
{ content: 'CC\nC' },
{ content: '// Footer', fileName: null }
],
output: {
content: '// Header\nAA\nA\nBBB\n// inbetween\nCC\nC\n// Footer',
sourceMap: '{"version":3,"file":"out.js","sources":["test2","test3","test5"],"names":[],"mappings":";AAAA;AACA;ACDA;;ACAA;AACA"}'
}
});

0 comments on commit 73d8e16

Please sign in to comment.