copy-webpack-options.js 2.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
const fs = require('fs')
const path = require('path')

fxy060608's avatar
fxy060608 已提交
4 5
const assetsDir = 'static'

fxy060608's avatar
fxy060608 已提交
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
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)
  }
}
// 暂未考虑动态添加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)
  }
  // 分包静态资源
  process.UNI_SUBPACKAGES && Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
    const subAssetsCopyOption = getAssetsCopyOption(path.join(root, assetsDir), {
      ignore
    })
    if (subAssetsCopyOption) {
      copyOptions.push(subAssetsCopyOption)
    }
  })
  return copyOptions
}
fxy060608's avatar
fxy060608 已提交
53

fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61
function getUniModulesAssetsCopyOptions (assetsDir) {
  const copyOptions = []
  global.uniModules.forEach(module => {
    copyOptions.push(...getAssetsCopyOptions('uni_modules/' + module + '/' + assetsDir))
  })
  return copyOptions
}

fxy060608's avatar
fxy060608 已提交
62
function getCopyWebpackPluginOptions (platformOptions, vueOptions) {
fxy060608's avatar
fxy060608 已提交
63
  const copyOptions = getAssetsCopyOptions(assetsDir).concat(getUniModulesAssetsCopyOptions(assetsDir))
fxy060608's avatar
fxy060608 已提交
64
  global.uniPlugin.copyWebpackOptions.forEach(copyWebpackOptions => {
fxy060608's avatar
fxy060608 已提交
65
    const platformCopyOptions = copyWebpackOptions(platformOptions, vueOptions, copyOptions) || []
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71
    platformCopyOptions.forEach(copyOption => {
      if (typeof copyOption === 'string') {
        copyOption = getAssetsCopyOption(copyOption)
      }
      copyOption && copyOptions.push(copyOption)
    })
fxy060608's avatar
fxy060608 已提交
72
  })
fxy060608's avatar
fxy060608 已提交
73 74 75
  if (process.env.UNI_PLATFORM === 'app-plus') {
    copyOptions.push({
      from: path.resolve(process.env.UNI_INPUT_DIR, 'android*.json'),
76 77 78 79
      to: '[name].[ext]',
      globOptions: {
        ignored: require('./util').getWatchOptions().ignored
      }
fxy060608's avatar
fxy060608 已提交
80 81
    })
  }
fxy060608's avatar
fxy060608 已提交
82
  return copyOptions
fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88
}

module.exports = {
  assetsDir,
  getCopyWebpackPluginOptions
}