index-new.js 4.9 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
  initTheme,
fxy060608's avatar
fxy060608 已提交
21 22 23
  parseTheme
} = require('@dcloudio/uni-cli-shared/lib/theme')

fxy060608's avatar
fxy060608 已提交
24
const {
fxy060608's avatar
fxy060608 已提交
25
  // pagesJsonJsFileName,
26
  initAutoImportComponents
fxy060608's avatar
fxy060608 已提交
27 28
} = require('@dcloudio/uni-cli-shared/lib/pages')

d-u-a's avatar
d-u-a 已提交
29 30
const uniI18n = require('@dcloudio/uni-cli-i18n')

fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39 40 41
const parseStyle = require('./util').parseStyle

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

fxy060608's avatar
fxy060608 已提交
42
module.exports = function (content, map) {
fxy060608's avatar
fxy060608 已提交
43 44
  this.cacheable && this.cacheable()

fxy060608's avatar
fxy060608 已提交
45 46
  initTheme()

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

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

fxy060608's avatar
fxy060608 已提交
57
  // this.addDependency(pagesJsonJsPath)
fxy060608's avatar
fxy060608 已提交
58 59
  this.addDependency(manifestJsonPath)

fxy060608's avatar
fxy060608 已提交
60
  let pagesJson = parsePagesJson(content, {
fxy060608's avatar
init v3  
fxy060608 已提交
61
    addDependency: (file) => {
fxy060608's avatar
fxy060608 已提交
62 63 64 65
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file))
      this.addDependency(file)
    }
  })
66

67
  if (!pagesJson.pages || pagesJson.pages.length === 0) {
d-u-a's avatar
d-u-a 已提交
68
    console.error(uniI18n.__('pagesLoader.pagesNodeCannotNull'))
69 70 71
    process.exit(0)
  }

fxy060608's avatar
fxy060608 已提交
72 73 74 75 76
  if (global.uniPlugin.defaultTheme) {
    pagesJson = parseTheme(pagesJson)
    this.addDependency(path.resolve(process.env.UNI_INPUT_DIR, 'theme.json'))
  }

77
  // 组件自动导入配置
78
  process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJson.easycom && pagesJson.easycom.autoscan === false)
79
  initAutoImportComponents(pagesJson.easycom)
80

fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88
  // 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 已提交
89
    return this.callback(null, require('./platforms/h5')(pagesJson, manifestJson, this), map)
fxy060608's avatar
fxy060608 已提交
90
  }
fxy060608's avatar
fxy060608 已提交
91
  if (process.env.UNI_PLATFORM === 'quickapp-native') {
fxy060608's avatar
fxy060608 已提交
92
    return this.callback(null, require('./platforms/quickapp-native')(pagesJson, manifestJson, this), map)
fxy060608's avatar
fxy060608 已提交
93
  }
fxy060608's avatar
fxy060608 已提交
94

fxy060608's avatar
init v3  
fxy060608 已提交
95 96 97 98 99 100 101 102 103
  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 已提交
104

105
  const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(pagesJson, manifestJson, isAppView)
fxy060608's avatar
fxy060608 已提交
106

fxy060608's avatar
fxy060608 已提交
107
  if (jsonFiles && jsonFiles.length) {
fxy060608's avatar
fxy060608 已提交
108
    if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
109 110 111
      let appConfigContent = ''
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
Q
qiang 已提交
112 113 114
          if (!isAppView && jsonFile.name === 'manifest.json') {
            const content = JSON.parse(jsonFile.content)
            if (!content.launch_path && content.plus['uni-app'].nvueLaunchMode === 'fast') {
Q
qiang 已提交
115
              console.log(uniI18n.__('pagesLoader.nvueFirstPageStartModeIsFast', { 0: 'https://ask.dcloud.net.cn/article/36749' }))
Q
qiang 已提交
116 117
            }
          }
fxy060608's avatar
fxy060608 已提交
118 119
          if (jsonFile.name === 'define-pages.js') {
            appConfigContent = jsonFile.content
120 121
          } else {
            // app-view 不需要生成 app-config-service.js,manifest.json
fxy060608's avatar
fxy060608 已提交
122
            !isAppView && this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
123 124 125
          }
        }
      })
fxy060608's avatar
fxy060608 已提交
126
      return this.callback(null, appConfigContent, map)
fxy060608's avatar
fxy060608 已提交
127
    }
fxy060608's avatar
fxy060608 已提交
128
    if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
129 130 131
      let appConfigContent = ''
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
132
          if (jsonFile.name === 'app-config.js' || jsonFile.name === 'define-pages.js') {
fxy060608's avatar
fxy060608 已提交
133 134 135 136 137 138
            appConfigContent = jsonFile.content
          } else {
            this.emitFile(jsonFile.name, jsonFile.content)
          }
        }
      })
fxy060608's avatar
fxy060608 已提交
139
      return this.callback(null, appConfigContent, map)
fxy060608's avatar
fxy060608 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152
    }

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

fxy060608's avatar
fxy060608 已提交
153
  this.callback(null, '', map)
d-u-a's avatar
d-u-a 已提交
154
}