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

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

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

const {
  updateAppJson,
  updatePageJson,
  updateProjectJson
fxy060608's avatar
fxy060608 已提交
17
} = require('@dcloudio/uni-cli-shared/lib/cache')
fxy060608's avatar
fxy060608 已提交
18

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

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

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

fxy060608's avatar
fxy060608 已提交
31
const parseStyle = require('./util').parseStyle
d-u-a's avatar
d-u-a 已提交
32

fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38
const {
  initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
const {
  parseI18nJson
} = require('@dcloudio/uni-i18n')
fxy060608's avatar
fxy060608 已提交
39 40

// 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并
fxy060608's avatar
fxy060608 已提交
41
function renameUsingComponents (jsonObj) {
fxy060608's avatar
fxy060608 已提交
42
  if (jsonObj.usingComponents) {
fxy060608's avatar
fxy060608 已提交
43 44
    jsonObj.customUsingComponents = jsonObj.usingComponents
    delete jsonObj.usingComponents
fxy060608's avatar
fxy060608 已提交
45
  }
fxy060608's avatar
fxy060608 已提交
46
  return jsonObj
fxy060608's avatar
fxy060608 已提交
47 48
}

fxy060608's avatar
fxy060608 已提交
49
module.exports = function (content, map) {
fxy060608's avatar
fxy060608 已提交
50
  this.cacheable && this.cacheable()
fxy060608's avatar
fxy060608 已提交
51

fxy060608's avatar
fxy060608 已提交
52
  initTheme()
fxy060608's avatar
fxy060608 已提交
53

fxy060608's avatar
fxy060608 已提交
54
  let isAppView = false
fxy060608's avatar
fxy060608 已提交
55
  if (this.resourceQuery) {
fxy060608's avatar
fxy060608 已提交
56 57
    const params = loaderUtils.parseQuery(this.resourceQuery)
    isAppView = params.type === 'view'
fxy060608's avatar
fxy060608 已提交
58 59
  }

fxy060608's avatar
fxy060608 已提交
60
  // const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName)
fxy060608's avatar
fxy060608 已提交
61 62
  const manifestJsonPath = path.resolve(
    process.env.UNI_INPUT_DIR,
fxy060608's avatar
fxy060608 已提交
63 64
    'manifest.json'
  )
fxy060608's avatar
fxy060608 已提交
65
  const manifestJson = parseManifestJson(
fxy060608's avatar
fxy060608 已提交
66 67
    fs.readFileSync(manifestJsonPath, 'utf8')
  )
fxy060608's avatar
fxy060608 已提交
68

fxy060608's avatar
fxy060608 已提交
69
  // this.addDependency(pagesJsonJsPath)
fxy060608's avatar
fxy060608 已提交
70
  this.addContextDependency(path.resolve(process.env.UNI_INPUT_DIR, 'locale'))
fxy060608's avatar
fxy060608 已提交
71
  this.addDependency(manifestJsonPath)
fxy060608's avatar
fxy060608 已提交
72

fxy060608's avatar
fxy060608 已提交
73
  let pagesJson = parsePagesJson(content, {
fxy060608's avatar
fxy060608 已提交
74 75 76
    addDependency: file => {
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(
        normalizePath(file)
fxy060608's avatar
fxy060608 已提交
77 78
      )
      this.addDependency(file)
fxy060608's avatar
fxy060608 已提交
79
    }
fxy060608's avatar
fxy060608 已提交
80
  })
81

82
  if (!pagesJson.pages || pagesJson.pages.length === 0) {
fxy060608's avatar
fxy060608 已提交
83 84
    console.error(uniI18n.__('pagesLoader.pagesNodeCannotNull'))
    process.exit(0)
85 86
  }

fxy060608's avatar
fxy060608 已提交
87
  if (global.uniPlugin.defaultTheme) {
fxy060608's avatar
fxy060608 已提交
88 89
    pagesJson = parseTheme(pagesJson)
    this.addDependency(path.resolve(process.env.UNI_INPUT_DIR, 'theme.json'))
fxy060608's avatar
fxy060608 已提交
90 91
  }

92
  // 组件自动导入配置
fxy060608's avatar
fxy060608 已提交
93 94
  process.UNI_AUTO_SCAN_COMPONENTS = !(
    pagesJson.easycom && pagesJson.easycom.autoscan === false
fxy060608's avatar
fxy060608 已提交
95 96
  )
  initAutoImportComponents(pagesJson.easycom)
97

fxy060608's avatar
fxy060608 已提交
98 99
  // TODO 与 usingComponents 放在一块读取设置
  if (manifestJson.transformPx === false) {
fxy060608's avatar
fxy060608 已提交
100
    process.UNI_TRANSFORM_PX = false
fxy060608's avatar
fxy060608 已提交
101
  } else {
fxy060608's avatar
fxy060608 已提交
102
    process.UNI_TRANSFORM_PX = true
fxy060608's avatar
fxy060608 已提交
103 104
  }

fxy060608's avatar
fxy060608 已提交
105
  if (process.env.UNI_PLATFORM === 'h5') {
fxy060608's avatar
fxy060608 已提交
106 107
    return this.callback(
      null,
fxy060608's avatar
fxy060608 已提交
108
      require('./platforms/h5')(pagesJson, manifestJson, this),
fxy060608's avatar
fxy060608 已提交
109
      map
fxy060608's avatar
fxy060608 已提交
110
    )
fxy060608's avatar
fxy060608 已提交
111
  }
fxy060608's avatar
fxy060608 已提交
112
  if (process.env.UNI_PLATFORM === 'quickapp-native') {
fxy060608's avatar
fxy060608 已提交
113 114
    return this.callback(
      null,
fxy060608's avatar
fxy060608 已提交
115
      require('./platforms/quickapp-native')(pagesJson, manifestJson, this),
fxy060608's avatar
fxy060608 已提交
116
      map
fxy060608's avatar
fxy060608 已提交
117
    )
fxy060608's avatar
fxy060608 已提交
118 119
  }
  // 仅限小程序
fxy060608's avatar
fxy060608 已提交
120
  if (process.env.UNI_PLATFORM !== 'app-plus') {
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125
    const i18nOptions = initI18nOptions(
      process.env.UNI_PLATFORM,
      process.env.UNI_INPUT_DIR,
      true,
      true
fxy060608's avatar
fxy060608 已提交
126
    )
fxy060608's avatar
fxy060608 已提交
127
    if (i18nOptions) {
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132
      const {
        locale,
        locales,
        delimiters
      } = i18nOptions
fxy060608's avatar
fxy060608 已提交
133
      parseI18nJson(pagesJson, locales[locale], delimiters)
fxy060608's avatar
fxy060608 已提交
134
    }
fxy060608's avatar
fxy060608 已提交
135
  }
fxy060608's avatar
init v3  
fxy060608 已提交
136
  if (!process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
137 138
    parsePages(
      pagesJson,
fxy060608's avatar
fxy060608 已提交
139
      function (page) {
fxy060608's avatar
fxy060608 已提交
140 141 142
        updatePageJson(
          page.path,
          renameUsingComponents(parseStyle(page.style))
fxy060608's avatar
fxy060608 已提交
143
        )
fxy060608's avatar
fxy060608 已提交
144
      },
fxy060608's avatar
fxy060608 已提交
145
      function (root, page) {
fxy060608's avatar
fxy060608 已提交
146 147 148
        updatePageJson(
          normalizePath(path.join(root, page.path)),
          renameUsingComponents(parseStyle(page.style, root))
fxy060608's avatar
fxy060608 已提交
149
        )
fxy060608's avatar
fxy060608 已提交
150
      }
fxy060608's avatar
fxy060608 已提交
151
    )
fxy060608's avatar
init v3  
fxy060608 已提交
152
  }
fxy060608's avatar
fxy060608 已提交
153

fxy060608's avatar
fxy060608 已提交
154
  const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(
fxy060608's avatar
fxy060608 已提交
155 156 157
    pagesJson,
    manifestJson,
    isAppView
fxy060608's avatar
fxy060608 已提交
158
  )
fxy060608's avatar
fxy060608 已提交
159

fxy060608's avatar
fxy060608 已提交
160
  if (jsonFiles && jsonFiles.length) {
fxy060608's avatar
fxy060608 已提交
161
    if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
162
      let appConfigContent = ''
fxy060608's avatar
fxy060608 已提交
163 164
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
165 166
          if (!isAppView && jsonFile.name === 'manifest.json') {
            const content = JSON.parse(jsonFile.content)
fxy060608's avatar
fxy060608 已提交
167 168
            if (
              !content.launch_path &&
fxy060608's avatar
fxy060608 已提交
169
              content.plus['uni-app'].nvueLaunchMode === 'fast'
fxy060608's avatar
fxy060608 已提交
170 171
            ) {
              console.log(
fxy060608's avatar
fxy060608 已提交
172 173
                uniI18n.__('pagesLoader.nvueFirstPageStartModeIsFast', {
                  0: 'https://ask.dcloud.net.cn/article/36749'
fxy060608's avatar
fxy060608 已提交
174
                })
fxy060608's avatar
fxy060608 已提交
175
              )
Q
qiang 已提交
176 177
            }
          }
fxy060608's avatar
fxy060608 已提交
178 179
          if (jsonFile.name === 'define-pages.js') {
            appConfigContent = jsonFile.content
180 181
          } else {
            // app-view 不需要生成 app-config-service.js,manifest.json
fxy060608's avatar
fxy060608 已提交
182
            !isAppView && this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
183 184
          }
        }
fxy060608's avatar
fxy060608 已提交
185 186
      })
      return this.callback(null, appConfigContent, map)
fxy060608's avatar
fxy060608 已提交
187
    }
fxy060608's avatar
fxy060608 已提交
188
    if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
189
      let appConfigContent = ''
fxy060608's avatar
fxy060608 已提交
190 191
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
192
          if (
fxy060608's avatar
fxy060608 已提交
193 194
            jsonFile.name === 'app-config.js' ||
            jsonFile.name === 'define-pages.js'
fxy060608's avatar
fxy060608 已提交
195
          ) {
fxy060608's avatar
fxy060608 已提交
196
            appConfigContent = jsonFile.content
fxy060608's avatar
fxy060608 已提交
197
          } else {
fxy060608's avatar
fxy060608 已提交
198
            this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
199 200
          }
        }
fxy060608's avatar
fxy060608 已提交
201 202
      })
      return this.callback(null, appConfigContent, map)
fxy060608's avatar
fxy060608 已提交
203 204 205 206
    }

    jsonFiles.forEach(jsonFile => {
      if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
207 208
        if (jsonFile.name === 'app') {
          updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content))
fxy060608's avatar
fxy060608 已提交
209
        } else {
fxy060608's avatar
fxy060608 已提交
210
          updateProjectJson(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
211 212
        }
      }
fxy060608's avatar
fxy060608 已提交
213
    })
fxy060608's avatar
fxy060608 已提交
214 215
  }

fxy060608's avatar
fxy060608 已提交
216
  this.callback(null, '', map)
fxy060608's avatar
fxy060608 已提交
217
}