template.js 1.9 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
const path = require('path')

const qs = require('querystring')

const {
  md5,
  removeExt,
  getPlatformExts
} = require('@dcloudio/uni-cli-shared')

const {
  cacheTemplate,
  cacheCompilerOptions,
  getPlatformTarget
} = require('./shared')

const templateExt = getPlatformExts().template
module.exports = function (content) {
  if (process.env.UNI_USING_COMPONENTS) {
    return require('./template-new').call(this, content)
  }
  this.cacheable && this.cacheable()

  const realResourcePath = path.relative(process.env.UNI_INPUT_DIR, this.resourcePath)

  if (process.env.UNI_USING_COMPONENTS) {
    // 向 uni-template-compier 传递 emitFile
fxy060608's avatar
fxy060608 已提交
28 29
    const vueLoaderOptions = this.loaders.find(loader => loader.ident === 'vue-loader-options')
    if (vueLoaderOptions) {
fxy060608's avatar
fxy060608 已提交
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 57 58 59
      Object.assign(vueLoaderOptions.options.compilerOptions, {
        resourcePath: removeExt(realResourcePath) + templateExt,
        emitFile: this.emitFile
      })
    } else {
      throw new Error('vue-loader-options parse error')
    }
  } else {
    if (!content.trim()) {
      content = '<view></view>'
    }

    cacheTemplate(realResourcePath, content)

    const query = qs.parse(this.resourceQuery.slice(1))

    const {
      id
    } = query

    const compilerOptions = {
      scopeId: query.scoped ? `data-v-${id}` : null,
      target: getPlatformTarget(),
      md5: md5(content.trim() + process.env.UNI_PLATFORM),
      realResourcePath
    }

    cacheCompilerOptions(realResourcePath, compilerOptions)

    // 向 vue-loader templateLoader 传递 compilerOptions
fxy060608's avatar
fxy060608 已提交
60 61
    const vueLoaderOptions = this.loaders.find(loader => loader.ident === 'vue-loader-options')
    if (vueLoaderOptions) {
fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67 68 69
      Object.assign(vueLoaderOptions.options.compilerOptions, compilerOptions)
    } else {
      throw new Error('vue-loader-options parse error')
    }
  }

  return content
}