Skip to content

Commit 683633e

Browse files
authored
Merge pull request #12 from chengpeiquan/develop
feat: add an option for debug
2 parents 9085af0 + 9ac6227 commit 683633e

File tree

6 files changed

+114
-27
lines changed

6 files changed

+114
-27
lines changed

README.md

+35-7
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,49 @@ Adds a banner to the top of each generated chunk.
2121
Install the package from npm (or yarn, or pnpm).
2222

2323
```bash
24-
npm install --save-dev vite-plugin-banner
24+
npm install -D vite-plugin-banner
2525
```
2626

2727
## Options
2828

29-
Since `v0.2.0`, This plugin support a `String` or an `Object` to be the plugin options.
30-
3129
Plugin Options Type|Description|Example
3230
:--|:--|:--
3331
string|The banner content|[Basic usage](#basic-usage)
34-
{ outDir: string; content: string }|content: The comment content of the banner<br>outDir: The output directory from vite|[Optional parameter format](#optional-parameter-format)
32+
BannerPluginOptions|See the type declarations below|[Optional parameter format](#optional-parameter-format)
33+
34+
· Type Declarations:
35+
36+
```ts
37+
/**
38+
* Some options from `vite.config.[ts|js]`
39+
* @tips This options type is supported since `0.2.0`
40+
*/
41+
export interface BannerPluginOptions {
42+
/**
43+
* The comment content of the banner
44+
*/
45+
content: string
46+
47+
/**
48+
* The output directory from the configuration of Vite.js
49+
* @default `dist`
50+
*/
51+
outDir?: string
52+
53+
/**
54+
* Whether to print error messages to the console
55+
* @tips This option is supported since `0.4.0`
56+
* @default `false`
57+
*/
58+
debug?: boolean
59+
}
60+
```
3561

3662
## Usage
3763

3864
In most cases, just use the `String` format as a plugin option.
3965

40-
In some special cases, such as in [VitePress](https://vitepress.vuejs.org/), you need to use `Object` format to pass in plugin options, see [Optional parameter format](#optional-parameter-format).
66+
In some special cases, such as in [VitePress](https://vitepress.vuejs.org/), you might need to use `Object` format to pass in plugin options, see [Optional parameter format](#optional-parameter-format).
4167

4268
### Basic usage
4369

@@ -155,7 +181,7 @@ var e=Object.assign;import{M as t,d as a,u as r,c......
155181

156182
### Optional parameter format
157183

158-
I'm not sure what other scenarios besides VitePress need to use this method to pass in options, so I use VitePress as an example, I hope it can give you a reference
184+
I'm not sure what other scenarios besides VitePress need to use this method to pass in options, so I use VitePress as an example, I hope it can give you a reference.
159185

160186
```ts
161187
// docs/.vitepress/config.ts
@@ -185,7 +211,9 @@ export default defineConfig({
185211

186212
Why do it?
187213

188-
Because in VitePress, what you get through `viteConfig.build.outDir` is always a `.temp` temporary directory, not the final output directory, so you need to manually specify the output directory to inform the plugin
214+
Because in VitePress, what you get through `viteConfig.build.outDir` is always a `.temp` temporary directory, not the final output directory, so you need to manually specify the output directory to inform the plugin.
215+
216+
Of course, with the updated version of Vitepress, this is not necessarily required, but you can choose to do so when you need it.
189217

190218
## License
191219

README.zh-CN.md

+34-6
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,44 @@ npm install --save-dev vite-plugin-banner
2626

2727
## 选项
2828

29-
`v0.2.0` 开始, 这个插件支持使用 `String` 或者 `Object` 格式来作为插件选项。
30-
3129
插件选项类型|作用描述|使用例子
3230
:--|:--|:--
3331
string|横幅注释的内容|[基础用法](#基础用法)
34-
{ outDir: string; content: string }|content: 横幅注释的内容<br>outDir: 来自 Vite 指定的输出目录|[可选参数格式](#可选参数格式)
32+
BannerPluginOptions|请参阅下方的类型声明|[可选参数格式](#可选参数格式)
33+
34+
· Type Declarations:
35+
36+
```ts
37+
/**
38+
* 来自 `vite.config.[ts|js]` 的一些选项
39+
* @tips 从 `0.2.0` 开始支持此选项类型
40+
*/
41+
export interface BannerPluginOptions {
42+
/**
43+
* Banner 的注释内容
44+
*/
45+
content: string
46+
47+
/**
48+
* Vite.js 配置的输出目录
49+
* @default `dist`
50+
*/
51+
outDir?: string
52+
53+
/**
54+
* 是否将错误信息打印到控制台
55+
* @tips 从 `0.4.0` 开始支持此选项
56+
* @default `false`
57+
*/
58+
debug?: boolean
59+
}
60+
```
3561

3662
## 用法
3763

3864
在大多数情况下,只需使用 `String` 格式作为插件选项。
3965

40-
在一些特殊情况下,比如在 [VitePress](https://vitepress.vuejs.org/) 中,需要使用 `Object` 格式来传递插件选项,详见 [可选参数格式](#可选参数格式)
66+
在一些特殊情况下,比如在 [VitePress](https://vitepress.vuejs.org/) 中,可能需要使用 `Object` 格式来传递插件选项,详见 [可选参数格式](#可选参数格式)
4167

4268
### 基础用法
4369

@@ -46,7 +72,7 @@ string|横幅注释的内容|[基础用法](#基础用法)
4672
```ts
4773
// vite.config.ts
4874
import banner from 'vite-plugin-banner'
49-
// Other dependencies...
75+
// 其他依赖...
5076

5177
export default defineConfig({
5278
plugins: [
@@ -85,7 +111,7 @@ var e=Object.assign;import{M as t,d as a,u as r,c......
85111
```ts
86112
// vite.config.ts
87113
import pkg from './package.json'
88-
// Other dependencies...
114+
// 其他依赖...
89115

90116
export default defineConfig({
91117
plugins: [
@@ -187,6 +213,8 @@ export default defineConfig({
187213

188214
因为在 VitePress 里,通过 viteConfig.build.outDir 拿到的永远是一个 `.temp` 的临时目录,不是最终的输出目录,所以你需要手动指定输出目录来告知插件。
189215

216+
当然,随着 Vitepress 的版本更新,不一定需要这么做,只是当你需要的时候,可以选择这么处理。
217+
190218
## License
191219

192220
MIT License © 2021 [chengpeiquan](https://github.com/chengpeiquan)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-banner",
33
"description": "A banner plugin for Vite. Adds a banner to the top of each generated chunk.",
4-
"version": "0.3.1",
4+
"version": "0.4.0",
55
"author": "chengpeiquan <[email protected]>",
66
"license": "MIT",
77
"homepage": "https://github.com/chengpeiquan/vite-plugin-banner",

src/libs/getConfig.ts src/libs/formatConfig.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import verifyBanner from './verifyBanner'
22
import type { BannerPluginOptions, PluginConfig } from '../types'
33

4-
export default function (options: string | BannerPluginOptions): PluginConfig {
4+
/**
5+
* Process options of different formats into a unified format
6+
* @param options - Some options from `vite.config.[ts|js]`
7+
* @returns A unified plugin option
8+
*/
9+
export default function formatConfig(
10+
options: string | BannerPluginOptions
11+
): PluginConfig {
512
// Set a default config
613
const config: PluginConfig = {
714
content: '',
8-
outDir: '',
15+
outDir: 'dist',
16+
debug: false,
917
}
1018

11-
// illegal type
19+
// Type of plugin options
1220
const type: string = Object.prototype.toString.call(options)
21+
22+
// Block illegal types
1323
if (!['[object String]', '[object Object]'].includes(type)) {
1424
throw new Error(
1525
'[vite-plugin-banner] The options must be a string or an object.'
@@ -36,12 +46,15 @@ export default function (options: string | BannerPluginOptions): PluginConfig {
3646
) {
3747
config.outDir = options.outDir
3848
}
49+
50+
// Update the `debug` option
51+
config.debug = Boolean(options.debug)
3952
}
4053

4154
// Verify the validity of the incoming comment content
42-
const error: string = verifyBanner(config.content)
43-
if (error) {
44-
throw new Error(`[vite-plugin-banner] ${error}`)
55+
const errMsg: string = verifyBanner(config.content)
56+
if (errMsg) {
57+
throw new Error(`[vite-plugin-banner] ${errMsg}`)
4558
}
4659

4760
return config

src/main.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs'
22
import { resolve } from 'path'
3-
import getConfig from './libs/getConfig'
3+
import formatConfig from './libs/formatConfig'
44
import type { ResolvedConfig } from 'vite'
55
import type { NormalizedOutputOptions, OutputBundle } from 'rollup'
66
import type { BannerPluginOptions, PluginConfig } from './types'
@@ -22,7 +22,7 @@ const excludeRegexp: RegExp = new RegExp(/vendor/)
2222
*/
2323
export default function (pluginOptions: string | BannerPluginOptions): any {
2424
// Get the plugin config
25-
const pluginConfig: PluginConfig = getConfig(pluginOptions)
25+
const pluginConfig: PluginConfig = formatConfig(pluginOptions)
2626

2727
// Handle files
2828
return {
@@ -64,7 +64,10 @@ export default function (pluginOptions: string | BannerPluginOptions): any {
6464
// Save
6565
fs.writeFileSync(filePath, data)
6666
} catch (e) {
67-
console.log(e)
67+
// The error log is only printed when the debug option is enabled
68+
if (pluginConfig.debug) {
69+
console.log(e)
70+
}
6871
}
6972
}
7073
}

src/types/index.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
/**
2-
* The plugin input parameter can be an object
3-
* @param content - The comment content of the banner
4-
* @param outDir - The output directory from vite
2+
* Some options from `vite.config.[ts|js]`
3+
* @tips This options type is supported since `0.2.0`
54
*/
65
export interface BannerPluginOptions {
6+
/**
7+
* The comment content of the banner
8+
*/
79
content: string
10+
11+
/**
12+
* The output directory from the configuration of Vite.js
13+
* @default `dist`
14+
*/
815
outDir?: string
16+
17+
/**
18+
* Whether to print error messages to the console
19+
* @tips This option is supported since `0.4.0`
20+
* @default `false`
21+
*/
22+
debug?: boolean
923
}
1024

1125
/**
12-
* The config for the plugin
26+
* Configuration of the plugin's internal runtime
1327
*/
1428
export interface PluginConfig {
1529
content: string
1630
outDir: string
31+
debug: boolean
1732
}

0 commit comments

Comments
 (0)