configResolved.ts 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7
import { Plugin } from 'vite'
import {
  removePlugins,
  injectAssetPlugin,
  injectCssPlugin,
  injectCssPostPlugin,
fxy060608's avatar
fxy060608 已提交
8
  normalizePath,
fxy060608's avatar
fxy060608 已提交
9 10 11 12
  resolveMainPathOnce,
  removeExt,
  transformScopedCss,
  normalizeMiniProgramFilename,
fxy060608's avatar
fxy060608 已提交
13
  relativeFile,
fxy060608's avatar
fxy060608 已提交
14
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
15
import { UniMiniProgramPluginOptions } from '.'
fxy060608's avatar
fxy060608 已提交
16
import { getNVueCssPaths } from '../plugins/pagesJson'
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21 22
import {
  isUniComponentUrl,
  isUniPageUrl,
  parseVirtualComponentPath,
  parseVirtualPagePath,
} from '../plugins/entry'
fxy060608's avatar
fxy060608 已提交
23 24

const debugNVueCss = debug('vite:uni:nvue-css')
fxy060608's avatar
fxy060608 已提交
25
const cssVars = `page{--status-bar-height:25px;--top-window-height:0px;--window-top:0px;--window-bottom:0px;--window-left:0px;--window-right:0px;--window-magin:0px}`
fxy060608's avatar
fxy060608 已提交
26 27 28 29

const genShadowCss = (cdn: string) => {
  return `page::after{position:fixed;content:'';left:-1000px;top:-1000px;-webkit-animation:shadow-preload .1s;-webkit-animation-delay:3s;animation:shadow-preload .1s;animation-delay:3s}@-webkit-keyframes shadow-preload{0%{background-image:url(${cdn}/img/shadow-grey.png)}100%{background-image:url(${cdn}/img/shadow-grey.png)}}@keyframes shadow-preload{0%{background-image:url(${cdn}/img/shadow-grey.png)}100%{background-image:url(${cdn}/img/shadow-grey.png)}}`
}
fxy060608's avatar
fxy060608 已提交
30 31
const genComponentCustomHiddenCss = (name: string) =>
  `[${name.replace(':', '')}="true"]{display: none !important;}`
fxy060608's avatar
fxy060608 已提交
32

fxy060608's avatar
fxy060608 已提交
33
export function createConfigResolved({
fxy060608's avatar
fxy060608 已提交
34
  cdn,
fxy060608's avatar
fxy060608 已提交
35
  style: { extname },
fxy060608's avatar
fxy060608 已提交
36
  template: { component },
fxy060608's avatar
fxy060608 已提交
37
}: UniMiniProgramPluginOptions): Plugin['configResolved'] {
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43
  function normalizeCssChunkFilename(id: string, extname: string) {
    return (
      removeExt(normalizeMiniProgramFilename(id, process.env.UNI_INPUT_DIR)) +
      extname
    )
  }
fxy060608's avatar
fxy060608 已提交
44
  return (config) => {
fxy060608's avatar
fxy060608 已提交
45
    const mainPath = resolveMainPathOnce(process.env.UNI_INPUT_DIR)
fxy060608's avatar
fxy060608 已提交
46 47 48
    removePlugins('vite:import-analysis', config)
    injectCssPlugin(config)
    injectCssPostPlugin(config, {
fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
      chunkCssFilename(id: string) {
        if (id === mainPath) {
          return 'app' + extname
        } else if (isUniPageUrl(id)) {
          return normalizeCssChunkFilename(parseVirtualPagePath(id), extname)
        } else if (isUniComponentUrl(id)) {
          return normalizeCssChunkFilename(
            parseVirtualComponentPath(id),
            extname
          )
        }
      },
      chunkCssCode(filename, cssCode) {
        cssCode = transformScopedCss(cssCode)
fxy060608's avatar
fxy060608 已提交
63
        if (filename === 'app' + extname) {
fxy060608's avatar
fxy060608 已提交
64 65 66 67 68
          const componentCustomHiddenCss =
            (component &&
              component.vShow &&
              genComponentCustomHiddenCss(component.vShow)) ||
            ''
fxy060608's avatar
fxy060608 已提交
69
          if (config.isProduction) {
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75
            return (
              cssCode +
              genShadowCss(`https://cdn${cdn || ''}.dcloud.net.cn`) +
              cssVars +
              componentCustomHiddenCss
            )
fxy060608's avatar
fxy060608 已提交
76
          } else {
fxy060608's avatar
fxy060608 已提交
77
            return cssCode + cssVars + componentCustomHiddenCss
fxy060608's avatar
fxy060608 已提交
78
          }
fxy060608's avatar
fxy060608 已提交
79
        }
fxy060608's avatar
fxy060608 已提交
80

fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87
        const nvueCssPaths = getNVueCssPaths(config)
        if (!nvueCssPaths || !nvueCssPaths.length) {
          return cssCode
        }
        const normalized = normalizePath(filename)
        if (nvueCssPaths.find((pageCssPath) => pageCssPath === normalized)) {
          debugNVueCss(normalized)
fxy060608's avatar
fxy060608 已提交
88 89 90 91
          return (
            `@import "${relativeFile(normalized, 'nvue' + extname)}";\n` +
            cssCode
          )
fxy060608's avatar
fxy060608 已提交
92 93 94
        }
        return cssCode
      },
fxy060608's avatar
fxy060608 已提交
95 96 97
    })
    injectAssetPlugin(config)
  }
fxy060608's avatar
fxy060608 已提交
98
}