2018-10-30 20:42:53 +00:00
|
|
|
import path from 'path'
|
|
|
|
import crypto from 'crypto'
|
2018-12-01 10:13:28 +00:00
|
|
|
import fs from 'fs'
|
2018-10-30 20:42:53 +00:00
|
|
|
import consola from 'consola'
|
2018-10-31 15:52:35 +00:00
|
|
|
import devalue from '@nuxtjs/devalue'
|
2018-11-07 23:37:06 +00:00
|
|
|
import invert from 'lodash/invert'
|
2018-10-31 15:52:35 +00:00
|
|
|
import template from 'lodash/template'
|
2018-10-30 20:42:53 +00:00
|
|
|
import { waitFor } from '@nuxt/common'
|
2018-10-31 15:52:35 +00:00
|
|
|
import { createBundleRenderer } from 'vue-server-renderer'
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
import SPAMetaRenderer from './spa-meta'
|
|
|
|
|
|
|
|
export default class VueRenderer {
|
|
|
|
constructor(context) {
|
|
|
|
this.context = context
|
|
|
|
|
|
|
|
// Will be set by createRenderer
|
2018-10-31 15:52:35 +00:00
|
|
|
this.renderer = {
|
2018-12-01 10:13:28 +00:00
|
|
|
ssr: undefined,
|
|
|
|
modern: undefined,
|
|
|
|
spa: undefined
|
2018-10-31 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Renderer runtime resources
|
|
|
|
Object.assign(this.context.resources, {
|
2018-12-01 10:13:28 +00:00
|
|
|
clientManifest: undefined,
|
|
|
|
modernManifest: undefined,
|
|
|
|
serverManifest: undefined,
|
|
|
|
ssrTemplate: undefined,
|
|
|
|
spaTemplate: undefined,
|
|
|
|
errorTemplate: this.parseTemplate('Nuxt.js Internal Server Error')
|
2018-10-30 20:42:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
get assetsMapping() {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (this._assetsMapping) {
|
|
|
|
return this._assetsMapping
|
|
|
|
}
|
2018-11-07 23:37:06 +00:00
|
|
|
|
|
|
|
const legacyAssets = this.context.resources.clientManifest.assetsMapping
|
|
|
|
const modernAssets = invert(this.context.resources.modernManifest.assetsMapping)
|
|
|
|
const mapping = {}
|
|
|
|
for (const legacyJsFile in legacyAssets) {
|
|
|
|
const chunkNamesHash = legacyAssets[legacyJsFile]
|
|
|
|
mapping[legacyJsFile] = modernAssets[chunkNamesHash]
|
|
|
|
}
|
|
|
|
delete this.context.resources.clientManifest.assetsMapping
|
|
|
|
delete this.context.resources.modernManifest.assetsMapping
|
|
|
|
this._assetsMapping = mapping
|
|
|
|
return mapping
|
|
|
|
}
|
|
|
|
|
|
|
|
renderScripts(context) {
|
|
|
|
if (this.context.options.modern === 'client') {
|
|
|
|
const publicPath = this.context.options.build.publicPath
|
2018-11-26 12:09:30 +00:00
|
|
|
const scriptPattern = /<script[^>]*?src="([^"]*?)"[^>]*?>[^<]*?<\/script>/g
|
2018-11-07 23:37:06 +00:00
|
|
|
return context.renderScripts().replace(scriptPattern, (scriptTag, jsFile) => {
|
|
|
|
const legacyJsFile = jsFile.replace(publicPath, '')
|
|
|
|
const modernJsFile = this.assetsMapping[legacyJsFile]
|
2018-12-05 16:21:58 +00:00
|
|
|
const crossorigin = this.context.options.build.crossorigin
|
|
|
|
const cors = `${crossorigin ? ` crossorigin="${crossorigin}"` : ''}`
|
|
|
|
const moduleTag = scriptTag.replace('<script', `<script type="module"${cors}`).replace(legacyJsFile, modernJsFile)
|
|
|
|
const noModuleTag = scriptTag.replace('<script', `<script nomodule${cors}`)
|
2018-11-07 23:37:06 +00:00
|
|
|
return noModuleTag + moduleTag
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return context.renderScripts()
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:09:30 +00:00
|
|
|
getModernFiles(legacyFiles = []) {
|
|
|
|
const modernFiles = []
|
|
|
|
for (const legacyJsFile of legacyFiles) {
|
2018-12-05 16:21:58 +00:00
|
|
|
const modernFile = { ...legacyJsFile, modern: true }
|
2018-11-26 12:09:30 +00:00
|
|
|
if (modernFile.asType === 'script') {
|
|
|
|
const file = this.assetsMapping[legacyJsFile.file]
|
|
|
|
modernFile.file = file
|
|
|
|
modernFile.fileWithoutQuery = file.replace(/\?.*/, '')
|
|
|
|
}
|
|
|
|
modernFiles.push(modernFile)
|
|
|
|
}
|
|
|
|
return modernFiles
|
|
|
|
}
|
|
|
|
|
|
|
|
getPreloadFiles(context) {
|
|
|
|
const preloadFiles = context.getPreloadFiles()
|
|
|
|
const modernMode = this.context.options.modern
|
|
|
|
// In eligible server modern mode, preloadFiles are modern bundles from modern renderer
|
|
|
|
return modernMode === 'client' ? this.getModernFiles(preloadFiles) : preloadFiles
|
|
|
|
}
|
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
renderResourceHints(context) {
|
|
|
|
if (this.context.options.modern === 'client') {
|
2018-11-26 12:09:30 +00:00
|
|
|
const publicPath = this.context.options.build.publicPath
|
|
|
|
const linkPattern = /<link[^>]*?href="([^"]*?)"[^>]*?as="script"[^>]*?>/g
|
|
|
|
return context.renderResourceHints().replace(linkPattern, (linkTag, jsFile) => {
|
|
|
|
const legacyJsFile = jsFile.replace(publicPath, '')
|
|
|
|
const modernJsFile = this.assetsMapping[legacyJsFile]
|
2018-12-05 16:21:58 +00:00
|
|
|
const crossorigin = this.context.options.build.crossorigin
|
|
|
|
const cors = `${crossorigin ? ` crossorigin="${crossorigin}"` : ''}`
|
|
|
|
return linkTag.replace('rel="preload"', `rel="modulepreload"${cors}`).replace(legacyJsFile, modernJsFile)
|
2018-11-26 12:09:30 +00:00
|
|
|
})
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|
|
|
|
return context.renderResourceHints()
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
async ready() {
|
2018-12-10 12:16:05 +00:00
|
|
|
// -- Development mode --
|
|
|
|
|
|
|
|
if (this.context.options.dev) {
|
2018-12-01 10:13:28 +00:00
|
|
|
this.context.nuxt.hook('build:resources', mfs => this.loadResources(mfs, true))
|
2018-12-10 12:16:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Production mode --
|
|
|
|
|
|
|
|
// Try once to load SSR resources from fs
|
|
|
|
await this.loadResources(fs)
|
|
|
|
|
|
|
|
// Without using`nuxt start` (Programatic, Tests and Generate)
|
|
|
|
if (!this.context.options._start) {
|
|
|
|
this.context.nuxt.hook('build:resources', () => this.loadResources(fs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify resources
|
|
|
|
if (this.context.options._start) {
|
|
|
|
if (!this.isReady) {
|
|
|
|
throw new Error(
|
|
|
|
'No build files found. Use either `nuxt build` or `builder.build()` or start nuxt in development mode.'
|
|
|
|
)
|
|
|
|
} else if (this.context.options.modern && !this.context.resources.modernManifest) {
|
|
|
|
throw new Error(
|
|
|
|
'No modern build files found. Use either `nuxt build --modern` or `modern` option to build modern files.'
|
|
|
|
)
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
loadResources(_fs, isMFS = false) {
|
2018-10-30 20:42:53 +00:00
|
|
|
const distPath = path.resolve(this.context.options.buildDir, 'dist', 'server')
|
|
|
|
const updated = []
|
2018-12-01 10:13:28 +00:00
|
|
|
const resourceMap = this.resourceMap
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
const readResource = (fileName, encoding) => {
|
|
|
|
try {
|
|
|
|
const fullPath = path.resolve(distPath, fileName)
|
|
|
|
if (!_fs.existsSync(fullPath)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const contents = _fs.readFileSync(fullPath, encoding)
|
|
|
|
if (isMFS) {
|
|
|
|
// Cleanup MFS as soon as possible to save memory
|
|
|
|
_fs.unlinkSync(fullPath)
|
|
|
|
}
|
|
|
|
return contents
|
|
|
|
} catch (err) {
|
|
|
|
consola.error('Unable to load resource:', fileName, err)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
}
|
2018-11-08 09:15:56 +00:00
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
for (const resourceName in resourceMap) {
|
|
|
|
const { fileName, transform, encoding } = resourceMap[resourceName]
|
|
|
|
|
|
|
|
// Load resource
|
|
|
|
let resource = readResource(fileName, encoding)
|
|
|
|
|
|
|
|
// Skip unavailable resources
|
|
|
|
if (!resource) {
|
|
|
|
consola.debug('Resource not available:', resourceName)
|
|
|
|
continue
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Apply transforms
|
|
|
|
if (typeof transform === 'function') {
|
|
|
|
resource = transform(resource, {
|
|
|
|
readResource,
|
|
|
|
oldValue: this.context.resources[resourceName]
|
|
|
|
})
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Update resource
|
|
|
|
this.context.resources[resourceName] = resource
|
|
|
|
updated.push(resourceName)
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
// Reload error template
|
|
|
|
const errorTemplatePath = path.resolve(this.context.options.buildDir, 'views/error.html')
|
|
|
|
if (fs.existsSync(errorTemplatePath)) {
|
2018-12-01 10:13:28 +00:00
|
|
|
this.context.resources.errorTemplate = this.parseTemplate(
|
2018-10-30 20:42:53 +00:00
|
|
|
fs.readFileSync(errorTemplatePath, 'utf8')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Reload loading template
|
2018-10-30 20:42:53 +00:00
|
|
|
const loadingHTMLPath = path.resolve(this.context.options.buildDir, 'loading.html')
|
|
|
|
if (fs.existsSync(loadingHTMLPath)) {
|
|
|
|
this.context.resources.loadingHTML = fs.readFileSync(loadingHTMLPath, 'utf8')
|
|
|
|
this.context.resources.loadingHTML = this.context.resources.loadingHTML
|
|
|
|
.replace(/\r|\n|[\t\s]{3,}/g, '')
|
|
|
|
} else {
|
|
|
|
this.context.resources.loadingHTML = ''
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Call createRenderer if any resource changed
|
2018-10-30 20:42:53 +00:00
|
|
|
if (updated.length > 0) {
|
|
|
|
this.createRenderer()
|
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Call resourcesLoaded hook
|
|
|
|
consola.debug('Resources loaded:', updated.join(','))
|
|
|
|
return this.context.nuxt.callHook('render:resourcesLoaded', this.context.resources)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
get noSSR() { /* Backward compatibility */
|
2018-10-30 20:42:53 +00:00
|
|
|
return this.context.options.render.ssr === false
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
get SSR() {
|
|
|
|
return this.context.options.render.ssr === true
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
get isReady() {
|
|
|
|
// SPA
|
|
|
|
if (!this.context.resources.spaTemplate || !this.renderer.spa) {
|
2018-10-30 20:42:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// SSR
|
|
|
|
if (this.SSR && (!this.context.resources.ssrTemplate || !this.renderer.ssr)) {
|
|
|
|
return false
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
get isResourcesAvailable() { /* Backward compatibility */
|
|
|
|
return this.isReady
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createRenderer() {
|
2018-12-01 10:13:28 +00:00
|
|
|
// Resource clientManifest is always required
|
|
|
|
if (!this.context.resources.clientManifest) {
|
2018-10-30 20:42:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Create SPA renderer
|
|
|
|
if (this.context.resources.spaTemplate) {
|
|
|
|
this.renderer.spa = new SPAMetaRenderer(this)
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Skip the rest if SSR resources are not available
|
|
|
|
if (!this.context.resources.ssrTemplate || !this.context.resources.serverManifest) {
|
2018-10-30 20:42:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasModules = fs.existsSync(path.resolve(this.context.options.rootDir, 'node_modules'))
|
2018-12-01 10:13:28 +00:00
|
|
|
|
2018-10-31 15:52:35 +00:00
|
|
|
const rendererOptions = {
|
|
|
|
clientManifest: this.context.resources.clientManifest,
|
|
|
|
// for globally installed nuxt command, search dependencies in global dir
|
|
|
|
basedir: hasModules ? this.context.options.rootDir : __dirname,
|
|
|
|
...this.context.options.render.bundleRenderer
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Create bundle renderer for SSR
|
2018-10-31 15:52:35 +00:00
|
|
|
this.renderer.ssr = createBundleRenderer(
|
2018-12-01 10:13:28 +00:00
|
|
|
this.context.resources.serverManifest,
|
2018-10-31 15:52:35 +00:00
|
|
|
rendererOptions
|
|
|
|
)
|
|
|
|
|
2018-11-26 22:49:47 +00:00
|
|
|
if (this.context.resources.modernManifest &&
|
|
|
|
!['client', false].includes(this.context.options.modern)) {
|
2018-10-31 15:52:35 +00:00
|
|
|
this.renderer.modern = createBundleRenderer(
|
2018-12-01 10:13:28 +00:00
|
|
|
this.context.resources.serverManifest,
|
2018-10-30 20:42:53 +00:00
|
|
|
{
|
2018-10-31 15:52:35 +00:00
|
|
|
...rendererOptions,
|
|
|
|
clientManifest: this.context.resources.modernManifest
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
)
|
2018-10-31 15:52:35 +00:00
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(ssr, opts) {
|
|
|
|
// Fix problem with HTMLPlugin's minify option (#3392)
|
|
|
|
opts.html_attrs = opts.HTML_ATTRS
|
2018-12-14 14:06:27 +00:00
|
|
|
opts.head_attrs = opts.HEAD_ATTRS
|
2018-10-30 20:42:53 +00:00
|
|
|
opts.body_attrs = opts.BODY_ATTRS
|
|
|
|
|
|
|
|
const fn = ssr ? this.context.resources.ssrTemplate : this.context.resources.spaTemplate
|
|
|
|
|
|
|
|
return fn(opts)
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:16:05 +00:00
|
|
|
async renderRoute(url, context = {}, retries = 5) {
|
2018-10-30 20:42:53 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (!this.isReady) {
|
2018-12-10 12:16:05 +00:00
|
|
|
if (this.context.options.dev && retries > 0) {
|
|
|
|
consola.info('Waiting for server resources...')
|
|
|
|
await waitFor(1000)
|
|
|
|
return this.renderRoute(url, context, retries - 1)
|
|
|
|
} else {
|
|
|
|
throw new Error('Server resources are not available!')
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log rendered url
|
|
|
|
consola.debug(`Rendering url ${url}`)
|
|
|
|
|
|
|
|
// Add url and isSever to the context
|
|
|
|
context.url = url
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Basic response if SSR is disabled or SPA data provided
|
2018-11-07 23:37:06 +00:00
|
|
|
const { req, res } = context
|
|
|
|
const spa = context.spa || (res && res.spa)
|
2018-10-30 20:42:53 +00:00
|
|
|
const ENV = this.context.options.env
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
if (!this.SSR || spa) {
|
2018-10-30 20:42:53 +00:00
|
|
|
const {
|
|
|
|
HTML_ATTRS,
|
2018-12-14 14:06:27 +00:00
|
|
|
HEAD_ATTRS,
|
2018-10-30 20:42:53 +00:00
|
|
|
BODY_ATTRS,
|
|
|
|
HEAD,
|
|
|
|
BODY_SCRIPTS,
|
|
|
|
getPreloadFiles
|
2018-10-31 15:52:35 +00:00
|
|
|
} = await this.renderer.spa.render(context)
|
2018-10-30 20:42:53 +00:00
|
|
|
const APP =
|
|
|
|
`<div id="${this.context.globals.id}">${this.context.resources.loadingHTML}</div>` + BODY_SCRIPTS
|
|
|
|
|
|
|
|
const html = this.renderTemplate(false, {
|
|
|
|
HTML_ATTRS,
|
2018-12-14 14:06:27 +00:00
|
|
|
HEAD_ATTRS,
|
2018-10-30 20:42:53 +00:00
|
|
|
BODY_ATTRS,
|
|
|
|
HEAD,
|
|
|
|
APP,
|
|
|
|
ENV
|
|
|
|
})
|
|
|
|
|
2018-11-26 12:09:30 +00:00
|
|
|
return { html, getPreloadFiles: this.getPreloadFiles.bind(this, { getPreloadFiles }) }
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 15:52:35 +00:00
|
|
|
let APP
|
2018-10-30 20:42:53 +00:00
|
|
|
// Call renderToString from the bundleRenderer and generate the HTML (will update the context as well)
|
2018-11-07 23:48:22 +00:00
|
|
|
if (req && req.modernMode) {
|
2018-10-31 15:52:35 +00:00
|
|
|
APP = await this.renderer.modern.renderToString(context)
|
|
|
|
} else {
|
|
|
|
APP = await this.renderer.ssr.renderToString(context)
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
if (!context.nuxt.serverRendered) {
|
|
|
|
APP = `<div id="${this.context.globals.id}"></div>`
|
|
|
|
}
|
|
|
|
const m = context.meta.inject()
|
|
|
|
let HEAD =
|
|
|
|
m.title.text() +
|
|
|
|
m.meta.text() +
|
|
|
|
m.link.text() +
|
|
|
|
m.style.text() +
|
|
|
|
m.script.text() +
|
|
|
|
m.noscript.text()
|
|
|
|
if (this.context.options._routerBaseSpecified) {
|
|
|
|
HEAD += `<base href="${this.context.options.router.base}">`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.context.options.render.resourceHints) {
|
2018-11-07 23:37:06 +00:00
|
|
|
HEAD += this.renderResourceHints(context)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.context.nuxt.callHook('render:routeContext', context.nuxt)
|
|
|
|
|
|
|
|
const serializedSession = `window.${this.context.globals.context}=${devalue(context.nuxt)};`
|
|
|
|
|
2018-12-12 06:29:28 +00:00
|
|
|
const cspScriptSrcHashes = []
|
2018-10-30 20:42:53 +00:00
|
|
|
if (this.context.options.render.csp) {
|
|
|
|
const { hashAlgorithm } = this.context.options.render.csp
|
|
|
|
const hash = crypto.createHash(hashAlgorithm)
|
|
|
|
hash.update(serializedSession)
|
2018-12-12 06:29:28 +00:00
|
|
|
cspScriptSrcHashes.push(`'${hashAlgorithm}-${hash.digest('base64')}'`)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
APP += `<script>${serializedSession}</script>`
|
2018-11-07 23:37:06 +00:00
|
|
|
APP += this.renderScripts(context)
|
2018-10-30 20:42:53 +00:00
|
|
|
APP += m.script.text({ body: true })
|
|
|
|
APP += m.noscript.text({ body: true })
|
|
|
|
|
|
|
|
HEAD += context.renderStyles()
|
|
|
|
|
|
|
|
const html = this.renderTemplate(true, {
|
|
|
|
HTML_ATTRS: 'data-n-head-ssr ' + m.htmlAttrs.text(),
|
2018-12-14 14:06:27 +00:00
|
|
|
HEAD_ATTRS: m.headAttrs.text(),
|
2018-10-30 20:42:53 +00:00
|
|
|
BODY_ATTRS: m.bodyAttrs.text(),
|
|
|
|
HEAD,
|
|
|
|
APP,
|
|
|
|
ENV
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
html,
|
2018-12-12 06:29:28 +00:00
|
|
|
cspScriptSrcHashes,
|
2018-11-26 12:09:30 +00:00
|
|
|
getPreloadFiles: this.getPreloadFiles.bind(this, context),
|
2018-10-30 20:42:53 +00:00
|
|
|
error: context.nuxt.error,
|
|
|
|
redirected: context.redirected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
get resourceMap() {
|
|
|
|
return {
|
|
|
|
clientManifest: {
|
|
|
|
fileName: 'client.manifest.json',
|
|
|
|
transform: src => JSON.parse(src)
|
2018-10-30 20:42:53 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
modernManifest: {
|
|
|
|
fileName: 'modern.manifest.json',
|
|
|
|
transform: src => JSON.parse(src)
|
2018-10-31 15:52:35 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
serverManifest: {
|
|
|
|
fileName: 'server.manifest.json',
|
|
|
|
// BundleRenderer needs resolved contents
|
|
|
|
transform: (src, { readResource, oldValue = { files: {}, maps: {} } }) => {
|
|
|
|
const serverManifest = JSON.parse(src)
|
|
|
|
|
2018-12-01 10:51:10 +00:00
|
|
|
const resolveAssets = (obj, oldObj) => {
|
2018-12-01 10:13:28 +00:00
|
|
|
Object.keys(obj).forEach((name) => {
|
2018-12-01 10:41:10 +00:00
|
|
|
obj[name] = readResource(obj[name])
|
2018-12-01 10:13:28 +00:00
|
|
|
// Try to reuse deleted MFS files if no new version exists
|
|
|
|
if (!obj[name]) {
|
|
|
|
obj[name] = oldObj[name]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = resolveAssets(serverManifest.files, oldValue.files)
|
2018-12-01 10:51:10 +00:00
|
|
|
const maps = resolveAssets(serverManifest.maps, oldValue.maps)
|
|
|
|
|
|
|
|
// Try to parse sourcemaps
|
|
|
|
for (const map in maps) {
|
|
|
|
if (maps[map] && maps[map].version) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
maps[map] = JSON.parse(maps[map])
|
|
|
|
} catch (e) {
|
|
|
|
maps[map] = { version: 3, sources: [], mappings: '' }
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...serverManifest,
|
|
|
|
files,
|
|
|
|
maps
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
ssrTemplate: {
|
2018-10-30 20:42:53 +00:00
|
|
|
fileName: 'index.ssr.html',
|
2018-12-01 10:13:28 +00:00
|
|
|
transform: src => this.parseTemplate(src)
|
2018-10-30 20:42:53 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
spaTemplate: {
|
2018-10-30 20:42:53 +00:00
|
|
|
fileName: 'index.spa.html',
|
2018-12-01 10:13:28 +00:00
|
|
|
transform: src => this.parseTemplate(src)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseTemplate(templateStr) {
|
|
|
|
return template(templateStr, {
|
|
|
|
interpolate: /{{([\s\S]+?)}}/g
|
|
|
|
})
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
|
|
|
close() {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (this.__closed) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
this.__closed = true
|
|
|
|
|
|
|
|
for (const key in this.renderer) {
|
|
|
|
delete this.renderer[key]
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|