index.js 5.4 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
const path = require('path')

const parser = require('@babel/parser')

const {
  parseComponent,
  compile,
  compileToFunctions,
  ssrCompile,
  ssrCompileToFunctions
} = require('vue-template-compiler')

const platforms = require('./platforms')
const traverseScript = require('./script/traverse')
const generateScript = require('./script/generate')
const traverseTemplate = require('./template/traverse')
const generateTemplate = require('./template/generate')

const compilerModule = require('./module')

const generateCodeFrame = require('./codeframe')

module.exports = {
  compile (source, options = {}) {
    if (!options.mp) { // h5
      return compile(source, options)
    }

    (options.modules || (options.modules = [])).push(compilerModule)

    const res = compile(source, Object.assign(options, {
      optimize: false
    }))

    options.mp.platform = platforms[options.mp.platform]

    options.mp.scopeId = options.scopeId

    options.mp.resourcePath = options.resourcePath

    options.mp.globalUsingComponents = options.globalUsingComponents || Object.create(null)
42 43

    options.mp.filterModules = Object.keys(options.filterModules || {})
fxy060608's avatar
fxy060608 已提交
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    // (可用的原生微信小程序组件,global+scoped)
    options.mp.wxComponents = options.wxComponents || Object.create(null)

    const state = {
      ast: {},
      script: '',
      template: '',
      errors: new Set(),
      tips: new Set(),
      options: options.mp
    }
    // console.log(`function render(){${res.render}}`)
    const ast = parser.parse(`function render(){${res.render}}`)

    res.render = generateScript(traverseScript(ast, state), state)

    let template = generateTemplate(traverseTemplate(ast, state), state)

    res.specialMethods = state.options.specialMethods || new Set()
    delete state.options.specialMethods

    res.files = state.files || {}
    delete state.files

    // resolve scoped slots
    res.generic = state.generic || []
    delete state.generic

    // define scoped slots
    res.componentGenerics = state.componentGenerics || {}
    delete state.componentGenerics

    state.errors.forEach(msg => {
      res.errors.push({
        msg
      })
    })

    const resourcePath = options.resourcePath.replace(path.extname(options.resourcePath), '')

    state.tips.forEach(msg => {
      console.log(`提示:${msg}
at ${resourcePath}.vue:1`)
    })

    /**
91 92 93 94 95 96
     * TODO
     * 方案0.最佳方案是在 loader 中直接 emitFile,但目前 vue template-loader 不好介入,自定义的 compiler 结果又无法顺利返回给 loader
     * 方案1.通过 loader 传递 emitFile 来提交生成 wxml,需要一个 template loader 来给自定义 compier 增加 emitFile
     * 方案2.缓存 wxml 内容,由 plugin 生成 assets 来提交生成 wxml
     * ...暂时使用方案1
     */
fxy060608's avatar
fxy060608 已提交
97 98 99 100
    if (options.emitFile) {
      if (options.updateSpecialMethods) {
        options.updateSpecialMethods(resourcePath, [...res.specialMethods])
      }
101 102
      const filterTemplate = []
      options.mp.filterModules.forEach(name => {
fxy060608's avatar
fxy060608 已提交
103 104 105 106 107 108
        filterTemplate.push(
          options.mp.platform.createFilterTag(
            options.filterTagName,
            options.filterModules[name]
          )
        )
109
      })
fxy060608's avatar
fxy060608 已提交
110

fxy060608's avatar
fxy060608 已提交
111 112 113 114
      if (filterTemplate.length) {
        template = filterTemplate.join('\n') + '\n' + template
      }

fxy060608's avatar
fxy060608 已提交
115 116
      if (
        process.UNI_ENTRY[resourcePath] &&
117 118
        process.env.UNI_PLATFORM !== 'app-plus' &&
        process.env.UNI_PLATFORM !== 'h5'
fxy060608's avatar
fxy060608 已提交
119 120 121 122 123 124 125 126 127
      ) {
        // 检查是否启用 shadow
        let colorType = false
        const pageJsonStr = options.getJsonFile(resourcePath)
        if (pageJsonStr) {
          try {
            const windowJson = JSON.parse(pageJsonStr)
            if (process.env.UNI_PLATFORM === 'mp-alipay') {
              colorType = windowJson.allowsBounceVertical === 'NO' &&
128 129
                windowJson.navigationBarShadow &&
                windowJson.navigationBarShadow.colorType
fxy060608's avatar
fxy060608 已提交
130 131
            } else {
              colorType = windowJson.disableScroll &&
132 133
                windowJson.navigationBarShadow &&
                windowJson.navigationBarShadow.colorType
fxy060608's avatar
fxy060608 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            }
          } catch (e) {}
        }
        if (colorType) {
          template = options.getShadowTemplate(colorType) + template
        }
      }

      options.emitFile(options.resourcePath, template)
      if (res.files) {
        Object.keys(res.files).forEach(name => {
          options.emitFile(name, res.files[name])
        })
      }

      if (state.options.usingGlobalComponents) {
        options.updateUsingGlobalComponents(
          resourcePath,
          state.options.usingGlobalComponents
        )
      }

      if (
        res.generic &&
158 159
        res.generic.length &&
        options.updateGenericComponents
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167
      ) {
        options.updateGenericComponents(
          resourcePath,
          res.generic
        )
      }
      if (
        res.componentGenerics &&
168 169
        Object.keys(res.componentGenerics).length &&
        options.updateComponentGenerics
fxy060608's avatar
fxy060608 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      ) {
        options.updateComponentGenerics(
          resourcePath,
          res.componentGenerics
        )
      }
    } else {
      res.template = template
    }
    return res
  },
  parseComponent,
  compileToFunctions,
  ssrCompile,
  ssrCompileToFunctions,
  generateCodeFrame
}