|
1 | | -import { writeFileSync } from 'fs' |
| 1 | +import { writeFile } from 'fs/promises' |
2 | 2 |
|
3 | 3 | import { nodeFileTrace } from '@vercel/nft' |
4 | | -import { copySync, emptyDirSync, readJsonSync, writeJSONSync } from 'fs-extra/esm' |
| 4 | +import { copy, emptyDir, ensureDir, readJson, writeJSON } from 'fs-extra/esm' |
5 | 5 |
|
6 | 6 | import { BUILD_DIR, SERVER_HANDLER_DIR, SERVER_HANDLER_NAME, PLUGIN_DIR } from './constants.js' |
7 | 7 |
|
8 | | -const pkg = readJsonSync(`${PLUGIN_DIR}/package.json`) |
| 8 | +const pkg = await readJson(`${PLUGIN_DIR}/package.json`) |
9 | 9 |
|
10 | 10 | /** |
11 | 11 | * Create a Netlify function to run the Next.js server |
12 | 12 | */ |
13 | 13 | export const createServerHandler = async () => { |
14 | | - // clear the handler directory |
15 | | - emptyDirSync(SERVER_HANDLER_DIR) |
| 14 | + // reset the handler directory |
| 15 | + await emptyDir(SERVER_HANDLER_DIR) |
| 16 | + await ensureDir(`${SERVER_HANDLER_DIR}/node_modules`) |
16 | 17 |
|
17 | 18 | // trace the handler dependencies |
18 | 19 | const { fileList } = await nodeFileTrace( |
19 | 20 | [`${PLUGIN_DIR}/dist/handlers/server.js`, `${PLUGIN_DIR}/dist/handlers/cache.cjs`], |
20 | 21 | { base: PLUGIN_DIR, ignore: ['package.json', 'node_modules/next/**'] }, |
21 | 22 | ) |
22 | 23 |
|
23 | | - // copy the handler dependencies |
24 | | - fileList.forEach((path) => { |
25 | | - copySync(`${PLUGIN_DIR}/${path}`, `${SERVER_HANDLER_DIR}/${path}`) |
26 | | - }) |
| 24 | + await Promise.all( |
| 25 | + // copy the handler dependencies |
| 26 | + [...fileList].map((path) => copy(`${PLUGIN_DIR}/${path}`, `${SERVER_HANDLER_DIR}/${path}`)), |
| 27 | + ) |
27 | 28 |
|
28 | 29 | // copy the next.js standalone build output to the handler directory |
29 | | - copySync(`${BUILD_DIR}/standalone/.next`, `${SERVER_HANDLER_DIR}/.next`) |
30 | | - copySync(`${BUILD_DIR}/standalone/node_modules`, `${SERVER_HANDLER_DIR}/node_modules`) |
| 30 | + await copy(`${BUILD_DIR}/standalone/.next`, `${SERVER_HANDLER_DIR}/.next`) |
| 31 | + await copy(`${BUILD_DIR}/standalone/node_modules`, `${SERVER_HANDLER_DIR}/node_modules`) |
31 | 32 |
|
32 | 33 | // create the handler metadata file |
33 | | - writeJSONSync(`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.json`, { |
| 34 | + await writeJSON(`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.json`, { |
34 | 35 | config: { |
35 | 36 | name: 'Next.js Server Handler', |
36 | 37 | generator: `${pkg.name}@${pkg.version}`, |
37 | 38 | nodeBundler: 'none', |
38 | | - includedFiles: [`${SERVER_HANDLER_NAME}*`, 'package.json', 'dist/**', '.next/**', 'node_modules/**'], |
| 39 | + includedFiles: [ |
| 40 | + `${SERVER_HANDLER_NAME}*`, |
| 41 | + 'package.json', |
| 42 | + 'dist/**', |
| 43 | + '.next/**', |
| 44 | + 'node_modules/**', |
| 45 | + ], |
39 | 46 | includedFilesBasePath: SERVER_HANDLER_DIR, |
40 | 47 | }, |
41 | 48 | version: 1, |
42 | 49 | }) |
43 | 50 |
|
44 | 51 | // configure ESM |
45 | | - writeFileSync(`${SERVER_HANDLER_DIR}/package.json`, JSON.stringify({ type: 'module' })) |
| 52 | + await writeFile(`${SERVER_HANDLER_DIR}/package.json`, JSON.stringify({ type: 'module' })) |
46 | 53 |
|
47 | 54 | // write the root handler file |
48 | | - writeFileSync( |
| 55 | + await writeFile( |
49 | 56 | `${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.js`, |
50 | 57 | `import handler from './dist/handlers/server.js';export default handler;export const config = {path:'/*'}`, |
51 | 58 | ) |
|
0 commit comments