From 88eeffac388b09d6f35272257ec0be38f4beef02 Mon Sep 17 00:00:00 2001 From: Justin Bay Date: Fri, 16 Dec 2016 22:33:04 -0500 Subject: [PATCH] Add toggle for picking up inline source maps. These can take up a lot of memory for large projects, especially if the original source is inlined into the source map. --- README.md | 34 ++++++++++++++++++++++++++++ fixtures/has-inline-source-map.js | 12 ++++++++++ package.json | 5 +++++ src/index.js | 6 ++++- test/babel-plugin-istanbul.js | 37 +++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 fixtures/has-inline-source-map.js diff --git a/README.md b/README.md index 436c937..dbbdf2d 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,40 @@ If you don't provide options in your Babel config, the plugin will look for `exc You can also use [istanbul's ignore hints](https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes) to specify specific lines of code to skip instrumenting. +## Source Maps + +By default, this plugin will pick up inline source maps and attach them to the instrumented code such that code coverage can be remapped back to the original source, even for multi-step build processes. This can be memory intensive. Set `useInlineSourceMaps` to prevent this behavior. + +```json +{ + "env": { + "test": { + "plugins": [ + ["istanbul", { + "useInlineSourceMaps": false + }] + ] + } + } +} +``` + +If you're instrumenting code programatically, you can pass a source map explicitly. +```js +import babelPluginIstanbul from 'babel-plugin-istanbul'; + +function instrument(sourceCode, sourceMap, fileName) { + return babel.transform(sourceCode, { + filename, + plugins: [ + [babelPluginIstanbul, { + inputSourceMap: sourceMap + }] + ] + }) +} +``` + ## Credit where credit is due The approach used in `babel-plugin-istanbul` was inspired by [Thai Pangsakulyanont](https://github.com/dtinth)'s original library [`babel-plugin-__coverage__`](https://github.com/dtinth/babel-plugin-__coverage__). diff --git a/fixtures/has-inline-source-map.js b/fixtures/has-inline-source-map.js new file mode 100644 index 0000000..0b4f480 --- /dev/null +++ b/fixtures/has-inline-source-map.js @@ -0,0 +1,12 @@ +"use strict"; +var Say = (function () { + function Say() { + } + Say.prototype.hello = function () { + console.log('hello'); + }; + return Say; +}()); +exports.__esModule = true; +exports["default"] = Say; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ2hlY2tib3gudGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIkNoZWNrYm94LnRlc3QudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTtJQUFBO0lBSUEsQ0FBQztJQUhDLG1CQUFLLEdBQUw7UUFDRSxPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQ3ZCLENBQUM7SUFDSCxVQUFDO0FBQUQsQ0FBQyxBQUpELElBSUMiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCBjbGFzcyBTYXkge1xyXG4gIGhlbGxvKCkge1xyXG4gICAgY29uc29sZS5sb2coJ2hlbGxvJyk7XHJcbiAgfVxyXG59Il19" \ No newline at end of file diff --git a/package.json b/package.json index fa96036..d36b0db 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,11 @@ "prepublish": "npm test && npm run release", "version": "standard-version" }, + "standard": { + "ignore": [ + "fixtures/has-inline-source-map.js" + ] + }, "repository": { "type": "git", "url": "git+https://github.com/istanbuljs/babel-plugin-istanbul.git" diff --git a/src/index.js b/src/index.js index 7a28594..13b7de8 100644 --- a/src/index.js +++ b/src/index.js @@ -43,9 +43,13 @@ function makeVisitor ({types: t}) { if (shouldSkip(realPath, this.opts)) { return } + let { inputSourceMap } = this.opts + if (this.opts.useInlineSourceMaps !== false) { + inputSourceMap = inputSourceMap || this.file.opts.inputSourceMap + } this.__dv__ = programVisitor(t, realPath, { coverageVariable: '__coverage__', - inputSourceMap: this.opts.inputSourceMap || this.file.opts.inputSourceMap + inputSourceMap }) this.__dv__.enter(path) }, diff --git a/test/babel-plugin-istanbul.js b/test/babel-plugin-istanbul.js index 0a8dbf3..2856150 100644 --- a/test/babel-plugin-istanbul.js +++ b/test/babel-plugin-istanbul.js @@ -32,6 +32,43 @@ describe('babel-plugin-istanbul', function () { }) }) + context('source maps', function () { + it('should use inline source map', function () { + var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { + plugins: [ + [makeVisitor({types: babel.types}), { + include: ['fixtures/has-inline-source-map.js'] + }] + ] + }) + result.code.should.match(/inputSourceMap/) + }) + + it('should not use inline source map if inputSourceMap is set to false', function () { + var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { + plugins: [ + [makeVisitor({types: babel.types}), { + include: ['fixtures/has-inline-source-map.js'], + useInlineSourceMaps: false + }] + ] + }) + result.code.should.not.match(/inputSourceMap/) + }) + + it('should use provided source map', function () { + var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { + plugins: [ + [makeVisitor({types: babel.types}), { + include: ['fixtures/has-inline-source-map.js'], + inputSourceMap: { asdfQwer: 'foo' } + }] + ] + }) + result.code.should.match(/inputSourceMap:\s*{\s*asdfQwer: "foo"\s*}/) + }) + }) + context('package.json "nyc" config', function () { it('should instrument file if shouldSkip returns false', function () { var result = babel.transformFileSync('./fixtures/should-cover.js', {