Skip to content

Commit 81d2a04

Browse files
authored
chore: make all paths explicit using the process.cwd() (#47)
1 parent 58ce563 commit 81d2a04

File tree

7 files changed

+41
-38
lines changed

7 files changed

+41
-38
lines changed

Diff for: src/build/constants.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { resolve } from 'node:path'
1+
import { join } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33

44
export const MODULE_DIR = fileURLToPath(new URL('.', import.meta.url))
5-
export const PLUGIN_DIR = resolve(`${MODULE_DIR}../..`)
6-
export const WORKING_DIR = process.cwd()
5+
export const PLUGIN_DIR = join(MODULE_DIR, '../..')
76

8-
export const BUILD_DIR = `${WORKING_DIR}/.netlify`
9-
export const REL_BUILD_DIR = '.netlify'
7+
/** A relative path where we store the build output */
8+
export const BUILD_DIR = '.netlify'
109

11-
export const SERVER_FUNCTIONS_DIR = `${BUILD_DIR}/functions-internal`
10+
export const SERVER_FUNCTIONS_DIR = join(BUILD_DIR, 'functions-internal')
1211
export const SERVER_HANDLER_NAME = '___netlify-server-handler'
13-
export const SERVER_HANDLER_DIR = `${SERVER_FUNCTIONS_DIR}/${SERVER_HANDLER_NAME}`
12+
export const SERVER_HANDLER_DIR = join(SERVER_FUNCTIONS_DIR, SERVER_HANDLER_NAME)
1413

15-
export const EDGE_FUNCTIONS_DIR = `${BUILD_DIR}/edge-functions`
14+
export const EDGE_FUNCTIONS_DIR = join(BUILD_DIR, 'edge-functions')
1615
export const EDGE_HANDLER_NAME = '___netlify-edge-handler'
17-
export const EDGE_HANDLER_DIR = `${EDGE_FUNCTIONS_DIR}/${EDGE_HANDLER_NAME}`
16+
export const EDGE_HANDLER_DIR = join(EDGE_FUNCTIONS_DIR, EDGE_HANDLER_NAME)

Diff for: src/build/content/prerendered.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join } from 'node:path'
66
import { cpus } from 'os'
77
import pLimit from 'p-limit'
88
import { parse, ParsedPath } from 'path'
9-
import { REL_BUILD_DIR } from '../constants.js'
9+
import { BUILD_DIR } from '../constants.js'
1010

1111
export type CacheEntry = {
1212
key: string
@@ -145,7 +145,7 @@ export const uploadPrerenderedContent = async ({
145145

146146
// read prerendered content and build JSON key/values for the blob store
147147
const entries = await Promise.allSettled(
148-
await buildPrerenderedContentEntries(join(process.cwd(), REL_BUILD_DIR, '.next')),
148+
await buildPrerenderedContentEntries(join(process.cwd(), BUILD_DIR, '.next')),
149149
)
150150
entries.forEach((result) => {
151151
if (result.status === 'rejected') {

Diff for: src/build/content/static.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { globby } from 'globby'
33
import { existsSync } from 'node:fs'
44
import { copyFile, cp, mkdir } from 'node:fs/promises'
55
import { ParsedPath, dirname, join, parse } from 'node:path'
6-
import { REL_BUILD_DIR, WORKING_DIR } from '../constants.js'
6+
import { BUILD_DIR } from '../constants.js'
77

88
/**
99
* Copy static pages (HTML without associated JSON data)
@@ -36,7 +36,7 @@ const copyStaticAssets = async ({
3636
PUBLISH_DIR,
3737
}: Pick<NetlifyPluginConstants, 'PUBLISH_DIR'>): Promise<void> => {
3838
try {
39-
const src = join(process.cwd(), REL_BUILD_DIR, '.next/static')
39+
const src = join(process.cwd(), BUILD_DIR, '.next/static')
4040
const dist = join(PUBLISH_DIR, '_next/static')
4141
await mkdir(dist, { recursive: true })
4242
cp(src, dist, { recursive: true, force: true })
@@ -51,7 +51,7 @@ const copyStaticAssets = async ({
5151
const copyPublicAssets = async ({
5252
PUBLISH_DIR,
5353
}: Pick<NetlifyPluginConstants, 'PUBLISH_DIR'>): Promise<void> => {
54-
const src = join(WORKING_DIR, 'public')
54+
const src = join(process.cwd(), 'public')
5555
const dist = join(PUBLISH_DIR, 'public')
5656
if (!existsSync(src)) {
5757
return
@@ -66,7 +66,7 @@ const copyPublicAssets = async ({
6666
*/
6767
export const copyStaticContent = async ({ PUBLISH_DIR }: NetlifyPluginConstants): Promise<void> => {
6868
await Promise.all([
69-
copyStaticPages(join(process.cwd(), REL_BUILD_DIR, '.next'), PUBLISH_DIR),
69+
copyStaticPages(join(process.cwd(), BUILD_DIR, '.next'), PUBLISH_DIR),
7070
copyStaticAssets({ PUBLISH_DIR }),
7171
copyPublicAssets({ PUBLISH_DIR }),
7272
])

Diff for: src/build/functions/edge.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { mkdir, rm } from 'node:fs/promises'
2+
import { join } from 'node:path'
23
import { EDGE_HANDLER_DIR } from '../constants.js'
34

45
/**
56
* Create a Netlify edge function to run the Next.js server
67
*/
78
export const createEdgeHandler = async () => {
89
// reset the handler directory
9-
await rm(EDGE_HANDLER_DIR, { recursive: true, force: true })
10-
await mkdir(EDGE_HANDLER_DIR, { recursive: true })
10+
await rm(join(process.cwd(), EDGE_HANDLER_DIR), { recursive: true, force: true })
11+
await mkdir(join(process.cwd(), EDGE_HANDLER_DIR), { recursive: true })
1112
}

Diff for: src/build/functions/server.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { nodeFileTrace } from '@vercel/nft'
22
import { writeFile, rm, mkdir, cp } from 'fs/promises'
3-
import { REL_BUILD_DIR, PLUGIN_DIR, SERVER_HANDLER_DIR, SERVER_HANDLER_NAME } from '../constants.js'
3+
import { BUILD_DIR, PLUGIN_DIR, SERVER_HANDLER_DIR, SERVER_HANDLER_NAME } from '../constants.js'
44
import { copyServerContent } from '../content/server.js'
55
import { join } from 'node:path'
66
import { readFileSync } from 'fs'
@@ -12,8 +12,8 @@ const pkg = JSON.parse(readFileSync(join(PLUGIN_DIR, 'package.json'), 'utf-8'))
1212
*/
1313
export const createServerHandler = async () => {
1414
// reset the handler directory
15-
await rm(SERVER_HANDLER_DIR, { force: true, recursive: true })
16-
await mkdir(SERVER_HANDLER_DIR, { recursive: true })
15+
await rm(join(process.cwd(), SERVER_HANDLER_DIR), { force: true, recursive: true })
16+
await mkdir(join(process.cwd(), SERVER_HANDLER_DIR), { recursive: true })
1717

1818
// trace the handler dependencies
1919
const { fileList } = await nodeFileTrace(
@@ -27,24 +27,26 @@ export const createServerHandler = async () => {
2727
// copy the handler dependencies
2828
await Promise.all(
2929
[...fileList].map((path) =>
30-
cp(join(PLUGIN_DIR, path), join(SERVER_HANDLER_DIR, path), { recursive: true }),
30+
cp(join(PLUGIN_DIR, path), join(process.cwd(), SERVER_HANDLER_DIR, path), {
31+
recursive: true,
32+
}),
3133
),
3234
)
3335

3436
// copy the next.js standalone build output to the handler directory
3537
await copyServerContent(
36-
join(process.cwd(), REL_BUILD_DIR, '.next/standalone/.next'),
37-
join(SERVER_HANDLER_DIR, '.next'),
38+
join(process.cwd(), BUILD_DIR, '.next/standalone/.next'),
39+
join(process.cwd(), SERVER_HANDLER_DIR, '.next'),
3840
)
3941
await cp(
40-
join(process.cwd(), REL_BUILD_DIR, '.next/standalone/node_modules'),
41-
join(SERVER_HANDLER_DIR, 'node_modules'),
42+
join(process.cwd(), BUILD_DIR, '.next/standalone/node_modules'),
43+
join(process.cwd(), SERVER_HANDLER_DIR, 'node_modules'),
4244
{ recursive: true },
4345
)
4446

4547
// create the handler metadata file
4648
await writeFile(
47-
join(SERVER_HANDLER_DIR, `${SERVER_HANDLER_NAME}.json`),
49+
join(process.cwd(), SERVER_HANDLER_DIR, `${SERVER_HANDLER_NAME}.json`),
4850
JSON.stringify({
4951
config: {
5052
name: 'Next.js Server Handler',
@@ -57,19 +59,22 @@ export const createServerHandler = async () => {
5759
'.next/**',
5860
'node_modules/**',
5961
],
60-
includedFilesBasePath: SERVER_HANDLER_DIR,
62+
includedFilesBasePath: join(process.cwd(), SERVER_HANDLER_DIR),
6163
},
6264
version: 1,
6365
}),
6466
'utf-8',
6567
)
6668

6769
// configure ESM
68-
await writeFile(join(SERVER_HANDLER_DIR, 'package.json'), JSON.stringify({ type: 'module' }))
70+
await writeFile(
71+
join(process.cwd(), SERVER_HANDLER_DIR, 'package.json'),
72+
JSON.stringify({ type: 'module' }),
73+
)
6974

7075
// write the root handler file
7176
await writeFile(
72-
join(SERVER_HANDLER_DIR, `${SERVER_HANDLER_NAME}.js`),
77+
join(process.cwd(), SERVER_HANDLER_DIR, `${SERVER_HANDLER_NAME}.js`),
7378
`import handler from './dist/run/handlers/server.js';export default handler;export const config = {path:'/*'}`,
7479
)
7580
}

Diff for: src/build/move-build-output.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NetlifyPluginConstants, NetlifyPluginUtils } from '@netlify/build'
22
import { existsSync } from 'node:fs'
33
import { mkdir, rename, rm } from 'node:fs/promises'
44
import { join } from 'node:path'
5-
import { REL_BUILD_DIR } from './constants.js'
5+
import { BUILD_DIR } from './constants.js'
66

77
/**
88
* Move the Next.js build output from the publish dir to a temp dir
@@ -17,7 +17,7 @@ export const moveBuildOutput = async (
1717
)
1818
}
1919

20-
const tempDir = join(process.cwd(), REL_BUILD_DIR, '.next')
20+
const tempDir = join(process.cwd(), BUILD_DIR, '.next')
2121

2222
try {
2323
// cleanup any existing directory

Diff for: tests/utils/fixture.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { cp, mkdtemp, rm } from 'node:fs/promises'
1414
import { tmpdir } from 'node:os'
1515
import { join } from 'node:path'
1616
import { fileURLToPath } from 'node:url'
17-
import { SERVER_HANDLER_NAME } from '../../src/build/constants.js'
17+
import { SERVER_FUNCTIONS_DIR, SERVER_HANDLER_NAME } from '../../src/build/constants.js'
1818
import { streamToString } from './stream-to-string.js'
1919

2020
export interface FixtureTestContext extends TestContext {
@@ -106,20 +106,18 @@ export async function runPlugin(
106106
} as NetlifyPluginUtils,
107107
} as unknown as NetlifyPluginOptions)
108108

109-
// We need to do a dynamic import as we mock the `process.cwd()` inside the createFixture function
110-
// If we import it before calling that it will resolve to the actual process working directory instead of the mocked one
111-
const { SERVER_FUNCTIONS_DIR } = await import('../../src/build/constants.js')
109+
const internalSrcFolder = join(ctx.cwd, SERVER_FUNCTIONS_DIR)
112110

113111
// create zip location in a new temp folder to avoid leaking node_modules through nodes resolve algorithm
114112
// that always looks up a parent directory for node_modules
115113
ctx.functionDist = await mkdtemp(join(tmpdir(), 'netlify-next-runtime-dist'))
116114
// bundle the function to get the bootstrap layer and all the important parts
117-
await zipFunctions(SERVER_FUNCTIONS_DIR, ctx.functionDist, {
115+
await zipFunctions([internalSrcFolder], ctx.functionDist, {
118116
basePath: ctx.cwd,
119117
manifest: join(ctx.functionDist, 'manifest.json'),
120118
repositoryRoot: ctx.cwd,
121-
configFileDirectories: [SERVER_FUNCTIONS_DIR],
122-
internalSrcFolder: SERVER_FUNCTIONS_DIR,
119+
configFileDirectories: [internalSrcFolder],
120+
internalSrcFolder,
123121
archiveFormat: 'none',
124122
})
125123
}

0 commit comments

Comments
 (0)