mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-05 05:27:59 +08:00
update bundle renderer bundle format
This commit is contained in:
parent
561447d278
commit
eb071980ff
@ -5,33 +5,35 @@ import { PassThrough } from 'stream'
|
||||
import type { Renderer, RenderOptions } from './create-renderer'
|
||||
|
||||
const INVALID_MSG =
|
||||
'Invalid server-rendering bundle format. Should be a string of bundled code ' +
|
||||
'or an Object of type { entry: string; chunks: { [filename: string]: string }}.'
|
||||
'Invalid server-rendering bundle format. Should be a string ' +
|
||||
'or a bundle Object of type:\n\n' +
|
||||
`{
|
||||
entry: string;
|
||||
files: { [filename: string]: string; };
|
||||
maps: { [filename: string]: string; };
|
||||
}\n`
|
||||
|
||||
// The render bundle can either be a string (single bundled file)
|
||||
// or an object containing a chunks hash of filename:code pairs with the
|
||||
// name of the entry file. The object format is used in conjunction with
|
||||
// Webpack's compilation output so that code-split chunks can also be loaded.
|
||||
// or a bundle manifest object generated by vue-ssr-webpack-plugin.
|
||||
type RenderBundle = string | {
|
||||
entry: string;
|
||||
chunks: {
|
||||
[filename: string]: string;
|
||||
};
|
||||
files: { [filename: string]: string; };
|
||||
maps: { [filename: string]: string; };
|
||||
};
|
||||
|
||||
export function createBundleRendererCreator (createRenderer: () => Renderer) {
|
||||
return (bundle: RenderBundle, rendererOptions?: RenderOptions) => {
|
||||
const renderer = createRenderer(rendererOptions)
|
||||
let chunks, entry
|
||||
let files, entry
|
||||
if (typeof bundle === 'object') {
|
||||
entry = bundle.entry
|
||||
chunks = bundle.chunks
|
||||
if (typeof entry !== 'string' || typeof chunks !== 'object') {
|
||||
files = bundle.files
|
||||
if (typeof entry !== 'string' || typeof files !== 'object') {
|
||||
throw new Error(INVALID_MSG)
|
||||
}
|
||||
} else if (typeof bundle === 'string') {
|
||||
entry = '__vue_ssr_bundle__'
|
||||
chunks = { '__vue_ssr_bundle__': bundle }
|
||||
files = { '__vue_ssr_bundle__': bundle }
|
||||
} else {
|
||||
throw new Error(INVALID_MSG)
|
||||
}
|
||||
@ -41,13 +43,13 @@ export function createBundleRendererCreator (createRenderer: () => Renderer) {
|
||||
cb = context
|
||||
context = {}
|
||||
}
|
||||
runInVm(entry, chunks, context).then(app => {
|
||||
runInVm(entry, files, context).then(app => {
|
||||
renderer.renderToString(app, cb)
|
||||
}).catch(cb)
|
||||
},
|
||||
renderToStream: (context?: Object) => {
|
||||
const res = new PassThrough()
|
||||
runInVm(entry, chunks, context).then(app => {
|
||||
runInVm(entry, files, context).then(app => {
|
||||
const renderStream = renderer.renderToStream(app)
|
||||
renderStream.on('error', err => {
|
||||
res.emit('error', err)
|
||||
|
@ -19,12 +19,12 @@ function createContext (context) {
|
||||
return sandbox
|
||||
}
|
||||
|
||||
function evaluateModule (filename, chunks, context, evaluatedModules) {
|
||||
function evaluateModule (filename, files, context, evaluatedModules) {
|
||||
if (evaluatedModules[filename]) {
|
||||
return evaluatedModules[filename]
|
||||
}
|
||||
|
||||
const code = chunks[filename]
|
||||
const code = files[filename]
|
||||
const wrapper = NativeModule.wrap(code)
|
||||
const compiledWrapper = vm.runInNewContext(wrapper, context, {
|
||||
filename,
|
||||
@ -33,8 +33,8 @@ function evaluateModule (filename, chunks, context, evaluatedModules) {
|
||||
const m = { exports: {}}
|
||||
const r = file => {
|
||||
file = path.join('.', file)
|
||||
if (chunks[file]) {
|
||||
return evaluateModule(file, chunks, context, evaluatedModules)
|
||||
if (files[file]) {
|
||||
return evaluateModule(file, files, context, evaluatedModules)
|
||||
} else {
|
||||
return require(file)
|
||||
}
|
||||
@ -48,10 +48,10 @@ function evaluateModule (filename, chunks, context, evaluatedModules) {
|
||||
return res
|
||||
}
|
||||
|
||||
export default function runInVm (entry, chunks, _context = {}) {
|
||||
export default function runInVm (entry, files, _context = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const context = createContext(_context)
|
||||
const res = evaluateModule(entry, chunks, context, {})
|
||||
const res = evaluateModule(entry, files, context, {})
|
||||
resolve(typeof res === 'function' ? res(_context) : res)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user