From bdcaba676e3e5112410ee4093d604b50beda640a Mon Sep 17 00:00:00 2001 From: Fernando Ferreira Date: Mon, 7 Jun 2021 15:40:04 +0100 Subject: [PATCH 1/2] fix(ssr): normalize manifest filenames Fixes #3303 --- packages/plugin-vue-jsx/index.js | 6 +++++- packages/plugin-vue/src/main.ts | 3 ++- packages/vite/src/node/ssr/ssrManifestPlugin.ts | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/plugin-vue-jsx/index.js b/packages/plugin-vue-jsx/index.js index 8e7898a58fdc8a..1a605b41ac00e8 100644 --- a/packages/plugin-vue-jsx/index.js +++ b/packages/plugin-vue-jsx/index.js @@ -4,6 +4,7 @@ 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 ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper' const ssrRegisterHelperCode = @@ -39,6 +40,7 @@ function ssrRegisterHelper(comp, filename) { * @returns {import('vite').Plugin} */ function vueJsxPlugin(options = {}) { + let root = '' let needHmr = false let needSourceMap = true @@ -63,6 +65,7 @@ function vueJsxPlugin(options = {}) { configResolved(config) { needHmr = config.command === 'serve' && !config.isProduction needSourceMap = config.command === 'serve' || !!config.build.sourcemap + root = config.root }, resolveId(id) { @@ -217,9 +220,10 @@ function vueJsxPlugin(options = {}) { } if (ssr) { + const normalizedId = path.relative(root, id) let ssrInjectCode = `\nimport { ssrRegisterHelper } from "${ssrRegisterHelperId}"` + - `\nconst __moduleId = ${JSON.stringify(id)}` + `\nconst __moduleId = ${JSON.stringify(normalizedId)}` for (const { local } of hotComponents) { ssrInjectCode += `\nssrRegisterHelper(${local}, __moduleId)` } diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index 98bca03db6f081..46da9e5d670ed1 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -143,13 +143,14 @@ export async function transformMain( // SSR module registration by wrapping user setup if (ssr) { + const normalizedFilename = path.relative(options.root, filename) 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 + normalizedFilename )})`, ` return _sfc_setup ? _sfc_setup(props, ctx) : undefined`, `}` diff --git a/packages/vite/src/node/ssr/ssrManifestPlugin.ts b/packages/vite/src/node/ssr/ssrManifestPlugin.ts index 93d2d19a503ee4..fbac7bd3457401 100644 --- a/packages/vite/src/node/ssr/ssrManifestPlugin.ts +++ b/packages/vite/src/node/ssr/ssrManifestPlugin.ts @@ -1,3 +1,4 @@ +import { relative } from 'path' import { ResolvedConfig } from '..' import { Plugin } from '../plugin' import { chunkToEmittedCssFileMap } from '../plugins/css' @@ -21,7 +22,9 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin { : chunkToEmittedCssFileMap.get(chunk) const assetFiles = chunkToEmittedAssetsMap.get(chunk) for (const id in chunk.modules) { - const mappedChunks = ssrManifest[id] || (ssrManifest[id] = []) + const normalizedId = relative(config.root, id) + const mappedChunks = + ssrManifest[normalizedId] || (ssrManifest[normalizedId] = []) if (!chunk.isEntry) { mappedChunks.push(base + chunk.fileName) } From 90fc5e2110d94bce0415a44a36f50baa4536b853 Mon Sep 17 00:00:00 2001 From: Fernando Ferreira Date: Sat, 19 Jun 2021 00:19:03 +0100 Subject: [PATCH 2/2] fix(ssr): normalize manifest filenames --- packages/plugin-vue-jsx/index.js | 4 ++-- packages/plugin-vue/src/main.ts | 5 ++++- packages/vite/src/node/ssr/ssrManifestPlugin.ts | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/plugin-vue-jsx/index.js b/packages/plugin-vue-jsx/index.js index 1a605b41ac00e8..61ab358ac40eda 100644 --- a/packages/plugin-vue-jsx/index.js +++ b/packages/plugin-vue-jsx/index.js @@ -2,7 +2,7 @@ const babel = require('@babel/core') const jsx = require('@vue/babel-plugin-jsx') const importMeta = require('@babel/plugin-syntax-import-meta') -const { createFilter } = require('@rollup/pluginutils') +const { createFilter, normalizePath } = require('@rollup/pluginutils') const hash = require('hash-sum') const path = require('path') @@ -220,7 +220,7 @@ function vueJsxPlugin(options = {}) { } if (ssr) { - const normalizedId = path.relative(root, id) + const normalizedId = normalizePath(path.relative(root, id)) let ssrInjectCode = `\nimport { ssrRegisterHelper } from "${ssrRegisterHelperId}"` + `\nconst __moduleId = ${JSON.stringify(normalizedId)}` diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index 46da9e5d670ed1..b6dfd03714cefd 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -8,6 +8,7 @@ import { setDescriptor } from './utils/descriptorCache' import { PluginContext, TransformPluginContext } from 'rollup' +import { normalizePath } from '@rollup/pluginutils' import { resolveScript } from './script' import { transformTemplateInMain } from './template' import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate' @@ -143,7 +144,9 @@ export async function transformMain( // SSR module registration by wrapping user setup if (ssr) { - const normalizedFilename = path.relative(options.root, filename) + const normalizedFilename = normalizePath( + path.relative(options.root, filename) + ) output.push( `import { useSSRContext as __vite_useSSRContext } from 'vue'`, `const _sfc_setup = _sfc_main.setup`, diff --git a/packages/vite/src/node/ssr/ssrManifestPlugin.ts b/packages/vite/src/node/ssr/ssrManifestPlugin.ts index fbac7bd3457401..d137f727b51847 100644 --- a/packages/vite/src/node/ssr/ssrManifestPlugin.ts +++ b/packages/vite/src/node/ssr/ssrManifestPlugin.ts @@ -1,4 +1,5 @@ import { relative } from 'path' +import { normalizePath } from '@rollup/pluginutils' import { ResolvedConfig } from '..' import { Plugin } from '../plugin' import { chunkToEmittedCssFileMap } from '../plugins/css' @@ -22,7 +23,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin { : chunkToEmittedCssFileMap.get(chunk) const assetFiles = chunkToEmittedAssetsMap.get(chunk) for (const id in chunk.modules) { - const normalizedId = relative(config.root, id) + const normalizedId = normalizePath(relative(config.root, id)) const mappedChunks = ssrManifest[normalizedId] || (ssrManifest[normalizedId] = []) if (!chunk.isEntry) {