index-new.js 4.1 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 {
20 21 22
  pagesJsonJsFileName,
  refreshAutoComponentMap,
  parseUsingAutoImportComponents
fxy060608's avatar
fxy060608 已提交
23 24
} = require('@dcloudio/uni-cli-shared/lib/pages')

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

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

36 37 38 39 40 41 42 43 44 45 46
let lastUsingAutoImportComponentsJson = ''

function initAutoImportComponents (usingAutoImportComponents = {}) {
  const newUsingAutoImportComponentsJson = JSON.stringify(usingAutoImportComponents)
  if (newUsingAutoImportComponentsJson !== lastUsingAutoImportComponentsJson) {
    lastUsingAutoImportComponentsJson = newUsingAutoImportComponentsJson
    process.UNI_AUTO_COMPONENTS = parseUsingAutoImportComponents(usingAutoImportComponents)
    refreshAutoComponentMap()
  }
}

fxy060608's avatar
fxy060608 已提交
47 48 49
module.exports = function (content) {
  this.cacheable && this.cacheable()

fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55
  let isAppView = false
  if (this.resourceQuery) {
    const params = loaderUtils.parseQuery(this.resourceQuery)
    isAppView = params.type === 'view'
  }

fxy060608's avatar
fxy060608 已提交
56
  const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName)
fxy060608's avatar
fxy060608 已提交
57 58 59
  const manifestJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json')
  const manifestJson = parseManifestJson(fs.readFileSync(manifestJsonPath, 'utf8'))

fxy060608's avatar
fxy060608 已提交
60
  this.addDependency(pagesJsonJsPath)
fxy060608's avatar
fxy060608 已提交
61 62
  this.addDependency(manifestJsonPath)

fxy060608's avatar
fxy060608 已提交
63
  const pagesJson = parsePagesJson(content, {
fxy060608's avatar
init v3  
fxy060608 已提交
64
    addDependency: (file) => {
fxy060608's avatar
fxy060608 已提交
65 66 67 68
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file))
      this.addDependency(file)
    }
  })
69 70 71 72

  // 组件自动导入配置
  initAutoImportComponents(pagesJson.usingAutoImportComponents)

fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78 79 80 81 82 83
  // TODO 与 usingComponents 放在一块读取设置
  if (manifestJson.transformPx === false) {
    process.UNI_TRANSFORM_PX = false
  } else {
    process.UNI_TRANSFORM_PX = true
  }

  if (process.env.UNI_PLATFORM === 'h5') {
    return require('./platforms/h5')(pagesJson, manifestJson)
  }

fxy060608's avatar
init v3  
fxy060608 已提交
84 85 86 87 88 89 90 91 92
  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 已提交
93 94 95

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

fxy060608's avatar
fxy060608 已提交
96
  if (jsonFiles && jsonFiles.length) {
fxy060608's avatar
fxy060608 已提交
97
    if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102
      let appConfigContent = ''
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
          if (jsonFile.name === 'define-pages.js') {
            appConfigContent = jsonFile.content
103 104
          } else {
            // app-view 不需要生成 app-config-service.js,manifest.json
fxy060608's avatar
fxy060608 已提交
105
            !isAppView && this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
          }
        }
      })
      return appConfigContent
    }
    if (process.env.UNI_USING_NATIVE) {
      let appConfigContent = ''
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
          if (jsonFile.name === 'app-config.js') {
            appConfigContent = jsonFile.content
          } else {
            this.emitFile(jsonFile.name, jsonFile.content)
          }
        }
      })
      return appConfigContent
fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }

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

  return ''
}