Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MuGuiLin
uni-app
提交
b2105d80
U
uni-app
项目概览
MuGuiLin
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b2105d80
编写于
5月 08, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(ssr): support build
上级
e586001f
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
65 addition
and
12 deletion
+65
-12
packages/vite-plugin-uni/lib/ssr/entry-server.js
packages/vite-plugin-uni/lib/ssr/entry-server.js
+1
-0
packages/vite-plugin-uni/src/cli/build.ts
packages/vite-plugin-uni/src/cli/build.ts
+27
-1
packages/vite-plugin-uni/src/cli/index.ts
packages/vite-plugin-uni/src/cli/index.ts
+2
-2
packages/vite-plugin-uni/src/cli/server.ts
packages/vite-plugin-uni/src/cli/server.ts
+3
-2
packages/vite-plugin-uni/src/cli/utils.ts
packages/vite-plugin-uni/src/cli/utils.ts
+7
-0
packages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts
...ages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts
+1
-2
packages/vite-plugin-uni/src/configResolved/plugins/ssr.ts
packages/vite-plugin-uni/src/configResolved/plugins/ssr.ts
+22
-3
packages/vite-plugin-uni/src/utils/ssr.ts
packages/vite-plugin-uni/src/utils/ssr.ts
+2
-2
未找到文件。
packages/vite-plugin-uni/lib/ssr/
rend
er.js
→
packages/vite-plugin-uni/lib/ssr/
entry-serv
er.js
浏览文件 @
b2105d80
import
'
./main
'
import
{
createVueSSRAppInstance
}
from
'
vue
'
import
{
renderToString
}
from
'
@vue/server-renderer
'
import
{
...
...
packages/vite-plugin-uni/src/cli/build.ts
浏览文件 @
b2105d80
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
,
})
}
packages/vite-plugin-uni/src/cli/index.ts
浏览文件 @
b2105d80
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
))
...
...
packages/vite-plugin-uni/src/cli/server.ts
浏览文件 @
b2105d80
...
...
@@ -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
)
...
...
packages/vite-plugin-uni/src/cli/utils.ts
浏览文件 @
b2105d80
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
}
...
...
packages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts
浏览文件 @
b2105d80
...
...
@@ -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
'
)}
`
...
...
packages/vite-plugin-uni/src/configResolved/plugins/ssr.ts
浏览文件 @
b2105d80
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
))
{
...
...
packages/vite-plugin-uni/src/utils/ssr.ts
浏览文件 @
b2105d80
...
...
@@ -18,9 +18,9 @@ export function generateSSREnvCode(define: Record<string, any>): string {
.
replace
(
'
__DEFINES__
'
,
serializeDefine
(
define
))
}
export
function
generateSSR
Rend
erCode
()
{
export
function
generateSSR
EntryServ
erCode
()
{
return
fs
.
readFileSync
(
path
.
join
(
__dirname
,
'
../../lib/ssr/
rend
er.js
'
),
path
.
join
(
__dirname
,
'
../../lib/ssr/
entry-serv
er.js
'
),
'
utf8
'
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录