Skip to content

Commit

Permalink
feat: add delayIstanbulWidgetInit option to support scenarios that …
Browse files Browse the repository at this point in the history
…might block js

release
  • Loading branch information
hemengke1997 committed May 23, 2024
1 parent 115ea87 commit be2ae6f
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 10 deletions.
7 changes: 7 additions & 0 deletions packages/vite-plugin-istanbul-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# vite-plugin-istanbul-widget

## 1.7.0

### Minor Changes

- feat: add `delayIstanbulWidgetInit` option to support scenarios that might block js
- add debug mode

## 1.6.3

### Patch Changes
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-istanbul-widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,10 @@ type VitePluginIstanbulWidgetOptions = {
* @description false 则关闭 istanbul-widget 控件
*/
istanbulWidgetConfig: IstanbulWidgetOptions | false
/**
* 延迟istanbul-widget初始化(ms)
* @default 0
*/
delayIstanbulWidgetInit?: number
}
```
4 changes: 3 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.6.3",
"version": "1.7.0",
"description": "集成了 istanbul-widget 和 vite-plugin-istanbul 的vite插件",
"type": "module",
"keywords": [
Expand Down Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@istanbuljs/load-nyc-config": "1.1.0",
"@minko-fe/lodash-pro": "^0.3.2",
"debug": "^4.3.4",
"espree": "10.0.1",
"istanbul-lib-instrument": "6.0.2",
"istanbul-widget": "workspace:*",
Expand All @@ -65,6 +66,7 @@
"test-exclude": "6.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/node": "^20.12.12",
"@types/serialize-javascript": "5.0.4",
"serialize-javascript": "6.0.2",
Expand Down
9 changes: 7 additions & 2 deletions packages/vite-plugin-istanbul-widget/src/astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AstroIntegration } from 'astro'
import { vendor, istanbulWidget as viteIstanbulWidget } from '../index'
import { type VitePluginIstanbulWidgetOptions } from '../types'
import { ensureArray, resolveInlineScript } from '../utils'
import { debug } from '../utils/debug'

export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
return {
Expand All @@ -28,7 +29,11 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
transform(code, id) {
if (opts.istanbulWidgetConfig !== false) {
if (id === 'astro:scripts/page.js') {
const { script } = resolveInlineScript('lib', opts.istanbulWidgetConfig)
debug('istanbulWidget transform:', id)

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

code = /*js*/ `${script}
\n${code}`
Expand Down Expand Up @@ -82,4 +87,4 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
} as AstroIntegration
}

export const exclude = [/istanbul-widget\..*\.js/]
export const exclude = [/istanbul-widget.*\.js/]
7 changes: 5 additions & 2 deletions packages/vite-plugin-istanbul-widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'node:fs'
import istanbul from 'vite-plugin-istanbul'
import { type VitePluginIstanbulWidgetOptions } from './types'
import { ensureArray, getCommitId, resolveInlineScript } from './utils'
import { debug } from './utils/debug'

export const vendor = 'vendor'
export const virtualIstanbulWidgetId = 'virtual:istanbul-widget'
Expand All @@ -16,12 +17,15 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
istanbulPluginConfig,
istanbulWidgetConfig,
checkProd = true,
delayIstanbulWidgetInit = 0,
} = opts || {}

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

if (!enabled) return undefined

debug('istanbulWidget options:', opts)

return [
{
name: 'vite:plugin-istanbul-widget:pre',
Expand Down Expand Up @@ -53,7 +57,7 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
order: 'pre',
handler(html) {
if (istanbulWidgetConfig !== false) {
const { src, script } = resolveInlineScript('min', istanbulWidgetConfig)
const { src, script } = resolveInlineScript('min', istanbulWidgetConfig, { delayIstanbulWidgetInit })
return {
html,
tags: [
Expand All @@ -70,7 +74,6 @@ export function istanbulWidget(opts: VitePluginIstanbulWidgetOptions): any {
tag: 'script',
attrs: {
type: 'module',
defer: true,
},
injectTo: 'body',
children: script,
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-istanbul-widget/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ export type VitePluginIstanbulWidgetOptions = {
* @description false 则关闭 istanbul-widget 控件
*/
istanbulWidgetConfig: IstanbulWidgetOptions | false
/**
* 延迟istanbul-widget初始化(ms)
* @default 0
*/
delayIstanbulWidgetInit?: number
}
3 changes: 3 additions & 0 deletions packages/vite-plugin-istanbul-widget/src/utils/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import createDebug from 'debug'

export const debug = createDebug('vite-plugin-istanbul-widget')
32 changes: 27 additions & 5 deletions packages/vite-plugin-istanbul-widget/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,53 @@ import { createRequire } from 'node:module'
import path from 'node:path'
import serialize from 'serialize-javascript'
import { normalizePath } from 'vite'
import { debug } from './debug'

export function getCommitId() {
try {
return execSync('git rev-parse HEAD').toString().trim()
const commitid = execSync('git rev-parse HEAD').toString().trim()
debug(`Resolved git HEAD: ${commitid}`)
return commitid
} catch (e) {
console.error(`Failed to resolve git HEAD:\n${e}`)
}
}

export function resolveInlineScript(mode: 'lib' | 'min', config: IstanbulWidgetOptions) {
export function resolveInlineScript(
mode: 'lib' | 'min',
config: IstanbulWidgetOptions,
options?: {
delayIstanbulWidgetInit?: number
},
) {
const require = createRequire(import.meta.url)
const { delayIstanbulWidgetInit = 0 } = options || {}

const istanbulWidgetPath = normalizePath(
`/@fs/${path.join(path.dirname(require.resolve('istanbul-widget')), `istanbul-widget.${mode}.js`)}`,
)

debug('istanbul-widget path:', istanbulWidgetPath)

const map = {
lib: {
src: istanbulWidgetPath,
script: /*js*/ `import { IstanbulWidget } from "${istanbulWidgetPath}";
new IstanbulWidget(${serialize(config)});`,
script: /*js*/ `
import { IstanbulWidget } from "${istanbulWidgetPath}";
const timer = setTimeout(() => {
new IstanbulWidget(${serialize(config)});
clearTimeout(timer);
}, ${delayIstanbulWidgetInit});
`,
},
min: {
src: istanbulWidgetPath,
script: /*js*/ `new window.IstanbulWidget(${serialize(config)});`,
script: /*js*/ `
const timer = setTimeout(() => {
new window.IstanbulWidget(${serialize(config)});
clearTimeout(timer);
}, ${delayIstanbulWidgetInit});
`,
},
}

Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-istanbul-widget/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createLogger } from 'vite'

export const logger = createLogger('info', {
prefix: '[vite-plugin-istanbul-widget]',
})
6 changes: 6 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 be2ae6f

Please sign in to comment.