Socket
Socket
Sign inDemoInstall

vite-plugin-istanbul

Package Overview
Dependencies
2
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.2 to 2.2.0

12

CHANGELOG.md

@@ -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)

90

dist/index.js
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc