| |
| |
| |
| |
|
|
| import ReactDOMServer from 'react-dom/server' |
| import React from 'react' |
| import { VFileCompatible } from 'vfile' |
| import { readFile, mkdtemp, cp, rm } from 'node:fs/promises' |
| import os from 'os' |
| import path from 'path' |
| import http from 'http' |
| import { fileURLToPath } from 'url' |
| import spawn from 'cross-spawn' |
| import { ChildProcess } from 'child_process' |
| import treeKill from 'tree-kill' |
| import { Server } from 'http' |
| import handler from 'serve-handler' |
|
|
| import { MDXRemote, MDXRemoteProps } from '../src/index' |
| import { serialize } from '../src/serialize' |
| import { SerializeOptions } from '../src/types' |
|
|
| const __dirname = fileURLToPath(new URL('.', import.meta.url)) |
|
|
| export async function renderStatic( |
| mdx: VFileCompatible, |
| { |
| components, |
| scope = {}, |
| mdxOptions, |
| parseFrontmatter, |
| }: Partial<SerializeOptions & Pick<MDXRemoteProps, 'components'>> = {} |
| ): Promise<string> { |
| const mdxSource = await serialize(mdx, { |
| mdxOptions, |
| parseFrontmatter, |
| }) |
|
|
| return ReactDOMServer.renderToStaticMarkup( |
| <MDXRemote {...mdxSource} components={components} scope={scope} /> |
| ) |
| } |
|
|
| export async function getPathToPackedPackage() { |
| const packageJson = JSON.parse( |
| await readFile(path.join(__dirname, '..', 'package.json'), 'utf-8') |
| ) |
|
|
| const filename = `${packageJson.name}-${packageJson.version}.tgz` |
|
|
| return path.join(__dirname, '..', 'dist', filename) |
| } |
|
|
| |
| |
| export async function createTmpTestDir(fixture: string) { |
| const tmpDir = await mkdtemp( |
| path.join(os.tmpdir(), `next-mdx-remote-${fixture}-`) |
| ) |
|
|
| |
| const pathToFixture = path.join( |
| process.cwd(), |
| '__tests__', |
| 'fixtures', |
| fixture |
| ) |
|
|
| await cp(pathToFixture, tmpDir, { recursive: true }) |
|
|
| |
| const pathToPackedPackage = await getPathToPackedPackage() |
|
|
| spawn.sync('npm', ['install', pathToPackedPackage], { |
| cwd: tmpDir, |
| }) |
|
|
| return tmpDir |
| } |
|
|
| export async function cleanupTmpTestDir(tmpDir: string) { |
| await rm(tmpDir, { recursive: true, force: true }) |
| } |
|
|
| |
| export async function startDevServer(dir: string) { |
| const childProcess = spawn('npx', ['next', 'dev', '-p', '12333'], { |
| stdio: ['ignore', 'pipe', 'pipe'], |
| cwd: dir, |
| env: { ...process.env, NODE_ENV: 'development', __NEXT_TEST_MODE: 'true' }, |
| }) |
|
|
| childProcess.stderr?.on('data', (chunk) => { |
| process.stdout.write(chunk) |
| }) |
|
|
| async function waitForStarted() { |
| return new Promise<undefined>((resolve) => { |
| childProcess.stdout?.on('data', (chunk) => { |
| const msg = chunk.toString() |
| process.stdout.write(chunk) |
|
|
| if (msg.includes('Ready in')) { |
| resolve(undefined) |
| } |
| }) |
| }) |
| } |
|
|
| await waitForStarted() |
|
|
| return childProcess |
| } |
|
|
| |
| export async function stopDevServer(childProcess: ChildProcess) { |
| console.log('stopping development server...') |
| const promise = new Promise((resolve) => { |
| childProcess.on('close', () => { |
| console.log('development server stopped') |
| resolve(undefined) |
| }) |
| }) |
|
|
| await new Promise((resolve) => { |
| treeKill(childProcess.pid!, 'SIGKILL', () => resolve(undefined)) |
| }) |
|
|
| childProcess.kill('SIGKILL') |
|
|
| await promise |
| } |
|
|
| |
| export function buildFixture(dir: string) { |
| if (!dir) { |
| throw new Error('dir is required') |
| } |
| spawn.sync('npx', ['next', 'build'], { |
| stdio: 'inherit', |
| cwd: dir, |
| env: { ...process.env, NODE_ENV: 'production', __NEXT_TEST_MODE: 'true' }, |
| }) |
| spawn.sync('npx', ['next', 'export'], { |
| stdio: 'inherit', |
| cwd: dir, |
| env: { ...process.env, NODE_ENV: 'production', __NEXT_TEST_MODE: 'true' }, |
| }) |
| } |
|
|
| |
| export async function readOutputFile(dir: string, name: string) { |
| return readFile(path.join(dir, 'out', `${name}.html`), 'utf8') |
| } |
|
|
| |
| |
| export function serveStatic(dir: string): Promise<Server> { |
| return new Promise((resolve) => { |
| const server = http.createServer((req, res) => |
| handler(req, res, { |
| public: path.join(dir, 'out'), |
| }) |
| ) |
| server.listen(1235, () => resolve(server)) |
| }) |
| } |
|
|