Skip to content

Commit ecd9460

Browse files
committed
feat: use async fs methods
1 parent a7f1588 commit ecd9460

9 files changed

+75
-36
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ indent_style = space
55
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
8-
max_line_length = 120
8+
max_line_length = 100
99
trim_trailing_whitespace = true
1010
insert_final_newline = true

.eslintrc.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module.exports = {
66
sourceType: 'module',
77
},
88
rules: {
9+
'arrow-body-style': 'off',
910
'no-param-reassign': ['error', { props: false }],
1011
'no-underscore-dangle': 'off',
12+
'no-magic-numbers': 'off',
1113
'n/no-sync': 'off',
1214
'n/prefer-global/process': 'off',
13-
'no-magic-numbers': 'off',
1415
'unicorn/numeric-separators-style': 'off',
1516
'unicorn/filename-case': ['error', { case: 'kebabCase' }],
1617
'import/no-namespace': 'off',

.prettierrc.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
...require('@netlify/eslint-config-node/.prettierrc.json'),
3+
printWidth: 100,
4+
}

.prettierrc.json

-1
This file was deleted.

package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"@netlify/build": "^29.20.6",
4040
"@netlify/functions": "^2.0.1",
4141
"@vercel/nft": "^0.24.3",
42-
"fs-extra": "^11.1.1"
42+
"fs-extra": "^11.1.1",
43+
"globby": "^13.2.2"
4344
},
4445
"devDependencies": {
4546
"@netlify/eslint-config-node": "^7.0.1",

src/helpers/files.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
import { existsSync } from 'node:fs'
2-
31
import { NetlifyPluginConstants } from '@netlify/build'
4-
import { copySync, moveSync } from 'fs-extra/esm'
2+
import { copy, move, remove } from 'fs-extra/esm'
3+
import { globby } from 'globby'
54

65
import { BUILD_DIR } from './constants.js'
76

87
/**
98
* Move the Next.js build output from the publish dir to a temp dir
109
*/
11-
export const stashBuildOutput = ({ PUBLISH_DIR }: NetlifyPluginConstants) => {
12-
moveSync(PUBLISH_DIR, BUILD_DIR, { overwrite: true })
10+
export const stashBuildOutput = async ({ PUBLISH_DIR }: NetlifyPluginConstants) => {
11+
await move(PUBLISH_DIR, BUILD_DIR, { overwrite: true })
12+
13+
// remove prerendered content from the standalone build (it's also in the main build dir)
14+
await Promise.all(
15+
getPrerenderedContent(`${BUILD_DIR}/standalone/`).map((filename: string) => remove(filename)),
16+
)
17+
}
18+
19+
/**
20+
* Glob for prerendered content in the build output
21+
*/
22+
const getPrerenderedContent = (cwd: string): string[] => {
23+
// TODO: test this
24+
// return globby('**/*.+(html|json|rsc|body|meta)', { cwd, extglob: true })
25+
return []
26+
}
27+
28+
/**
29+
* Upload prerendered content from the main build dir to the blob store
30+
*/
31+
export const storePrerenderedContent = () => {
32+
// TODO: implement
1333
}
1434

1535
/**
16-
* Move static assets to the publish dir so they upload to the CDN
36+
* Move static assets to the publish dir so they are uploaded to the CDN
1737
*/
1838
export const publishStaticAssets = ({ PUBLISH_DIR }: NetlifyPluginConstants) => {
19-
if (existsSync('public')) {
20-
copySync('public', PUBLISH_DIR)
21-
}
22-
copySync(`${BUILD_DIR}/static/`, `${PUBLISH_DIR}/_next/static`)
39+
return Promise.all([
40+
copy('public', PUBLISH_DIR),
41+
copy(`${BUILD_DIR}/static/`, `${PUBLISH_DIR}/_next/static`),
42+
])
2343
}

src/helpers/functions.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
1-
import { writeFileSync } from 'fs'
1+
import { writeFile } from 'fs/promises'
22

33
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'
55

66
import { BUILD_DIR, SERVER_HANDLER_DIR, SERVER_HANDLER_NAME, PLUGIN_DIR } from './constants.js'
77

8-
const pkg = readJsonSync(`${PLUGIN_DIR}/package.json`)
8+
const pkg = await readJson(`${PLUGIN_DIR}/package.json`)
99

1010
/**
1111
* Create a Netlify function to run the Next.js server
1212
*/
1313
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`)
1617

1718
// trace the handler dependencies
1819
const { fileList } = await nodeFileTrace(
1920
[`${PLUGIN_DIR}/dist/handlers/server.js`, `${PLUGIN_DIR}/dist/handlers/cache.cjs`],
2021
{ base: PLUGIN_DIR, ignore: ['package.json', 'node_modules/next/**'] },
2122
)
2223

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+
)
2728

2829
// 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`)
3132

3233
// create the handler metadata file
33-
writeJSONSync(`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.json`, {
34+
await writeJSON(`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.json`, {
3435
config: {
3536
name: 'Next.js Server Handler',
3637
generator: `${pkg.name}@${pkg.version}`,
3738
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+
],
3946
includedFilesBasePath: SERVER_HANDLER_DIR,
4047
},
4148
version: 1,
4249
})
4350

4451
// 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' }))
4653

4754
// write the root handler file
48-
writeFileSync(
55+
await writeFile(
4956
`${SERVER_HANDLER_DIR}/${SERVER_HANDLER_NAME}.js`,
5057
`import handler from './dist/handlers/server.js';export default handler;export const config = {path:'/*'}`,
5158
)

src/index.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import type { NetlifyPluginOptions } from '@netlify/build'
22

33
import { setBuildConfig } from './helpers/config.js'
4-
import { publishStaticAssets, stashBuildOutput } from './helpers/files.js'
4+
import { stashBuildOutput, publishStaticAssets, storePrerenderedContent } from './helpers/files.js'
55
import { createServerHandler } from './helpers/functions.js'
66

7-
type NetlifyPluginOptionsWithFlags = NetlifyPluginOptions & { featureFlags?: Record<string, unknown> }
7+
type NetlifyPluginOptionsWithFlags = NetlifyPluginOptions & {
8+
featureFlags?: Record<string, unknown>
9+
}
810

911
export const onPreBuild = () => {
1012
setBuildConfig()
1113
}
1214

1315
export const onBuild = async ({ constants }: NetlifyPluginOptionsWithFlags) => {
14-
stashBuildOutput(constants)
15-
publishStaticAssets(constants)
16-
await createServerHandler()
16+
await stashBuildOutput(constants)
17+
18+
return Promise.all([
19+
publishStaticAssets(constants),
20+
storePrerenderedContent(),
21+
createServerHandler(),
22+
])
1723
}

0 commit comments

Comments
 (0)