index.js 3.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const fs = require('fs')
const path = require('path')
const loaderUtils = require('loader-utils')

const {
  parsePages,
  normalizePath,
  parsePagesJson,
  parseManifestJson
} = require('@dcloudio/uni-cli-shared')

const {
  getPagesJson
} = require('@dcloudio/uni-cli-shared/lib/cache')

fxy060608's avatar
fxy060608 已提交
16 17 18 19
const {
  pagesJsonJsFileName
} = require('@dcloudio/uni-cli-shared/lib/pages')

fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28 29 30 31
const parseStyle = require('./util').parseStyle

const emitFileCaches = {}

function checkEmitFile (filePath, jsonObj, changedEmitFiles) {
  const content = JSON.stringify(jsonObj, null, 2)
  if (emitFileCaches[filePath] !== content) {
    changedEmitFiles.push(filePath)
    emitFileCaches[filePath] = content
  }
}

fxy060608's avatar
fxy060608 已提交
32
module.exports = function (content, map) {
fxy060608's avatar
fxy060608 已提交
33
  // content = JSON.stringify(require('@dcloudio/uni-cli-shared/lib/uni_modules').getPagesJson(content))
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42 43
  if (this.resourceQuery) {
    const params = loaderUtils.parseQuery(this.resourceQuery)
    if (params) {
      if (params.type === 'style') {
        return `export default ${JSON.stringify(getPagesJson())}`
      } else if (params.type === 'stat') {
        return `export default ${JSON.stringify(process.UNI_STAT_CONFIG || {})}`
      }
    }
  }
fxy060608's avatar
fxy060608 已提交
44
  // add deps
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50
  // global.uniModules.forEach(module => {
  //   const uniModulePagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, 'pages.json')
  //   if (fs.existsSync(uniModulePagesJsonPath)) {
  //     this.addDependency(uniModulePagesJsonPath)
  //   }
  // })
fxy060608's avatar
fxy060608 已提交
51

fxy060608's avatar
fxy060608 已提交
52 53 54
  if (
    process.env.UNI_USING_COMPONENTS ||
    process.env.UNI_PLATFORM === 'h5' ||
fxy060608's avatar
fxy060608 已提交
55
    process.env.UNI_PLATFORM === 'quickapp-native'
fxy060608's avatar
fxy060608 已提交
56
  ) {
fxy060608's avatar
fxy060608 已提交
57
    return require('./index-new').call(this, content, map)
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62
  }

  this.cacheable && this.cacheable()

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

  this.addDependency(manifestJsonPath)
fxy060608's avatar
fxy060608 已提交
67
  this.addDependency(pagesJsonJsPath)
fxy060608's avatar
fxy060608 已提交
68

fxy060608's avatar
fxy060608 已提交
69 70 71 72 73 74
  const pagesJson = parsePagesJson(content, {
    addDependency: (file) => {
      (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file))
      this.addDependency(file)
    }
  })
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  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)
  }

  const changedEmitFiles = []

  function checkPageEmitFile (pagePath, pageStyle) {
    checkEmitFile(pagePath, parseStyle(pageStyle), changedEmitFiles)
  }

  parsePages(pagesJson, function (page) {
    checkPageEmitFile(page.path, page.style)
  }, function (root, page) {
    checkPageEmitFile(normalizePath(path.join(root, page.path)), page.style)
  })

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

  if (jsonFiles && jsonFiles.length) {
    jsonFiles.forEach(jsonFile => {
      jsonFile && checkEmitFile(jsonFile.name, jsonFile.content, changedEmitFiles)
    })
  }

  changedEmitFiles.forEach(name => {
    this.emitFile(name + '.json', emitFileCaches[name])
  })

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