split-chunks.js 6.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const path = require('path')

fxy060608's avatar
fxy060608 已提交
3 4 5 6
const {
  normalizePath
} = require('@dcloudio/uni-cli-shared')

7 8 9 10 11 12 13
const subPkgsInfo = Object.values(process.UNI_SUBPACKAGES);
const normalFilter = ({ independent }) => !independent;
const independentFilter = ({ independent }) => independent;
const map2Root = ({ root }) => root + '/';
const normalSubPackageRoots = subPkgsInfo.filter(normalFilter).map(map2Root);
const independentSubpackageRoots = subPkgsInfo.filter(independentFilter).map(map2Root);

14
function createCacheGroups () {
fxy060608's avatar
fxy060608 已提交
15 16 17
  const cacheGroups = {}
  if (process.UNI_CONFUSION) { // 加密
    cacheGroups.confusion = {
fxy060608's avatar
fxy060608 已提交
18
      enforce: true,
19
      test: function (module) {
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
        if (!module.resource) {
          return false
        }
        if (process.UNI_CONFUSION.includes(normalizePath(module.resource))) {
          return true
        }
        return false
      },
      name: 'app-confusion',
      chunks: 'all'
    }
  }

  process.env.UNI_OPT_SUBPACKAGES && Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
    cacheGroups[root] = {
fxy060608's avatar
fxy060608 已提交
35
      enforce: true,
36
      test: function (module) {
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        if (!module.resource) {
          return false
        }
        if (normalizePath(module.resource).includes(root + '/')) {
          return true
        }
        return false
      },
      name: root + '/app-sub-service',
      chunks: 'all'
    }
  })
  return cacheGroups
}

52
module.exports = function getSplitChunks () {
fxy060608's avatar
fxy060608 已提交
53 54 55 56
  const {
    normalizePath
  } = require('@dcloudio/uni-cli-shared')

fxy060608's avatar
init v3  
fxy060608 已提交
57
  if (process.env.UNI_USING_V3) {
fxy060608's avatar
fxy060608 已提交
58
    return {
fxy060608's avatar
fxy060608 已提交
59
      cacheGroups: createCacheGroups()
fxy060608's avatar
fxy060608 已提交
60
    }
fxy060608's avatar
init v3  
fxy060608 已提交
61
  }
fxy060608's avatar
fxy060608 已提交
62

fxy060608's avatar
fxy060608 已提交
63 64 65 66 67 68 69 70 71 72 73
  if (!process.env.UNI_USING_COMPONENTS) {
    return {
      cacheGroups: {
        commons: {
          minChunks: 2,
          name: 'common/vendor',
          chunks: 'all'
        }
      }
    }
  }
fxy060608's avatar
fxy060608 已提交
74 75 76

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

fxy060608's avatar
fxy060608 已提交
77 78
  if (!process.env.UNI_OPT_SUBPACKAGES) {
    return {
79
      chunks (chunk) { // 防止 node_modules 内 vue 组件被 split
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85
        return chunk.name.indexOf('node-modules') !== 0
      },
      cacheGroups: {
        default: false,
        vendors: false,
        commons: {
86
          test (module) {
fxy060608's avatar
fxy060608 已提交
87 88 89 90
            if (module.type === 'css/mini-extract') {
              return false
            }
            if (module.resource && (
91
              module.resource.indexOf('.vue') !== -1 ||
fxy060608's avatar
fxy060608 已提交
92
                module.resource.indexOf('.nvue') !== -1 ||
fxy060608's avatar
fxy060608 已提交
93
                normalizePath(module.resource).indexOf(mainPath) === 0 // main.js
94
            )) {
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103 104 105 106
              return false
            }
            return true
          },
          minChunks: 1,
          name: 'common/vendor',
          chunks: 'all'
        }
      }
    }
  }

107
  function baseTest (module) {
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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: {
129
      test (module, chunks) {
fxy060608's avatar
fxy060608 已提交
130 131 132
        if (!baseTest(module)) {
          return false
        }
133 134
        const matchSubPackages = findSubPackages(chunks)
        const matchSubPackagesCount = matchSubPackages.size
fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140
        const isMainPackage = ( // 非分包 或 两个及以上分包 或 主包内有使用
          matchSubPackagesCount === 0 ||
          matchSubPackagesCount > 1 ||
          (
            matchSubPackagesCount === 1 &&
            hasMainPackage(chunks)
141 142 143 144
          ) ||
          (
            matchSubPackagesCount === 1 &&
            hasMainPackageComponent(module, matchSubPackages.values().next().value)
fxy060608's avatar
fxy060608 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
          )
        )
        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'
    }
  }

159
  const findSubPackages = function (chunks) {
fxy060608's avatar
fxy060608 已提交
160 161
    return chunks.reduce((pkgs, item) => {
      const name = normalizePath(item.name)
162
      const pkgRoot = normalSubPackageRoots.find(root => name.indexOf(root) === 0)
fxy060608's avatar
fxy060608 已提交
163 164 165 166 167
      pkgRoot && pkgs.add(pkgRoot)
      return pkgs
    }, new Set())
  }

168
  const hasMainPackage = function (chunks) {
fxy060608's avatar
fxy060608 已提交
169 170 171
    return chunks.find(item => !subPackageRoots.find(root => item.name.indexOf(root) === 0))
  }

172 173 174 175
  const hasMainPackageComponent = function (module, subPackageRoot) {
    if (module.resource && module.reasons) {
      for (let index = 0; index < module.reasons.length; index++) {
        const m = module.reasons[index]
fxy060608's avatar
fxy060608 已提交
176

177 178 179 180 181 182 183 184 185 186 187
        if (m.module && m.module.resource) {
          const resource = normalizePath(m.module.resource)
          if (
            resource.indexOf('.vue') !== -1 ||
            resource.indexOf('.nvue') !== -1
          ) {
            if (resource.indexOf(subPackageRoot) === -1) {
              if (process.env.UNI_OPT_TRACE) {
                console.log('move module to main chunk:', module.resource,
                  'from', subPackageRoot, 'for component in main package:', resource)
              }
188 189 190 191 192 193

              // 独立分包除外
              const independentRoot = independentSubpackageRoots.find(root => resource.indexOf(root) >= 0);
              if (!independentRoot) {
                return true;
              }
194
            }
195 196
          } else {
            return hasMainPackageComponent(m.module, subPackageRoot)
197 198 199 200 201 202 203
          }
        }
      }
    }
    return false
  }

204
  const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES).map(root => root + '/')
fxy060608's avatar
fxy060608 已提交
205 206

  Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
207
    (function (root) {
fxy060608's avatar
fxy060608 已提交
208
      cacheGroups[root + '/commons'] = {
209
        test (module, chunks) {
fxy060608's avatar
fxy060608 已提交
210 211 212 213 214 215
          if (!baseTest(module)) {
            return false
          }
          const matchSubPackages = findSubPackages(chunks)
          if (
            matchSubPackages.size === 1 &&
216
            matchSubPackages.has(root + '/') &&
217 218
            !hasMainPackage(chunks) &&
            !hasMainPackageComponent(module, matchSubPackages.values().next().value)
219
          ) {
fxy060608's avatar
fxy060608 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            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 {
235
    chunks (chunk) { // 防止 node_modules 内 vue 组件被 split
fxy060608's avatar
fxy060608 已提交
236 237 238 239
      return chunk.name.indexOf('node-modules') !== 0
    },
    cacheGroups
  }
240
}