build.ts 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import fs from 'fs'
fxy060608's avatar
fxy060608 已提交
2 3 4
import path from 'path'
import { UserConfig } from 'vite'

fxy060608's avatar
fxy060608 已提交
5
import {
fxy060608's avatar
fxy060608 已提交
6
  emptyDir,
fxy060608's avatar
fxy060608 已提交
7 8 9 10 11 12 13 14 15 16 17
  isCSSRequest,
  normalizePath,
  resolveMainPathOnce,
} from '@dcloudio/uni-cli-shared'
import { GetManualChunk, GetModuleInfo } from 'rollup'
import {
  isUniComponentUrl,
  isUniPageUrl,
  parseVirtualComponentPath,
  parseVirtualPagePath,
} from '../plugins/virtual'
fxy060608's avatar
fxy060608 已提交
18 19

export function buildOptions(): UserConfig['build'] {
fxy060608's avatar
fxy060608 已提交
20
  const inputDir = process.env.UNI_INPUT_DIR
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25
  const outputDir = process.env.UNI_OUTPUT_DIR
  // 开始编译时,清空输出目录
  if (fs.existsSync(outputDir)) {
    emptyDir(outputDir)
  }
fxy060608's avatar
fxy060608 已提交
26 27
  return {
    // sourcemap: 'inline', // TODO
fxy060608's avatar
fxy060608 已提交
28
    emptyOutDir: false, // 不清空输出目录,否则会影响自定义的一些文件输出,比如wxml
fxy060608's avatar
fxy060608 已提交
29
    assetsInlineLimit: 0, // TODO
fxy060608's avatar
fxy060608 已提交
30 31 32 33
    lib: {
      entry: resolveMainPathOnce(inputDir),
      formats: ['cjs'],
    },
fxy060608's avatar
fxy060608 已提交
34 35
    rollupOptions: {
      output: {
fxy060608's avatar
fxy060608 已提交
36 37
        entryFileNames: 'app.js',
        manualChunks: createMoveToVendorChunkFn(),
fxy060608's avatar
fxy060608 已提交
38 39
        chunkFileNames(chunk) {
          if (chunk.isDynamicEntry && chunk.facadeModuleId) {
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
            let id = chunk.facadeModuleId
            if (isUniPageUrl(id)) {
              id = path.resolve(
                process.env.UNI_INPUT_DIR,
                parseVirtualPagePath(id)
              )
            } else if (isUniComponentUrl(id)) {
              id = path.resolve(
                process.env.UNI_INPUT_DIR,
                parseVirtualComponentPath(id)
              )
            }
            const filepath = path.relative(inputDir, id)
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60 61 62 63
            return normalizePath(
              filepath.replace(path.extname(filepath), '.js')
            )
          }
          return '[name].js'
        },
        assetFileNames: '[name][extname]',
      },
    },
  }
}
fxy060608's avatar
fxy060608 已提交
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

function createMoveToVendorChunkFn(): GetManualChunk {
  const cache = new Map<string, boolean>()
  return (id, { getModuleInfo }) => {
    if (
      (id.includes('node_modules') ||
        id.includes('plugin-vue:export-helper')) &&
      !isCSSRequest(id) &&
      staticImportedByEntry(id, getModuleInfo, cache)
    ) {
      return 'vendor'
    }
  }
}

function staticImportedByEntry(
  id: string,
  getModuleInfo: GetModuleInfo,
  cache: Map<string, boolean>,
  importStack: string[] = []
): boolean {
  if (cache.has(id)) {
    return cache.get(id) as boolean
  }
  if (importStack.includes(id)) {
    // circular deps!
    cache.set(id, false)
    return false
  }
  const mod = getModuleInfo(id)
  if (!mod) {
    cache.set(id, false)
    return false
  }

  if (mod.isEntry) {
    cache.set(id, true)
    return true
  }
  const someImporterIs = mod.importers.some((importer) =>
    staticImportedByEntry(
      importer,
      getModuleInfo,
      cache,
      importStack.concat(id)
    )
  )
  cache.set(id, someImporterIs)
  return someImporterIs
}