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

fxy060608's avatar
fxy060608 已提交
3 4 5 6
import {
  defineUniManifestJsonPlugin,
  normalizeAppManifestJson,
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
7 8

export function uniManifestJsonPlugin(): Plugin {
fxy060608's avatar
fxy060608 已提交
9
  let manifestJson: Record<string, any>
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16 17
  return defineUniManifestJsonPlugin((opts) => {
    return {
      name: 'vite:uni-app-manifest-json',
      enforce: 'pre',
      transform(code, id) {
        if (!opts.filter(id)) {
          return
        }
fxy060608's avatar
fxy060608 已提交
18 19 20 21
        manifestJson = normalizeAppManifestJson(JSON.parse(code))
        return ''
      },
      generateBundle() {
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27
        // 生成一个空的app-config.js,兼容基座已有规范
        this.emitFile({
          fileName: `app-config.js`,
          type: 'asset',
          source: '(function(){})();',
        })
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32
        this.emitFile({
          fileName: `manifest.json`,
          type: 'asset',
          source: JSON.stringify(manifestJson, null, 2),
        })
fxy060608's avatar
fxy060608 已提交
33 34 35 36
      },
    }
  })
}