Skip to content

Commit

Permalink
fix(ssr): normalize manifest filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinando-ferreira committed May 9, 2021
1 parent d555dd6 commit e96ad05
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/plugin-vue-jsx/index.js
Expand Up @@ -4,6 +4,8 @@ const jsx = require('@vue/babel-plugin-jsx')
const importMeta = require('@babel/plugin-syntax-import-meta')
const { createFilter } = require('@rollup/pluginutils')
const hash = require('hash-sum')
const path = require('path')
const { createHash } = require('crypto')

const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper'
const ssrRegisterHelperCode =
Expand Down Expand Up @@ -39,6 +41,7 @@ function ssrRegisterHelper(comp, filename) {
* @returns {import('vite').Plugin}
*/
function vueJsxPlugin(options = {}) {
let root = ''
let needHmr = false
let needSourceMap = true

Expand All @@ -63,6 +66,7 @@ function vueJsxPlugin(options = {}) {
configResolved(config) {
needHmr = config.command === 'serve' && !config.isProduction
needSourceMap = config.command === 'serve' || !!config.build.sourcemap
root = config.root
},

resolveId(id) {
Expand Down Expand Up @@ -217,9 +221,13 @@ function vueJsxPlugin(options = {}) {
}

if (ssr) {
const normalizedId = path.relative(root, id)
const hashedId = createHash('sha256')
.update(normalizedId)
.digest('hex')
let ssrInjectCode =
`\nimport { ssrRegisterHelper } from "${ssrRegisterHelperId}"` +
`\nconst __moduleId = ${JSON.stringify(id)}`
`\nconst __moduleId = ${JSON.stringify(hashedId)}`
for (const { local } of hotComponents) {
ssrInjectCode += `\nssrRegisterHelper(${local}, __moduleId)`
}
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-vue/src/main.ts
@@ -1,5 +1,6 @@
import qs from 'querystring'
import path from 'path'
import { createHash } from 'crypto'
import { rewriteDefault, SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import { ResolvedOptions } from '.'
import {
Expand Down Expand Up @@ -143,13 +144,17 @@ export async function transformMain(

// SSR module registration by wrapping user setup
if (ssr) {
const normalizedFilename = path.relative(options.root, filename)
const hashedFilename = createHash('sha256')
.update(normalizedFilename)
.digest('hex')
output.push(
`import { useSSRContext as __vite_useSSRContext } from 'vue'`,
`const _sfc_setup = _sfc_main.setup`,
`_sfc_main.setup = (props, ctx) => {`,
` const ssrContext = __vite_useSSRContext()`,
` ;(ssrContext.modules || (ssrContext.modules = new Set())).add(${JSON.stringify(
filename
hashedFilename
)})`,
` return _sfc_setup ? _sfc_setup(props, ctx) : undefined`,
`}`
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/ssr/ssrManifestPlugin.ts
@@ -1,3 +1,5 @@
import { relative } from 'path'
import { createHash } from 'crypto'
import { ResolvedConfig } from '..'
import { Plugin } from '../plugin'
import { chunkToEmittedCssFileMap } from '../plugins/css'
Expand All @@ -18,7 +20,12 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
// TODO: also include non-CSS assets
const cssFiles = chunkToEmittedCssFileMap.get(chunk)
for (const id in chunk.modules) {
const mappedChunks = ssrManifest[id] || (ssrManifest[id] = [])
const normalizedId = relative(config.root, id)
const hashedId = createHash('sha256')
.update(normalizedId)
.digest('hex')
const mappedChunks =
ssrManifest[hashedId] || (ssrManifest[hashedId] = [])
mappedChunks.push(base + chunk.fileName)
if (cssFiles) {
cssFiles.forEach((file) => {
Expand Down

0 comments on commit e96ad05

Please sign in to comment.