|
| 1 | +import { readFile } from 'node:fs/promises' |
| 2 | +import { join } from 'node:path' |
| 3 | + |
| 4 | +import { NetlifyPluginOptions } from '@netlify/build' |
| 5 | +import { expect, test, vi } from 'vitest' |
| 6 | + |
| 7 | +import { mockFileSystem } from '../../../tests/index.js' |
| 8 | +import { PluginContext } from '../plugin-context.js' |
| 9 | + |
| 10 | +import { copyNextServerCode } from './server.js' |
| 11 | + |
| 12 | +vi.mock('node:fs', async () => { |
| 13 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, unicorn/no-await-expression-member |
| 14 | + const unionFs: any = (await import('unionfs')).default |
| 15 | + const fs = await vi.importActual('node:fs') |
| 16 | + unionFs.reset = () => { |
| 17 | + unionFs.fss = [fs] |
| 18 | + } |
| 19 | + |
| 20 | + const united = unionFs.use(fs) |
| 21 | + return { default: united, ...united } |
| 22 | +}) |
| 23 | + |
| 24 | +vi.mock('node:fs/promises', async () => { |
| 25 | + const fs = await import('node:fs') |
| 26 | + return fs.promises |
| 27 | +}) |
| 28 | + |
| 29 | +test('should not modify the required-server-files.json distDir on simple next app', async () => { |
| 30 | + const reqServerFiles = JSON.stringify({ config: { distDir: '.next' } }) |
| 31 | + const reqServerPath = '.next/required-server-files.json' |
| 32 | + const reqServerPathStandalone = join('.next/standalone', reqServerPath) |
| 33 | + const { cwd } = mockFileSystem({ |
| 34 | + [reqServerPath]: reqServerFiles, |
| 35 | + [reqServerPathStandalone]: reqServerFiles, |
| 36 | + }) |
| 37 | + const ctx = new PluginContext({ constants: {} } as NetlifyPluginOptions) |
| 38 | + await copyNextServerCode(ctx) |
| 39 | + expect(await readFile(join(cwd, reqServerPathStandalone), 'utf-8')).toBe(reqServerFiles) |
| 40 | +}) |
| 41 | + |
| 42 | +test('should not modify the required-server-files.json distDir on monorepo', async () => { |
| 43 | + const reqServerFiles = JSON.stringify({ config: { distDir: '.next' } }) |
| 44 | + const reqServerPath = 'apps/my-app/.next/required-server-files.json' |
| 45 | + const reqServerPathStandalone = join('apps/my-app/.next/standalone', reqServerPath) |
| 46 | + const { cwd } = mockFileSystem({ |
| 47 | + [reqServerPath]: reqServerFiles, |
| 48 | + [reqServerPathStandalone]: reqServerFiles, |
| 49 | + }) |
| 50 | + const ctx = new PluginContext({ |
| 51 | + constants: { |
| 52 | + PACKAGE_PATH: 'apps/my-app', |
| 53 | + }, |
| 54 | + } as NetlifyPluginOptions) |
| 55 | + await copyNextServerCode(ctx) |
| 56 | + expect(await readFile(join(cwd, reqServerPathStandalone), 'utf-8')).toBe(reqServerFiles) |
| 57 | +}) |
| 58 | + |
| 59 | +test('should not modify the required-server-files.json distDir on monorepo', async () => { |
| 60 | + const reqServerFiles = JSON.stringify({ config: { distDir: '.next' } }) |
| 61 | + const reqServerPath = 'apps/my-app/.next/required-server-files.json' |
| 62 | + const reqServerPathStandalone = join('apps/my-app/.next/standalone', reqServerPath) |
| 63 | + const { cwd } = mockFileSystem({ |
| 64 | + [reqServerPath]: reqServerFiles, |
| 65 | + [reqServerPathStandalone]: reqServerFiles, |
| 66 | + }) |
| 67 | + const ctx = new PluginContext({ |
| 68 | + constants: { |
| 69 | + PACKAGE_PATH: 'apps/my-app', |
| 70 | + }, |
| 71 | + } as NetlifyPluginOptions) |
| 72 | + await copyNextServerCode(ctx) |
| 73 | + expect(await readFile(join(cwd, reqServerPathStandalone), 'utf-8')).toBe(reqServerFiles) |
| 74 | +}) |
| 75 | + |
| 76 | +// case of nx-integrated |
| 77 | +test('should modify the required-server-files.json distDir on distDir outside of packagePath', async () => { |
| 78 | + const reqServerFiles = JSON.stringify({ config: { distDir: '../../dist/apps/my-app/.next' } }) |
| 79 | + const reqServerPath = 'dist/apps/my-app/.next/required-server-files.json' |
| 80 | + const reqServerPathStandalone = join('dist/apps/my-app/.next/standalone', reqServerPath) |
| 81 | + const { cwd } = mockFileSystem({ |
| 82 | + [reqServerPath]: reqServerFiles, |
| 83 | + [reqServerPathStandalone]: reqServerFiles, |
| 84 | + }) |
| 85 | + const ctx = new PluginContext({ |
| 86 | + constants: { |
| 87 | + PACKAGE_PATH: 'apps/my-app', |
| 88 | + PUBLISH_DIR: 'dist/apps/my-app/.next', |
| 89 | + }, |
| 90 | + } as NetlifyPluginOptions) |
| 91 | + await copyNextServerCode(ctx) |
| 92 | + expect(await readFile(join(cwd, reqServerPathStandalone), 'utf-8')).toBe( |
| 93 | + '{"config":{"distDir":".next"}}', |
| 94 | + ) |
| 95 | +}) |
0 commit comments