add-plugin-wrapper.js 1.2 KB
Newer Older
Q
qiang 已提交
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
const { createSource } = require('../shared')

module.exports = function (compilation) {
  if (process.env.UNI_PLATFORM !== 'mp-alipay') {
    return
  }
  // fix mp-alipay plugin
  const appJsonName = 'app.json'
  const appJsonFile = compilation.getAsset(appJsonName)
  if (appJsonFile) {
    const componentName = 'plugin-wrapper'
    const obj = JSON.parse(appJsonFile.source.source())
    obj.usingComponents = obj.usingComponents || {}
    if (!(componentName in obj.usingComponents)) {
      obj.usingComponents[componentName] = `/${componentName}`
      const source = JSON.stringify(obj, null, 2)
      const newSource = createSource(source)
      compilation.updateAsset(appJsonName, newSource)
      const files = [
        {
          ext: 'axml',
          source: '<slot></slot>'
        },
        {
          ext: 'js',
          source: 'Component({onInit(){this.props.onPluginWrap(this)},didUnmount(){this.props.onPluginWrap(this,true)}})'
        },
        {
          ext: 'json',
          source: '{"component":true}'
        }
      ]
      files.forEach(({ ext, source }) => {
        compilation.emitAsset(`${componentName}.${ext}`, createSource(source))
      })
    }
  }
}