版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # vite-plugin-removelog
  2. 生产环境下移除 `console` 的 `vite` 插件
  3. <br />
  4. <br />
  5. ## 动机 🦒
  6. 不希望开发时的日志在生产环境下被人看到
  7. <br />
  8. <br />
  9. <br />
  10. ## 特性 🦖
  11. - 支持 `.js`,`.ts`,`.jsx`,`.tsx`,`.vue`
  12. <br />
  13. <br />
  14. <br />
  15. ## 使用 🦕
  16. ### 安装
  17. ```shell
  18. npm i vite-plugin-removelog -D
  19. ```
  20. <br />
  21. <br />
  22. ### 配置
  23. ```js
  24. // vite.config.js
  25. import Removelog from 'vite-plugin-removelog'
  26. export default defineConfig({
  27. plugins: [Removelog()]
  28. })
  29. ```
  30. <br />
  31. #### 包含
  32. ```js
  33. // vite.config.js
  34. import Removelog from 'vite-plugin-removelog'
  35. export default defineConfig({
  36. plugins: [
  37. Removelog({
  38. // 默认包含 log,warn,error
  39. include: ['log', 'warn']
  40. })
  41. ]
  42. })
  43. ```
  44. <br />
  45. #### 规范
  46. 可以通过 `normalize` 来自定义哪些模块需要被作用
  47. ```js
  48. // vite.config.js
  49. import Removelog from 'vite-plugin-removelog'
  50. export default defineConfig({
  51. plugins: [
  52. Removelog({
  53. // 返回 Truthy 时,模块被将作用
  54. normalize(id) {
  55. return /(\.vue|\.[jt]sx?)$/.test(id)
  56. }
  57. })
  58. ]
  59. })
  60. ```
  61. <br />
  62. #### 忽略 `node_modules`
  63. 可以通过 `ignoreNodeModules` 忽略 `node_modules` 包的处理
  64. ```js
  65. // vite.config.js
  66. import Removelog from 'vite-plugin-removelog'
  67. export default defineConfig({
  68. plugins: [
  69. Removelog({
  70. // 默认为 true
  71. ignoreNodeModules: true
  72. })
  73. ]
  74. })
  75. ```
  76. <br />
  77. <br />
  78. ## 原理
  79. 该插件不传入 `normalize` 时由 [vite](https://cn.vitejs.dev/) 内置的 [esbuild](https://esbuild.github.io/) 进行转换,当传入 `normalize` 时,则为 [gogocode](https://github.com/thx/gogocode/issues) 进行转换。
  80. [gogocode](https://github.com/thx/gogocode/issues) 实现的转换也是导出的 👇
  81. ```js
  82. import { gogocodeRemovelog } from 'vite-plugin-removelog'
  83. const code = `
  84. const foo = 1
  85. console.log("foo")
  86. `
  87. const dest = gogocodeRemovelog(code)
  88. console.log(dest) // const foo = 1
  89. ```
  90. <br />
  91. <br />
  92. ## 组织 🦔
  93. 欢迎关注 **帝莎编程**
  94. - [官网](http://dishaxy.dishait.cn/)
  95. - [Gitee](https://gitee.com/dishait)
  96. - [Github](https://github.com/dishait)
  97. - [网易云课堂](https://study.163.com/provider/480000001892585/index.htm?share=2&shareId=480000001892585)
  98. <br />
  99. <br />
  100. <br />
  101. ## License
  102. Made with [markthree](https://github.com/markthree)
  103. Published under [MIT License](./LICENSE).
  104. <br />