vite-plugin-uni-provider.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import path from 'path'
  2. import c from 'picocolors'
  3. import normallize from 'normalize-path'
  4. import { pages, subPackages } from '../src/pages.json'
  5. export interface Options {
  6. pagesRE: RegExp
  7. name: string
  8. configFile: string
  9. pagesBasePath: string
  10. configPath: string
  11. pluginName: string
  12. DEBUG: boolean
  13. }
  14. // 处理分包路由
  15. const subPages = []
  16. subPackages.forEach((item) => {
  17. if (item.pages && Array.isArray(item.pages)) {
  18. item.pages.forEach((p) => {
  19. subPages.push(`${item.root}/${p.path}.vue`)
  20. })
  21. }
  22. })
  23. // 需要拦截的页面
  24. const pageRouter = [...pages.map((item) => `${item.path}.vue`), ...subPages]
  25. export default function (options: Partial<Options> = {}) {
  26. let {
  27. pagesRE = /src[\/\\]pages[\/\\]((?!.+(component(s)?|static).+).)*\.vue$/,
  28. name = 'MConfigProvider',
  29. pluginName = 'uni-provider',
  30. DEBUG = process.env.DEBUG,
  31. } = options
  32. return {
  33. name: 'vite-plugin-' + pluginName,
  34. enforce: 'pre',
  35. transform(code, id) {
  36. id = normalizePagePathFromBase(id)
  37. if (pageRouter.includes(normalizePagePathFromBase(id))) {
  38. // 三种情况:
  39. // 1. 前后都存在页面根级组件 => 不做操作
  40. // 2. 页面根级组件只存在于第一行 => 第一行修正结束符,最后一行添加结束符
  41. // 3. 前后都不存在页面根级组件 => 第一行添加开始符,最后一行添加结束符
  42. // 其他情况直接语法报错,不需要处理
  43. let startTag = new RegExp(`\<${name}`).test(code)
  44. let endTag = new RegExp(`\<\/${name}`).test(code)
  45. if (startTag && !endTag)
  46. code = code
  47. .replace(new RegExp(`(?<=\<${name}.*?)(\/\>|>.*?\<\/${name}\>)`), '>')
  48. .replace(/([\s\S]*)(\<\/template\>)/, `$1</${name}>\n</template>`)
  49. if (!startTag && !endTag)
  50. code = code
  51. .replace('<template>', `<template>\n<${name}>`)
  52. .replace(/([\s\S]*)(\<\/template\>)/, `$1</${name}>\n</template>`)
  53. debug(c.yellow(id), `startTag: ${startTag}`, `endTag: ${endTag}`)
  54. }
  55. return { code, map: null }
  56. },
  57. }
  58. function normalizePagePathFromBase(file) {
  59. return normallize(path.relative(process.cwd(), file.replace('src/', '')))
  60. }
  61. function debug(...args) {
  62. DEBUG &&
  63. console.log(
  64. c.dim(new Date().toLocaleTimeString()),
  65. c.bold(c.red(`[debug:${pluginName}]`)),
  66. ...args
  67. )
  68. }
  69. }