├── .codecov.yml
├── .eslintignore
├── .eslintrc.js
├── .github
└── workflows
│ ├── ci.yml
│ └── release.yml
├── .gitignore
├── .husky
├── commit-msg
└── pre-commit
├── .npmrc
├── .nvmrc
├── LICENSE
├── README-zh_CN.md
├── README.md
├── assets
├── deep_file.png
├── dog_delimiter.png
├── file_with_line.png
├── filename.png
├── linefeed.png
├── log_multi_line.png
├── log_multi_line_res.png
├── only_file_name.png
├── option_example.png
├── pig_pretip.png
└── semicolon_delimiter.png
├── commitlint.config.js
├── package.json
├── playgrounds
└── react
│ ├── .gitignore
│ ├── index.html
│ ├── package.json
│ ├── src
│ ├── app.tsx
│ ├── favicon.svg
│ ├── global.d.ts
│ ├── index.css
│ ├── logo.svg
│ ├── main.tsx
│ └── xxx
│ │ └── yyy
│ │ └── zzzzzzzzzzz
│ │ └── index.tsx
│ ├── tsconfig.json
│ └── vite.config.ts
├── pnpm-lock.yaml
├── pnpm-workspace.yaml
├── src
└── babel-plugin-enhance-log.ts
├── test
├── __snapshots__
│ └── index.test.ts.snap
└── index.test.ts
├── tsconfig.json
├── tsup.config.ts
└── vitest.config.ts
/.codecov.yml:
--------------------------------------------------------------------------------
1 | coverage:
2 | status:
3 | project:
4 | default:
5 | target: 95%
6 | patch:
7 | default:
8 | target: 95%
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | *.md
2 | test
3 | vitest.config.ts
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | extends: '@antfu',
3 | rules: {
4 | '@typescript-eslint/ban-ts-comment': 'off',
5 | '@typescript-eslint/prefer-ts-expect-error': 'off',
6 | 'no-console': 'off',
7 | },
8 | }
9 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 |
8 | pull_request:
9 | branches:
10 | - master
11 |
12 | jobs:
13 | lint:
14 | runs-on: ubuntu-latest
15 | steps:
16 | - name: Checkout
17 | uses: actions/checkout@v3
18 |
19 | - name: Install pnpm
20 | uses: pnpm/action-setup@v2
21 |
22 | - name: Install Node.js
23 | uses: actions/setup-node@v3
24 | with:
25 | node-version: 16.x
26 | cache: pnpm
27 |
28 | - name: Setup
29 | run: npm i -g @antfu/ni
30 |
31 | - name: Install
32 | run: nci
33 |
34 | - name: Lint
35 | run: nr lint
36 |
37 | typecheck:
38 | runs-on: ubuntu-latest
39 | steps:
40 | - name: Checkout
41 | uses: actions/checkout@v3
42 |
43 | - name: Install pnpm
44 | uses: pnpm/action-setup@v2
45 |
46 | - name: Install Node.js
47 | uses: actions/setup-node@v3
48 | with:
49 | node-version: 18.x
50 | cache: pnpm
51 |
52 | - name: Setup
53 | run: npm i -g @antfu/ni
54 |
55 | - name: Install
56 | run: nci
57 |
58 | test:
59 | runs-on: ${{ matrix.os }}
60 |
61 | strategy:
62 | matrix:
63 | node: [16.x, 18.x]
64 | os: [ubuntu-latest, windows-latest, macos-latest]
65 | fail-fast: false
66 |
67 | steps:
68 | - name: Checkout
69 | uses: actions/checkout@v3
70 |
71 | - name: Install pnpm
72 | uses: pnpm/action-setup@v2
73 |
74 | - name: Set node ${{ matrix.node }}
75 | uses: actions/setup-node@v3
76 | with:
77 | node-version: ${{ matrix.node }}
78 | cache: pnpm
79 |
80 | - name: Setup
81 | run: npm i -g @antfu/ni
82 |
83 | - name: Install
84 | run: nci
85 |
86 | - name: Test
87 | run: nr test --run --coverage
88 |
89 | - name: Build
90 | run: nr build
91 |
92 | - name: Upload coverage report to Codecov
93 | uses: codecov/codecov-action@v3
94 | with:
95 | token: ${{ secrets.CODECOV_TOKEN }}
96 | files: coverage/lcov.info
97 | fail_ci_if_error: true
98 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release
2 |
3 | permissions:
4 | contents: write
5 |
6 | on:
7 | push:
8 | tags:
9 | - 'v*'
10 |
11 | jobs:
12 | release:
13 | runs-on: ubuntu-latest
14 | steps:
15 | - uses: actions/checkout@v3
16 | with:
17 | fetch-depth: 0
18 |
19 | - name: Install pnpm
20 | uses: pnpm/action-setup@v2
21 |
22 | - name: Set node
23 | uses: actions/setup-node@v3
24 | with:
25 | node-version: 16.x
26 | registry-url: 'https://registry.npmjs.org'
27 |
28 | - name: ChangeLog
29 | run: npx changelogithub
30 | env:
31 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
32 |
33 | - name: Setup
34 | run: npm i -g @antfu/ni
35 |
36 | - name: Install
37 | run: nci
38 |
39 | - name: Release
40 | run: |
41 | if [[ ${{ github.ref }} == *refs/tags/*-beta* ]]; then
42 | npm publish --tag beta
43 | elif [[ ${{ github.ref }} == *refs/tags/*-alpha* ]]; then
44 | npm publish --tag alpha
45 | else
46 | npm publish
47 | fi
48 | env:
49 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
50 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .idea
3 | .vscode
4 | npm-debug.log
5 | yarn-error.log
6 | .stylelintcache
7 | .eslintcache
8 | node_modules
9 | dist
10 | coverage/
--------------------------------------------------------------------------------
/.husky/commit-msg:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 | . "$(dirname -- "$0")/_/husky.sh"
3 |
4 | npx --no -- commitlint --edit $1
5 |
--------------------------------------------------------------------------------
/.husky/pre-commit:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 | . "$(dirname -- "$0")/_/husky.sh"
3 |
4 | lint-staged
5 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | registry = http://registry.npmjs.org
2 | auto-install-peers=false
--------------------------------------------------------------------------------
/.nvmrc:
--------------------------------------------------------------------------------
1 | 16.14
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 暴走老七
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README-zh_CN.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
babel-plugin-enhance-log
4 |
5 |
6 |
7 | 一个用来为console.log添加代码行数,log所在文件名,log参数名以及添加分隔符的插件
8 |
9 | [![NPM version][npm-image]][npm-url] ![NPM downloads][download-image]
10 |
11 | ![Test][test-badge] ![codecov][codecov-badge]
12 |
13 |
14 | [npm-image]: https://img.shields.io/npm/v/babel-plugin-enhance-log.svg?style=flat-square
15 | [npm-url]: http://npmjs.org/package/babel-plugin-enhance-log
16 |
17 |
18 | [download-image]: https://img.shields.io/npm/dm/babel-plugin-enhance-log.svg?style=flat-square
19 |
20 |
21 |
22 | [test-badge]: https://github.com/baozouai/babel-plugin-enhance-log/actions/workflows/ci.yml/badge.svg
23 |
24 | [codecov-badge]: https://codecov.io/github/baozouai/plugin-babel-plugin-enhance-log/branch/master/graph/badge.svg
25 |
26 |
27 |
28 |
29 | 中文 | [英文](./README.md)
30 |
31 | ## 📦 安装
32 |
33 | ```sh
34 | pnpm add babel-plugin-enhance-log -D
35 | # or
36 | yarn add babel-plugin-enhance-log -D
37 | # or
38 | npm i babel-plugin-enhance-log -D
39 | ```
40 | ## ⚙️ 参数
41 |
42 | ```ts
43 | interface Options {
44 | /**
45 | * 打印的前缀提示,这样方便快速找到log 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
46 | * @example
47 | * console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ...)
48 | */
49 | preTip?: string
50 | /** 每个参数分隔符,默认空字符串,你也可以使用换行符\n,分号;逗号,甚至猪猪🐖都行~ */
51 | splitBy?: boolean
52 | /**
53 | * 是否需要endLine
54 | * @example
55 | * console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ..., 'line of 10 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀')
56 | * */
57 | endLine?: boolean
58 | /**
59 | * 打印文件名
60 | * 如果你文件名太长,希望不显示文件path的目录,比如src/pages/xxx/yyy/a.tsx, 那么可以配置enableDir为false,则只打印a.tsx
61 | *
62 | * @default true
63 | */
64 | enableFileName?: boolean | {
65 | enableDir?: boolean
66 | }
67 | /** 可以指定项目根目录地址,默认是process.cwd(),会用于处理文件名生成 */
68 | root?: boolean
69 | }
70 | ```
71 |
72 | ## 🔨 使用
73 |
74 | ```js
75 | // babel.config.js
76 | /** @type {import('babel-plugin-enhance-log').Options} */
77 | const enhanceOption = {
78 | preTip: '🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', // default
79 | splitBy: '', // default
80 | endLine: false // default
81 | }
82 | module.exports = {
83 |
84 | plugins: [
85 | ['enhance-log',
86 | enhanceOption
87 | ]
88 | ],
89 | }
90 | ```
91 | 
92 |
93 | ## 👇 例子
94 |
95 | > 📢 注意,从0.4.0开始,如果打印文件名的话,会将log所在行放到文件名后面,类似这样:
96 | > 
97 |
98 | 比如说,你不喜欢小 🚀,你喜欢猪猪 🐖,那可以配置 preTip 为 🐖🐖🐖🐖🐖🐖🐖🐖🐖🐖:
99 |
100 | 
101 |
102 | 比如说,在参数较多的情况下,你希望 log 每个参数都换行,那可以配置 splitBy 为 `\n`:
103 |
104 | 
105 |
106 | 或者分隔符是`;`:
107 |
108 | 
109 |
110 | 当然,你也可以随意指定,比如用个狗头🐶来分隔:
111 |
112 | 
113 |
114 | 又比如说,有个 log 跨了多行,你希望 log 开始和结束的行数,中间是 log 实体,那可以将 endLine 设置为 true:
115 |
116 | 
117 |
118 | 
119 |
120 | > 我们可以看到开始的行数是13,结束的行数是44,跟源码一致
121 |
122 | 又比如说,你希望知道log所在的文件名,那么可以配置enableFileName为true(当然默认就是true):
123 |
124 | 
125 |
126 | 如果文件路径太长:
127 | 
128 |
129 |
130 | 你只希望打印文件名,不需要目录前缀,那么可以配置 `enableFileName: { enableDir: false }`:
131 | 
132 |
133 | ## 📄 协议
134 |
135 | babel-plugin-enhance-log 遵循 [MIT 协议](./LICENSE).
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
babel-plugin-enhance-log
4 |
5 |
6 |
7 | A babel Plugin to add log line, log filename, add log argument name and separator
8 |
9 | [![NPM version][npm-image]][npm-url] ![NPM downloads][download-image]
10 |
11 | ![Test][test-badge] ![codecov][codecov-badge]
12 |
13 |
14 | [npm-image]: https://img.shields.io/npm/v/babel-plugin-enhance-log.svg?style=flat-square
15 | [npm-url]: http://npmjs.org/package/babel-plugin-enhance-log
16 |
17 |
18 | [download-image]: https://img.shields.io/npm/dm/babel-plugin-enhance-log.svg?style=flat-square
19 |
20 |
21 |
22 | [test-badge]: https://github.com/baozouai/babel-plugin-enhance-log/actions/workflows/ci.yml/badge.svg
23 |
24 | [codecov-badge]: https://codecov.io/github/baozouai/plugin-babel-plugin-enhance-log/branch/master/graph/badge.svg
25 |
26 |
27 |
28 |
29 | English | [中文](./README-zh_CN.md)
30 |
31 | ## 📦 Install
32 |
33 | ```sh
34 | pnpm add babel-plugin-enhance-log -D
35 | # or
36 | yarn add babel-plugin-enhance-log -D
37 | # or
38 | npm i babel-plugin-enhance-log -D
39 | ```
40 |
41 |
42 | ## ⚙️ Options
43 |
44 | ```ts
45 | interface Options {
46 | /**
47 | * tip of start argument default 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
48 | * @example
49 | * console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ...)
50 | */
51 | preTip?: string
52 | /** the delimiter for each parameter is an empty string by default, you can also use a newline \n, a semicolon';' a comma',' or even a pig '🐖' */
53 | splitBy?: boolean
54 | /**
55 | * need endLine, default false
56 | * @example
57 | * console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ..., 'line of 10 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀')
58 | * */
59 | endLine?: boolean
60 | /**
61 | * log file name
62 | * If your file name is too long,
63 | * and you don;t want to log the directory of the file path,
64 | * such as src/pages/xxxyyy/a.tsx,
65 | * then you can configure enableDir to false, and only print a.tsx
66 | * @default true
67 | */
68 | enableFileName?: boolean | {
69 | enableDir?: boolean
70 | }
71 | /**
72 | * You can specify the project root directory address, which
73 | * will be used to process file name generation
74 | * @default process.cwd()
75 | * */
76 | root?: boolean
77 | }
78 | ```
79 | ## 🔨 Usage
80 |
81 | ```js
82 | // babel.config.js
83 |
84 | module.exports = {
85 |
86 | plugins: [
87 | ['enhance-log',
88 | /** @type {import('babel-plugin-enhance-log').Options} */
89 | {
90 | preTip: '🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', // default
91 | splitBy: '', // default
92 | endLine: false
93 | }]
94 | ],
95 | }
96 | ```
97 | 
98 |
99 | ## 👇 Example
100 |
101 | > 📢 Note that since 0.4.0, if you print the filename, it puts the line where the log is after the filename, something like this:
102 | > 
103 |
104 |
105 | For example, if you don't like small 🚀, but you like piggy 🐖, you can configure preTip as 🐖🐖🐖🐖🐖🐖🐖🐖🐖🐖:
106 |
107 | 
108 |
109 | For example, in the case of many parameters, you want log to wrap each parameter, then you can configure splitBy as `\n`:
110 |
111 | 
112 |
113 | Or the delimiter is `;`:
114 |
115 | 
116 |
117 | Of course, you can also specify it at will, such as using a dog head 🐶 to separate:
118 |
119 | 
120 |
121 | For another example, if there is a log that spans multiple lines, you want the number of lines at the beginning and end of the log, with the log entity in the middle, then you can set endLine to true:
122 |
123 | 
124 |
125 | 
126 |
127 | > We can see that the number of lines at the beginning is 13, and the number of lines at the end is 44, which is consistent with the source code
128 |
129 | For example, if you want to know the file name where the log is located, you can configure enableFileName to be true (of course the default is true):
130 |
131 | 
132 |
133 | If the file path is too long:
134 | 
135 |
136 |
137 | and you only want to print the file name without the directory prefix, you can configure `enableFileName: { enableDir: false }`:
138 | 
139 |
140 | ## 📄 License
141 |
142 | babel-plugin-enhance-log is [MIT licensed](./LICENSE).
--------------------------------------------------------------------------------
/assets/deep_file.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/deep_file.png
--------------------------------------------------------------------------------
/assets/dog_delimiter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/dog_delimiter.png
--------------------------------------------------------------------------------
/assets/file_with_line.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/file_with_line.png
--------------------------------------------------------------------------------
/assets/filename.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/filename.png
--------------------------------------------------------------------------------
/assets/linefeed.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/linefeed.png
--------------------------------------------------------------------------------
/assets/log_multi_line.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/log_multi_line.png
--------------------------------------------------------------------------------
/assets/log_multi_line_res.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/log_multi_line_res.png
--------------------------------------------------------------------------------
/assets/only_file_name.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/only_file_name.png
--------------------------------------------------------------------------------
/assets/option_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/option_example.png
--------------------------------------------------------------------------------
/assets/pig_pretip.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/pig_pretip.png
--------------------------------------------------------------------------------
/assets/semicolon_delimiter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/assets/semicolon_delimiter.png
--------------------------------------------------------------------------------
/commitlint.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | extends: ['@commitlint/config-conventional'], // 检测规则
3 | rules: {},
4 | }
5 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "babel-plugin-enhance-log",
3 | "version": "0.4.2",
4 | "packageManager": "pnpm@8.3.1",
5 | "description": "A babel Plugin to add log line, add log argument name and separator",
6 | "author": "baozouai ",
7 | "license": "MIT",
8 | "homepage": "https://github.com/baozouai/babel-plugin-enhance-log",
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/baozouai/babel-plugin-enhance-log.git"
12 | },
13 | "bugs": {
14 | "url": "https://github.com/baozouai/babel-plugin-enhance-log/issues"
15 | },
16 | "keywords": [
17 | "babel",
18 | "plugin",
19 | "enhance log",
20 | "log filename",
21 | "line",
22 | "babel-plugin",
23 | "add log argument name",
24 | "add separator for every argument"
25 | ],
26 | "main": "dist/babel-plugin-enhance-log.js",
27 | "module": "dist/babel-plugin-enhance-log.mjs",
28 | "types": "dist/babel-plugin-enhance-log.d.ts",
29 | "files": [
30 | "dist"
31 | ],
32 | "scripts": {
33 | "clean": "rm -rf dist",
34 | "lint": "eslint . --fix",
35 | "dev": "npm run build -- --watch --ignore-watch playgrounds",
36 | "build": "tsup",
37 | "test": "vitest",
38 | "tag": "bumpp",
39 | "prepublishOnly": "npm run clean && npm run build",
40 | "release": "npm publish",
41 | "prepare": "husky install",
42 | "play": "pnpm --filter react dev"
43 | },
44 | "dependencies": {
45 | "@babel/generator": "^7.21.4",
46 | "@babel/helper-plugin-utils": "^7.20.2",
47 | "@babel/plugin-syntax-typescript": "^7.21.4"
48 | },
49 | "devDependencies": {
50 | "@antfu/eslint-config": "^0.36.0",
51 | "@babel/core": "^7.21.4",
52 | "@babel/preset-env": "^7.21.4",
53 | "@babel/types": "^7.21.4",
54 | "@commitlint/cli": "^17.4.4",
55 | "@commitlint/config-conventional": "^17.4.4",
56 | "@types/babel__core": "^7.20.0",
57 | "@types/babel__generator": "^7.6.4",
58 | "@types/babel__helper-plugin-utils": "^7.10.0",
59 | "@types/node": "^18.15.13",
60 | "@vitest/coverage-c8": "^0.30.1",
61 | "bumpp": "^9.1.0",
62 | "cz-conventional-changelog": "^3.3.0",
63 | "eslint": "^8.35.0",
64 | "husky": "^8.0.3",
65 | "lint-staged": "^13.1.2",
66 | "pnpm": "^8.3.1",
67 | "simple-git-hooks": "^2.8.1",
68 | "tsup": "^7.1.0",
69 | "typescript": "^4.9.5",
70 | "unbuild": "^1.2.1",
71 | "vitest": "^0.30.1"
72 | },
73 | "husky": {
74 | "hooks": {
75 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
76 | "pre-commit": "lint-staged"
77 | }
78 | },
79 | "publishConfig": {
80 | "registry": "https://registry.npmjs.org",
81 | "access": "public"
82 | },
83 | "config": {
84 | "commitizen": {
85 | "path": "node_modules/cz-conventional-changelog"
86 | }
87 | },
88 | "simple-git-hooks": {
89 | "pre-commit": "pnpm lint-staged"
90 | },
91 | "lint-staged": {
92 | "src/**/*.ts": "eslint --fix"
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/playgrounds/react/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .DS_Store
3 | dist
4 | dist-ssr
5 | *.local
6 |
--------------------------------------------------------------------------------
/playgrounds/react/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Vite App
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/playgrounds/react/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react",
3 | "private": true,
4 | "scripts": {
5 | "dev": "nodemon --watch ../../dist/*.js -x 'DEBUG=babel-plugin-enhance-log:* vite'",
6 | "build": "DEBUG=babel-plugin-enhance-log vite build",
7 | "serve": "vite preview"
8 | },
9 | "dependencies": {
10 | "react": "^18.2.0",
11 | "react-dom": "^18.2.0",
12 | "react-router-dom": "^6.9.0",
13 | "vite-plugin-inspect": "^0.7.32"
14 | },
15 | "devDependencies": {
16 | "@types/react": "^18.0.28",
17 | "@types/react-dom": "^18.0.11",
18 | "@vitejs/plugin-react": "^3.1.0",
19 | "babel-plugin-enhance-log": "workspace:*",
20 | "nodemon": "^2.0.21",
21 | "typescript": "^4.9.5",
22 | "vite": "^4.1.4"
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/playgrounds/react/src/app.tsx:
--------------------------------------------------------------------------------
1 | import type { FC } from 'react'
2 | import DeepFile from './xxx/yyy/zzzzzzzzzzz'
3 |
4 | const index: FC = () => {
5 | const a = 1
6 | const b = 2
7 | console.log(a, b)
8 | return (
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | )
18 | }
19 |
20 | export default index
21 |
--------------------------------------------------------------------------------
/playgrounds/react/src/favicon.svg:
--------------------------------------------------------------------------------
1 |
16 |
--------------------------------------------------------------------------------
/playgrounds/react/src/global.d.ts:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/baozouai/babel-plugin-enhance-log/47788900cc2fc23e71b101f51cc4fad123d48990/playgrounds/react/src/global.d.ts
--------------------------------------------------------------------------------
/playgrounds/react/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/playgrounds/react/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/playgrounds/react/src/main.tsx:
--------------------------------------------------------------------------------
1 | import { createRoot } from 'react-dom/client'
2 | import App from './app'
3 |
4 | import './index.css'
5 |
6 | createRoot(document.getElementById('root')!).render(
7 | ,
8 | )
9 | const theFileName = 'main.tsx'
10 | const arg1 = 1
11 | const arg2 = '123'
12 | const arg3 = true
13 | const arg4 = false
14 | const arg5 = null
15 | const arg6 = undefined
16 | const arg7 = {
17 | arg1,
18 | arg2,
19 | argn: 'xjxjxj',
20 | }
21 |
22 | console.log(theFileName, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
23 |
--------------------------------------------------------------------------------
/playgrounds/react/src/xxx/yyy/zzzzzzzzzzz/index.tsx:
--------------------------------------------------------------------------------
1 | export default () => {
2 | const a = 1
3 | const b = 2
4 | console.log(a,
5 |
6 | b)
7 | return 目录很深的文件
8 | }
9 |
--------------------------------------------------------------------------------
/playgrounds/react/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ESNext",
4 | "lib": ["DOM", "DOM.Iterable", "ESNext"],
5 | "types": ["vite/client"],
6 | "allowJs": false,
7 | "skipLibCheck": false,
8 | "esModuleInterop": false,
9 | "allowSyntheticDefaultImports": true,
10 | "strict": true,
11 | "forceConsistentCasingInFileNames": true,
12 | "module": "ESNext",
13 | "moduleResolution": "Node",
14 | "resolveJsonModule": true,
15 | "isolatedModules": true,
16 | "noEmit": true,
17 | "jsx": "preserve"
18 | },
19 | "include": ["./src"]
20 | }
21 |
--------------------------------------------------------------------------------
/playgrounds/react/vite.config.ts:
--------------------------------------------------------------------------------
1 | import { defineConfig } from 'vite'
2 | import react from '@vitejs/plugin-react'
3 | import Inspect from 'vite-plugin-inspect'
4 |
5 | // https://vitejs.dev/config/
6 | export default defineConfig({
7 | plugins: [
8 | react({
9 | babel: {
10 | plugins: [['enhance-log', {
11 | splitBy: '\n',
12 |
13 | }]],
14 | },
15 | }),
16 | Inspect(),
17 | ],
18 | })
19 |
--------------------------------------------------------------------------------
/pnpm-workspace.yaml:
--------------------------------------------------------------------------------
1 | packages:
2 | - playgrounds/*
3 |
--------------------------------------------------------------------------------
/src/babel-plugin-enhance-log.ts:
--------------------------------------------------------------------------------
1 | import { declare } from '@babel/helper-plugin-utils'
2 | import generater from '@babel/generator'
3 | import type { StringLiteral } from '@babel/types'
4 | import { stringLiteral } from '@babel/types'
5 |
6 | export type EnableFileName = boolean | {
7 | /**
8 | * @default true
9 | * @example
10 | * the file name path is src/index.ts
11 | * if enableDir is true, the log will be src/index.ts
12 | * if enableDir is false, the log will be index.ts
13 | */
14 | enableDir?: boolean
15 | }
16 |
17 | export interface Options {
18 | /**
19 | * tip of start argument default 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
20 | * @example
21 | * console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ...)
22 | */
23 | preTip?: string
24 | /**
25 | * every arg split by
26 | * @example
27 | * \n
28 | * ;
29 | * ,
30 | */
31 | splitBy?: string
32 | /** need endLine, default false */
33 | endLine?: boolean
34 | /**
35 | * to log filename, default true
36 | */
37 | enableFileName?: EnableFileName
38 | /**
39 | * @description the babel.config.js base path
40 | * @default process.cwd()
41 | * */
42 | root?: string
43 | }
44 |
45 | const DEFAULT_PRE_TIP = '🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀'
46 | const SKIP_KEY = '@@babel-plugin-enhance-logSkip'
47 |
48 | // const colorGreen = '\x1B[32m'
49 | // const colorBlue = '\x1B[34m'
50 | // const colorReset = '\x1B[0m'
51 |
52 | // function handleStartFileNameTip(filePath: string, lineNumber: number) {
53 | // if (!filePath)
54 | // return ''
55 | // return ` ~ ${colorGreen}${filePath}:${colorBlue}${lineNumber}${colorReset}`
56 | // }
57 |
58 | function handleFileNameTip(filePath: string, lineNumber: number) {
59 | if (!filePath)
60 | return ''
61 | return ` ~ ${filePath}:${lineNumber}`
62 | }
63 |
64 | function generateStrNode(str: string): StringLiteral & { skip: boolean } {
65 | const node = stringLiteral(str)
66 | // @ts-ignore
67 | node.skip = true
68 | // @ts-ignore
69 | return node
70 | }
71 |
72 | export default declare((babel, { preTip = DEFAULT_PRE_TIP, splitBy = '', endLine: enableEndLine = false, enableFileName = true, root = process.cwd() }) => {
73 | const { types: t } = babel
74 | const splitNode = generateStrNode(splitBy)
75 |
76 | function generateLineOfTip(relativeFilename: string, lineNumber: number) {
77 | return `${relativeFilename ? '' : `line of ${lineNumber} `}${preTip}`
78 | }
79 | const rootReg = new RegExp(`${root}\\/?`)
80 | return {
81 | name: 'enhance-log',
82 | visitor: {
83 | CallExpression(path, { filename }) {
84 | const calleeCode = generater(path.node.callee).code
85 | if (calleeCode === 'console.log') {
86 | // add comment to skip if enter next time
87 | const { trailingComments } = path.node
88 | const shouldSkip = (trailingComments || []).some((item) => {
89 | return item.type === 'CommentBlock' && item.value === SKIP_KEY
90 | })
91 | if (shouldSkip)
92 | return
93 |
94 | t.addComment(path.node, 'trailing', SKIP_KEY)
95 |
96 | const nodeArguments = path.node.arguments
97 | for (let i = 0; i < nodeArguments.length; i++) {
98 | const argument = nodeArguments[i]
99 | // @ts-ignore
100 | if (argument.skip)
101 | continue
102 | if (!t.isLiteral(argument)) {
103 | if (t.isIdentifier(argument) && argument.name === 'undefined') {
104 | nodeArguments.splice(i + 1, 0, splitNode)
105 | continue
106 | }
107 | // @ts-ignore
108 | argument.skip = true
109 | const node = generateStrNode(`${generater(argument).code} =`)
110 |
111 | nodeArguments.splice(i, 0, node)
112 | nodeArguments.splice(i + 2, 0, splitNode)
113 | }
114 | else {
115 | nodeArguments.splice(i + 1, 0, splitNode)
116 | }
117 | }
118 | // the last needn't split
119 | if (nodeArguments[nodeArguments.length - 1] === splitNode)
120 | nodeArguments.pop()
121 | const { loc } = path.node
122 | let relativeFilename = ''
123 |
124 | if (enableFileName && filename) {
125 | relativeFilename = filename.replace(rootReg, '')
126 | if (typeof enableFileName === 'object' && !enableFileName.enableDir)
127 | relativeFilename = relativeFilename.replace(/.*\//, '')
128 | }
129 | if (loc) {
130 | const startLine = loc.start.line
131 |
132 | const startLineTipNode = t.stringLiteral(`${generateLineOfTip(relativeFilename, startLine)}${handleFileNameTip(relativeFilename, startLine)}\n`)
133 | nodeArguments.unshift(startLineTipNode)
134 | if (enableEndLine) {
135 | const endLine = loc.end.line
136 | if (endLine === startLine)
137 | return
138 | const endLineTipNode = t.stringLiteral(`\n${generateLineOfTip(relativeFilename, endLine)}${handleFileNameTip(relativeFilename, endLine)}`)
139 | nodeArguments.push(endLineTipNode)
140 | }
141 | }
142 | }
143 | },
144 | },
145 | }
146 | })
147 |
--------------------------------------------------------------------------------
/test/__snapshots__/index.test.ts.snap:
--------------------------------------------------------------------------------
1 | // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2 |
3 | exports[`Transforms log with endLine, splitBy \\n 1`] = `
4 | "const a = 1;
5 | const b = 2;
6 | const e = {
7 | w: {
8 | c: '123'
9 | }
10 | };
11 | console.log(\\"line of 9 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", '1', \\"\\\\n\\", false, \\"\\\\n\\", 2, \\"\\\\n\\", null, \\"\\\\n\\", undefined, \\"\\\\n\\", \\"a =\\", a, \\"\\\\n\\", \\"e.w.c =\\", e.w.c, \\"\\\\n\\", \\"b =\\", b, \\"\\\\n\\", \\"e =\\", e) /*@@babel-plugin-enhance-logSkip*/;"
12 | `;
13 |
14 | exports[`Transforms log with endLine, splitBy 🐖 1`] = `
15 | "const a = 1;
16 | const b = 2;
17 | const e = {
18 | w: {
19 | c: '123'
20 | }
21 | };
22 | console.log(\\"line of 9 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", '1', \\"\\\\uD83D\\\\uDC16\\", false, \\"\\\\uD83D\\\\uDC16\\", 2, \\"\\\\uD83D\\\\uDC16\\", null, \\"\\\\uD83D\\\\uDC16\\", undefined, \\"\\\\uD83D\\\\uDC16\\", \\"a =\\", a, \\"\\\\uD83D\\\\uDC16\\", \\"e.w.c =\\", e.w.c, \\"\\\\uD83D\\\\uDC16\\", \\"b =\\", b, \\"\\\\uD83D\\\\uDC16\\", \\"e =\\", e) /*@@babel-plugin-enhance-logSkip*/;"
23 | `;
24 |
25 | exports[`Transforms log with splitBy
26 | 1`] = `
27 | "const a = 1;
28 | const b = 2;
29 | const e = {
30 | w: {
31 | c: '123'
32 | }
33 | };
34 | console.log(\\"line of 9 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", '1', \\"\\\\n\\", false, \\"\\\\n\\", 2, \\"\\\\n\\", null, \\"\\\\n\\", undefined, \\"\\\\n\\", \\"a =\\", a, \\"\\\\n\\", \\"e.w.c =\\", e.w.c, \\"\\\\n\\", \\"b =\\", b, \\"\\\\n\\", \\"e =\\", e) /*@@babel-plugin-enhance-logSkip*/;"
35 | `;
36 |
37 | exports[`Transforms log with splitBy ; 1`] = `
38 | "const a = 1;
39 | const b = 2;
40 | const e = {
41 | w: {
42 | c: '123'
43 | }
44 | };
45 | console.log(\\"line of 9 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", '1', \\";\\", false, \\";\\", 2, \\";\\", null, \\";\\", undefined, \\";\\", \\"a =\\", a, \\";\\", \\"e.w.c =\\", e.w.c, \\";\\", \\"b =\\", b, \\";\\", \\"e =\\", e) /*@@babel-plugin-enhance-logSkip*/;"
46 | `;
47 |
48 | exports[`Transforms log 1`] = `
49 | "const a = 1;
50 | const b = 2;
51 | const e = {
52 | w: {
53 | c: '123'
54 | }
55 | };
56 | console.log(\\"line of 9 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", '1', \\"\\", false, \\"\\", 2, \\"\\", null, \\"\\", undefined, \\"\\", \\"a =\\", a, \\"\\", \\"e.w.c =\\", e.w.c, \\"\\", \\"b =\\", b) /*@@babel-plugin-enhance-logSkip*/;"
57 | `;
58 |
59 | exports[`Transforms log default 1`] = `
60 | "const a = 1,
61 | b = true;
62 | console.log(\\"line of 3 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", \\"a =\\", a, \\"\\", \\"b =\\", b) /*@@babel-plugin-enhance-logSkip*/;"
63 | `;
64 |
65 | exports[`Transforms log width preTip 🐖🐖🐖🐖🐖🐖🐖🐖🐖 1`] = `
66 | "const a = 1;
67 | const b = 2;
68 | const e = {
69 | w: {
70 | c: '123'
71 | }
72 | };
73 | console.log(\\"line of 9 \\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\uD83D\\\\uDC16\\\\n\\", '1', \\"\\", false, \\"\\", 2, \\"\\", null, \\"\\", undefined, \\"\\", \\"a =\\", a, \\"\\", \\"e.w.c =\\", e.w.c, \\"\\", \\"b =\\", b, \\"\\", \\"e =\\", e) /*@@babel-plugin-enhance-logSkip*/;"
74 | `;
75 |
76 | exports[`Transforms log with lineFeed 1`] = `
77 | "const a = 1;
78 | const b = 2;
79 | const e = {
80 | w: {
81 | c: '123'
82 | }
83 | };
84 | console.log(\\"line of 9 \\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\uD83D\\\\uDE80\\\\n\\", '1', \\"\\", false, \\"\\", 2, \\"\\", null, \\"\\", undefined, \\"\\", \\"a =\\", a, \\"\\", \\"e.w.c =\\", e.w.c, \\"\\", \\"b =\\", b, \\"\\", \\"e =\\", e) /*@@babel-plugin-enhance-logSkip*/;"
85 | `;
86 |
--------------------------------------------------------------------------------
/test/index.test.ts:
--------------------------------------------------------------------------------
1 | import { expect, it } from 'vitest'
2 | import { transformSync } from '@babel/core'
3 | import plugin from '../src/babel-plugin-enhance-log'
4 |
5 |
6 |
7 | it('Transforms log default', () => {
8 | const { code } = transformSync(`
9 | const a = 1, b = true
10 | console.log(a, b)
11 | `, {
12 | plugins: [plugin],
13 | })!
14 | expect(code).toMatchSnapshot()
15 | })
16 |
17 | it('Transforms log', () => {
18 | const { code } = transformSync(`
19 | const a = 1
20 | const b = 2
21 | const e = {
22 | w: {
23 | c: '123'
24 | }
25 | }
26 | console.log('1', false, 2, null, undefined, a, e.w.c, b)
27 | `, {
28 | plugins: [plugin],
29 | })!
30 | expect(code).toMatchSnapshot()
31 | })
32 |
33 |
34 | const heartPreTip = '🐖🐖🐖🐖🐖🐖🐖🐖🐖'
35 | const multiTypeArg = `
36 | const a = 1
37 | const b = 2
38 | const e = {
39 | w: {
40 | c: '123'
41 | }
42 | }
43 | console.log('1', false, 2, null, undefined, a, e.w.c, b, e)
44 | `
45 | it(`Transforms log width preTip ${heartPreTip}`, () => {
46 | const { code } = transformSync(multiTypeArg, {
47 | plugins: [[plugin, {
48 | preTip: heartPreTip
49 | }]]
50 | })!
51 | expect(code).toMatchSnapshot()
52 | })
53 |
54 | it(`Transforms log with lineFeed`, () => {
55 | const { code } = transformSync(multiTypeArg, {
56 | plugins: [[plugin, {
57 | lineFeed: true,
58 | }]]
59 | })!
60 | console.log(code);
61 | expect(code).toMatchSnapshot()
62 | })
63 |
64 | it(`Transforms log with splitBy ;`, () => {
65 | const { code } = transformSync(multiTypeArg, {
66 | plugins: [[plugin, {
67 | splitBy: ';',
68 | }]]
69 | })!
70 | console.log(code);
71 | expect(code).toMatchSnapshot()
72 | })
73 |
74 | it(`Transforms log with splitBy \n`, () => {
75 | const { code } = transformSync(multiTypeArg, {
76 | plugins: [[plugin, {
77 | splitBy: '\n',
78 | }]]
79 | })!
80 | console.log(code);
81 | expect(code).toMatchSnapshot()
82 | })
83 |
84 | it(`Transforms log with endLine, splitBy \\n`, () => {
85 | const { code } = transformSync(multiTypeArg, {
86 | plugins: [[plugin, {
87 | endLine: true,
88 | splitBy: '\n',
89 | }]]
90 | })!
91 | console.log(code);
92 | expect(code).toMatchSnapshot()
93 | })
94 |
95 | it(`Transforms log with endLine, splitBy 🐖`, () => {
96 | const { code } = transformSync(multiTypeArg, {
97 | plugins: [[plugin, {
98 | endLine: true,
99 | splitBy: '🐖',
100 | }]]
101 | })!
102 | console.log(code);
103 | expect(code).toMatchSnapshot()
104 | })
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es6",
4 | "module": "commonjs",
5 | "moduleResolution": "node",
6 | "declaration": true,
7 | "outDir": "dist",
8 | "forceConsistentCasingInFileNames": true,
9 | "strict": true,
10 | "skipLibCheck": true
11 | },
12 | "include": ["src/**/*"]
13 | }
14 |
--------------------------------------------------------------------------------
/tsup.config.ts:
--------------------------------------------------------------------------------
1 | import { defineConfig } from 'tsup'
2 |
3 | export default defineConfig({
4 | entry: ['src/babel-plugin-enhance-log.ts'],
5 | format: ['cjs', 'esm'],
6 | dts: {
7 | resolve: true,
8 | },
9 | clean: true,
10 | sourcemap: true,
11 | })
12 |
--------------------------------------------------------------------------------
/vitest.config.ts:
--------------------------------------------------------------------------------
1 | import { defineConfig } from 'vitest/config'
2 | export default defineConfig({
3 | test: { coverage: { provider: 'c8', reporter: process.env.CI === 'true' ? 'lcovonly' : 'lcov' } },
4 | })
5 |
--------------------------------------------------------------------------------