template.js 2.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const path = require('path')

fxy060608's avatar
fxy060608 已提交
3
const loaderUtils = require('loader-utils')
fxy060608's avatar
fxy060608 已提交
4 5 6

const {
  removeExt,
fxy060608's avatar
fxy060608 已提交
7
  normalizePath,
fxy060608's avatar
fxy060608 已提交
8 9
  getPlatformExts,
  getShadowTemplate
fxy060608's avatar
fxy060608 已提交
10 11 12
} = require('@dcloudio/uni-cli-shared')

const {
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19 20 21
  getJsonFile,
  getWXComponents,
  updateSpecialMethods,
  getGlobalUsingComponents,
  updateGenericComponents, // resolve
  updateComponentGenerics, // define
  updateUsingGlobalComponents
} = require('@dcloudio/uni-cli-shared/lib/cache')

fxy060608's avatar
fxy060608 已提交
22 23 24 25
const {
  isBuiltInComponentPath
} = require('@dcloudio/uni-cli-shared/lib/pages')

fxy060608's avatar
fxy060608 已提交
26 27 28 29 30 31
const {
  getPlatformFilterTag
} = require('@dcloudio/uni-cli-shared/lib/platform')

const {
  normalizeNodeModules
fxy060608's avatar
fxy060608 已提交
32 33 34
} = require('./shared')

const templateExt = getPlatformExts().template
fxy060608's avatar
fxy060608 已提交
35
const filterTagName = getPlatformFilterTag() || ''
fxy060608's avatar
fxy060608 已提交
36

fxy060608's avatar
fxy060608 已提交
37 38
function parseFilterModules (filterModules) {
  if (filterModules) {
fxy060608's avatar
fxy060608 已提交
39
    return JSON.parse(Buffer.from(filterModules, 'base64').toString('utf8'))
fxy060608's avatar
fxy060608 已提交
40
  }
fxy060608's avatar
fxy060608 已提交
41
  return {}
fxy060608's avatar
fxy060608 已提交
42
}
fxy060608's avatar
fxy060608 已提交
43

fxy060608's avatar
fxy060608 已提交
44
module.exports = function (content, map) {
fxy060608's avatar
fxy060608 已提交
45 46
  this.cacheable && this.cacheable()

fxy060608's avatar
fxy060608 已提交
47 48 49 50
  const vueLoaderOptions = this.loaders.find(loader => loader.ident === 'vue-loader-options')
  if (vueLoaderOptions) {
    const globalUsingComponents = getGlobalUsingComponents()
    const realResourcePath = path.relative(process.env.UNI_INPUT_DIR, this.resourcePath)
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55
    let resourcePath = normalizeNodeModules(removeExt(realResourcePath) + templateExt)

    if ( // windows 上 page-meta, navigation-bar 可能在不同盘上
      /^win/.test(process.platform) &&
      path.isAbsolute(resourcePath) &&
fxy060608's avatar
fxy060608 已提交
56
      isBuiltInComponentPath(resourcePath)
fxy060608's avatar
fxy060608 已提交
57 58 59 60
    ) {
      resourcePath = normalizePath(path.relative(process.env.UNI_CLI_CONTEXT, resourcePath))
    }

fxy060608's avatar
fxy060608 已提交
61
    const wxComponents = getWXComponents(resourcePath.replace(path.extname(resourcePath), ''))
fxy060608's avatar
fxy060608 已提交
62

fxy060608's avatar
fxy060608 已提交
63 64 65 66 67
    const params = loaderUtils.parseQuery(this.resourceQuery)
    /* eslint-disable no-mixed-operators */
    const filterModules = parseFilterModules(params && params['filter-modules'])
    Object.assign(vueLoaderOptions.options.compilerOptions, {
      mp: {
68
        platform: process.env.UNI_PLATFORM,
69 70
        scopedSlotsCompiler: process.env.SCOPED_SLOTS_COMPILER,
        mergeVirtualHostAttributes: process.env.MERGE_VIRTUAL_HOST_ATTRIBUTES === 'true'
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84
      },
      filterModules,
      filterTagName,
      resourcePath,
      emitFile: this.emitFile,
      wxComponents,
      getJsonFile,
      getShadowTemplate,
      updateSpecialMethods,
      globalUsingComponents,
      updateGenericComponents,
      updateComponentGenerics,
      updateUsingGlobalComponents
    })
fxy060608's avatar
fxy060608 已提交
85
  } else {
fxy060608's avatar
fxy060608 已提交
86
    throw new Error('vue-loader-options parse error')
fxy060608's avatar
fxy060608 已提交
87
  }
fxy060608's avatar
fxy060608 已提交
88
  this.callback(null, content, map)
89
}