Skip to content

Commit

Permalink
Merge 9d7fa53 into 6f546c3
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 1, 2017
2 parents 6f546c3 + 9d7fa53 commit 557a79f
Show file tree
Hide file tree
Showing 6 changed files with 860 additions and 3 deletions.
34 changes: 33 additions & 1 deletion README.md
@@ -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
106 changes: 106 additions & 0 deletions index.js
@@ -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;
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -17,13 +17,17 @@
"index.js"
],
"scripts": {
"lint": "eslint . && jscs index.js test/",
"lint": "eslint . && jscs index.js test/index.js",
"pretest": "npm run lint",
"test": "mocha --async-only",
"cover": "istanbul cover _mocha --report lcovonly",
"coveralls": "npm run cover && istanbul-coveralls"
},
"dependencies": {},
"dependencies": {
"convert-source-map": "thlorenz/convert-source-map#comment-values",
"normalize-path": "^2.0.1",
"through2": "^2.0.3"
},
"devDependencies": {
"eslint": "^1.7.3",
"eslint-config-gulp": "^2.0.0",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/helloworld.js
@@ -0,0 +1,5 @@
'use strict';

function helloWorld() {
console.log('Hello world!');
}
7 changes: 7 additions & 0 deletions test/fixtures/helloworld.map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 557a79f

Please sign in to comment.