manifestJson.ts 1.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import path from 'path'
fxy060608's avatar
fxy060608 已提交
2 3
import { Plugin } from 'vite'

fxy060608's avatar
fxy060608 已提交
4 5
import {
  defineUniManifestJsonPlugin,
6
  getLocaleFiles,
fxy060608's avatar
fxy060608 已提交
7
  normalizeAppManifestJson,
8
  parseJson,
fxy060608's avatar
fxy060608 已提交
9
  parsePagesJsonOnce,
fxy060608's avatar
fxy060608 已提交
10
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
11 12 13

export function uniManifestJsonPlugin(): Plugin {
  return defineUniManifestJsonPlugin((opts) => {
fxy060608's avatar
fxy060608 已提交
14
    const inputDir = process.env.UNI_INPUT_DIR
fxy060608's avatar
fxy060608 已提交
15
    return {
fxy060608's avatar
fxy060608 已提交
16
      name: 'uni:app-manifest-json',
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
      enforce: 'pre',
      transform(code, id) {
        if (!opts.filter(id)) {
          return
        }
fxy060608's avatar
fxy060608 已提交
22 23
        this.addWatchFile(path.resolve(inputDir, 'manifest.json'))
        getLocaleFiles(path.resolve(inputDir, 'locale')).forEach((filepath) => {
24 25
          this.addWatchFile(filepath)
        })
fxy060608's avatar
fxy060608 已提交
26
        const manifestJson = normalizeAppManifestJson(
27
          parseJson(code),
fxy060608's avatar
fxy060608 已提交
28
          parsePagesJsonOnce(inputDir, process.env.UNI_PLATFORM)
fxy060608's avatar
fxy060608 已提交
29
        )
fxy060608's avatar
fxy060608 已提交
30 31

        // 生成一个空的 app-config.js,兼容基座已有规范
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36
        this.emitFile({
          fileName: `app-config.js`,
          type: 'asset',
          source: '(function(){})();',
        })
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41
        this.emitFile({
          fileName: `manifest.json`,
          type: 'asset',
          source: JSON.stringify(manifestJson, null, 2),
        })
fxy060608's avatar
fxy060608 已提交
42 43
        return {
          code: '',
fxy060608's avatar
fxy060608 已提交
44
          map: null,
fxy060608's avatar
fxy060608 已提交
45
        }
fxy060608's avatar
fxy060608 已提交
46 47 48 49
      },
    }
  })
}