emosheeep / vite-plugin-virtual-mpa

Out-of-box MPA plugin for Vite, generate multiple entries using only one template.
https://stackblitz.com/~/github.com/emosheeep/vite-plugin-virtual-mpa
MIT License
116 stars 15 forks source link

expose the option MpaOptions from the package #4

Closed ccbeango closed 1 year ago

ccbeango commented 1 year ago

1.1.0中有这个类型,但是在1.2.1中没有导出,还请暴露此类型

emosheeep commented 1 year ago

想问一下是有什么特殊的用途吗hhh

ccbeango commented 1 year ago

插件单独写一个函数中,动态生成入口页配置,会用到。

import { createMpaPlugin } from 'vite-plugin-virtual-mpa'
import type { MpaOptions } from 'vite-plugin-virtual-mpa'
import fs from 'fs'
import pkg from '../../../package.json'
import { GLOB_CONFIG_FILE_NAME } from '../../constant'

type PageOption = MpaOptions<string, string, string, string>['pages']

export default function configMpaPlugin(
  indexAppName: string,
  appTitle: string,
  publicPath = '',
  isBuild: boolean
) {
  const getAppConfigSrc = () =>
    `${publicPath || '/'}${GLOB_CONFIG_FILE_NAME}?v=${
      pkg.version
    }-${new Date().getTime()}`

  const entrys = fs.readdirSync('src/apps')

  const pages = entrys.reduce<PageOption>((res, pageName) => {
    res.push({
      name: pageName,
      entry: `/src/apps/${pageName}/main.ts`,
      filename:
        pageName === indexAppName
          ? `${pageName}.html`
          : `pages/${pageName}.html`,
      data: {
        title: appTitle,
        injectScript:
          `
            <script type="module" src="/test.js"></script>
          ` + (isBuild ? `<script src="${getAppConfigSrc()}"></script>` : '')
      }
    })

    return res
  }, [])

  return createMpaPlugin({
    verbose: false,
    template: 'public/index.html',
    pages
    // rewrites: [
    //   {
    //     from: new RegExp(normalizePath(`/(index|hello)`)),
    //     to: ctx => {
    //       return normalizePath(`/pages/${ctx.match[1]}.html`)
    //     }
    //   }
    // ]
  })
}
emosheeep commented 1 year ago

已发布1.2.2,可以试下