Skip to content

Commit fa3f12f

Browse files
committed
chore: refactor handler paths
1 parent 33483e9 commit fa3f12f

File tree

7 files changed

+18
-33
lines changed

7 files changed

+18
-33
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@ module.exports = {
1919
overrides: [
2020
...overrides,
2121
{
22-
files: ['src/templates/**'],
22+
files: ['src/handlers/**'],
2323
rules: {
24-
'@typescript-eslint/no-var-requires': 'off',
25-
'@typescript-eslint/ban-ts-comment': 'off',
26-
'unicorn/no-abusive-eslint-disable': 'off',
27-
'eslint-comments/no-unlimited-disable': 'off',
28-
'import/no-unresolved': 'off',
29-
'import/no-unassigned-import': 'off',
3024
'import/no-anonymous-default-export': 'off',
31-
'n/no-missing-require': 'off',
32-
'n/no-missing-import': 'off',
3325
},
3426
},
3527
],
File renamed without changes.

src/templates/server-handler.ts renamed to src/handlers/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { toComputeResponse, toReqRes } from '@fastly/http-compute-js'
22
import type { WorkerRequestHandler } from 'next/dist/server/lib/types.js'
33

4-
import { RUNTIME_DIR } from '../helpers/constants.js'
4+
import { TASK_DIR } from '../helpers/constants.js'
55

66
let nextHandler: WorkerRequestHandler
77

@@ -16,7 +16,7 @@ export default async (request: Request) => {
1616
;[nextHandler] = await getRequestHandlers({
1717
port: 3000,
1818
hostname: 'localhost',
19-
dir: RUNTIME_DIR,
19+
dir: TASK_DIR,
2020
isDev: false,
2121
})
2222
}

src/helpers/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFile } from 'node:fs/promises'
22

3-
import { RUNTIME_DIR } from './constants.js'
3+
import { TASK_DIR } from './constants.js'
44

55
/**
66
* Enable Next.js standalone mode
@@ -10,12 +10,12 @@ export const setBuildConfig = () => {
1010
}
1111

1212
export const setRequestConfig = async () => {
13-
const runtimeConfig = JSON.parse(await readFile('./.next/required-server-files.json', 'utf-8'))
13+
const runtimeConfig = JSON.parse(await readFile(`${TASK_DIR}/.next/required-server-files.json`, 'utf-8'))
1414

1515
// set the path to the cache handler
1616
runtimeConfig.config.experimental = {
1717
...runtimeConfig.config.experimental,
18-
incrementalCacheHandlerPath: `${RUNTIME_DIR}/dist/templates/cache-handler.cjs`,
18+
incrementalCacheHandlerPath: `${TASK_DIR}/dist/handlers/cache.cjs`,
1919
}
2020

2121
// set config from the build output

src/helpers/constants.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33

4-
let dir
5-
try {
6-
dir = __dirname
7-
} catch {
8-
dir = fileURLToPath(new URL('.', import.meta.url))
9-
}
10-
11-
export const MODULE_DIR = dir
4+
export const MODULE_DIR = fileURLToPath(new URL('.', import.meta.url))
125
export const PLUGIN_DIR = resolve(`${MODULE_DIR}../..`)
13-
export const RUNTIME_DIR = resolve(`${MODULE_DIR}../..`)
6+
export const TASK_DIR = process.cwd()
147

15-
export const NEXT_BUILD_DIR = '.netlify/.next'
8+
export const BUILD_DIR = `${TASK_DIR}/.netlify/.next`
169

17-
export const FUNCTIONS_INTERNAL_DIR = '.netlify/functions-internal'
10+
export const FUNCTIONS_INTERNAL_DIR = `${TASK_DIR}/.netlify/functions-internal`
1811
export const FUNCTIONS_URL = '/.netlify/functions'
1912

2013
export const SERVER_HANDLER_NAME = '___netlify-server-handler'

src/helpers/files.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { existsSync } from 'node:fs'
33
import { NetlifyPluginConstants } from '@netlify/build'
44
import { copySync, moveSync } from 'fs-extra/esm'
55

6-
import { NEXT_BUILD_DIR } from './constants.js'
6+
import { BUILD_DIR } from './constants.js'
77

88
/**
99
* Move the Next.js build output to a temporary directory
1010
*/
1111
export const moveBuildOutput = ({ PUBLISH_DIR }: NetlifyPluginConstants) => {
12-
moveSync(PUBLISH_DIR, NEXT_BUILD_DIR, { overwrite: true })
12+
moveSync(PUBLISH_DIR, BUILD_DIR, { overwrite: true })
1313
}
1414

1515
/**
@@ -19,5 +19,5 @@ export const moveStaticAssets = ({ PUBLISH_DIR }: NetlifyPluginConstants) => {
1919
if (existsSync('public')) {
2020
copySync('public', PUBLISH_DIR)
2121
}
22-
copySync(`${NEXT_BUILD_DIR}/static/`, `${PUBLISH_DIR}/_next/static`)
22+
copySync(`${BUILD_DIR}/static/`, `${PUBLISH_DIR}/_next/static`)
2323
}

src/helpers/functions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { writeFileSync } from 'fs'
33
import { nodeFileTrace } from '@vercel/nft'
44
import { copySync, emptyDirSync, readJsonSync, writeJSONSync } from 'fs-extra/esm'
55

6-
import { NEXT_BUILD_DIR, SERVER_HANDLER_DIR, SERVER_HANDLER_NAME, PLUGIN_DIR } from './constants.js'
6+
import { BUILD_DIR, SERVER_HANDLER_DIR, SERVER_HANDLER_NAME, PLUGIN_DIR } from './constants.js'
77

88
const pkg = readJsonSync(`${PLUGIN_DIR}/package.json`)
99

@@ -18,7 +18,7 @@ export const createServerHandler = async () => {
1818

1919
// trace the handler dependencies
2020
const { fileList } = await nodeFileTrace(
21-
[`${PLUGIN_DIR}/dist/templates/server-handler.js`, `${PLUGIN_DIR}/dist/templates/cache-handler.cjs`],
21+
[`${PLUGIN_DIR}/dist/handlers/server.js`, `${PLUGIN_DIR}/dist/handlers/cache.cjs`],
2222
{ base: PLUGIN_DIR, ignore: ['package.json', 'node_modules/next/**'] },
2323
)
2424

@@ -28,8 +28,8 @@ export const createServerHandler = async () => {
2828
})
2929

3030
// copy the next.js standalone build output to the handler directory
31-
copySync(`${NEXT_BUILD_DIR}/standalone/.next`, `${SERVER_HANDLER_DIR}/.next`)
32-
copySync(`${NEXT_BUILD_DIR}/standalone/node_modules`, `${SERVER_HANDLER_DIR}/node_modules`)
31+
copySync(`${BUILD_DIR}/standalone/.next`, `${SERVER_HANDLER_DIR}/.next`)
32+
copySync(`${BUILD_DIR}/standalone/node_modules`, `${SERVER_HANDLER_DIR}/node_modules`)
3333

3434
// create the handler metadata file
3535
writeJSONSync(`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.json`, {
@@ -49,6 +49,6 @@ export const createServerHandler = async () => {
4949
// write the root handler file
5050
writeFileSync(
5151
`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.js`,
52-
`import handler from './dist/templates/server-handler.js';export default handler;export const config = { path: '/*' }`,
52+
`import handler from './dist/handlers/server.js';export default handler;export const config = {path:'/*'}`,
5353
)
5454
}

0 commit comments

Comments
 (0)