myPlugin.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. export default function myPlugin() {
  2. return {
  3. name: 'myplugin',
  4. enforce: 'pre',
  5. transform(code, id) {
  6. if (!/\.vue$/.test(id))
  7. return
  8. //找到带有标记的代码,以后估计安装路径直接找
  9. if (isInVue(id)){
  10. // //为全部el开头的组件外面再套一层
  11. // code = wrapElementComponents(code)
  12. // code = injectStyles(code)
  13. }
  14. const css = code
  15. if (css) {
  16. return {
  17. code: css,
  18. map: null,
  19. }
  20. }
  21. },
  22. handleHotUpdate(ctx) {
  23. const read = ctx.read
  24. if (/\.vue$/.test(ctx.file)) {
  25. ctx.read = async () => {
  26. const code = await read()
  27. return `${code}`
  28. }
  29. }
  30. },
  31. }
  32. }
  33. const regex = /<!--watchstart-->([\s\S]*?)<!--watchend-->/;
  34. function isInVue(id) {
  35. return id == 'D:/workspace/RuoYi-Vue/ruoyi-ui/src/views/index.vue'
  36. }
  37. function wrapElementComponents(code) {
  38. // 匹配el-col和el-row的开始标签,并包裹div
  39. let wrappedCode = code.replace(
  40. /<(el-(row|col))([^>]*)>/g,
  41. '<$1$3><div class="grid-content">'
  42. );
  43. // 匹配el-col和el-row的结束标签前插入div闭合标签
  44. wrappedCode = wrappedCode.replace(
  45. /<\/(el-(row|col))>/g,
  46. '</div></$1>'
  47. );
  48. return wrappedCode;
  49. }
  50. // 在myPlugin.js中添加样式注入逻辑
  51. function injectStyles(code) {
  52. // 匹配现有的style标签
  53. const styleRegex = /<style[^>]*scoped[^>]*>([\s\S]*?)<\/style>/i;
  54. if (styleRegex.test(code)) {
  55. // 如果已有scoped样式,追加内容
  56. return code.replace(styleRegex, (match, p1) => {
  57. return `<style scoped>${p1}\n${targetStyles}</style>`;
  58. });
  59. } else {
  60. // 如果没有样式标签,添加新样式块
  61. return code.replace(/<\/template>/, `</template>\n<style scoped>\n${targetStyles}\n</style>`);
  62. }
  63. }
  64. //样式
  65. const targetStyles = `.grid-content {
  66. border: 1px solid #e4e7ed;
  67. min-height: 36px;
  68. display: flex;
  69. align-items: center;
  70. justify-content: center;
  71. padding: 20px;
  72. transition: all 0.3s ease;
  73. &:hover {
  74. background-color: #f5f7fa;
  75. border-color: #409eff;
  76. transform: translateY(-2px);
  77. }
  78. }
  79. .el-row {
  80. margin-bottom: 20px;
  81. &:last-child {
  82. margin-bottom: 0;
  83. }
  84. }`;