generate-app.js 2.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
const {
  getPlatformExts
} = require('../shared')

const {
  getShadowCss,
  getPlatformGlobal
} = require('@dcloudio/uni-cli-shared')

const {
  getSpecialMethods
} = require('@dcloudio/uni-cli-shared/lib/cache')

module.exports = function generateApp (compilation) {
  const ext = getPlatformExts().style

  let importMainCss = ''
  let importVendorCss = ''

  if (
    process.env.NODE_ENV === 'production' &&
    process.env.UNI_PLATFORM !== 'app-plus'
  ) {
fxy060608's avatar
fxy060608 已提交
24
    const targetCssName = `common/main${ext}`
fxy060608's avatar
fxy060608 已提交
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 53 54 55 56

    if (!compilation.assets[targetCssName]) {
      compilation.assets[targetCssName] = {
        size () {
          return Buffer.byteLength(getShadowCss(), 'utf8')
        },
        source () {
          return getShadowCss()
        }
      }
    } else {
      const source = compilation.assets[targetCssName].source() + getShadowCss()
      compilation.assets[targetCssName] = {
        size () {
          return Buffer.byteLength(source, 'utf8')
        },
        source () {
          return source
        }
      }
    }
  }

  if (compilation.assets[`common/main${ext}`]) { // 是否存在 main.css
    importMainCss = `@import './common/main${ext}';`
  }

  if (compilation.assets[`common/vendor${ext}`]) { // 是否存在 vendor.css
    importVendorCss += `@import './common/vendor${ext}';`
  }

  const runtimeJsPath = 'common/runtime.js'
fxy060608's avatar
fxy060608 已提交
57 58

  const asset = compilation.assets[runtimeJsPath]
fxy060608's avatar
fxy060608 已提交
59 60 61
  if ( // app 和 baidu 不需要
    process.env.UNI_PLATFORM !== 'app-plus' &&
    process.env.UNI_PLATFORM !== 'mp-baidu' &&
fxy060608's avatar
fxy060608 已提交
62 63 64
    asset &&
    !asset.source.__$wrappered
  ) {
fxy060608's avatar
fxy060608 已提交
65 66
    const source =
      `
fxy060608's avatar
fxy060608 已提交
67
  !function(){try{var a=Function("return this")();a&&!a.Math&&(Object.assign(a,{isFinite:isFinite,Array:Array,Date:Date,Error:Error,Function:Function,Math:Math,Object:Object,RegExp:RegExp,String:String,TypeError:TypeError,setTimeout:setTimeout,clearTimeout:clearTimeout,setInterval:setInterval,clearInterval:clearInterval}),"undefined"!=typeof Reflect&&(a.Reflect=Reflect))}catch(a){}}();
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73 74
  ${asset.source()}
  `
    const newSource = function () {
      return source
    }
    newSource.__$wrappered = true
    compilation.assets[runtimeJsPath].source = newSource
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  }

  const specialMethods = getSpecialMethods()

  let beforeCode = ''
  if (Object.keys(specialMethods).length) {
    beforeCode = `${getPlatformGlobal()}.specialMethods = ${JSON.stringify(specialMethods)}`
  }

  return [{
    file: 'app.js',
    source: `${beforeCode}
require('./common/runtime.js')
require('./common/vendor.js')
require('./common/main.js')`
  }, {
    file: 'app' + ext,
    source: `${importMainCss}
${importVendorCss}`
  }]
}