manifestJson.ts 1.4 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 14 15 16 17 18 19 20

export function uniManifestJsonPlugin(): Plugin {
  return defineUniManifestJsonPlugin((opts) => {
    return {
      name: 'vite:uni-app-manifest-json',
      enforce: 'pre',
      transform(code, id) {
        if (!opts.filter(id)) {
          return
        }
fxy060608's avatar
fxy060608 已提交
21 22 23
        this.addWatchFile(
          path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json')
        )
24 25 26 27 28
        getLocaleFiles(
          path.resolve(process.env.UNI_INPUT_DIR, 'locale')
        ).forEach((filepath) => {
          this.addWatchFile(filepath)
        })
fxy060608's avatar
fxy060608 已提交
29
        const manifestJson = normalizeAppManifestJson(
30
          parseJson(code),
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35
          parsePagesJsonOnce(
            process.env.UNI_INPUT_DIR,
            process.env.UNI_PLATFORM
          )
        )
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41
        // 生成一个空的app-config.js,兼容基座已有规范
        this.emitFile({
          fileName: `app-config.js`,
          type: 'asset',
          source: '(function(){})();',
        })
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46
        this.emitFile({
          fileName: `manifest.json`,
          type: 'asset',
          source: JSON.stringify(manifestJson, null, 2),
        })
fxy060608's avatar
fxy060608 已提交
47 48 49 50
        return {
          code: '',
          map: this.getCombinedSourcemap(),
        }
fxy060608's avatar
fxy060608 已提交
51 52 53 54
      },
    }
  })
}