Skip to content

Commit

Permalink
New: Accept function for the clear option for conditional removal (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and phated committed Feb 19, 2019
1 parent 87fbe65 commit 812a8eb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ Takes a object containing options for this plugin.
Seting this option `true` will cause the sources content to be deleted instead
of initialized.

A function can be passed for clear, this allows removing sources content associated
with some files but not others. The function is called with `filename` argument for
each source, returning `true` causes the contents for that file to be cleared.

```js
gulp.src(...)
/* ... */
.pipe(sourcesContent({
clear: function(filename, mainFile) {
/* Clear all sourceContent elements except the one
* associated with sourceMap.file. */
return filename !== mainFile
}
}))
.pipe(gulp.dest(...))
```

## License

MIT
Expand Down
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ function sourcesContent(options) {
}

if (options.clear) {
delete sourceMap.sourcesContent;
var allClear = true;
if (typeof options.clear === 'function' && sourceMap.sources
&& sourceMap.sourcesContent) {
sourceMap.sources.forEach(function(filename, idx) {
if (options.clear(filename, sourceMap.file)) {
sourceMap.sourcesContent[idx] = null;
} else {
allClear = false;
}
});
}

if (allClear) {
delete sourceMap.sourcesContent;
}

return cb(null, file);
}
Expand Down
54 changes: 54 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,60 @@ describe('sourcesContent', function() {
runTest([file], assert, done, { clear: true });
});

it('clear function sets sourcesContent elements null but not sources', function(done) {
var file = makeFile();
file.sourceMap.sourcesContent = [helloWorld, helloWorld2];

function assert(files) {
expect(files.length).toEqual(1);
expect(typeof files[0].sourceMap).toEqual('object');
expect(files[0].sourceMap.sources.length).toEqual(2);
expect(files[0].sourceMap.sourcesContent.length).toEqual(2);
expect(files[0].sourceMap.sourcesContent[0]).toEqual(helloWorld);
expect(files[0].sourceMap.sourcesContent[1]).toEqual(null);
}

function clear(filename, mainFile) {
expect(mainFile).toEqual(file.sourceMap.file);
return filename !== mainFile;
}

runTest([file], assert, done, { clear: clear });
});

it('always true clear function deletes sourcesContent but not sources', function(done) {
var file = makeFile();
file.sourceMap.sourcesContent = [helloWorld, helloWorld2];

function assert(files) {
expect(files.length).toEqual(1);
expect(typeof files[0].sourceMap).toEqual('object');
expect(files[0].sourceMap.sources.length).toEqual(2);
expect(typeof files[0].sourceMap.sourcesContent).toEqual('undefined');
}

function clear() {
return true;
}

runTest([file], assert, done, { clear: clear });
});

it('clear deletes sourcesContent even with no sources', function(done) {
var file = makeFile();
file.sourceMap.sourcesContent = ['/**/', '/**/'];
delete file.sourceMap.sources;

function assert(files) {
expect(files.length).toEqual(1);
expect(typeof files[0].sourceMap).toEqual('object');
expect(typeof files[0].sourceMap.sources).toEqual('undefined');
expect(typeof files[0].sourceMap.sourcesContent).toEqual('undefined');
}

runTest([file], assert, done, { clear: true });
});

it('clear ignores a file without sourceMap property', function(done) {
var file = makeFile();
delete file.sourceMap;
Expand Down

0 comments on commit 812a8eb

Please sign in to comment.