'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
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
74
75
76
77
78
const fs = require('fs')
const path = require('path')
 
const loaderUtils = require('loader-utils')
 
const {
  parseComponent
} = require('weex-template-compiler')
 
const {
  attrsToQuery
} = require('vue-loader/lib/codegen/utils')
 
const {
  normalizePath,
  getPlatformStat
} = require('@dcloudio/uni-cli-shared')
 
const appVuePath = path.resolve(process.env.UNI_INPUT_DIR, 'App.vue')
 
function genStyleRequest (style, i, stringifyRequest) {
  const src = style.src || normalizePath(appVuePath)
  const attrsQuery = attrsToQuery(style.attrs, 'css')
  const query = `?vue&type=style&index=${i}${attrsQuery}`
  return stringifyRequest(src + query)
}
 
function getAppStyleCode (stringifyRequest) {
  if (!process.env.UNI_USING_NVUE_COMPILER) {
    return ''
  }
  let code = 'Vue.prototype.__$appStyle__ = {}\n'
  let styles = []
  try {
    if (fs.existsSync(appVuePath)) {
      styles = parseComponent(fs.readFileSync(appVuePath, 'utf8')).styles
    }
  } catch (e) {}
  styles.forEach((style, index) => {
    code = code +
      `Vue.prototype.__merge_style && Vue.prototype.__merge_style(require(${genStyleRequest(style, index, stringifyRequest)}).default,Vue.prototype.__$appStyle__)\n`
  })
  return code
}
 
module.exports = function (content, map) {
  this.cacheable && this.cacheable()
 
  const loaderContext = this
 
  const statCode = getPlatformStat()
 
  if (this.resourceQuery) {
    const params = loaderUtils.parseQuery(this.resourceQuery)
    if (params) {
      if (params.page) {
        params.page = decodeURIComponent(params.page)
        // import Vue from 'vue'是为了触发 vendor 合并
        return `
        ${statCode}
        import 'uni-app-style'
        import 'uni-polyfill'
        import App from './${normalizePath(params.page)}.nvue?mpType=page'
        App.mpType = 'page'
        App.route = '${params.page}'
        App.el = '#root'
        new Vue(App)
        `
      } else if (params.type === 'appStyle') {
        const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
        return `${getAppStyleCode(stringifyRequest)}`
      }
    }
  }
  const automatorCode = process.env.UNI_AUTOMATOR_WS_ENDPOINT ? 'import \'@dcloudio/uni-app-plus/dist/automator\';'
    : ''
  return automatorCode + statCode + content
}