From a2e465494cd6f81c10304b450d4ae1d99e0edf98 Mon Sep 17 00:00:00 2001 From: Billow Z Date: Mon, 8 Jul 2019 14:21:55 +0800 Subject: [PATCH] fix: update dependencies and fix sourcemap support Closes #21 --- README.md | 20 ++++++++--- package.json | 6 ++-- src/index.js | 47 ++++++++++++++----------- test/test.js | 96 +++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 113 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 6511c4f..09c3dc6 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,18 @@ Can be a minimatch pattern or an array of minimatch patterns. If is omitted or o Can be a minimatch pattern or an array of minimatch patterns. Files to exclude, commonly the test files. +#### `extensions` + +Array of strings that specifies the file extensions to process. default: ['js'] + +#### `compact` + +generate compact code. default: `true` + +#### `sourceMap` + +produce a source map for the instrumented code. default: `true` + #### `instrumenterConfig` An object of options that will be passed to the instrumenter. @@ -59,14 +71,12 @@ Default value: ```js { esModules: true, - codeGenerationOptions: { - sourceMap: id, - sourceMapWithCode: true - } + compact: true, + produceSourceMap: true } ``` -[More info](http://gotwarlost.github.io/istanbul/public/apidocs/classes/Instrumenter.html#method_Instrumenter) about options. +[More info](https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-instrument/api.md#parameters-1) about options. #### `instrumenter` diff --git a/package.json b/package.json index d5e932e..b5726f9 100644 --- a/package.json +++ b/package.json @@ -33,15 +33,15 @@ "lint": "eslint src/index.js test/*.js" }, "dependencies": { - "istanbul-lib-instrument": "^1.9.1", - "rollup-pluginutils": "^2.0.1" + "istanbul-lib-instrument": "^3.3.0", + "rollup-pluginutils": "^2.8.1" }, "devDependencies": { "babel-cli": "^6.26.0", "babel-preset-env": "^1.6.1", "eslint": "^4.12.1", "mocha": "^4.0.1", - "rollup": "^0.52.1", + "rollup": "^1.0.0", "rollup-plugin-babel": "^3.0.2" }, "repository": { diff --git a/src/index.js b/src/index.js index f436d51..ff0e458 100644 --- a/src/index.js +++ b/src/index.js @@ -1,34 +1,41 @@ import { createFilter } from 'rollup-pluginutils'; import istanbul from 'istanbul-lib-instrument'; +import { extname } from 'path'; + +function makeFilter (opts, extensions) { + const filter = createFilter(opts.include, opts.exclude); + + extensions = opts.extensions || extensions; + if (!extensions || extensions === '*') { + return filter; + } + + if (!Array.isArray(extensions)) { + extensions = [extensions]; + } + extensions = extensions.map(e => (e[0] !== '.' ? `.${e}` : e)); + + return id => filter(id) && extensions.indexOf(extname(id)) > -1; +} export default function (options = {}) { - const filter = createFilter(options.include, options.exclude); + const filter = makeFilter(options, ['.js']), + opts = Object.assign( + { esModules: true, compact: options.compact !== false }, + options.instrumenterConfig, + { produceSourceMap: options.sourceMap !== false } + ), + instrumenter = new (options.instrumenter || istanbul).createInstrumenter(opts); return { + name: 'istanbul', transform (code, id) { if (!filter(id)) return; - var instrumenter; - var sourceMap = !!options.sourceMap; - var opts = Object.assign({}, options.instrumenterConfig); - - if (sourceMap) { - opts.codeGenerationOptions = Object.assign({}, - opts.codeGenerationOptions || {format: {compact: !opts.noCompact}}, - {sourceMap: id, sourceMapWithCode: true} - ); - } - - opts.esModules = true; - instrumenter = new (options.instrumenter || istanbul).createInstrumenter(opts); - + // TODO require the inputSourceMap that generated by the previous plugins code = instrumenter.instrumentSync(code, id); - var map = sourceMap ? - instrumenter.lastSourceMap().toJSON() : - {mappings: ''}; - - return { code, map }; + return { code, map: instrumenter.lastSourceMap() }; } }; } diff --git a/test/test.js b/test/test.js index b768f25..fdc179e 100644 --- a/test/test.js +++ b/test/test.js @@ -1,39 +1,79 @@ -var assert = require('assert'); -var rollup = require('rollup'); -var istanbulPlugin = require( '..' ); +const assert = require('assert'); +const rollup = require('rollup'); +const istanbulPlugin = require('..'); -process.chdir( __dirname ); +process.chdir(__dirname); describe('rollup-plugin-istanbul', function () { this.timeout(15000); it('transforms code through istanbul instrumenter', function () { - return rollup.rollup({ - input: 'fixtures/main.js', - plugins: [ istanbulPlugin() ], - globals: { - whatever: 'whatever' - } - }).then( function ( bundle ) { - return bundle.generate({format: 'iife'}); - }).then(generated => { - var code = generated.code; - assert.ok(code.indexOf('coverage[path]') !== -1, code); - }); + return rollup + .rollup({ + input: 'fixtures/main.js', + plugins: [istanbulPlugin()] + }) + .then(bundle => + bundle.generate({ + format: 'iife', + globals: { + whatever: 'whatever' + } + }) + ) + .then(generated => { + const code = generated.output[0].code; + assert.ok(code.indexOf('coverage[path]') !== -1, code); + }); }); it('adds the file name properly', function () { - return rollup.rollup({ - input: 'fixtures/main.js', - plugins: [ istanbulPlugin() ], - globals: { - whatever: 'whatever' - } - }).then( function ( bundle ) { - return bundle.generate({format: 'iife'}); - }).then(generated => { - var code = generated.code; - assert.ok(code.indexOf('fixtures/main.js') !== -1, code); - }); + return rollup + .rollup({ + input: 'fixtures/main.js', + plugins: [istanbulPlugin()] + }) + .then(bundle => + bundle.generate({ + format: 'iife', + globals: { + whatever: 'whatever' + } + }) + ) + .then(generated => { + const code = generated.output[0].code; + assert.ok(/(\/|\\\\)fixtures(\/|\\\\)main\.js"/.test(code), code); + }); + }); + + it('transforms code through istanbul instrumenter with source map', function () { + return rollup + .rollup({ + input: 'fixtures/main.js', + plugins: [istanbulPlugin()] + }) + .then(bundle => + bundle.generate({ + sourcemap: true, + format: 'iife', + globals: { + whatever: 'whatever' + } + }) + ) + .then(generated => { + const { code, map } = generated.output[0]; + + assert.ok(code.indexOf('coverage[path]') !== -1, code); + assert.ok(/(\/|\\\\)fixtures(\/|\\\\)main\.js"/.test(code), code); + + assert.ok(map.sources[0] === 'fixtures/main.js', code); + assert.ok( + map.sourcesContent[0] === + 'function foo(bar) {\n if (bar) {\n whatever.do();\n } else {\n whatever.stop();\n }\n}\n', + map + ); + }); }); });