index-new.js 4.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
const fs = require('fs')
const path = require('path')

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

fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
const {
  parsePages,
  normalizePath,
  parsePagesJson,
  parseManifestJson
} = require('@dcloudio/uni-cli-shared')

const {
  updateAppJson,
  updatePageJson,
  updateProjectJson
} = require('@dcloudio/uni-cli-shared/lib/cache')

fxy060608's avatar
fxy060608 已提交
19
const {
fxy060608's avatar
fxy060608 已提交
20
  // pagesJsonJsFileName,
21
  initAutoImportComponents
fxy060608's avatar
fxy060608 已提交
22 23
} = require('@dcloudio/uni-cli-shared/lib/pages')

fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
const parseStyle = require('./util').parseStyle

// 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并
function renameUsingComponents (jsonObj) {
  if (jsonObj.usingComponents) {
    jsonObj.customUsingComponents = jsonObj.usingComponents
    delete jsonObj.usingComponents
  }
  return jsonObj
}

module.exports = function (content) {
  this.cacheable && this.cacheable()

fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43
  let isAppView = false
  if (this.resourceQuery) {
    const params = loaderUtils.parseQuery(this.resourceQuery)
    isAppView = params.type === 'view'
  }

fxy060608's avatar
fxy060608 已提交
44
  // const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName)
fxy060608's avatar
fxy060608 已提交
45 46 47
  const manifestJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json')
  const manifestJson = parseManifestJson(fs.readFileSync(manifestJsonPath, 'utf8'))

fxy060608's avatar
fxy060608 已提交
48
  // this.addDependency(pagesJsonJsPath)
fxy060608's avatar
fxy060608 已提交
49 50
  this.addDependency(manifestJsonPath)

fxy060608's avatar
fxy060608 已提交
51
  const pagesJson = parsePagesJson(content, {
fxy060608's avatar
init v3  
fxy060608 已提交
52
    addDependency: (file) => {
fxy060608's avatar
fxy060608 已提交
53 54 55 56
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file))
      this.addDependency(file)
    }
  })
57 58

  // 组件自动导入配置
59
  process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJson.easycom && pagesJson.easycom.autoscan === false)
60
  initAutoImportComponents(pagesJson.easycom)
61

fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67 68 69
  // TODO 与 usingComponents 放在一块读取设置
  if (manifestJson.transformPx === false) {
    process.UNI_TRANSFORM_PX = false
  } else {
    process.UNI_TRANSFORM_PX = true
  }

  if (process.env.UNI_PLATFORM === 'h5') {
fxy060608's avatar
fxy060608 已提交
70
    return require('./platforms/h5')(pagesJson, manifestJson, this)
fxy060608's avatar
fxy060608 已提交
71
  }
fxy060608's avatar
fxy060608 已提交
72 73
  if (process.env.UNI_PLATFORM === 'quickapp-native') {
    return require('./platforms/quickapp-native')(pagesJson, manifestJson, this)
fxy060608's avatar
fxy060608 已提交
74
  }
fxy060608's avatar
fxy060608 已提交
75

fxy060608's avatar
init v3  
fxy060608 已提交
76 77 78 79 80 81 82 83 84
  if (!process.env.UNI_USING_V3) {
    parsePages(pagesJson, function (page) {
      updatePageJson(page.path, renameUsingComponents(parseStyle(page.style)))
    }, function (root, page) {
      updatePageJson(normalizePath(path.join(root, page.path)), renameUsingComponents(
        parseStyle(page.style, root)
      ))
    })
  }
fxy060608's avatar
fxy060608 已提交
85 86 87

  const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(pagesJson, manifestJson)

fxy060608's avatar
fxy060608 已提交
88
  if (jsonFiles && jsonFiles.length) {
fxy060608's avatar
fxy060608 已提交
89
    if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
90 91 92
      let appConfigContent = ''
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
Q
qiang 已提交
93 94 95 96 97 98
          if (!isAppView && jsonFile.name === 'manifest.json') {
            const content = JSON.parse(jsonFile.content)
            if (!content.launch_path && content.plus['uni-app'].nvueLaunchMode === 'fast') {
              console.log('Nvue 首页启动模式: fast 详见: https://ask.dcloud.net.cn/article/36749')
            }
          }
fxy060608's avatar
fxy060608 已提交
99 100
          if (jsonFile.name === 'define-pages.js') {
            appConfigContent = jsonFile.content
101 102
          } else {
            // app-view 不需要生成 app-config-service.js,manifest.json
fxy060608's avatar
fxy060608 已提交
103
            !isAppView && this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108
          }
        }
      })
      return appConfigContent
    }
fxy060608's avatar
fxy060608 已提交
109
    if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
110 111 112
      let appConfigContent = ''
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
113
          if (jsonFile.name === 'app-config.js' || jsonFile.name === 'define-pages.js') {
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118 119 120
            appConfigContent = jsonFile.content
          } else {
            this.emitFile(jsonFile.name, jsonFile.content)
          }
        }
      })
      return appConfigContent
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }

    jsonFiles.forEach(jsonFile => {
      if (jsonFile) {
        if (jsonFile.name === 'app') {
          updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content))
        } else {
          updateProjectJson(jsonFile.name, jsonFile.content)
        }
      }
    })
  }

  return ''
135
}