Skip to content

Implementation? #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
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
106 changes: 106 additions & 0 deletions index.js
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;
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.

Loading