vite-plugin-istanbul
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -6,2 +6,4 @@ import type { Plugin } from 'vite'; | ||
extension?: string | string[]; | ||
requireEnv?: boolean; | ||
cypress?: boolean; | ||
} | ||
@@ -8,0 +10,0 @@ declare global { |
@@ -7,5 +7,5 @@ "use strict"; | ||
function createConfigureServer() { | ||
return ({ app }) => { | ||
return ({ middlewares }) => { | ||
// Return global code coverage (will probably be null). | ||
app.use((req, res, next) => { | ||
middlewares.use((req, res, next) => { | ||
var _a; | ||
@@ -29,7 +29,25 @@ if (req.url !== COVERAGE_PUBLIC_PATH) { | ||
} | ||
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, | ||
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 plugins = [babel_plugin_istanbul_1.default]; | ||
const cwd = process.cwd(); | ||
const exclude = new TestExclude({ | ||
cwd, | ||
cwd: process.cwd(), | ||
include: opts.include, | ||
@@ -41,2 +59,3 @@ exclude: opts.exclude, | ||
return function (srcCode, id) { | ||
var _a, _b; | ||
if (process.env.NODE_ENV == 'production' || id.startsWith('/@modules/')) { | ||
@@ -48,10 +67,20 @@ // do not transform if this is a dep | ||
if (exclude.shouldInstrument(id)) { | ||
const { code, map } = core_1.transformSync(srcCode, { | ||
plugins, cwd, | ||
filename: id, | ||
ast: false, | ||
sourceMaps: true, | ||
inputSourceMap: this.getCombinedSourcemap(), | ||
}); | ||
return { code, map }; | ||
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; | ||
} | ||
@@ -61,2 +90,8 @@ }; | ||
function istanbulPlugin(opts) { | ||
// Only instrument when we want to, as we only want instrumentation in test | ||
const env = opts.cypress ? process.env.CYPRESS_COVERAGE : process.env.VITE_COVERAGE; | ||
const defaultValue = opts.requireEnv ? '' : 'true'; | ||
if ((env || defaultValue).toLowerCase() == 'true') { | ||
return { name: 'vite:istanbul' }; | ||
} | ||
return { | ||
@@ -63,0 +98,0 @@ name: 'vite:istanbul', |
{ | ||
"name": "vite-plugin-istanbul", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "iFaxity (christian@faxity.se)", | ||
"license": "MIT", | ||
"module": "dist/index.js", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist/*", | ||
"src/*" | ||
"dist/*" | ||
], | ||
@@ -32,4 +31,4 @@ "scripts": { | ||
], | ||
"peerDependencies": { | ||
"vite": "^2.0.0-beta.12" | ||
"dependencies": { | ||
"vite": "^2.1.2" | ||
}, | ||
@@ -36,0 +35,0 @@ "devDependencies": { |
@@ -11,4 +11,6 @@ vite-plugin-istanbul | ||
Version v2.x for Vite v2.0, for Vite v1.0 install v1.x of this plugin | ||
Version v2.x for Vite v2.0, for Vite v1.0 install v1.x of this plugin. | ||
As of v2.1.0 you can toggle the coverage off by setting the env variable `VITE_COVERAGE='false'`, by default it will always instrument the code. To require the explicit definition of the variable, set the option `requireEnv` to **true**. | ||
Installation | ||
@@ -40,2 +42,4 @@ -------------------------- | ||
* `opts.extension {string|string[]}` - Optional string or array of strings of extensions to include (dot prefixed like .js or .ts) | ||
* `opts.requireEnv {string}` - Optional string to require env to be true to instrument to code, otherwise it will instrument even if env variable is not set | ||
* `opts.cypress {string}` - Optional string to change the env to CYPRESS_COVERAGE instead of VITE_COVERAGE. For more ease of use with @cypress/code-coverage | ||
@@ -42,0 +46,0 @@ Examples |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12920
70
0
7
111
3
+ Addedvite@^2.1.2