cnf
2025-05-10 386fa0eca75ddc88165f9b73038f2a2239e1072e
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require('fs')
const path = require('path')
const hash = require('hash-sum')
const loaderUtils = require('loader-utils')
 
const {
  parse
} = require(require.resolve('@vue/component-compiler-utils', {
  paths: [require.resolve('vue-loader')]
})) // 确保使用的与 vue-loader 一致
 
const {
  getGlobalUsingComponentsCode
} = require('@dcloudio/uni-cli-shared/lib/pages')
 
const {
  jsPreprocessOptions
} = require('@dcloudio/uni-cli-shared/lib/platform')
 
const preprocessor = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader/preprocess')
 
const traverse = require('@dcloudio/webpack-uni-mp-loader/lib/babel/global-component-traverse')
 
const genStylesCode = require('../../vue-loader/lib/codegen/styleInjection')
 
const {
  parseComponents
} = require('./util')
 
function getDefineComponents ({
  components
}) {
  return components.map(({
    name,
    source
  }) => `Vue.component('${name}',require('${source}').default)`)
}
 
const appVueFilePath = path.resolve(process.env.UNI_INPUT_DIR, 'App.vue')
 
function getStylesCode (loaderContext) {
  if (!fs.existsSync(appVueFilePath)) {
    return
  }
  const source = fs.readFileSync(appVueFilePath, 'utf8')
  const {
    minimize,
    sourceMap,
    rootContext,
    resourcePath,
    resourceQuery
  } = loaderContext
 
  const options = loaderUtils.getOptions(loaderContext) || {}
 
  const filename = path.basename(resourcePath)
  const context = rootContext || process.cwd()
  const sourceRoot = path.dirname(path.relative(context, resourcePath))
  const descriptor = parse({
    source,
    compiler: options.compiler,
    filename,
    sourceRoot,
    needMap: sourceMap
  })
 
  // styles
  let stylesCode = ''
  if (descriptor.styles.length) {
    const isProduction = options.productionMode || minimize || process.env.NODE_ENV === 'production'
 
    const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
 
    // module id for scoped CSS & hot-reload
    const rawShortFilePath = path
      .relative(context, resourcePath)
      .replace(/^(\.\.[/\\])+/, '')
 
    const shortFilePath = rawShortFilePath.replace(/\\/g, '/') + resourceQuery
 
    const needsHotReload = false
 
    const id = hash(
      isProduction
        ? (shortFilePath + '\n' + source)
        : shortFilePath
    )
    stylesCode = genStylesCode(
      loaderContext,
      descriptor.styles,
      id,
      resourcePath,
      stringifyRequest,
      needsHotReload,
      true, // needs explicit injection?
      'app-vue'
    )
  }
  return stylesCode.replace(/main\.[jt]s/g, 'App.vue')
}
 
module.exports = function (source, map) {
  // 需要执行一遍条件编译 source
  if (source.indexOf('#ifdef') !== -1) {
    source = preprocessor.preprocess(source, jsPreprocessOptions.context, {
      type: jsPreprocessOptions.type
    })
  }
  // 追加小程序全局自定义组件(仅v3)
  source = getGlobalUsingComponentsCode() + source
  const automatorCode = process.env.UNI_AUTOMATOR_WS_ENDPOINT
    ? 'import \'@dcloudio/uni-app-plus/dist/automator.view\''
    : ''
  this.callback(null,
    `
// @ts-nocheck
import 'uni-pages?${JSON.stringify({ type: 'view' })}'
${automatorCode}
function initView(){
    ${getStylesCode(this)}
    typeof injectStyles ==='function' && injectStyles()
    ${getDefineComponents(parseComponents(source, traverse)).join('\n')}
    UniViewJSBridge.publishHandler('webviewReady')
}
if(typeof plus !== 'undefined'){
  initView()
} else {
  document.addEventListener('plusready',initView)
}
`,
    map)
}