|
| 1 | +import { createHash } from 'node:crypto' |
| 2 | +import fs from 'node:fs' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | + |
| 6 | +import type { RspressPlugin, UserConfig } from '@rspress/core' |
| 7 | +import { afterEach, describe, expect, test } from '@rstest/core' |
| 8 | + |
| 9 | +import { globalPlugin, type GlobalVirtual } from '#plugins/global/index.ts' |
| 10 | +import type { SharedSidebarPayload } from '#plugins/global/sidebar.ts' |
| 11 | + |
| 12 | +const dirs: string[] = [] |
| 13 | +const temporaryOutput = () => { |
| 14 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'doom-sidebar-')) |
| 15 | + dirs.push(dir) |
| 16 | + return dir |
| 17 | +} |
| 18 | +const configure = (plugin: RspressPlugin, config: UserConfig) => |
| 19 | + plugin.config?.(config, { addPlugin() {}, removePlugin() {} }, true) |
| 20 | +const runtime = async ( |
| 21 | + plugin: RspressPlugin, |
| 22 | + config: UserConfig, |
| 23 | + isProd = true, |
| 24 | +): Promise<GlobalVirtual> => { |
| 25 | + // eslint-disable-next-line @typescript-eslint/no-deprecated -- exercise the runtime hook implemented by this plugin |
| 26 | + const modules = await plugin.addRuntimeModules?.(config, isProd) |
| 27 | + return JSON.parse( |
| 28 | + modules!['doom-@global-virtual'].replace(/^export default /, ''), |
| 29 | + ) as GlobalVirtual |
| 30 | +} |
| 31 | +const readSidebar = (outDir: string) => { |
| 32 | + const staticDir = path.join(outDir, 'static') |
| 33 | + const manifest = JSON.parse( |
| 34 | + fs.readFileSync(path.join(staticDir, 'sidebar.json'), 'utf8'), |
| 35 | + ) as { fileName: string } |
| 36 | + const content = fs.readFileSync( |
| 37 | + path.join(staticDir, manifest.fileName), |
| 38 | + 'utf8', |
| 39 | + ) |
| 40 | + expect(manifest.fileName).toBe( |
| 41 | + `sidebar.${createHash('sha256').update(content).digest('hex')}.json`, |
| 42 | + ) |
| 43 | + return { |
| 44 | + fileName: manifest.fileName, |
| 45 | + content, |
| 46 | + payload: JSON.parse(content) as SharedSidebarPayload, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +afterEach(() => { |
| 51 | + for (const dir of dirs.splice(0)) { |
| 52 | + fs.rmSync(dir, { recursive: true, force: true }) |
| 53 | + } |
| 54 | +}) |
| 55 | + |
| 56 | +describe('globalPlugin shared sidebar', () => { |
| 57 | + test('writes locale sidebars and exposes a manifest under the site base', async () => { |
| 58 | + const outDir = path.join(temporaryOutput(), 'site') |
| 59 | + const sidebar = { '/': [{ text: 'Home', link: '/' }] } |
| 60 | + const localeSidebar = { '/en/': [{ text: 'English', link: '/en/' }] } |
| 61 | + const config: UserConfig = { |
| 62 | + root: 'docs', |
| 63 | + base: '/manual', |
| 64 | + outDir, |
| 65 | + themeConfig: { |
| 66 | + sidebar, |
| 67 | + locales: [{ lang: 'en', label: 'English', sidebar: localeSidebar }], |
| 68 | + }, |
| 69 | + } |
| 70 | + const plugin = globalPlugin({}) |
| 71 | + |
| 72 | + expect(await configure(plugin, config)).toBe(config) |
| 73 | + expect((await runtime(plugin, config)).sidebarManifestUrl).toBe( |
| 74 | + '/manual/static/sidebar.json', |
| 75 | + ) |
| 76 | + await plugin.afterBuild?.(config, true) |
| 77 | + expect(readSidebar(outDir).payload).toEqual({ |
| 78 | + sidebar, |
| 79 | + locales: [{ lang: 'en', sidebar: localeSidebar }], |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + test('changes the content address after a title edit without changing the manifest URL', async () => { |
| 84 | + const outDir = temporaryOutput() |
| 85 | + const config: UserConfig = { |
| 86 | + root: 'docs', |
| 87 | + outDir, |
| 88 | + themeConfig: { sidebar: { '/': [{ text: 'Before', link: '/' }] } }, |
| 89 | + } |
| 90 | + const plugin = globalPlugin({}) |
| 91 | + await configure(plugin, config) |
| 92 | + await plugin.afterBuild?.(config, true) |
| 93 | + const before = readSidebar(outDir) |
| 94 | + const firstUrl = (await runtime(plugin, config)).sidebarManifestUrl |
| 95 | + |
| 96 | + config.themeConfig!.sidebar = { '/': [{ text: 'After', link: '/' }] } |
| 97 | + await configure(plugin, config) |
| 98 | + await plugin.afterBuild?.(config, true) |
| 99 | + const after = readSidebar(outDir) |
| 100 | + |
| 101 | + expect(after.fileName).not.toBe(before.fileName) |
| 102 | + expect(after.payload.sidebar).toEqual(config.themeConfig!.sidebar) |
| 103 | + expect((await runtime(plugin, config)).sidebarManifestUrl).toBe(firstUrl) |
| 104 | + expect(firstUrl).toBe('/static/sidebar.json') |
| 105 | + }) |
| 106 | + |
| 107 | + test('can write the assets when the config hook has not run', async () => { |
| 108 | + const outDir = temporaryOutput() |
| 109 | + const config: UserConfig = { root: 'docs', outDir } |
| 110 | + const plugin = globalPlugin({}) |
| 111 | + |
| 112 | + expect(await runtime(plugin, config, false)).not.toHaveProperty( |
| 113 | + 'sidebarManifestUrl', |
| 114 | + ) |
| 115 | + await plugin.afterBuild?.(config, true) |
| 116 | + expect(readSidebar(outDir).payload).toEqual({}) |
| 117 | + expect((await runtime(plugin, config)).sidebarManifestUrl).toBe( |
| 118 | + '/static/sidebar.json', |
| 119 | + ) |
| 120 | + }) |
| 121 | + |
| 122 | + test('keeps only the main script URL stable', () => { |
| 123 | + const filename = globalPlugin({}).builderConfig?.output?.filename?.js |
| 124 | + expect(typeof filename).toBe('function') |
| 125 | + if (typeof filename !== 'function') { |
| 126 | + throw new Error('expected a script filename function') |
| 127 | + } |
| 128 | + expect(filename({ chunk: { name: 'index' } }, {})).toBe('[name].js') |
| 129 | + expect(filename({ chunk: { name: 'search' } }, {})).toBe( |
| 130 | + '[name].[contenthash:10].js', |
| 131 | + ) |
| 132 | + expect(filename({}, {})).toBe('[name].[contenthash:10].js') |
| 133 | + }) |
| 134 | +}) |
0 commit comments