Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
860 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,34 @@ | ||
# comment | ||
# @gulp-sourcemaps/comment | ||
|
||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] | ||
|
||
Gulp plugin for working with the sourceMappingURL comment of a file. | ||
|
||
## Example | ||
|
||
```js | ||
TODO | ||
``` | ||
|
||
## API | ||
|
||
### TODO | ||
|
||
## License | ||
|
||
MIT | ||
|
||
[vinyl-url]: https://github.com/gulpjs/vinyl | ||
|
||
[downloads-image]: http://img.shields.io/npm/dm/@gulp-sourcemaps/comment.svg | ||
[npm-url]: https://npmjs.org/package/@gulp-sourcemaps/comment | ||
[npm-image]: http://img.shields.io/npm/v/@gulp-sourcemaps/comment.svg | ||
|
||
[travis-url]: https://travis-ci.org/gulp-sourcemaps/comment | ||
[travis-image]: http://img.shields.io/travis/gulp-sourcemaps/comment.svg?label=travis-ci | ||
|
||
[appveyor-url]: https://ci.appveyor.com/project/phated/comment | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/phated/comment.svg?label=appveyor | ||
|
||
[coveralls-url]: https://coveralls.io/r/gulp-sourcemaps/comment | ||
[coveralls-image]: http://img.shields.io/coveralls/gulp-sourcemaps/comment.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var through = require('through2'); | ||
var convert = require('convert-source-map'); | ||
var normalize = require('normalize-path'); | ||
|
||
function processInline(contents, mapper) { | ||
var sourceMappingURL = convert.getCommentValue(contents); | ||
|
||
var result = mapper(sourceMappingURL); | ||
|
||
if (!result) { | ||
return convert.removeComments(contents); | ||
} | ||
|
||
if (result !== sourceMappingURL) { | ||
// TODO: use the same comment type as original | ||
// TODO: we don't want to parse/convert so this works but should be named different | ||
result = convert.generateMapFileComment(result); | ||
// TODO: add `replaceComments` to convert-source-map | ||
return contents.replace(convert.commentRegex, result); | ||
} | ||
} | ||
|
||
function processExternal(contents, mapper) { | ||
var sourceMappingURL = convert.getMapFileCommentValue(contents); | ||
|
||
var result = mapper(sourceMappingURL); | ||
|
||
if (!result) { | ||
return convert.removeMapFileComments(contents); | ||
} | ||
|
||
if (result !== sourceMappingURL) { | ||
// TODO: use the same comment type as original | ||
result = convert.generateMapFileComment(result); | ||
// TODO: add `replaceMapFileComments` to convert-source-map | ||
return contents.replace(convert.mapFileCommentRegex, result); | ||
} | ||
} | ||
|
||
function comment(mapFn) { | ||
|
||
function transform(file, _, cb) { | ||
// TODO: should this error? Probably not | ||
if (!file.isBuffer()) { | ||
return cb(null, file); | ||
} | ||
|
||
var contents = file.contents.toString(); | ||
|
||
var hasInlineSourcemap = convert.commentRegex.test(contents); | ||
var hasExternalSourcemap = convert.mapFileCommentRegex.test(contents); | ||
|
||
if (!hasInlineSourcemap && !hasExternalSourcemap) { | ||
return cb(null, file); | ||
} | ||
|
||
function mapper(sourceMappingURL) { | ||
var result = sourceMappingURL; | ||
if (typeof mapFn === 'function') { | ||
result = mapFn(sourceMappingURL, file); | ||
} | ||
|
||
// This is inverted because hasExternalSourcemap covers inline also | ||
if (hasInlineSourcemap || !result) { | ||
return result; | ||
} | ||
|
||
return normalize(result); | ||
} | ||
|
||
// Always one of the 2 because we bail if neither | ||
var processor = hasInlineSourcemap ? processInline : processExternal; | ||
|
||
var result = processor(contents, mapper); | ||
|
||
if (result) { | ||
file.contents = new Buffer(result); | ||
} | ||
|
||
return cb(null, file); | ||
} | ||
|
||
return through.obj(transform); | ||
} | ||
|
||
function prefix(str) { | ||
// TODO: should this somehow check if it is a path vs data-uri? | ||
return comment(function(sourceMappingURL) { | ||
// TODO: url instead of path? | ||
return str + path.join('/', sourceMappingURL); | ||
}); | ||
} | ||
comment.prefix = prefix; | ||
|
||
function remove() { | ||
return comment(function() { | ||
// Returning anything falsey removes the comment | ||
return null; | ||
}); | ||
} | ||
comment.remove = remove; | ||
|
||
module.exports = comment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
function helloWorld() { | ||
console.log('Hello world!'); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.