copy-webpack-options.js 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const fs = require('fs')
const path = require('path')
fxy060608's avatar
fxy060608 已提交
3 4
const { compileI18nJsonStr } = require('@dcloudio/uni-i18n')
const { initI18nOptions } = require('@dcloudio/uni-cli-shared/lib/i18n')
fxy060608's avatar
fxy060608 已提交
5 6
const assetsDir = 'static'

fxy060608's avatar
fxy060608 已提交
7 8 9
function getAssetsCopyOption (from, options = {}) {
  if (path.isAbsolute(from)) {
    if (fs.existsSync(from)) {
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16
      return Object.assign(
        {
          from,
          to: path.resolve(process.env.UNI_OUTPUT_DIR)
        },
        options
      )
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
    }
  }
  const to = from
  from = path.resolve(process.env.UNI_INPUT_DIR, from)
  if (fs.existsSync(from)) {
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27 28
    return Object.assign(
      {
        from,
        to: path.resolve(process.env.UNI_OUTPUT_DIR, to)
      },
      options
    )
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  }
}
// 暂未考虑动态添加static目录
function getAssetsCopyOptions (assetsDir) {
  const ignore = []

  global.uniPlugin.platforms.forEach(platform => {
    if (global.uniPlugin.name !== platform) {
      ignore.push(platform + '/**/*')
    }
  })

  const copyOptions = []
  // 主包静态资源
  const mainAssetsCopyOption = getAssetsCopyOption(assetsDir, {
    ignore
  })
  if (mainAssetsCopyOption) {
    copyOptions.push(mainAssetsCopyOption)
  }
  // 分包静态资源
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55 56 57 58 59 60
  process.UNI_SUBPACKAGES &&
    Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
      const subAssetsCopyOption = getAssetsCopyOption(
        path.join(root, assetsDir),
        {
          ignore
        }
      )
      if (subAssetsCopyOption) {
        copyOptions.push(subAssetsCopyOption)
      }
fxy060608's avatar
fxy060608 已提交
61 62 63
    })
  return copyOptions
}
fxy060608's avatar
fxy060608 已提交
64

fxy060608's avatar
fxy060608 已提交
65 66 67
function getUniModulesAssetsCopyOptions (assetsDir) {
  const copyOptions = []
  global.uniModules.forEach(module => {
fxy060608's avatar
fxy060608 已提交
68 69 70
    copyOptions.push(
      ...getAssetsCopyOptions('uni_modules/' + module + '/' + assetsDir)
    )
fxy060608's avatar
fxy060608 已提交
71 72 73 74
  })
  return copyOptions
}

fxy060608's avatar
fxy060608 已提交
75
function getCopyWebpackPluginOptions (platformOptions, vueOptions) {
fxy060608's avatar
fxy060608 已提交
76 77 78
  const copyOptions = getAssetsCopyOptions(assetsDir).concat(
    getUniModulesAssetsCopyOptions(assetsDir)
  )
fxy060608's avatar
fxy060608 已提交
79
  global.uniPlugin.copyWebpackOptions.forEach(copyWebpackOptions => {
fxy060608's avatar
fxy060608 已提交
80 81
    const platformCopyOptions =
      copyWebpackOptions(platformOptions, vueOptions, copyOptions) || []
fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87
    platformCopyOptions.forEach(copyOption => {
      if (typeof copyOption === 'string') {
        copyOption = getAssetsCopyOption(copyOption)
      }
      copyOption && copyOptions.push(copyOption)
    })
fxy060608's avatar
fxy060608 已提交
88
  })
fxy060608's avatar
fxy060608 已提交
89 90 91
  if (process.env.UNI_PLATFORM === 'app-plus') {
    copyOptions.push({
      from: path.resolve(process.env.UNI_INPUT_DIR, 'android*.json'),
92 93 94
      to: '[name].[ext]',
      globOptions: {
        ignored: require('./util').getWatchOptions().ignored
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
      },
      transform (content, path) {
        if (path.endsWith('androidPrivacy.json')) {
          const options = initI18nOptions(
            process.env.UNI_PLATFORM,
            process.env.UNI_INPUT_DIR,
            false,
            true
          )
          if (!options) {
            return content
          }
          return compileI18nJsonStr(content.toString(), options)
        }
        return content
110
      }
fxy060608's avatar
fxy060608 已提交
111 112
    })
  }
fxy060608's avatar
fxy060608 已提交
113
  return copyOptions
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118
}

module.exports = {
  assetsDir,
  getCopyWebpackPluginOptions
fxy060608's avatar
fxy060608 已提交
119
}