Skip to content

Commit

Permalink
fix: revert to support vite ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed May 7, 2024
1 parent 027f9b1 commit f8e967a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
3 changes: 3 additions & 0 deletions playground/spa-file-mode/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { injectScripts, publicTypescript } from 'vite-plugin-public-typescript'
// https://vitejs.dev/config/
export default defineConfig(() => ({
base: '/vite-plugin-public-typescript/',
server: {
port: 3001,
},
define: {
custom_define: JSON.stringify('custom define!'),
hello_world: JSON.stringify({ hello: 'world' }),
Expand Down
3 changes: 3 additions & 0 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { injectScripts, publicTypescript } from 'vite-plugin-public-typescript'
// https://vitejs.dev/config/
export default defineConfig(() => ({
base: '/vite-plugin-public-typescript/',
server: {
port: 3000,
},
define: {
custom_define: JSON.stringify('custom define!'),
hello_world: JSON.stringify({ hello: 'world' }),
Expand Down
1 change: 1 addition & 0 deletions playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function createServer(root = process.cwd(), isProd = process.env.NO
attrs: {
src: manifest.ssr,
},
injectTo: 'body',
},
]
})
Expand Down
7 changes: 0 additions & 7 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
export * from 'vitest-e2e/test-utils'

export const ports = {
ssr: 9604,
}
export const hmrPorts = {
ssr: 24685,
}
2 changes: 1 addition & 1 deletion src/node/helper/html.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type DefaultTreeAdapterMap, type ParserError, type Token } from 'parse5'
import { type HtmlTagDescriptor } from 'vite'

export const VPPT_DATA_ATTR = 'data-vppt'
export const VPPT_DATA_ATTR = 'data-vite-plugin-public-typescript'

export async function traverseHtml(
html: string,
Expand Down
4 changes: 3 additions & 1 deletion src/node/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type ResolvedConfig, createLogger, normalizePath } from 'vite'
import { type VPPTPluginOptions } from '..'
import { name as pkgName } from '../../../package.json'
import { globalConfig } from '../global-config'
import { manifestCache } from '../manifest-cache'
import { manifestCache, saveManifestPathToDisk } from '../manifest-cache'
import { initCacheProcessor } from '../processor/processor'
import { disableManifestHmr } from './server'

Expand Down Expand Up @@ -211,6 +211,8 @@ export async function setupManifestCache(viteConfig: ResolvedConfig, opts: Optio

manifestCache.setManifestPath(`${cacheDir}/${opts.manifestName}.json`)

saveManifestPathToDisk()

// no need to set `_pathToDisk` manually anymore
manifestCache.beforeSet = (value) => {
if (value?.path) {
Expand Down
38 changes: 34 additions & 4 deletions src/node/manifest-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { isEmptyObject } from '../helper/utils'
import fs from 'fs-extra'
import path from 'node:path'
import { normalizePath } from 'vite'
import { globalConfig } from '../global-config'
import { DEFAULT_OPTIONS } from '../helper/default-options'
import { readJsonFile, writeJsonFile } from '../helper/io'
import { isEmptyObject, isInTest } from '../helper/utils'
import { type CacheValue, ManifestCache } from './ManifestCache'

export type CacheValueEx = {
Expand All @@ -9,16 +15,40 @@ export type CacheValueEx = {

export const manifestCache = new ManifestCache<CacheValueEx>()

export function getManifest(): Record<string, string> {
// 从内存中读取
const ManifestCachePath = normalizePath(`${DEFAULT_OPTIONS.cacheDir}/_manifest_path.json`)

function getManifestPath(root?: string) {
if (isInTest()) {
root = process.env.__Manifest_Path__
}
if (!root) {
root = globalConfig.get('viteConfig')?.root || process.cwd()
}

return normalizePath(path.resolve(root, ManifestCachePath))
}

export function saveManifestPathToDisk() {
// save manifest path to cache dir
fs.ensureDir(DEFAULT_OPTIONS.cacheDir)
writeJsonFile(getManifestPath(), {
manifestPath: manifestCache.manifestPath,
})
}

export function getManifest(root?: string): Record<string, string> {
if (!isEmptyObject(manifestCache.getManifestJson())) {
return manifestCache.getManifestJson()
}

// 从manifest.json中读取
if (!isEmptyObject(manifestCache.readManifestFile())) {
return manifestCache.readManifestFile()
}

const manifestPath = readJsonFile(getManifestPath(root))?.manifestPath
if (manifestPath) {
return readJsonFile(manifestPath) || {}
}

return {}
}

0 comments on commit f8e967a

Please sign in to comment.