@@ -14,7 +14,6 @@ import {
14
14
unlink ,
15
15
existsSync ,
16
16
readFileSync ,
17
- copy ,
18
17
ensureDir ,
19
18
readJson ,
20
19
pathExists ,
@@ -24,9 +23,7 @@ import {
24
23
import path from "path"
25
24
import process from "process"
26
25
import os from "os"
27
- import cpy from "cpy"
28
26
import { dir as getTmpDir } from "tmp-promise"
29
- import { getExtendedApiRouteConfigs } from "../packages/runtime/src/helpers/functions"
30
27
// @ts -expect-error - TODO: Convert runtime export to ES6
31
28
import nextRuntimeFactory from "../packages/runtime/src"
32
29
const nextRuntime = nextRuntimeFactory ( { } )
@@ -38,13 +35,12 @@ import {
38
35
updateRequiredServerFiles ,
39
36
generateCustomHeaders ,
40
37
} from "../packages/runtime/src/helpers/config"
41
- import { dirname , resolve } from "path"
38
+ import { resolve } from "path"
42
39
import { getProblematicUserRewrites } from "../packages/runtime/src/helpers/verification"
43
40
import type { NetlifyPluginOptions } from '@netlify/build'
41
+ import { changeCwd , useFixture , moveNextDist } from "./test-utils"
44
42
45
43
const chance = new Chance ( )
46
- const FIXTURES_DIR = `${ __dirname } /fixtures`
47
- const SAMPLE_PROJECT_DIR = `${ __dirname } /../demos/default`
48
44
const constants = {
49
45
INTERNAL_FUNCTIONS_SRC : '.netlify/functions-internal' ,
50
46
PUBLISH_DIR : '.next' ,
@@ -65,68 +61,9 @@ const utils = {
65
61
66
62
const normalizeChunkNames = ( source ) => source . replaceAll ( / \/ c h u n k s \/ \d + \. j s / g, '/chunks/CHUNK_ID.js' )
67
63
68
- // Temporary switch cwd
69
- const changeCwd = function ( cwd ) {
70
- const originalCwd = process . cwd ( )
71
- process . chdir ( cwd )
72
- return ( ) => {
73
- process . chdir ( originalCwd )
74
- }
75
- }
76
-
77
64
const onBuildHasRun = ( netlifyConfig ) =>
78
65
Boolean ( netlifyConfig . functions [ HANDLER_FUNCTION_NAME ] ?. included_files ?. some ( ( file ) => file . includes ( 'BUILD_ID' ) ) )
79
66
80
- const rewriteAppDir = async function ( dir = '.next' ) {
81
- const manifest = path . join ( dir , 'required-server-files.json' )
82
- const manifestContent = await readJson ( manifest )
83
- manifestContent . appDir = process . cwd ( )
84
-
85
- await writeJSON ( manifest , manifestContent )
86
- }
87
-
88
- // Move .next from sample project to current directory
89
- export const moveNextDist = async function ( dir = '.next' , copyMods = false ) {
90
- if ( copyMods ) {
91
- await copyModules ( [ 'next' , 'sharp' ] )
92
- } else {
93
- await stubModules ( [ 'next' , 'sharp' ] )
94
- }
95
- await ensureDir ( dirname ( dir ) )
96
- await copy ( path . join ( SAMPLE_PROJECT_DIR , '.next' ) , path . join ( process . cwd ( ) , dir ) )
97
-
98
- for ( const file of [ 'pages' , 'app' , 'src' , 'components' , 'public' , 'components' , 'hello.txt' , 'package.json' ] ) {
99
- const source = path . join ( SAMPLE_PROJECT_DIR , file )
100
- if ( existsSync ( source ) ) {
101
- await copy ( source , path . join ( process . cwd ( ) , file ) )
102
- }
103
- }
104
-
105
- await rewriteAppDir ( dir )
106
- }
107
-
108
- const copyModules = async function ( modules ) {
109
- for ( const mod of modules ) {
110
- const source = dirname ( require . resolve ( `${ mod } /package.json` ) )
111
- const dest = path . join ( process . cwd ( ) , 'node_modules' , mod )
112
- await copy ( source , dest )
113
- }
114
- }
115
-
116
- const stubModules = async function ( modules ) {
117
- for ( const mod of modules ) {
118
- const dir = path . join ( process . cwd ( ) , 'node_modules' , mod )
119
- await ensureDir ( dir )
120
- await writeJSON ( path . join ( dir , 'package.json' ) , { name : mod } )
121
- }
122
- }
123
-
124
- // Copy fixture files to the current directory
125
- const useFixture = async function ( fixtureName ) {
126
- const fixtureDir = `${ FIXTURES_DIR } /${ fixtureName } `
127
- await cpy ( '**' , process . cwd ( ) , { cwd : fixtureDir , parents : true , overwrite : true , dot : true } )
128
- }
129
-
130
67
const netlifyConfig = { build : { command : 'npm run build' } , functions : { } , redirects : [ ] , headers : [ ] } as NetlifyPluginOptions [ "netlifyConfig" ]
131
68
const defaultArgs = {
132
69
netlifyConfig,
@@ -1528,28 +1465,6 @@ describe('function helpers', () => {
1528
1465
} )
1529
1466
} )
1530
1467
1531
- describe ( 'api route file analysis' , ( ) => {
1532
- it ( 'extracts correct route configs from source files' , async ( ) => {
1533
- await moveNextDist ( )
1534
- const configs = await getExtendedApiRouteConfigs ( '.next' , process . cwd ( ) )
1535
- // Using a Set means the order doesn't matter
1536
- expect ( new Set ( configs ) ) . toEqual (
1537
- new Set ( [
1538
- {
1539
- compiled : 'pages/api/hello-background.js' ,
1540
- config : { type : 'experimental-background' } ,
1541
- route : '/api/hello-background' ,
1542
- } ,
1543
- {
1544
- compiled : 'pages/api/hello-scheduled.js' ,
1545
- config : { schedule : '@hourly' , type : 'experimental-scheduled' } ,
1546
- route : '/api/hello-scheduled' ,
1547
- } ,
1548
- ] ) ,
1549
- )
1550
- } )
1551
- } )
1552
-
1553
1468
const middlewareSourceTs = /* typescript */ `
1554
1469
import { NextResponse } from 'next/server'
1555
1470
export async function middleware(req: NextRequest) {
0 commit comments