split-chunks.js 3.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
const path = require('path')

module.exports = function getSplitChunks () {
  if (!process.env.UNI_USING_COMPONENTS) {
    return {
      cacheGroups: {
        commons: {
          minChunks: 2,
          name: 'common/vendor',
          chunks: 'all'
        }
      }
    }
  }
  if (!process.env.UNI_OPT_SUBPACKAGES) {
    return {
      chunks (chunk) { // 防止 node_modules 内 vue 组件被 split
        return chunk.name.indexOf('node-modules') !== 0
      },
      cacheGroups: {
        default: false,
        vendors: false,
        commons: {
          test (module) {
            if (module.type === 'css/mini-extract') {
              return false
            }
            if (module.resource && (
              module.resource.indexOf('.vue') !== -1 ||
                module.resource.indexOf('.nvue') !== -1
            )) {
              return false
            }
            return true
          },
          minChunks: 1,
          name: 'common/vendor',
          chunks: 'all'
        }
      }
    }
  }
  const {
    normalizePath
  } = require('@dcloudio/uni-cli-shared')

  const mainPath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, 'main.'))

  function baseTest (module) {
    if (module.type === 'css/mini-extract') {
      return false
    }
    if (module.resource) {
      const resource = normalizePath(module.resource)
      if (
        resource.indexOf('.vue') !== -1 ||
        resource.indexOf('.nvue') !== -1 ||
        resource.indexOf(mainPath) === 0 // main.js
      ) {
        return false
      }
    }
    return true
  }
  // TODO 独立分包

  const cacheGroups = {
    default: false,
    vendors: false,
    commons: {
      test (module, chunks) {
        if (!baseTest(module)) {
          return false
        }
        const matchSubPackagesCount = findSubPackages(chunks).size
        const isMainPackage = ( // 非分包 或 两个及以上分包 或 主包内有使用
          matchSubPackagesCount === 0 ||
          matchSubPackagesCount > 1 ||
          (
            matchSubPackagesCount === 1 &&
            hasMainPackage(chunks)
          )
        )
        if (isMainPackage && process.env.UNI_OPT_TRACE) {
          console.log('main', module.resource, chunks.map(chunk => chunk.name))
        }
        return isMainPackage
      },
      minSize: 0,
      minChunks: 1,
      name: 'common/vendor',
      chunks: 'all'
    }
  }

  const findSubPackages = function (chunks) {
    return chunks.reduce((pkgs, item) => {
      const name = normalizePath(item.name)
      const pkgRoot = subPackageRoots.find(root => name.indexOf(root) === 0)
      pkgRoot && pkgs.add(pkgRoot)
      return pkgs
    }, new Set())
  }

  const hasMainPackage = function (chunks) {
    return chunks.find(item => !subPackageRoots.find(root => item.name.indexOf(root) === 0))
  }

  const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES)

  Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
    (function (root) {
      cacheGroups[root + '/commons'] = {
        test (module, chunks) {
          if (!baseTest(module)) {
            return false
          }
          const matchSubPackages = findSubPackages(chunks)
          if (
            matchSubPackages.size === 1 &&
            matchSubPackages.has(root) &&
            !hasMainPackage(chunks)
          ) {
            if (process.env.UNI_OPT_TRACE) {
              console.log(root, module.resource, chunks.map(chunk => chunk.name))
            }
            return true
          }
        },
        minSize: 0,
        minChunks: 1,
        name: normalizePath(path.join(root, 'common/vendor')),
        chunks: 'all'
      }
    })(root)
  })

  return {
    chunks (chunk) { // 防止 node_modules 内 vue 组件被 split
      return chunk.name.indexOf('node-modules') !== 0
    },
    cacheGroups
  }
}