vite-plugin-istanbul
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -1,2 +0,2 @@ | ||
## [2.1.2](https://github.com/ifaxity/vite-plugin-istanbul/compare/v2.1.1...v2.1.2) (2021-06-29) | ||
# [2.2.0](https://github.com/ifaxity/vite-plugin-istanbul/compare/v2.1.2...v2.2.0) (2021-07-14) | ||
@@ -6,2 +6,10 @@ | ||
* should ignore user provided babel configs ([12d7b54](https://github.com/ifaxity/vite-plugin-istanbul/commit/12d7b54b78cc0b7ef8f1c577f93c318ca0b2edc8)) | ||
* remove the vue specific code and avoid transforming in prod ([a8d8110](https://github.com/ifaxity/vite-plugin-istanbul/commit/a8d8110b10fe883dc3177d5d00edbb044b5f1a4c)) | ||
* remove useless regexp ([44b24b2](https://github.com/ifaxity/vite-plugin-istanbul/commit/44b24b2f7e4f297b7a725243f22f48c7e49cf949)) | ||
* Swapped package babel-plugin-istanbul to istanbul-lib-instrument ([4727c56](https://github.com/ifaxity/vite-plugin-istanbul/commit/4727c569d6a9fc2b43d4fc68f63b333a04c99456)) | ||
* use a strict typescript and make the plugin a post ([59e7132](https://github.com/ifaxity/vite-plugin-istanbul/commit/59e71327d857369581d2ef80b7b76068f7813289)) | ||
### Features | ||
* accept babelConfig option making ts possible ([a0c7aa7](https://github.com/ifaxity/vite-plugin-istanbul/commit/a0c7aa7aa9e7eaa279b8130a01f0a107f77adf2e)), closes [#1](https://github.com/ifaxity/vite-plugin-istanbul/issues/1) |
"use strict"; | ||
const core_1 = require("@babel/core"); | ||
const babel_plugin_istanbul_1 = require("babel-plugin-istanbul"); | ||
const TestExclude = require("test-exclude"); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
const istanbul_lib_instrument_1 = require("istanbul-lib-instrument"); | ||
const test_exclude_1 = __importDefault(require("test-exclude")); | ||
// Custom extensions to include .vue files | ||
const DEFAULT_EXTENSION = ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue']; | ||
const COVERAGE_PUBLIC_PATH = '/__coverage__'; | ||
const PLUGIN_NAME = 'vite:istanbul'; | ||
function sanitizeSourceMap(sourceMap) { | ||
// JSON parse/stringify trick required for istanbul to accept the SourceMap | ||
return JSON.parse(JSON.stringify(sourceMap)); | ||
} | ||
function createConfigureServer() { | ||
return ({ middlewares }) => { | ||
// Return global code coverage (will probably be null). | ||
// Returns the current code coverage in the global scope | ||
middlewares.use((req, res, next) => { | ||
@@ -28,73 +37,48 @@ var _a; | ||
} | ||
function transformCode(srcCode, id, opts) { | ||
const plugins = [[babel_plugin_istanbul_1.default, opts]]; | ||
const cwd = process.cwd(); | ||
const { code, map } = core_1.transformSync(srcCode, { | ||
plugins, cwd, | ||
filename: id, | ||
ast: false, | ||
sourceMaps: true, | ||
comments: true, | ||
compact: true, | ||
babelrc: false, | ||
configFile: false, | ||
parserOpts: { | ||
allowReturnOutsideFunction: true, | ||
sourceType: 'module', | ||
}, | ||
// Only keep primitive properties | ||
inputSourceMap: JSON.parse(JSON.stringify(this.getCombinedSourcemap())), | ||
}); | ||
// Required to cast to correct mapping value | ||
return { code, map: JSON.parse(JSON.stringify(map)) }; | ||
} | ||
function createTransform(opts = {}) { | ||
const exclude = new TestExclude({ | ||
var _a; | ||
const exclude = new test_exclude_1.default({ | ||
cwd: process.cwd(), | ||
include: opts.include, | ||
exclude: opts.exclude, | ||
extension: opts.extension, | ||
extension: (_a = opts.extension) !== null && _a !== void 0 ? _a : DEFAULT_EXTENSION, | ||
excludeNodeModules: true, | ||
}); | ||
const instrumenter = istanbul_lib_instrument_1.createInstrumenter({ | ||
preserveComments: true, | ||
produceSourceMap: true, | ||
autoWrap: true, | ||
esModules: true, | ||
}); | ||
return function (srcCode, id) { | ||
var _a, _b; | ||
if (process.env.NODE_ENV == 'production' || id.startsWith('/@modules/')) { | ||
if (id.startsWith('/@modules/')) { | ||
// do not transform if this is a dep | ||
// do not transform for production builds | ||
return; | ||
} | ||
if (exclude.shouldInstrument(id)) { | ||
if (!id.endsWith('.vue')) { | ||
return transformCode.call(this, srcCode, id, opts); | ||
} | ||
// Vue files are special, it requires a hack to fix the source mappings | ||
// We take the source code from within the <script> tag and instrument this | ||
// Then we pad the lines to get the correct line numbers for the mappings | ||
let startIndex = srcCode.indexOf('<script>'); | ||
const endIndex = srcCode.indexOf('</script>'); | ||
if (startIndex == -1 || endIndex == -1) { | ||
// ignore this vue file, doesn't contain any javascript | ||
return; | ||
} | ||
const lines = (_b = (_a = srcCode.slice(0, endIndex).match(/\n/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; | ||
const startOffset = '<script>'.length; | ||
srcCode = '\n'.repeat(lines) + srcCode.slice(startIndex + startOffset, endIndex); | ||
const res = transformCode.call(this, srcCode, id, opts); | ||
res.code = `${srcCode.slice(0, startIndex + startOffset)}\n${res.code}\n${srcCode.slice(endIndex)}`; | ||
return res; | ||
const sourceMap = sanitizeSourceMap(this.getCombinedSourcemap()); | ||
const code = instrumenter.instrumentSync(srcCode, id, sourceMap); | ||
const map = instrumenter.lastSourceMap(); | ||
// Required to cast to correct mapping value | ||
return { code, map }; | ||
} | ||
}; | ||
} | ||
function istanbulPlugin(opts) { | ||
function istanbulPlugin(opts = {}) { | ||
var _a; | ||
// Only instrument when we want to, as we only want instrumentation in test | ||
// By default the plugin is always on | ||
const env = opts.cypress ? process.env.CYPRESS_COVERAGE : process.env.VITE_COVERAGE; | ||
const requireEnv = (_a = opts.requireEnv) !== null && _a !== void 0 ? _a : false; | ||
if (requireEnv && (env === null || env === void 0 ? void 0 : env.toLowerCase()) === 'false') { | ||
return { name: 'vite:istanbul' }; | ||
if (process.env.NODE_ENV == 'production' && requireEnv && (env === null || env === void 0 ? void 0 : env.toLowerCase()) === 'false') { | ||
return { name: PLUGIN_NAME }; | ||
} | ||
return { | ||
name: 'vite:istanbul', | ||
name: PLUGIN_NAME, | ||
transform: createTransform(opts), | ||
configureServer: createConfigureServer(), | ||
// istanbul only knows how to instrument JavaScript, | ||
// this allows us to wait until the whole code is JavaScript to | ||
// instrument and sourcemap | ||
enforce: 'post', | ||
}; | ||
@@ -101,0 +85,0 @@ } |
{ | ||
"name": "vite-plugin-istanbul", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"author": "iFaxity (christian@faxity.se)", | ||
@@ -32,14 +32,12 @@ "license": "MIT", | ||
"dependencies": { | ||
"vite": "^2.1.2" | ||
"istanbul-lib-instrument": "^4.0.3", | ||
"test-exclude": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.11.1", | ||
"@types/babel__core": "^7.1.12", | ||
"@types/node": "^14.0.27", | ||
"babel-plugin-istanbul": "^6.0.0", | ||
"test-exclude": "^6.0.0", | ||
"typescript": "^3.9.7", | ||
"@semantic-release/changelog": "^5.0.1", | ||
"@semantic-release/git": "^9.0.0" | ||
"@semantic-release/git": "^9.0.0", | ||
"@types/node": "^16.3.1", | ||
"typescript": "^4.3.5", | ||
"vite": "^2.4.2" | ||
} | ||
} |
@@ -9,3 +9,3 @@ vite-plugin-istanbul | ||
A Vite plugin to instrument your code for nyc/istanbul code coverage. In similar way as the Webpack Loader istanbul-instrumenter-loader. Only intended for use in development. | ||
A Vite plugin to instrument your code for nyc/istanbul code coverage. In similar way as the Webpack Loader istanbul-instrumenter-loader. Only intended for use in development while running tests. | ||
@@ -41,5 +41,5 @@ Version v2.x for Vite v2.0, for Vite v1.0 install v1.x of this plugin. | ||
* `opts.exclude {string|string[]}` - Optional string or array of strings of glob patterns to exclude | ||
* `opts.extension {string|string[]}` - Optional string or array of strings of extensions to include (dot prefixed like .js or .ts) | ||
* `opts.extension {string|string[]}` - Optional string or array of strings of extensions to include (dot prefixed like .js or .ts). By default this is set to `['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue']` | ||
* `opts.requireEnv {boolean}` - Optional boolean to require env to be true to instrument to code, otherwise it will instrument even if env variable is not set | ||
* `opts.cypress {boolean}` - Optional boolean to change the env to CYPRESS_COVERAGE instead of VITE_COVERAGE. For more ease of use with @cypress/code-coverage | ||
* `opts.cypress {boolean}` - Optional boolean to change the env to CYPRESS_COVERAGE instead of VITE_COVERAGE. For ease of use with @cypress/code-coverage | ||
@@ -53,5 +53,5 @@ Examples | ||
// vite.config.js | ||
const istanbul = require('vite-plugin-istanbul'); | ||
import istanbul from 'vite-plugin-istanbul'; | ||
module.exports = { | ||
export default { | ||
open: true, | ||
@@ -63,3 +63,4 @@ port: 3000, | ||
exclude: ['node_modules', 'test/'], | ||
extension: [ '.js', '.ts' ], | ||
extension: [ '.js', '.ts', '.vue' ], | ||
requireEnv: true, | ||
}), | ||
@@ -66,0 +67,0 @@ ], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
5
71
12676
2
98
+ Addedtest-exclude@^6.0.0
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.24.7(transitive)
+ Added@babel/compat-data@7.24.7(transitive)
+ Added@babel/core@7.24.7(transitive)
+ Added@babel/generator@7.24.7(transitive)
+ Added@babel/helper-compilation-targets@7.24.7(transitive)
+ Added@babel/helper-environment-visitor@7.24.7(transitive)
+ Added@babel/helper-function-name@7.24.7(transitive)
+ Added@babel/helper-hoist-variables@7.24.7(transitive)
+ Added@babel/helper-module-imports@7.24.7(transitive)
+ Added@babel/helper-module-transforms@7.24.7(transitive)
+ Added@babel/helper-simple-access@7.24.7(transitive)
+ Added@babel/helper-split-export-declaration@7.24.7(transitive)
+ Added@babel/helper-string-parser@7.24.7(transitive)
+ Added@babel/helper-validator-identifier@7.24.7(transitive)
+ Added@babel/helper-validator-option@7.24.7(transitive)
+ Added@babel/helpers@7.24.7(transitive)
+ Added@babel/highlight@7.24.7(transitive)
+ Added@babel/parser@7.24.7(transitive)
+ Added@babel/template@7.24.7(transitive)
+ Added@babel/traverse@7.24.7(transitive)
+ Added@babel/types@7.24.7(transitive)
+ Added@istanbuljs/schema@0.1.3(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.4.15(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbrowserslist@4.23.1(transitive)
+ Addedcaniuse-lite@1.0.30001640(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addeddebug@4.3.5(transitive)
+ Addedelectron-to-chromium@1.4.819(transitive)
+ Addedescalade@3.1.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedistanbul-lib-coverage@3.2.2(transitive)
+ Addedistanbul-lib-instrument@4.0.3(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@2.5.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedms@2.1.2(transitive)
+ Addednode-releases@2.0.14(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedtest-exclude@6.0.0(transitive)
+ Addedto-fast-properties@2.0.0(transitive)
+ Addedupdate-browserslist-db@1.1.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyallist@3.1.1(transitive)
- Removedvite@^2.1.2
- Removed@esbuild/linux-loong64@0.14.54(transitive)
- Removedesbuild@0.14.54(transitive)
- Removedesbuild-android-64@0.14.54(transitive)
- Removedesbuild-android-arm64@0.14.54(transitive)
- Removedesbuild-darwin-64@0.14.54(transitive)
- Removedesbuild-darwin-arm64@0.14.54(transitive)
- Removedesbuild-freebsd-64@0.14.54(transitive)
- Removedesbuild-freebsd-arm64@0.14.54(transitive)
- Removedesbuild-linux-32@0.14.54(transitive)
- Removedesbuild-linux-64@0.14.54(transitive)
- Removedesbuild-linux-arm@0.14.54(transitive)
- Removedesbuild-linux-arm64@0.14.54(transitive)
- Removedesbuild-linux-mips64le@0.14.54(transitive)
- Removedesbuild-linux-ppc64le@0.14.54(transitive)
- Removedesbuild-linux-riscv64@0.14.54(transitive)
- Removedesbuild-linux-s390x@0.14.54(transitive)
- Removedesbuild-netbsd-64@0.14.54(transitive)
- Removedesbuild-openbsd-64@0.14.54(transitive)
- Removedesbuild-sunos-64@0.14.54(transitive)
- Removedesbuild-windows-32@0.14.54(transitive)
- Removedesbuild-windows-64@0.14.54(transitive)
- Removedesbuild-windows-arm64@0.14.54(transitive)
- Removedfsevents@2.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedis-core-module@2.14.0(transitive)
- Removednanoid@3.3.7(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpostcss@8.4.39(transitive)
- Removedresolve@1.22.8(transitive)
- Removedrollup@2.77.3(transitive)
- Removedsource-map-js@1.2.0(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedvite@2.9.18(transitive)