Skip to content

Commit

Permalink
chore: release
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed May 27, 2024
1 parent 7b546f1 commit 16a93d4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
5 changes: 5 additions & 0 deletions packages/vite-plugin-istanbul-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# vite-plugin-istanbul-widget

## 1.7.2
### Patch Changes

- resolve astro options and check enabled

## 1.7.1

### Patch Changes
Expand Down
3 changes: 2 additions & 1 deletion packages/vite-plugin-istanbul-widget/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-istanbul-widget",
"version": "1.7.1",
"version": "1.7.2",
"description": "集成了 istanbul-widget 和 vite-plugin-istanbul 的vite插件",
"type": "module",
"keywords": [
Expand Down Expand Up @@ -59,6 +59,7 @@
"@minko-fe/lodash-pro": "^0.3.3",
"debug": "^4.3.4",
"espree": "10.0.1",
"execa": "^8.0.1",
"istanbul-lib-instrument": "6.0.2",
"istanbul-widget": "workspace:*",
"picocolors": "1.0.0",
Expand Down
25 changes: 16 additions & 9 deletions packages/vite-plugin-istanbul-widget/src/astro/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { AstroIntegration } from 'astro'
import { vendor, istanbulWidget as viteIstanbulWidget } from '../index'
import { type VitePluginIstanbulWidgetOptions } from '../types'
import { ensureArray, resolveInlineScript } from '../utils'
import { checkPluginEnabled, ensureArray, resolveInlineScript, resolveOptions } from '../utils'
import { debug } from '../utils/debug'

export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
const { enabled, istanbulPluginConfig, istanbulWidgetConfig, checkProd, delayIstanbulWidgetInit } =
resolveOptions(opts)

if (!checkPluginEnabled(enabled, checkProd)) return

debug('astro istanbulWidget options:', opts)

return {
name: 'vite-plugin-istanbul-widget-integration',
hooks: {
Expand All @@ -15,9 +22,9 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
viteIstanbulWidget({
...opts,
istanbulPluginConfig: {
...opts.istanbulPluginConfig,
...istanbulPluginConfig,
exclude: [
...ensureArray(opts.istanbulPluginConfig?.exclude),
...ensureArray(istanbulPluginConfig?.exclude),
'**/astro:scripts/*.js',
'**/node_modules/**',
],
Expand All @@ -27,12 +34,12 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
name: 'vite:plugin-istanbul-widget:astro:pre',
enforce: 'pre',
transform(code, id) {
if (opts.istanbulWidgetConfig !== false) {
if (istanbulWidgetConfig !== false) {
if (id === 'astro:scripts/page.js') {
debug('istanbulWidget transform:', id)

const { script } = resolveInlineScript('lib', opts.istanbulWidgetConfig, {
delayIstanbulWidgetInit: opts.delayIstanbulWidgetInit,
const { script } = resolveInlineScript('lib', istanbulWidgetConfig, {
delayIstanbulWidgetInit,
})

code = /*js*/ `${script}
Expand All @@ -51,12 +58,12 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
enforce: 'post',
generateBundle: {
order: 'post',
handler(opts, bundle) {
handler(options, bundle) {
for (const file in bundle) {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
if ([vendor].includes(chunk.name)) {
const { format } = opts
const { format } = options
const emptyCss = `\\/\\*\\s*empty\\s*css\\s*\\*\\/`
const emptyChunkRE = new RegExp(
format === 'es'
Expand All @@ -75,7 +82,7 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
],
},
})
if (opts.istanbulWidgetConfig !== false) {
if (istanbulWidgetConfig !== false) {
injectScript(
'page',
// hack to inject istanbul-widget
Expand Down
21 changes: 7 additions & 14 deletions packages/vite-plugin-istanbul-widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,34 @@ import { isArray, set } from '@minko-fe/lodash-pro'
import fs from 'node:fs'
import istanbul from 'vite-plugin-istanbul'
import { type VitePluginIstanbulWidgetOptions } from './types'
import { ensureArray, getCommitId, resolveInlineScript } from './utils'
import { checkPluginEnabled, ensureArray, getCommitId, resolveInlineScript, resolveOptions } from './utils'
import { debug } from './utils/debug'

export const vendor = 'vendor'
export const virtualIstanbulWidgetId = 'virtual:istanbul-widget'
const resolvedVirtualIstanbulWidgetId = `\0${virtualIstanbulWidgetId}.js`

export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
const {
enabled = false,
fullReport = true,
istanbulPluginConfig,
istanbulWidgetConfig,
checkProd = true,
delayIstanbulWidgetInit = 0,
} = opts || {}
const { enabled, fullReport, istanbulPluginConfig, istanbulWidgetConfig, checkProd, delayIstanbulWidgetInit } =
resolveOptions(opts)

if (checkProd && process.env.NODE_ENV === 'production') return undefined

if (!enabled) return undefined
if (!checkPluginEnabled(enabled, checkProd)) return

debug('istanbulWidget options:', opts)

return [
{
name: 'vite:plugin-istanbul-widget:pre',
enforce: 'pre',
config(c) {
async config(c) {
if (c.build?.ssr) return
if (!c.build?.sourcemap) {
c.build ??= {}
c.build.sourcemap = 'hidden'
}
return {
define: {
__GIT_COMMIT_ID__: JSON.stringify(getCommitId()),
__GIT_COMMIT_ID__: JSON.stringify(await getCommitId()),
},
}
},
Expand Down
37 changes: 32 additions & 5 deletions packages/vite-plugin-istanbul-widget/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { execaCommand } from 'execa'
import { type IstanbulWidgetOptions } from 'istanbul-widget'
import { execSync } from 'node:child_process'
import { createRequire } from 'node:module'
import path from 'node:path'
import serialize from 'serialize-javascript'
import { normalizePath } from 'vite'
import { type VitePluginIstanbulWidgetOptions } from '../types'
import { debug } from './debug'
import { logger } from './logger'

export function getCommitId() {
export async function getCommitId() {
try {
const commitid = execSync('git rev-parse HEAD').toString().trim()
const { stdout } = await execaCommand('git rev-parse HEAD', { stdio: 'pipe' })
const commitid = stdout.trim()
debug(`Resolved git HEAD: ${commitid}`)
return commitid
} catch (e) {
console.error(`Failed to resolve git HEAD:\n${e}`)
} catch {
logger.warnOnce(`[vite-plugin-istanbul-widget]: Failed to resolve git HEAD\n`)
return ''
}
}

Expand Down Expand Up @@ -66,3 +70,26 @@ export function ensureArray<T>(value: T | T[] | undefined): T[] {
}
return [value]
}

export function resolveOptions(opts: VitePluginIstanbulWidgetOptions) {
const defaultOptions: VitePluginIstanbulWidgetOptions = {
enabled: false,
fullReport: true,
istanbulPluginConfig: {},
istanbulWidgetConfig: {},
checkProd: true,
delayIstanbulWidgetInit: 0,
}
return {
...defaultOptions,
...opts,
} as Required<VitePluginIstanbulWidgetOptions>
}

export function checkPluginEnabled(enabled: boolean, checkProd: boolean) {
if (checkProd && process.env.NODE_ENV === 'production') {
return false
}

return enabled
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16a93d4

Please sign in to comment.