Skip to content

Commit

Permalink
Merge pull request #12 from chengpeiquan/develop
Browse files Browse the repository at this point in the history
feat: add an option for debug
  • Loading branch information
chengpeiquan authored Aug 22, 2022
2 parents 9085af0 + 9ac6227 commit 683633e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 27 deletions.
42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,49 @@ Adds a banner to the top of each generated chunk.
Install the package from npm (or yarn, or pnpm).

```bash
npm install --save-dev vite-plugin-banner
npm install -D vite-plugin-banner
```

## Options

Since `v0.2.0`, This plugin support a `String` or an `Object` to be the plugin options.

Plugin Options Type|Description|Example
:--|:--|:--
string|The banner content|[Basic usage](#basic-usage)
{ outDir: string; content: string }|content: The comment content of the banner<br>outDir: The output directory from vite|[Optional parameter format](#optional-parameter-format)
BannerPluginOptions|See the type declarations below|[Optional parameter format](#optional-parameter-format)

· Type Declarations:

```ts
/**
* Some options from `vite.config.[ts|js]`
* @tips This options type is supported since `0.2.0`
*/
export interface BannerPluginOptions {
/**
* The comment content of the banner
*/
content: string

/**
* The output directory from the configuration of Vite.js
* @default `dist`
*/
outDir?: string

/**
* Whether to print error messages to the console
* @tips This option is supported since `0.4.0`
* @default `false`
*/
debug?: boolean
}
```

## Usage

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

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).
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).

### Basic usage

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

### Optional parameter format

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
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.

```ts
// docs/.vitepress/config.ts
Expand Down Expand Up @@ -185,7 +211,9 @@ export default defineConfig({

Why do it?

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
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.

Of course, with the updated version of Vitepress, this is not necessarily required, but you can choose to do so when you need it.

## License

Expand Down
40 changes: 34 additions & 6 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,44 @@ npm install --save-dev vite-plugin-banner

## 选项

`v0.2.0` 开始, 这个插件支持使用 `String` 或者 `Object` 格式来作为插件选项。

插件选项类型|作用描述|使用例子
:--|:--|:--
string|横幅注释的内容|[基础用法](#基础用法)
{ outDir: string; content: string }|content: 横幅注释的内容<br>outDir: 来自 Vite 指定的输出目录|[可选参数格式](#可选参数格式)
BannerPluginOptions|请参阅下方的类型声明|[可选参数格式](#可选参数格式)

· Type Declarations:

```ts
/**
* 来自 `vite.config.[ts|js]` 的一些选项
* @tips 从 `0.2.0` 开始支持此选项类型
*/
export interface BannerPluginOptions {
/**
* Banner 的注释内容
*/
content: string

/**
* Vite.js 配置的输出目录
* @default `dist`
*/
outDir?: string

/**
* 是否将错误信息打印到控制台
* @tips 从 `0.4.0` 开始支持此选项
* @default `false`
*/
debug?: boolean
}
```

## 用法

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

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

### 基础用法

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

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

export default defineConfig({
plugins: [
Expand Down Expand Up @@ -187,6 +213,8 @@ export default defineConfig({

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

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

## License

MIT License © 2021 [chengpeiquan](https://github.com/chengpeiquan)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-plugin-banner",
"description": "A banner plugin for Vite. Adds a banner to the top of each generated chunk.",
"version": "0.3.1",
"version": "0.4.0",
"author": "chengpeiquan <chengpeiquan@chengpeiquan.com>",
"license": "MIT",
"homepage": "https://github.com/chengpeiquan/vite-plugin-banner",
Expand Down
25 changes: 19 additions & 6 deletions src/libs/getConfig.ts → src/libs/formatConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import verifyBanner from './verifyBanner'
import type { BannerPluginOptions, PluginConfig } from '../types'

export default function (options: string | BannerPluginOptions): PluginConfig {
/**
* Process options of different formats into a unified format
* @param options - Some options from `vite.config.[ts|js]`
* @returns A unified plugin option
*/
export default function formatConfig(
options: string | BannerPluginOptions
): PluginConfig {
// Set a default config
const config: PluginConfig = {
content: '',
outDir: '',
outDir: 'dist',
debug: false,
}

// illegal type
// Type of plugin options
const type: string = Object.prototype.toString.call(options)

// Block illegal types
if (!['[object String]', '[object Object]'].includes(type)) {
throw new Error(
'[vite-plugin-banner] The options must be a string or an object.'
Expand All @@ -36,12 +46,15 @@ export default function (options: string | BannerPluginOptions): PluginConfig {
) {
config.outDir = options.outDir
}

// Update the `debug` option
config.debug = Boolean(options.debug)
}

// Verify the validity of the incoming comment content
const error: string = verifyBanner(config.content)
if (error) {
throw new Error(`[vite-plugin-banner] ${error}`)
const errMsg: string = verifyBanner(config.content)
if (errMsg) {
throw new Error(`[vite-plugin-banner] ${errMsg}`)
}

return config
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'
import { resolve } from 'path'
import getConfig from './libs/getConfig'
import formatConfig from './libs/formatConfig'
import type { ResolvedConfig } from 'vite'
import type { NormalizedOutputOptions, OutputBundle } from 'rollup'
import type { BannerPluginOptions, PluginConfig } from './types'
Expand All @@ -22,7 +22,7 @@ const excludeRegexp: RegExp = new RegExp(/vendor/)
*/
export default function (pluginOptions: string | BannerPluginOptions): any {
// Get the plugin config
const pluginConfig: PluginConfig = getConfig(pluginOptions)
const pluginConfig: PluginConfig = formatConfig(pluginOptions)

// Handle files
return {
Expand Down Expand Up @@ -64,7 +64,10 @@ export default function (pluginOptions: string | BannerPluginOptions): any {
// Save
fs.writeFileSync(filePath, data)
} catch (e) {
console.log(e)
// The error log is only printed when the debug option is enabled
if (pluginConfig.debug) {
console.log(e)
}
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
/**
* The plugin input parameter can be an object
* @param content - The comment content of the banner
* @param outDir - The output directory from vite
* Some options from `vite.config.[ts|js]`
* @tips This options type is supported since `0.2.0`
*/
export interface BannerPluginOptions {
/**
* The comment content of the banner
*/
content: string

/**
* The output directory from the configuration of Vite.js
* @default `dist`
*/
outDir?: string

/**
* Whether to print error messages to the console
* @tips This option is supported since `0.4.0`
* @default `false`
*/
debug?: boolean
}

/**
* The config for the plugin
* Configuration of the plugin's internal runtime
*/
export interface PluginConfig {
content: string
outDir: string
debug: boolean
}

0 comments on commit 683633e

Please sign in to comment.