xwt
20 小时以前 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
62
63
const {
  initAutoImportScanComponents
} = require('@dcloudio/uni-cli-shared/lib/pages')
const {
  initUniModules
} = require('@dcloudio/uni-cli-shared/lib/uni_modules')
const uniI18n = require('@dcloudio/uni-cli-i18n')
 
let compiling = false
 
class WebpackUniAppPlugin {
  apply (compiler) {
    if (process.UNI_CONFUSION) {
      compiler.hooks.emit.tapPromise('webpack-uni-app-emit', compilation => {
        return new Promise((resolve, reject) => {
          if (compilation.assets['app-confusion.js']) { // 存在加密
            const manifestJson = JSON.parse(`${compilation.assets['manifest.json'].source()}`)
            manifestJson.plus.confusion.resources['app-confusion.js'] = {}
            const source = JSON.stringify(manifestJson)
            compilation.assets['manifest.json'] = {
              size () {
                return Buffer.byteLength(source, 'utf8')
              },
              source () {
                return source
              }
            }
          }
          resolve()
        })
      })
    }
 
    compiler.hooks.invalid.tap('webpack-uni-app-invalid', (fileName, changeTime) => {
      if (!process.env.UNI_USING_V3) {
        if (!compiling) {
          compiling = true
          console.log(uniI18n.__('performingHotReload'))
        }
      }
      if (fileName && typeof fileName === 'string') {
        if (fileName.indexOf('.vue') !== -1 || fileName.indexOf('.nvue') !== -1) {
          if (process.UNI_AUTO_SCAN_COMPONENTS) {
            // TODO 需要处理copy webpack
            initUniModules()
            initAutoImportScanComponents()
          }
        }
      }
    })
    // v3 版本在webpack-app-plus-plugin/index.js中处理,目前太乱了。后续要整理
    if (!process.env.UNI_USING_V3) {
      compiler.hooks.done.tapPromise('webpack-uni-app-done', compilation => {
        return new Promise((resolve, reject) => {
          compiling = false
          resolve()
        })
      })
    }
  }
}
 
module.exports = WebpackUniAppPlugin