plugin.js 3.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
const path = require('path')

const initPreprocessContext = require('./preprocess')

const Plugin = {
  options: {},
  // 初步校验相关配置是否正确
  validate: [], // (platformOptions, manifestJson) {},
fxy060608's avatar
fxy060608 已提交
9
  configureEnv: [], // (){},
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  // 以 H5 为基准的平台特殊配置
  configureH5: [], // (h5Options) {},
  // 链式修改 webpack config
  chainWebpack: [], // (config, vueOptions, api) {},
  // 修改 webpack config
  configureWebpack: [], // (config, vueOptions, api) {},
  // 配置额外的资源拷贝
  copyWebpackOptions: [] // (platformOptions, vueOptions) {}
}

const PLUGIN_KEYS = Object.keys(Plugin)

function initPlugin (plugin) {
  let pluginApi
  try {
    pluginApi = require(path.join(plugin.id, (plugin.config.main || '/lib/uni.config.js')))
  } catch (e) {
fxy060608's avatar
fxy060608 已提交
27
    console.warn(`${plugin.id} 缺少 uni.config.js `)
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  }

  pluginApi && PLUGIN_KEYS.forEach(name => {
    if (pluginApi[name]) {
      if (Array.isArray(Plugin[name])) { // hooks
        Plugin[name].push(pluginApi[name])
      } else { // options
        Object.assign(Plugin[name], pluginApi[name])
      }
    }
  })
}

const pluginRE = /^(uni-|@[\w-]+(\.)?[\w-]+\/uni-)/

const officialPlugins = [
  '@dcloudio/uni-app-plus',
fxy060608's avatar
fxy060608 已提交
45 46
  '@dcloudio/uni-h5',
  '@dcloudio/uni-mp-360',
fxy060608's avatar
fxy060608 已提交
47
  '@dcloudio/uni-mp-alipay',
fxy060608's avatar
fxy060608 已提交
48 49 50
  '@dcloudio/uni-mp-baidu',
  '@dcloudio/uni-mp-qq',
  '@dcloudio/uni-mp-quickapp',
fxy060608's avatar
fxy060608 已提交
51
  '@dcloudio/uni-mp-toutiao',
fxy060608's avatar
fxy060608 已提交
52
  '@dcloudio/uni-mp-welink',
fxy060608's avatar
fxy060608 已提交
53
  '@dcloudio/uni-mp-weixin'
fxy060608's avatar
fxy060608 已提交
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
]

function resolvePlugins () {
  const pkg = require(path.resolve(process.env.UNI_CLI_CONTEXT, 'package.json'))
  return Object.keys(pkg.devDependencies || {})
    .concat(Object.keys(pkg.dependencies || {}))
    .map(id => {
      let isPlatformPlugin = false
      if (id.startsWith('@dcloudio/')) {
        if (!officialPlugins.includes(id)) {
          return
        }
        isPlatformPlugin = true
      } else {
        isPlatformPlugin = pluginRE.test(id)
      }
      if (!isPlatformPlugin) {
        return
      }
      try {
        const pluginPkg = require(id + '/package.json')
        const config = pluginPkg['uni-app']
        if (!config) {
          return
        }
        if (!config.name) {
          return console.warn(`${id}/package.json->uni-app 缺少 name 属性`)
        }
        return {
          id,
          name: config.name,
          config
        }
      } catch (e) {}
    }).filter(Boolean)
}

function initExtends (name, plugin, plugins) {
  const extendsPlatform = plugin.config['extends']
  if (extendsPlatform) {
    if (extendsPlatform !== 'h5') {
      console.error(`目前仅支持基于 h5 平台做扩展`)
      process.exit(0)
    }
    const extendsPlugin = plugins.find(plugin => plugin.name === extendsPlatform)
    if (!plugin) {
      console.error(`缺少平台 ${extendsPlatform} 插件`)
      process.exit(0)
    }
    process.env.UNI_SUB_PLATFORM = name
    process.env.UNI_PLATFORM = extendsPlatform
    initPlugin(extendsPlugin)
  }
}

module.exports = {
  init () {
    const plugins = resolvePlugins()
    const plugin = plugins.find(plugin => plugin.name === process.env.UNI_PLATFORM)
    if (!plugin) {
      console.error(`缺少平台 ${process.env.UNI_PLATFORM} 插件`)
      process.exit(0)
    }
    const name = plugin.name

    initExtends(name, plugin, plugins)

    initPlugin(plugin)
fxy060608's avatar
fxy060608 已提交
122 123 124

    Plugin.name = name
    Plugin.id = plugin.id
fxy060608's avatar
fxy060608 已提交
125 126 127 128 129 130 131
    Plugin.config = plugin.config
    Plugin.platforms = plugins.map(plugin => plugin.name)
    Plugin.preprocess = initPreprocessContext(name, Plugin.platforms, process.UNI_SCRIPT_DEFINE)

    return Plugin
  }
}