options.ts 3.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import path from 'path'
fxy060608's avatar
fxy060608 已提交
2
import type { CompilerOptions } from '@vue/compiler-core'
3
import {
fxy060608's avatar
fxy060608 已提交
4
  COMPONENT_CUSTOM_HIDDEN,
fxy060608's avatar
fxy060608 已提交
5
  copyMiniProgramPluginJson,
fxy060608's avatar
fxy060608 已提交
6 7
  MiniProgramCompilerOptions,
  transformComponentLink,
fxy060608's avatar
fxy060608 已提交
8
  transformRef,
9
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
10
import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite'
11 12
import { transformAd } from './transforms/transformAd'

fxy060608's avatar
fxy060608 已提交
13
import source from './project.config.json'
fxy060608's avatar
fxy060608 已提交
14

15 16 17 18 19
export const customElements = [
  'page-container',
  'page-meta',
  'navigation-bar',
  'match-media',
d-u-a's avatar
d-u-a 已提交
20
  'ad-custom',
21
]
fxy060608's avatar
fxy060608 已提交
22

fxy060608's avatar
fxy060608 已提交
23
export const compilerOptions: CompilerOptions = {
24
  nodeTransforms: [transformRef, transformComponentLink, transformAd],
fxy060608's avatar
fxy060608 已提交
25 26
}

27 28
const COMPONENTS_DIR = 'wxcomponents'

fxy060608's avatar
fxy060608 已提交
29 30 31 32 33
export const miniProgram: MiniProgramCompilerOptions = {
  class: {
    array: true,
  },
  slot: {
34 35
    fallbackContent: false,
    dynamicSlotNames: true,
fxy060608's avatar
fxy060608 已提交
36
  },
37 38 39
  event: {
    key: true,
  },
fxy060608's avatar
fxy060608 已提交
40
  directive: 'wx:',
fxy060608's avatar
fxy060608 已提交
41
  lazyElement: {
42 43 44 45 46 47 48 49 50 51 52 53 54
    canvas: [
      { name: 'bind', arg: ['canvas-id', 'id'] },
      {
        name: 'on',
        arg: ['touchstart', 'touchmove', 'touchcancel', 'touchend'],
      },
    ],
    editor: [
      {
        name: 'on',
        arg: ['ready'],
      },
    ],
55
    // iOS 平台需要延迟
56
    input: [{ name: 'bind', arg: ['type'] }],
57
    textarea: [{ name: 'on', arg: ['input'] }],
fxy060608's avatar
fxy060608 已提交
58
  },
fxy060608's avatar
fxy060608 已提交
59
  component: {
60
    dir: COMPONENTS_DIR,
fxy060608's avatar
fxy060608 已提交
61
    vShow: COMPONENT_CUSTOM_HIDDEN,
fxy060608's avatar
fxy060608 已提交
62
    getPropertySync: false, // 为了避免 Setting data field "uP" to undefined is invalid 警告
63 64
    normalizeName: (name) =>
      name.startsWith('wx-') ? name.replace('wx-', 'weixin-') : name,
fxy060608's avatar
fxy060608 已提交
65
  },
fxy060608's avatar
fxy060608 已提交
66
}
fxy060608's avatar
fxy060608 已提交
67 68
const projectConfigFilename = 'project.config.json'

fxy060608's avatar
fxy060608 已提交
69
export const options: UniMiniProgramPluginOptions = {
fxy060608's avatar
fxy060608 已提交
70
  cdn: 1,
fxy060608's avatar
fxy060608 已提交
71
  vite: {
fxy060608's avatar
fxy060608 已提交
72
    inject: {
fxy060608's avatar
fxy060608 已提交
73
      uni: [path.resolve(__dirname, 'uni.api.esm.js'), 'default'],
fxy060608's avatar
fxy060608 已提交
74
    },
fxy060608's avatar
fxy060608 已提交
75
    alias: {
fxy060608's avatar
fxy060608 已提交
76
      'uni-mp-runtime': path.resolve(__dirname, 'uni.mp.esm.js'),
fxy060608's avatar
fxy060608 已提交
77
    },
fxy060608's avatar
fxy060608 已提交
78
    copyOptions: {
79
      assets: [COMPONENTS_DIR],
fxy060608's avatar
fxy060608 已提交
80
      targets: [
fxy060608's avatar
fxy060608 已提交
81
        ...(process.env.UNI_MP_PLUGIN ? [copyMiniProgramPluginJson] : []),
fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87 88
        {
          src: [
            'theme.json',
            'sitemap.json',
            'ext.json',
            'custom-tab-bar',
            'functional-pages',
89
            'project.private.config.json',
fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95 96 97
            projectConfigFilename,
          ],
          get dest() {
            return process.env.UNI_OUTPUT_DIR
          },
        },
      ],
    },
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102
  },
  global: 'wx',
  app: {
    darkmode: true,
    subpackages: true,
fxy060608's avatar
fxy060608 已提交
103
    plugins: true,
104
    usingComponents: true,
fxy060608's avatar
fxy060608 已提交
105 106
  },
  project: {
fxy060608's avatar
fxy060608 已提交
107
    filename: projectConfigFilename,
fxy060608's avatar
fxy060608 已提交
108
    config: ['project.wx.json', 'project.config.json'],
fxy060608's avatar
fxy060608 已提交
109
    source,
fxy060608's avatar
fxy060608 已提交
110 111
  },
  template: {
fxy060608's avatar
fxy060608 已提交
112 113
    /* eslint-disable no-restricted-syntax */
    ...miniProgram,
fxy060608's avatar
fxy060608 已提交
114
    customElements,
fxy060608's avatar
fxy060608 已提交
115 116
    filter: {
      extname: '.wxs',
fxy060608's avatar
fxy060608 已提交
117
      lang: 'wxs',
fxy060608's avatar
fxy060608 已提交
118 119 120
      generate(filter, filename) {
        if (filename) {
          return `<wxs src="${filename}.wxs" module="${filter.name}"/>`
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126
        }
        return `<wxs module="${filter.name}">
${filter.code}
</wxs>`
      },
    },
fxy060608's avatar
fxy060608 已提交
127
    extname: '.wxml',
fxy060608's avatar
fxy060608 已提交
128
    compilerOptions,
fxy060608's avatar
fxy060608 已提交
129 130 131 132 133
  },
  style: {
    extname: '.wxss',
  },
}