subpackage.ts 1.2 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
import { OutputAsset, OutputChunk } from 'rollup'
import type { Plugin } from 'vite'
import { isPageFile, relativeFile } from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '../plugin'

export function uniSubpackagePlugin({
  style: { extname },
}: UniMiniProgramPluginOptions): Plugin {
  return {
    name: 'vite:uni-mp-subpackage',
    enforce: 'post',
    generateBundle(_, bundle) {
      ;['project.config.json', 'app.json'].forEach((name) => {
        delete bundle[name]
      })
      const appJsFile = 'app.js'
      const appCssFile = 'app' + extname
      Object.keys(bundle).forEach((name) => {
        if (!isPageFile(name)) {
          return
        }
        // 仅页面级 wxss 需要补充 app.wxss
        if (name.endsWith(extname)) {
          const cssFile = bundle[name] as OutputAsset
          cssFile.source =
            `@import "${relativeFile(name, appCssFile)}";\n` +
            cssFile.source.toString()
        } else if (name.endsWith('.js')) {
          const jsFile = bundle[name] as OutputChunk
          jsFile.code =
            `require('${relativeFile(name, appJsFile)}');\n` + jsFile.code
        }
      })
    },
  }
}