index-new.js 6.8 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) {
42 43 44
  if (jsonObj.usingComponents || jsonObj.usingSwanComponents) {
    // 暂定 usingComponents 优先级高于 usingSwanComponents
    jsonObj.customUsingComponents = Object.assign({}, jsonObj.usingSwanComponents, jsonObj.usingComponents)
fxy060608's avatar
fxy060608 已提交
45
    delete jsonObj.usingComponents
46
    delete jsonObj.usingSwanComponents
fxy060608's avatar
fxy060608 已提交
47
  }
fxy060608's avatar
fxy060608 已提交
48
  return jsonObj
fxy060608's avatar
fxy060608 已提交
49 50
}

fxy060608's avatar
fxy060608 已提交
51
module.exports = function (content, map) {
fxy060608's avatar
fxy060608 已提交
52
  this.cacheable && this.cacheable()
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

D
DCloud_LXH 已提交
69 70
  initTheme(manifestJson)

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

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

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

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

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

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

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

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

D
DCloud_LXH 已提交
126 127 128 129 130
  if (process.env.VUE_APP_DARK_MODE !== 'true') {
    const { pages, globalStyle, tabBar } = parseTheme(pagesJson)
    Object.assign(pagesJson, { pages, globalStyle, tabBar })
  }

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

fxy060608's avatar
fxy060608 已提交
184
  const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(
D
DCloud_LXH 已提交
185
    pagesJson,
fxy060608's avatar
fxy060608 已提交
186 187
    manifestJson,
    isAppView
fxy060608's avatar
fxy060608 已提交
188
  )
fxy060608's avatar
fxy060608 已提交
189

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

    jsonFiles.forEach(jsonFile => {
      if (jsonFile) {
fxy060608's avatar
fxy060608 已提交
237 238
        if (jsonFile.name === 'app') {
          updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content))
fxy060608's avatar
fxy060608 已提交
239
        } else {
fxy060608's avatar
fxy060608 已提交
240
          updateProjectJson(jsonFile.name, jsonFile.content)
fxy060608's avatar
fxy060608 已提交
241 242
        }
      }
fxy060608's avatar
fxy060608 已提交
243
    })
fxy060608's avatar
fxy060608 已提交
244 245
  }

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