Skip to content

Commit

Permalink
feat: a vite plugin to log filename, log argument name and separator
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Jul 2, 2023
0 parents commit 26e7576
Show file tree
Hide file tree
Showing 53 changed files with 7,522 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
target: 95%
patch:
default:
target: 95%
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.md
test
vitest.config.ts
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
extends: '@antfu',
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/prefer-ts-expect-error': 'off',
'no-console': 'off',
},
}
97 changes: 97 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: CI

on:
push:
branches:
- master

pull_request:
branches:
- master

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: pnpm

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

- name: Lint
run: nr lint

typecheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: pnpm

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node: [16.x, 18.x]
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Set node ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: pnpm

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

# - name: Test
# run: nr test --run --coverage

- name: Build
run: nr build

# - name: Upload coverage report to Codecov
# uses: codecov/codecov-action@v3
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# files: coverage/lcov.info
# fail_ci_if_error: true
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Release

permissions:
contents: write

on:
push:
tags:
- 'v*'

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Set node
uses: actions/setup-node@v3
with:
node-version: 16.x
registry-url: 'https://registry.npmjs.org'

- name: ChangeLog
run: npx changelogithub
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

- name: Release
run: nr release
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
.idea
.vscode
npm-debug.log
yarn-error.log
.stylelintcache
.eslintcache
node_modules
dist
coverage/
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

lint-staged
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
registry = http://registry.npmjs.org
auto-install-peers=false
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.14
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 暴走老七

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
141 changes: 141 additions & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

<p align="center">
<h1 align="center">vite-plugin-enhance-log</h1>
</p>

<div align="center">
一个用来为console.log添加代码所在文件名,log参数名以及添加分隔符的 vite 插件

[![NPM version][npm-image]][npm-url] ![NPM downloads][download-image]

![Test][test-badge] ![codecov][codecov-badge]


[npm-image]: https://img.shields.io/npm/v/vite-plugin-enhance-log.svg?style=flat-square
[npm-url]: http://npmjs.org/package/vite-plugin-enhance-log


[download-image]: https://img.shields.io/npm/dm/vite-plugin-enhance-log.svg?style=flat-square



[test-badge]: https://github.com/baozouai/vite-plugin-enhance-log/actions/workflows/ci.yml/badge.svg

[codecov-badge]: https://codecov.io/github/baozouai/plugin-vite-plugin-enhance-log/branch/master/graph/badge.svg


</div>

中文 | [英文](./README.md)

## 📦 安装

```sh
pnpm add vite-plugin-enhance-log -D
# or
yarn add vite-plugin-enhance-log -D
# or
npm i vite-plugin-enhance-log -D
```
## ⚙️ 参数

```ts
interface Options {
/**
* 打印文件名
* 如果你文件名太长,希望不显示文件path的目录,比如src/pages/xxx/yyy/a.tsx, 那么可以配置enableDir为false,则只打印a.tsx
*
* @default true
*/
enableFileName?: boolean | {
enableDir?: boolean
}
/**
* 打印的前缀提示,这样方便快速找到log 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
* @example
* console.log(' 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ...)
*/
preTip?: string
/** 每个参数分隔符,默认空字符串,你也可以使用换行符\n,分号;逗号,甚至猪猪🐖都行~ */
splitBy?: boolean
/**
* 是否需要endLine
* @example
* console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ..., 'line of 10 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀')
* */
endLine?: boolean
}
```

## 🔨 使用

```ts
import { defineConfig } from 'vite'
import EnhanceLog from 'vite-plugin-enhance-log'

const config = defineConfig({
plugins: [

EnhanceLog({
splitBy: '\n',
preTip: '🐖🐖🐖🐖🐖🐖🐖🐖🐖🐖',
enableFileName: true, // or enableFileName: { enableDir: false}
}),
],
})

export default config

```

## 👇 例子

拉项目后通过运行启动playgrounds:
```shell
pnpm play # 对应vue
# 或者
pnpm play:react # 对应react
```

来启动项目

具体配置可以看 [vue/vite.config.ts](./playgrounds/vue/vite.config.ts) 或者 [react/vite.config.ts](./playgrounds/react/vite.config.ts)

比如说,你不喜欢小 🚀,你喜欢猪猪 🐖,那可以配置 preTip 为 🐖🐖🐖🐖🐖🐖🐖🐖🐖🐖:

![img](./assets/pig_pretip.png)

比如说,在参数较多的情况下,你希望 log 每个参数都换行,那可以配置 splitBy 为 `\n`

![img](./assets/linefeed.png)

或者分隔符是`;`:

![img](./assets/semicolon_delimiter.png)

当然,你也可以随意指定,比如用个狗头🐶来分隔:

![img](./assets/dog_delimiter.png)

比如说,你希望知道log所在的文件名,那么可以配置enableFileName为true(当然默认就是true):

![img](./assets/filename.png)

如果文件路径太长:
![img](./assets/deep_file.png)


你只希望打印文件名,不需要目录前缀,那么可以配置 `enableFileName: { enableDir: false }`:
![img](./assets/only_file_name.png)

<!-- 又比如说,有个 log 跨了多行,你希望 log 开始和结束的行数,中间是 log 实体,那可以将 endLine 设置为 true:
![img](./assets/log_multi_line.png)
![img](./assets/log_multi_line_res.png)
> 我们可以看到开始的行数是13,结束的行数是44,跟源码一致 -->

## 📄 协议

vite-plugin-enhance-log 遵循 [MIT 协议](./LICENSE).
Loading

0 comments on commit 26e7576

Please sign in to comment.