platform.js 4.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
const fs = require('fs')
const path = require('path')

const {
fxy060608's avatar
fxy060608 已提交
5 6 7
  isInHBuilderX,
  isInHBuilderXAlpha,
  normalizeNodeModules
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
} = require('./util')

const {
  SCSS,
  SASS
} = require('./scss')

function getShadowCss () {
  let tagName = 'page'
  if (process.env.UNI_PLATFORM === 'h5') {
    tagName = 'body'
  }
  return `${tagName}::after{position:fixed;content:'';left:-1000px;top:-1000px;-webkit-animation:shadow-preload .1s;-webkit-animation-delay:3s;animation:shadow-preload .1s;animation-delay:3s}@-webkit-keyframes shadow-preload{0%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}100%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}}@keyframes shadow-preload{0%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}100%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}}`
}

// 解决 vue-cli-service lint 时 UNI_PLATFORM 不存在
process.env.UNI_PLATFORM = process.env.UNI_PLATFORM || 'h5'

fxy060608's avatar
fxy060608 已提交
26
const uniPluginOptions = global.uniPlugin.options || {}
fxy060608's avatar
fxy060608 已提交
27

fxy060608's avatar
fxy060608 已提交
28 29 30 31 32
const {
  vueContext: preprocessContext,
  nvueContext: nvuePreprocessContext
} = global.uniPlugin.preprocess
// TODO 暂时保留原有导出,减少影响,后续再整理一下
33
module.exports = {
fxy060608's avatar
fxy060608 已提交
34
  normalizeNodeModules,
fxy060608's avatar
fxy060608 已提交
35 36
  isInHBuilderX,
  isInHBuilderXAlpha,
fxy060608's avatar
fxy060608 已提交
37
  runByHBuilderX: isInHBuilderX || fs.existsSync(path.resolve(process.env.UNI_HBUILDERX_PLUGINS || '', 'weapp-tools')),
fxy060608's avatar
fxy060608 已提交
38 39
  getFlexDirection (json) {
    let flexDir = 'column'
fxy060608's avatar
fxy060608 已提交
40 41
    if (json && json.nvue && json.nvue['flex-direction']) {
      flexDir = json.nvue['flex-direction']
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      const flexDirs = ['row', 'row-reverse', 'column', 'column-reverse']
      if (flexDirs.indexOf(flexDir) === -1) {
        flexDir = 'column'
      }
    }
    return flexDir
  },
  jsPreprocessOptions: {
    type: 'js',
    context: preprocessContext
  },
  cssPreprocessOptions: {
    type: 'css',
    context: preprocessContext
  },
  htmlPreprocessOptions: {
    type: 'html',
    context: preprocessContext
  },
  nvueCssPreprocessOptions: {
    type: 'css',
    context: nvuePreprocessContext
  },
  nvueJsPreprocessOptions: {
    type: 'js',
    context: nvuePreprocessContext
  },
  nvueHtmlPreprocessOptions: {
    type: 'html',
    context: nvuePreprocessContext
  },
  isSupportSubPackages () {
fxy060608's avatar
fxy060608 已提交
74
    return !!uniPluginOptions.subPackages
fxy060608's avatar
fxy060608 已提交
75 76
  },
  getPlatforms () {
fxy060608's avatar
fxy060608 已提交
77
    return global.uniPlugin.platforms
fxy060608's avatar
fxy060608 已提交
78
  },
fxy060608's avatar
fxy060608 已提交
79 80
  getPlatformGlobal () { // 目前仅mp-alipay有用
    return uniPluginOptions.global
fxy060608's avatar
fxy060608 已提交
81
  },
fxy060608's avatar
fxy060608 已提交
82 83
  getPlatformExts () { // 小程序扩展名
    return uniPluginOptions.extnames
fxy060608's avatar
fxy060608 已提交
84
  },
fxy060608's avatar
fxy060608 已提交
85 86
  getPlatformProject () { // 开发工具项目配置名
    return uniPluginOptions.project
fxy060608's avatar
fxy060608 已提交
87
  },
88
  getPlatformFilterTag () {
fxy060608's avatar
fxy060608 已提交
89
    return uniPluginOptions.filterTag
90
  },
fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96
  getMPRuntimePath () {
    if (process.env.UNI_USING_VUE3) {
      return require.resolve('@dcloudio/uni-' + process.env.UNI_PLATFORM + '/dist/uni.mp.esm.js')
    }
    return require.resolve('@dcloudio/uni-' + process.env.UNI_PLATFORM)
  },
fxy060608's avatar
fxy060608 已提交
97
  getPlatformVue (vueOptions) {
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104
    if (uniPluginOptions.vue) {
      return uniPluginOptions.vue
    }
    if (process.env.UNI_USING_VUE3) {
      return '@dcloudio/uni-mp-vue'
    }
    return '@dcloudio/vue-cli-plugin-uni/packages/mp-vue'
fxy060608's avatar
fxy060608 已提交
105 106
  },
  getPlatformCssVars () {
107
    return uniPluginOptions.cssVars || {}
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114
  },
  getPlatformCssnano () {
    return {
      calc: false,
      orderedValues: false,
      mergeLonghand: false,
      mergeRules: false,
fxy060608's avatar
fxy060608 已提交
115
      cssDeclarationSorter: false,
fxy060608's avatar
fxy060608 已提交
116
      uniqueSelectors: false, // 标签排序影响头条小程序
117
      minifySelectors: false, // 标签排序影响头条小程序
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122 123 124
      discardComments: false,
      discardDuplicates: false // 条件编译会导致重复
    }
  },
  getShadowCss,
  getShadowTemplate (colorType = 'grey') {
    let tagName = 'cover-image'
P
panyiming.325 已提交
125
    if (process.env.UNI_PLATFORM === 'mp-toutiao' || process.env.UNI_PLATFORM === 'mp-lark') {
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131 132 133 134
      tagName = 'image'
    }
    return `<${tagName} src="https://cdn.dcloud.net.cn/img/shadow-${colorType}.png" style="z-index:998;position:fixed;left:0;top:0;width:100%;height:3px;"/>`
  },
  getPlatformScss () {
    return SCSS
  },
  getPlatformSass () {
    return SASS
fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
  },
  getBabelParserOptions () {
    return {
      sourceType: 'module',
      plugins: [
        'optionalChaining',
        'typescript',
        ['decorators', {
          decoratorsBeforeExport: true
        }],
        'classProperties'
      ]
    }
fxy060608's avatar
fxy060608 已提交
148
  }
A
Aero 已提交
149
}