vite-plugin-istanbul
Advanced tools
Comparing version 1.1.0 to 2.0.0
import type { Plugin } from 'vite'; | ||
import { IstanbulPluginOptions } from './transform'; | ||
interface IstanbulPluginOptions { | ||
include?: string | string[]; | ||
exclude?: string | string[]; | ||
extension?: string | string[]; | ||
} | ||
declare global { | ||
var __coverage__: any; | ||
} | ||
declare function istanbulPlugin(opts?: IstanbulPluginOptions): Plugin; | ||
export = istanbulPlugin; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
const transform_1 = require("./transform"); | ||
const server_1 = require("./server"); | ||
const core_1 = require("@babel/core"); | ||
const babel_plugin_istanbul_1 = require("babel-plugin-istanbul"); | ||
const TestExclude = require("test-exclude"); | ||
const COVERAGE_PUBLIC_PATH = '/__coverage__'; | ||
function createConfigureServer() { | ||
return ({ app }) => { | ||
// Return global code coverage (will probably be null). | ||
app.use((req, res, next) => { | ||
var _a; | ||
if (req.url !== COVERAGE_PUBLIC_PATH) { | ||
return next(); | ||
} | ||
const coverage = (_a = (global.__coverage__)) !== null && _a !== void 0 ? _a : null; | ||
let data; | ||
try { | ||
data = JSON.stringify(coverage, null, 4); | ||
} | ||
catch (ex) { | ||
return next(ex); | ||
} | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.statusCode = 200; | ||
res.end(data); | ||
}); | ||
}; | ||
} | ||
function createTransform(opts = {}) { | ||
const plugins = [babel_plugin_istanbul_1.default]; | ||
const cwd = process.cwd(); | ||
const exclude = new TestExclude({ | ||
cwd, | ||
include: opts.include, | ||
exclude: opts.exclude, | ||
extension: opts.extension, | ||
excludeNodeModules: true, | ||
}); | ||
return function (srcCode, id) { | ||
if (process.env.NODE_ENV == 'production' || id.startsWith('/@modules/')) { | ||
// do not transform if this is a dep | ||
// do not transform for production builds | ||
return; | ||
} | ||
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 }; | ||
} | ||
}; | ||
} | ||
function istanbulPlugin(opts) { | ||
const transform = transform_1.createTransform(opts); | ||
return { | ||
transforms: [transform], | ||
configureServer: server_1.serverPlugin, | ||
name: 'vite:istanbul', | ||
transform: createTransform(opts), | ||
configureServer: createConfigureServer(), | ||
}; | ||
@@ -10,0 +62,0 @@ } |
{ | ||
"name": "vite-plugin-istanbul", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"author": "iFaxity (christian@faxity.se)", | ||
@@ -33,12 +33,12 @@ "license": "MIT", | ||
"peerDependencies": { | ||
"vite": "^1.0.0-rc.4" | ||
"vite": "^2.0.0-beta.12" | ||
}, | ||
"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", | ||
"vite": "^1.0.0-rc.4" | ||
"typescript": "^3.9.7" | ||
} | ||
} |
@@ -11,9 +11,11 @@ vite-plugin-istanbul | ||
Version v2.x for Vite v2.0, for Vite v1.0 install v1.x of this plugin | ||
Installation | ||
-------------------------- | ||
`$ npm i -D vite-plugin-istanbul` | ||
`npm i -D vite-plugin-istanbul` | ||
or if you use yarn | ||
`$ yarn add -D vite-plugin-istanbul` | ||
`yarn add -D vite-plugin-istanbul` | ||
@@ -61,9 +63,2 @@ API | ||
Testing (TODO: not yet finished) | ||
-------------------------- | ||
```sh | ||
$ npm run test | ||
``` | ||
License | ||
@@ -70,0 +65,0 @@ -------------------------- |
@@ -1,15 +0,82 @@ | ||
import type { Plugin } from 'vite'; | ||
import { IstanbulPluginOptions, createTransform } from './transform'; | ||
import { serverPlugin } from './server'; | ||
import type { Plugin, ServerHook } from 'vite'; | ||
import type { TransformHook } from 'rollup'; | ||
import { transformSync } from '@babel/core'; | ||
import BabelPluginIstanbul from 'babel-plugin-istanbul'; | ||
import * as TestExclude from 'test-exclude'; | ||
interface IstanbulPluginOptions { | ||
include?: string|string[]; | ||
exclude?: string|string[]; | ||
extension?: string|string[]; | ||
} | ||
declare global { | ||
var __coverage__: any; | ||
} | ||
const COVERAGE_PUBLIC_PATH = '/__coverage__'; | ||
function createConfigureServer(): ServerHook { | ||
return ({ app }) => { | ||
// Return global code coverage (will probably be null). | ||
app.use((req, res, next) => { | ||
if (req.url !== COVERAGE_PUBLIC_PATH) { | ||
return next(); | ||
} | ||
const coverage = (global.__coverage__) ?? null; | ||
let data: string; | ||
try { | ||
data = JSON.stringify(coverage, null, 4); | ||
} catch (ex) { | ||
return next(ex); | ||
} | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.statusCode = 200; | ||
res.end(data); | ||
}); | ||
}; | ||
} | ||
function createTransform(opts: IstanbulPluginOptions = {}): TransformHook { | ||
const plugins = [ BabelPluginIstanbul ]; | ||
const cwd = process.cwd(); | ||
const exclude = new TestExclude({ | ||
cwd, | ||
include: opts.include, | ||
exclude: opts.exclude, | ||
extension: opts.extension, | ||
excludeNodeModules: true, | ||
}); | ||
return function (srcCode, id) { | ||
if (process.env.NODE_ENV == 'production' || id.startsWith('/@modules/')) { | ||
// do not transform if this is a dep | ||
// do not transform for production builds | ||
return; | ||
} | ||
if (exclude.shouldInstrument(id)) { | ||
const { code, map } = transformSync(srcCode, { | ||
plugins, cwd, | ||
filename: id, | ||
ast: false, | ||
sourceMaps: true, | ||
inputSourceMap: this.getCombinedSourcemap(), | ||
}); | ||
return { code, map }; | ||
} | ||
}; | ||
} | ||
function istanbulPlugin(opts?: IstanbulPluginOptions): Plugin { | ||
const transform = createTransform(opts); | ||
return { | ||
transforms: [ transform ], | ||
configureServer: serverPlugin, | ||
name: 'vite:istanbul', | ||
transform: createTransform(opts), | ||
configureServer: createConfigureServer(), | ||
}; | ||
} | ||
export = istanbulPlugin; |
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
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
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
10938
8
144
66