提交 b2105d80 编写于 作者: fxy060608's avatar fxy060608

feat(ssr): support build

上级 e586001f
import './main'
import { createVueSSRAppInstance } from 'vue'
import { renderToString } from '@vue/server-renderer'
import {
......
import path from 'path'
import { build as buildByVite, BuildOptions } from 'vite'
import { CliOptions } from '.'
import { cleanOptions } from './utils'
......@@ -11,4 +12,29 @@ export async function build(options: CliOptions) {
})
}
export async function buildSSR(options: CliOptions) {}
export async function buildSSR(options: CliOptions) {
const outputDir = process.env.UNI_OUTPUT_DIR
process.env.UNI_OUTPUT_DIR = path.resolve(outputDir, 'client')
const ssrBuildClientOptions: BuildOptions = cleanOptions(options)
ssrBuildClientOptions.ssrManifest = true
ssrBuildClientOptions.outDir = process.env.UNI_OUTPUT_DIR
await buildByVite({
root: process.env.VITE_ROOT_DIR,
logLevel: options.logLevel,
clearScreen: options.clearScreen,
build: ssrBuildClientOptions,
})
process.env.UNI_OUTPUT_DIR = path.resolve(outputDir, 'server')
const ssrBuildServerOptions: BuildOptions = cleanOptions(options)
ssrBuildServerOptions.ssr = path.resolve(
process.env.UNI_INPUT_DIR,
'entry-server.js'
)
ssrBuildServerOptions.outDir = process.env.UNI_OUTPUT_DIR
await buildByVite({
root: process.env.VITE_ROOT_DIR,
logLevel: options.logLevel,
clearScreen: options.clearScreen,
build: ssrBuildServerOptions,
})
}
import { cac } from 'cac'
import chalk from 'chalk'
import { LogLevel, createLogger, ServerOptions } from 'vite'
import { LogLevel, createLogger, ServerOptions, BuildOptions } from 'vite'
import { build, buildSSR } from './build'
import { createServer, createSSRServer } from './server'
......@@ -85,7 +85,7 @@ cli
)
.option('-m, --mode <mode>', `[string] set env mode`)
.option('-w, --watch', `[boolean] rebuilds when modules have changed on disk`)
.action(async (options: CliOptions) => {
.action(async (options: CliOptions & BuildOptions) => {
initEnv(options)
try {
await (options.ssr ? buildSSR(options) : build(options))
......
......@@ -9,7 +9,6 @@ import {
} from 'vite'
import express from 'express'
import { hasOwn } from '@vue/shared'
import { resolveMainPathOnce } from '@dcloudio/uni-cli-shared'
import { CliOptions } from '.'
import { cleanOptions } from './utils'
......@@ -56,7 +55,9 @@ export async function createSSRServer(options: CliOptions & ServerOptions) {
)
)
const render = (
await vite.ssrLoadModule(resolveMainPathOnce(process.env.UNI_INPUT_DIR))
await vite.ssrLoadModule(
path.resolve(process.env.UNI_INPUT_DIR, 'entry-server.js')
)
).render
const [appHtml, preloadLinks, appContext] = await render(url)
......
import path from 'path'
import { BuildOptions } from 'vite'
import { CliOptions } from '.'
export const PLATFORMS = [
......@@ -15,9 +16,15 @@ export const PLATFORMS = [
export function initEnv(options: CliOptions) {
process.env.VITE_ROOT_DIR = process.env.UNI_INPUT_DIR || process.cwd()
process.env.UNI_INPUT_DIR =
process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), 'src')
process.env.UNI_OUTPUT_DIR =
(options as BuildOptions).outDir ||
process.env.UNI_OUTPUT_DIR ||
path.resolve(process.cwd(), 'dist')
process.env.UNI_PLATFORM = options.platform as UniApp.PLATFORM
}
......
......@@ -2,7 +2,6 @@ import path from 'path'
import slash from 'slash'
import { Plugin, ResolvedConfig } from 'vite'
import { VitePluginUniResolvedOptions } from '../..'
import { generateSSRRenderCode } from '../../utils'
export function uniMainJsPlugin(
config: ResolvedConfig,
......@@ -46,7 +45,7 @@ function createSSRClientApp(code: string) {
}
function createSSRServerApp(code: string) {
return `function createApp(App) {return createVueSSRApp(App).use(plugin)};${generateSSRRenderCode()};${code.replace(
return `function createApp(App) {return createVueSSRApp(App).use(plugin)};${code.replace(
'createApp',
'createVueSSRApp'
)}`
......
import path from 'path'
import debug from 'debug'
import crypto from 'crypto'
import { Plugin } from 'vite'
import { walk } from 'estree-walker'
import { CallExpression } from 'estree'
import { createFilter } from '@rollup/pluginutils'
import { UniPluginFilterOptions } from '.'
import { isIdentifier, isCallExpression, isMemberExpression } from '../../utils'
import { MagicString } from '@vue/compiler-sfc'
import { UniPluginFilterOptions } from '.'
import {
isIdentifier,
isCallExpression,
isMemberExpression,
generateSSREntryServerCode,
} from '../../utils'
const debugSSR = debug('vite:uni:ssr')
const KEYED_FUNC_RE = /(ssrRef|shallowSsrRef)/
const ENTRY_SERVER_JS = 'entry-server.js'
export function uniSSRPlugin(options: UniPluginFilterOptions): Plugin {
const filter = createFilter(options.include, options.exclude)
const entryServerJs = path.join(options.inputDir, ENTRY_SERVER_JS)
const entryServerJsCode = generateSSREntryServerCode()
return {
name: 'vite:uni-ssr',
resolveId(id) {
if (id.endsWith(ENTRY_SERVER_JS)) {
return entryServerJs
}
},
load(id) {
if (id.endsWith(ENTRY_SERVER_JS)) {
return entryServerJsCode
}
},
transform(code, id) {
if (!filter(id)) return null
if (!KEYED_FUNC_RE.test(code)) {
......
......@@ -18,9 +18,9 @@ export function generateSSREnvCode(define: Record<string, any>): string {
.replace('__DEFINES__', serializeDefine(define))
}
export function generateSSRRenderCode() {
export function generateSSREntryServerCode() {
return fs.readFileSync(
path.join(__dirname, '../../lib/ssr/render.js'),
path.join(__dirname, '../../lib/ssr/entry-server.js'),
'utf8'
)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册