copy-webpack-options.js 3.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const fs = require('fs')
const path = require('path')
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7 8
const {
  compileI18nJsonStr
} = require('@dcloudio/uni-i18n')
const {
  initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
fxy060608's avatar
fxy060608 已提交
9
const assetsDir = 'static'
10
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
fxy060608's avatar
fxy060608 已提交
11

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

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

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

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

fxy060608's avatar
fxy060608 已提交
79
function getCopyWebpackPluginOptions (platformOptions, vueOptions) {
fxy060608's avatar
fxy060608 已提交
80 81 82
  const copyOptions = getAssetsCopyOptions(assetsDir).concat(
    getUniModulesAssetsCopyOptions(assetsDir)
  )
fxy060608's avatar
fxy060608 已提交
83
  global.uniPlugin.copyWebpackOptions.forEach(copyWebpackOptions => {
fxy060608's avatar
fxy060608 已提交
84 85
    const platformCopyOptions =
      copyWebpackOptions(platformOptions, vueOptions, copyOptions) || []
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91
    platformCopyOptions.forEach(copyOption => {
      if (typeof copyOption === 'string') {
        copyOption = getAssetsCopyOption(copyOption)
      }
      copyOption && copyOptions.push(copyOption)
    })
fxy060608's avatar
fxy060608 已提交
92
  })
fxy060608's avatar
fxy060608 已提交
93 94
  // 自动化测试时,不启用androidPrivacy.json
  if (process.env.UNI_PLATFORM === 'app-plus' && !process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
fxy060608's avatar
fxy060608 已提交
95 96
    copyOptions.push({
      from: path.resolve(process.env.UNI_INPUT_DIR, 'android*.json'),
97 98
      to: `[name]${CopyWebpackPluginVersion > 5 ? '' : '.'}[ext]`,
      noErrorOnMissing: true,
99 100
      globOptions: {
        ignored: require('./util').getWatchOptions().ignored
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      },
      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
116
      }
fxy060608's avatar
fxy060608 已提交
117 118
    })
  }
fxy060608's avatar
fxy060608 已提交
119
  return copyOptions
fxy060608's avatar
fxy060608 已提交
120 121 122 123 124
}

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