mainJs.ts 2.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import path from 'path'
import slash from 'slash'
fxy060608's avatar
fxy060608 已提交
3
import { Plugin, ResolvedConfig } from 'vite'
fxy060608's avatar
fxy060608 已提交
4 5
import { VitePluginUniResolvedOptions } from '../..'

fxy060608's avatar
fxy060608 已提交
6 7 8 9
export function uniMainJsPlugin(
  config: ResolvedConfig,
  options: VitePluginUniResolvedOptions
): Plugin {
fxy060608's avatar
fxy060608 已提交
10 11 12 13
  const mainPath = slash(path.resolve(options.inputDir, 'main'))
  const mainJsPath = mainPath + '.js'
  const mainTsPath = mainPath + '.ts'
  const pagesJsonJsPath = slash(path.resolve(options.inputDir, 'pages.json.js'))
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19
  const isSSR =
    config.command === 'serve'
      ? !!config.server.middlewareMode
      : config.command === 'build'
      ? !!(config.build.ssr || config.build.ssrManifest)
      : false
fxy060608's avatar
fxy060608 已提交
20 21
  return {
    name: 'vite:uni-main-js',
fxy060608's avatar
fxy060608 已提交
22
    transform(code, id, ssr) {
fxy060608's avatar
fxy060608 已提交
23
      if (id === mainJsPath || id === mainTsPath) {
fxy060608's avatar
fxy060608 已提交
24 25
        if (!isSSR) {
          code = createApp(code)
fxy060608's avatar
fxy060608 已提交
26
        } else {
fxy060608's avatar
fxy060608 已提交
27
          code = ssr ? createSSRServerApp(code) : createSSRClientApp(code)
fxy060608's avatar
fxy060608 已提交
28
        }
fxy060608's avatar
fxy060608 已提交
29
        return {
fxy060608's avatar
fxy060608 已提交
30
          code: `import { plugin } from '@dcloudio/uni-h5';import '${pagesJsonJsPath}';${code}`,
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36
          map: this.getCombinedSourcemap(),
        }
      }
    },
  }
}
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45

function createApp(code: string) {
  return `function createApp(rootComponent,rootProps){return createVueApp(rootComponent, rootProps).use(plugin)};${code.replace(
    'createApp',
    'createVueApp'
  )}`
}

function createSSRClientApp(code: string) {
fxy060608's avatar
fxy060608 已提交
46
  return `import { UNI_SSR, UNI_SSR_STORE } from '@dcloudio/uni-shared';function createApp(rootComponent, rootProps) {const app = createSSRApp(rootComponent, rootProps).use(plugin);const oldMount = app.mount;app.mount = (selector) => {window[UNI_SSR] && window[UNI_SSR][UNI_SSR_STORE] && app.config.globalProperties.$store.replaceState(window[UNI_SSR][UNI_SSR_STORE]);app.router.isReady().then(() => oldMount.call(app, selector));};return app;};${code.replace(
fxy060608's avatar
fxy060608 已提交
47
    'createApp',
fxy060608's avatar
fxy060608 已提交
48
    'createSSRApp'
fxy060608's avatar
fxy060608 已提交
49 50 51 52
  )}`
}

function createSSRServerApp(code: string) {
fxy060608's avatar
fxy060608 已提交
53
  return `function createApp(App) {return createVueSSRApp(App).use(plugin)};${code.replace(
fxy060608's avatar
fxy060608 已提交
54 55 56 57
    'createApp',
    'createVueSSRApp'
  )}`
}