platform.js 4.1 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 40 41 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
  getFlexDirection (json) {
    let flexDir = 'column'
    if (json && json['nvue'] && json['nvue']['flex-direction']) {
      flexDir = json['nvue']['flex-direction']
      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
  getPlatformVue (vueOptions) {
fxy060608's avatar
fxy060608 已提交
92
    return uniPluginOptions.vue || '@dcloudio/vue-cli-plugin-uni/packages/mp-vue'
fxy060608's avatar
fxy060608 已提交
93 94
  },
  getPlatformCssVars () {
fxy060608's avatar
fxy060608 已提交
95
    return uniPluginOptions.cssVars
fxy060608's avatar
fxy060608 已提交
96 97 98 99 100 101 102
  },
  getPlatformCssnano () {
    return {
      calc: false,
      orderedValues: false,
      mergeLonghand: false,
      mergeRules: false,
fxy060608's avatar
fxy060608 已提交
103
      cssDeclarationSorter: false,
fxy060608's avatar
fxy060608 已提交
104
      uniqueSelectors: false, // 标签排序影响头条小程序
105
      minifySelectors: false, // 标签排序影响头条小程序
fxy060608's avatar
fxy060608 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      discardComments: false,
      discardDuplicates: false // 条件编译会导致重复
    }
  },
  getShadowCss,
  getShadowTemplate (colorType = 'grey') {
    let tagName = 'cover-image'
    if (process.env.UNI_PLATFORM === 'mp-toutiao') {
      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 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
  },
  getBabelParserOptions () {
    return {
      sourceType: 'module',
      plugins: [
        'optionalChaining',
        'typescript',
        ['decorators', {
          decoratorsBeforeExport: true
        }],
        'classProperties'
      ]
    }
fxy060608's avatar
fxy060608 已提交
136
  }
fxy060608's avatar
fxy060608 已提交
137
}