pagesJson.ts 1.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10
import path from 'path'
import { Plugin } from 'vite'

import {
  defineUniPagesJsonPlugin,
  normalizeAppConfigService,
  normalizePagesJson,
  parseManifestJsonOnce,
  getLocaleFiles,
  normalizeAppNVuePagesJson,
fxy060608's avatar
fxy060608 已提交
11
  APP_CONFIG_SERVICE,
fxy060608's avatar
fxy060608 已提交
12 13
} from '@dcloudio/uni-cli-shared'

fxy060608's avatar
fxy060608 已提交
14
export function uniPagesJsonPlugin({
fxy060608's avatar
fxy060608 已提交
15
  renderer,
fxy060608's avatar
fxy060608 已提交
16 17
  appService,
}: {
fxy060608's avatar
fxy060608 已提交
18
  renderer?: 'native'
fxy060608's avatar
fxy060608 已提交
19 20
  appService: boolean
}): Plugin {
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  return defineUniPagesJsonPlugin((opts) => {
    return {
      name: 'uni:app-nvue-pages-json',
      enforce: 'pre',
      transform(code, id) {
        if (!opts.filter(id)) {
          return
        }
        this.addWatchFile(path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'))
        getLocaleFiles(
          path.resolve(process.env.UNI_INPUT_DIR, 'locale')
        ).forEach((filepath) => {
          this.addWatchFile(filepath)
        })
        const pagesJson = normalizePagesJson(code, process.env.UNI_PLATFORM)
        pagesJson.pages.forEach((page) => {
          if (page.style.isNVue) {
            this.addWatchFile(
              path.resolve(process.env.UNI_INPUT_DIR, page.path + '.nvue')
            )
          }
        })
fxy060608's avatar
fxy060608 已提交
43
        if (renderer === 'native' && appService) {
fxy060608's avatar
fxy060608 已提交
44
          this.emitFile({
fxy060608's avatar
fxy060608 已提交
45
            fileName: APP_CONFIG_SERVICE,
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52
            type: 'asset',
            source: normalizeAppConfigService(
              pagesJson,
              parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
            ),
          })
          return {
fxy060608's avatar
fxy060608 已提交
53
            code: '',
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63 64
            map: { mappings: '' },
          }
        }
        return {
          code: normalizeAppNVuePagesJson(pagesJson),
          map: { mappings: '' },
        }
      },
    }
  })
}