cnf
2025-05-10 386fa0eca75ddc88165f9b73038f2a2239e1072e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const fs = require('fs')
const path = require('path')
const {
  compileI18nJsonStr
} = require('@dcloudio/uni-i18n')
const {
  normalizePath
} = require('@dcloudio/uni-cli-shared')
const {
  initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
const assetsDir = 'static'
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
 
function getAssetsCopyOption (from, options = {}) {
  if (path.isAbsolute(from)) {
    if (fs.existsSync(from)) {
      return Object.assign({
        from,
        to: path.resolve(process.env.UNI_OUTPUT_DIR)
      },
      options
      )
    }
  }
  const to = from
  from = path.resolve(process.env.UNI_INPUT_DIR, from)
  if (fs.existsSync(from)) {
    return Object.assign({
      from,
      to: path.resolve(process.env.UNI_OUTPUT_DIR, to)
    },
    options
    )
  }
}
 
function addIgnore (ignore, assetsDir, platform, ignoreStatic) {
  if (CopyWebpackPluginVersion > 5) {
    if (platform === 'app-plus') {
      ignore.push(normalizePath(path.resolve(process.env.UNI_INPUT_DIR, assetsDir, 'app/**/*')))
    } else if (platform === 'h5') {
      ignore.push(normalizePath(path.resolve(process.env.UNI_INPUT_DIR, assetsDir, 'web/**/*')))
    }
    ignore.push(normalizePath(path.resolve(process.env.UNI_INPUT_DIR, assetsDir, platform + '/**/*')))
  } else {
    if (platform === 'app-plus') {
      ignore.push('app/**/*')
    } else if (platform === 'h5') {
      ignore.push('web/**/*')
    }
    ignore.push(platform + '/**/*')
  }
  if (platform === 'app-plus') {
    ignoreStatic.push(['static', 'app'])
  } else if (platform === 'h5') {
    ignoreStatic.push(['static', 'web'])
  }
  ignoreStatic.push(['static', platform])
}
 
let isIgnoreChecked = false
 
function checkIgnoreStatic (ignoreStatic) {
  if (isIgnoreChecked) {
    return
  }
  isIgnoreChecked = true
  const existIgnore = new Set()
  ignoreStatic.forEach(ignore => {
    const dir = path.resolve(process.env.UNI_INPUT_DIR, ...ignore)
    if (fs.existsSync(dir)) {
      existIgnore.add(ignore.join('/'))
    }
  })
  if (existIgnore.size) {
    console.log('已忽略静态资源目录:' + [...existIgnore].join('、') +
      '。详见:https://uniapp.dcloud.net.cn/tutorial/platform.html#static')
  }
}
 
// 暂未考虑动态添加static目录
function getAssetsCopyOptions (assetsDir) {
  const ignore = []
  const ignoreStatic = []
  global.uniPlugin.platforms.forEach(platform => {
    if (global.uniPlugin.name !== platform) {
      addIgnore(ignore, assetsDir, platform, ignoreStatic)
    }
  })
  checkIgnoreStatic(ignoreStatic)
  const copyOptions = []
  // 主包静态资源
  const mainAssetsCopyOption = getAssetsCopyOption(assetsDir, CopyWebpackPluginVersion > 5 ? {
    noErrorOnMissing: true,
    globOptions: {
      ignore
    }
  } : {
    ignore
  })
  if (mainAssetsCopyOption) {
    copyOptions.push(mainAssetsCopyOption)
  }
  // 分包静态资源
  process.UNI_SUBPACKAGES &&
    Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
      const subAssetsCopyOption = getAssetsCopyOption(path.join(root, assetsDir), CopyWebpackPluginVersion > 5 ? {
        noErrorOnMissing: true,
        globOptions: {
          ignore
        }
      } : {
        ignore
      })
      if (subAssetsCopyOption) {
        copyOptions.push(subAssetsCopyOption)
      }
    })
  return copyOptions
}
 
function getUniModulesAssetsCopyOptions (assetsDir) {
  const copyOptions = []
  global.uniModules.forEach(module => {
    copyOptions.push(
      ...getAssetsCopyOptions('uni_modules/' + module + '/' + assetsDir)
    )
  })
  return copyOptions
}
 
function getCopyWebpackPluginOptions (platformOptions, vueOptions) {
  const copyOptions = getAssetsCopyOptions(assetsDir).concat(
    getUniModulesAssetsCopyOptions(assetsDir)
  )
  global.uniPlugin.copyWebpackOptions.forEach(copyWebpackOptions => {
    const platformCopyOptions =
      copyWebpackOptions(platformOptions, vueOptions, copyOptions) || []
    platformCopyOptions.forEach(copyOption => {
      if (typeof copyOption === 'string') {
        copyOption = getAssetsCopyOption(copyOption)
      }
      copyOption && copyOptions.push(copyOption)
    })
  })
  // 自动化测试时,不启用androidPrivacy.json
  if (process.env.UNI_PLATFORM === 'app-plus' && !process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
    const fileName = 'androidPrivacy.json'
    const context = path.resolve(process.env.UNI_INPUT_DIR)
    if (fs.existsSync(path.join(context, fileName))) {
      copyOptions.push({
        from: fileName,
        context,
        to: fileName,
        transform (content) {
          const options = initI18nOptions(
            process.env.UNI_PLATFORM,
            process.env.UNI_INPUT_DIR,
            false,
            true
          )
          if (!options) {
            return content
          }
          return compileI18nJsonStr(content.toString(), options)
        }
      })
    }
  }
  return copyOptions
}
 
module.exports = {
  assetsDir,
  getCopyWebpackPluginOptions
}