options.ts 2.9 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'
fxy060608's avatar
fxy060608 已提交
11

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

14 15 16 17 18 19
export const customElements = [
  'page-container',
  'page-meta',
  'navigation-bar',
  'match-media',
]
fxy060608's avatar
fxy060608 已提交
20

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

25 26
const COMPONENTS_DIR = 'wxcomponents'

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

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