pagesJson.ts 970 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { Plugin } from 'vite'

fxy060608's avatar
fxy060608 已提交
3 4
import {
  defineUniPagesJsonPlugin,
fxy060608's avatar
fxy060608 已提交
5
  normalizeAppPagesJson,
fxy060608's avatar
fxy060608 已提交
6 7
  normalizeAppConfigService,
  normalizePagesJson,
fxy060608's avatar
fxy060608 已提交
8
  parseManifestJsonOnce,
fxy060608's avatar
fxy060608 已提交
9 10 11
} from '@dcloudio/uni-cli-shared'

export function uniPagesJsonPlugin(): Plugin {
fxy060608's avatar
fxy060608 已提交
12
  let pagesJson: UniApp.PagesJson
fxy060608's avatar
fxy060608 已提交
13 14 15 16
  return defineUniPagesJsonPlugin((opts) => {
    return {
      name: 'vite:uni-app-pages-json',
      enforce: 'pre',
fxy060608's avatar
fxy060608 已提交
17 18 19
      transform(code, id) {
        if (!opts.filter(id)) {
          return
fxy060608's avatar
fxy060608 已提交
20
        }
fxy060608's avatar
fxy060608 已提交
21
        pagesJson = normalizePagesJson(code, process.env.UNI_PLATFORM)
fxy060608's avatar
fxy060608 已提交
22
        return (
fxy060608's avatar
fxy060608 已提交
23
          `import './manifest.json.js'\n` + normalizeAppPagesJson(pagesJson)
fxy060608's avatar
fxy060608 已提交
24
        )
fxy060608's avatar
fxy060608 已提交
25
      },
fxy060608's avatar
fxy060608 已提交
26 27 28 29
      generateBundle() {
        this.emitFile({
          fileName: `app-config-service.js`,
          type: 'asset',
fxy060608's avatar
fxy060608 已提交
30 31 32 33
          source: normalizeAppConfigService(
            pagesJson,
            parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
          ),
fxy060608's avatar
fxy060608 已提交
34 35
        })
      },
fxy060608's avatar
fxy060608 已提交
36 37 38
    }
  })
}