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

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

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

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

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

// 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并
fxy060608's avatar
fxy060608 已提交
42
function renameUsingComponents (jsonObj) {
43 44 45
  if (jsonObj.usingComponents || jsonObj.usingSwanComponents) {
    // 暂定 usingComponents 优先级高于 usingSwanComponents
    jsonObj.customUsingComponents = Object.assign({}, jsonObj.usingSwanComponents, jsonObj.usingComponents)
fxy060608's avatar
fxy060608 已提交
46
    delete jsonObj.usingComponents
47
    delete jsonObj.usingSwanComponents
fxy060608's avatar
fxy060608 已提交
48
  }
fxy060608's avatar
fxy060608 已提交
49
  return jsonObj
fxy060608's avatar
fxy060608 已提交
50 51
}

fxy060608's avatar
fxy060608 已提交
52
module.exports = function (content, map) {
fxy060608's avatar
fxy060608 已提交
53
  this.cacheable && this.cacheable()
fxy060608's avatar
fxy060608 已提交
54

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

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

D
DCloud_LXH 已提交
70 71
  initTheme(manifestJson)

fxy060608's avatar
fxy060608 已提交
72
  // this.addDependency(pagesJsonJsPath)
73 74 75 76 77
  const localePath = path.resolve(process.env.UNI_INPUT_DIR, 'locale')
  // 路径不存在时会触发 webpack5 差量编译
  if (fs.existsSync(localePath)) {
    this.addContextDependency(localePath)
  }
fxy060608's avatar
fxy060608 已提交
78
  this.addDependency(manifestJsonPath)
fxy060608's avatar
fxy060608 已提交
79

D
DCloud_LXH 已提交
80
  const pagesJson = parsePagesJson(content, {
fxy060608's avatar
fxy060608 已提交
81 82 83
    addDependency: file => {
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(
        normalizePath(file)
fxy060608's avatar
fxy060608 已提交
84 85
      )
      this.addDependency(file)
fxy060608's avatar
fxy060608 已提交
86
    }
fxy060608's avatar
fxy060608 已提交
87
  })
88

D
DCloud_LXH 已提交
89
  if (!pagesJson.pages || pagesJson.pages.length === 0) {
fxy060608's avatar
fxy060608 已提交
90 91
    console.error(uniI18n.__('pagesLoader.pagesNodeCannotNull'))
    process.exit(0)
92 93
  }

雪洛's avatar
雪洛 已提交
94 95 96 97
  if (this.resourceQuery) {
    const queryParam = loaderUtils.parseQuery(this.resourceQuery)
    if (queryParam) {
      if (queryParam.type === 'origin-pages-json') {
D
DCloud_LXH 已提交
98
        return `export default ${JSON.stringify(pagesJson)}`
雪洛's avatar
雪洛 已提交
99 100 101 102
      }
    }
  }

D
DCloud_LXH 已提交
103 104
  const platformManifestJson = manifestJson[process.env.UNI_PLATFORM] || {}

fxy060608's avatar
fxy060608 已提交
105
  if (global.uniPlugin.defaultTheme) {
D
DCloud_LXH 已提交
106 107 108 109 110 111
    this.addDependency(
      path.resolve(
        process.env.UNI_INPUT_DIR,
        platformManifestJson.themeLocation || 'theme.json'
      )
    )
fxy060608's avatar
fxy060608 已提交
112 113
  }

114
  // 组件自动导入配置
fxy060608's avatar
fxy060608 已提交
115 116
  process.UNI_AUTO_SCAN_COMPONENTS = !(
    pagesJson.easycom && pagesJson.easycom.autoscan === false
fxy060608's avatar
fxy060608 已提交
117 118
  )
  initAutoImportComponents(pagesJson.easycom)
119

fxy060608's avatar
fxy060608 已提交
120 121
  // TODO 与 usingComponents 放在一块读取设置
  if (manifestJson.transformPx === false) {
fxy060608's avatar
fxy060608 已提交
122
    process.UNI_TRANSFORM_PX = false
fxy060608's avatar
fxy060608 已提交
123
  } else {
fxy060608's avatar
fxy060608 已提交
124
    process.UNI_TRANSFORM_PX = true
fxy060608's avatar
fxy060608 已提交
125 126
  }

D
DCloud_LXH 已提交
127 128 129 130
  if (
    (process.env.UNI_PLATFORM.indexOf('mp') !== -1 && !darkmode()) ||
     process.env.VUE_APP_DARK_MODE !== 'true'
  ) {
D
DCloud_LXH 已提交
131
    const { pages, globalStyle, tabBar } = parseTheme(pagesJson)
D
DCloud_LXH 已提交
132
    Object.assign(pagesJson, JSON.parse(JSON.stringify({ pages, globalStyle, tabBar })))
D
DCloud_LXH 已提交
133 134
  }

fxy060608's avatar
fxy060608 已提交
135
  if (process.env.UNI_PLATFORM === 'h5') {
fxy060608's avatar
fxy060608 已提交
136 137
    return this.callback(
      null,
D
DCloud_LXH 已提交
138
      require('./platforms/h5')(
D
DCloud_LXH 已提交
139
        pagesJson,
D
DCloud_LXH 已提交
140 141 142
        manifestJson,
        this
      ),
fxy060608's avatar
fxy060608 已提交
143
      map
fxy060608's avatar
fxy060608 已提交
144
    )
fxy060608's avatar
fxy060608 已提交
145
  }
fxy060608's avatar
fxy060608 已提交
146
  if (process.env.UNI_PLATFORM === 'quickapp-native') {
fxy060608's avatar
fxy060608 已提交
147 148
    return this.callback(
      null,
fxy060608's avatar
fxy060608 已提交
149
      require('./platforms/quickapp-native')(pagesJson, manifestJson, this),
fxy060608's avatar
fxy060608 已提交
150
      map
fxy060608's avatar
fxy060608 已提交
151
    )
fxy060608's avatar
fxy060608 已提交
152 153
  }
  // 仅限小程序
fxy060608's avatar
fxy060608 已提交
154
  if (process.env.UNI_PLATFORM !== 'app-plus') {
fxy060608's avatar
fxy060608 已提交
155 156 157 158 159
    const i18nOptions = initI18nOptions(
      process.env.UNI_PLATFORM,
      process.env.UNI_INPUT_DIR,
      true,
      true
fxy060608's avatar
fxy060608 已提交
160
    )
fxy060608's avatar
fxy060608 已提交
161
    if (i18nOptions) {
fxy060608's avatar
fxy060608 已提交
162 163 164 165 166
      const {
        locale,
        locales,
        delimiters
      } = i18nOptions
fxy060608's avatar
fxy060608 已提交
167
      parseI18nJson(pagesJson, locales[locale], delimiters)
fxy060608's avatar
fxy060608 已提交
168
    }
fxy060608's avatar
fxy060608 已提交
169
  }
fxy060608's avatar
init v3  
fxy060608 已提交
170
  if (!process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
171 172
    parsePages(
      pagesJson,
fxy060608's avatar
fxy060608 已提交
173
      function (page) {
fxy060608's avatar
fxy060608 已提交
174 175 176
        updatePageJson(
          page.path,
          renameUsingComponents(parseStyle(page.style))
fxy060608's avatar
fxy060608 已提交
177
        )
fxy060608's avatar
fxy060608 已提交
178
      },
fxy060608's avatar
fxy060608 已提交
179
      function (root, page) {
fxy060608's avatar
fxy060608 已提交
180 181 182
        updatePageJson(
          normalizePath(path.join(root, page.path)),
          renameUsingComponents(parseStyle(page.style, root))
fxy060608's avatar
fxy060608 已提交
183
        )
fxy060608's avatar
fxy060608 已提交
184
      }
fxy060608's avatar
fxy060608 已提交
185
    )
fxy060608's avatar
init v3  
fxy060608 已提交
186
  }
fxy060608's avatar
fxy060608 已提交
187

fxy060608's avatar
fxy060608 已提交
188
  const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(
D
DCloud_LXH 已提交
189
    pagesJson,
fxy060608's avatar
fxy060608 已提交
190 191
    manifestJson,
    isAppView
fxy060608's avatar
fxy060608 已提交
192
  )
fxy060608's avatar
fxy060608 已提交
193

fxy060608's avatar
fxy060608 已提交
194
  if (jsonFiles && jsonFiles.length) {
fxy060608's avatar
fxy060608 已提交
195
    if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
196
      let appConfigContent = ''
fxy060608's avatar
fxy060608 已提交
197 198
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
199 200
          if (!isAppView && jsonFile.name === 'manifest.json') {
            const content = JSON.parse(jsonFile.content)
fxy060608's avatar
fxy060608 已提交
201 202
            if (
              !content.launch_path &&
fxy060608's avatar
fxy060608 已提交
203
              content.plus['uni-app'].nvueLaunchMode === 'fast'
fxy060608's avatar
fxy060608 已提交
204 205
            ) {
              console.log(
fxy060608's avatar
fxy060608 已提交
206 207
                uniI18n.__('pagesLoader.nvueFirstPageStartModeIsFast', {
                  0: 'https://ask.dcloud.net.cn/article/36749'
fxy060608's avatar
fxy060608 已提交
208
                })
fxy060608's avatar
fxy060608 已提交
209
              )
Q
qiang 已提交
210 211
            }
          }
fxy060608's avatar
fxy060608 已提交
212 213
          if (jsonFile.name === 'define-pages.js') {
            appConfigContent = jsonFile.content
214 215
          } else {
            // app-view 不需要生成 app-config-service.js,manifest.json
fxy060608's avatar
fxy060608 已提交
216
            !isAppView && this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
217 218
          }
        }
fxy060608's avatar
fxy060608 已提交
219 220
      })
      return this.callback(null, appConfigContent, map)
fxy060608's avatar
fxy060608 已提交
221
    }
fxy060608's avatar
fxy060608 已提交
222
    if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
223
      let appConfigContent = ''
fxy060608's avatar
fxy060608 已提交
224 225
      jsonFiles.forEach(jsonFile => {
        if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
226
          if (
fxy060608's avatar
fxy060608 已提交
227 228
            jsonFile.name === 'app-config.js' ||
            jsonFile.name === 'define-pages.js'
fxy060608's avatar
fxy060608 已提交
229
          ) {
fxy060608's avatar
fxy060608 已提交
230
            appConfigContent = jsonFile.content
fxy060608's avatar
fxy060608 已提交
231
          } else {
fxy060608's avatar
fxy060608 已提交
232
            this.emitFile(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
233 234
          }
        }
fxy060608's avatar
fxy060608 已提交
235 236
      })
      return this.callback(null, appConfigContent, map)
fxy060608's avatar
fxy060608 已提交
237 238 239 240
    }

    jsonFiles.forEach(jsonFile => {
      if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
241 242
        if (jsonFile.name === 'app') {
          updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content))
fxy060608's avatar
fxy060608 已提交
243
        } else {
fxy060608's avatar
fxy060608 已提交
244
          updateProjectJson(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
245 246
        }
      }
fxy060608's avatar
fxy060608 已提交
247
    })
fxy060608's avatar
fxy060608 已提交
248 249
  }

fxy060608's avatar
fxy060608 已提交
250
  this.callback(null, '', map)
P
panyiming.325 已提交
251
}