xwt
15 小时以前 8a5fc169c691543f60109b2b3a4e000762f247c2
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
const path = require('path')
const utils = require('loader-utils')
 
const {
  normalizePath
} = require('@dcloudio/uni-cli-shared')
 
const preprocessor = require('./preprocess/lib/preprocess')
 
const ERRORS = {
  html: `条件编译失败,参考示例(注意 ifdef 与 endif 必须配对使用):
<!--  #ifdef  %PLATFORM% -->
模板代码
<!--  #endif -->
`,
  js: `条件编译失败,参考示例(注意 ifdef 与 endif 必须配对使用):
// #ifdef  %PLATFORM%
js代码
// #endif
`,
  css: `条件编译失败,参考示例(注意 ifdef 与 endif 必须配对使用):
/*  #ifdef  %PLATFORM%  */
css代码
/*  #endif  */
`
}
 
const TAGS = {
  html: 'template',
  js: 'script',
  css: 'style'
}
 
module.exports = function (content, map) {
  this.cacheable && this.cacheable()
 
  let types = utils.getOptions(this).type || 'js'
 
  const context = utils.getOptions(this).context || {}
 
  if (!Array.isArray(types)) {
    types = [types]
  }
  const resourcePath = this.resourcePath
  types.forEach(type => {
    try {
      content = preprocessor.preprocess(content, context, {
        type
      })
    } catch (e) {
      if (~['.nvue', '.vue'].indexOf(path.extname(resourcePath))) {
        console.error(`${TAGS[type]}节点 ${ERRORS[type]} at ` + normalizePath(path.relative(process.env.UNI_INPUT_DIR,
          resourcePath)) + ':1')
      } else {
        console.error(`${ERRORS[type]} at ` + normalizePath(path.relative(process.env.UNI_INPUT_DIR,
          resourcePath)) + ':1')
      }
    }
  })
  this.callback(null, content, map)
}