Skip to content

Commit d448b11

Browse files
authored
fix: use platform-agnostic paths, and add test to be sure (#736)
* fix: use platform-agnostic paths, and add test to be sure * chore: release 4.0.0-beta.3 Release-as: 4.0.0-beta.3
1 parent 01fa113 commit d448b11

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
"chalk": "^4.1.2",
5959
"fs-extra": "^10.0.0",
6060
"moize": "^6.1.0",
61-
"node-stream-zip": "^1.15.0",
6261
"node-fetch": "^2.6.5",
62+
"node-stream-zip": "^1.15.0",
6363
"outdent": "^0.8.0",
6464
"p-limit": "^3.1.0",
6565
"pathe": "^0.2.0",

src/helpers/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
const { readJSON } = require('fs-extra')
33
const { join, dirname, relative } = require('pathe')
4+
const slash = require('slash')
45

56
const defaultFailBuild = (message, { error }) => {
67
throw new Error(`${message}\n${error && error.stack}`)
@@ -131,7 +132,7 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) =>
131132
`${publish}/serverless/**`,
132133
`${publish}/*.json`,
133134
`${publish}/BUILD_ID`,
134-
...ignore.map((path) => `!${path}`),
135+
...ignore.map((path) => `!${slash(path)}`),
135136
)
136137

137138
const nextRoot = resolveModuleRoot('next')

src/helpers/functions.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const { join, relative } = require('path')
2-
31
const { copyFile, ensureDir, writeFile, writeJSON } = require('fs-extra')
2+
const { join, relative } = require('pathe')
43

54
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } = require('../constants')
65
const getHandler = require('../templates/getHandler')

test/index.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir } = require('fs-extra')
1+
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir, readJson } = require('fs-extra')
22
const path = require('path')
33
const process = require('process')
44
const os = require('os')
@@ -41,10 +41,19 @@ const changeCwd = function (cwd) {
4141
const onBuildHasRun = (netlifyConfig) =>
4242
Boolean(netlifyConfig.functions[HANDLER_FUNCTION_NAME]?.included_files?.some((file) => file.includes('BUILD_ID')))
4343

44+
const rewriteAppDir = async function () {
45+
const manifest = path.join('.next', 'required-server-files.json')
46+
const manifestContent = await readJson(manifest)
47+
manifestContent.appDir = process.cwd()
48+
49+
await writeJSON(manifest, manifestContent)
50+
}
51+
4452
// Move .next from sample project to current directory
4553
const moveNextDist = async function () {
4654
await stubModules(['next', 'sharp'])
4755
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), '.next'))
56+
await rewriteAppDir()
4857
}
4958

5059
const stubModules = async function (modules) {
@@ -231,6 +240,25 @@ describe('onBuild()', () => {
231240
expect(readFileSync(handlerPagesFile, 'utf8')).toMatchSnapshot()
232241
expect(readFileSync(odbHandlerPagesFile, 'utf8')).toMatchSnapshot()
233242
})
243+
244+
test('generates entrypoints with correct references', async () => {
245+
await moveNextDist()
246+
await plugin.onBuild(defaultArgs)
247+
248+
const handlerFile = path.join(
249+
constants.INTERNAL_FUNCTIONS_SRC,
250+
HANDLER_FUNCTION_NAME,
251+
`${HANDLER_FUNCTION_NAME}.js`,
252+
)
253+
const odbHandlerFile = path.join(constants.INTERNAL_FUNCTIONS_SRC, ODB_FUNCTION_NAME, `${ODB_FUNCTION_NAME}.js`)
254+
expect(existsSync(handlerFile)).toBeTruthy()
255+
expect(existsSync(odbHandlerFile)).toBeTruthy()
256+
257+
expect(readFileSync(handlerFile, 'utf8')).toMatch(`(config, "../../..", pageRoot, staticManifest)`)
258+
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`(config, "../../..", pageRoot, staticManifest)`)
259+
expect(readFileSync(handlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`)
260+
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`)
261+
})
234262
})
235263

236264
describe('onPostBuild', () => {

0 commit comments

Comments
 (0)