Skip to content

fix: use platform-agnostic paths, and add test to be sure #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"chalk": "^4.1.2",
"fs-extra": "^10.0.0",
"moize": "^6.1.0",
"node-stream-zip": "^1.15.0",
"node-fetch": "^2.6.5",
"node-stream-zip": "^1.15.0",
"outdent": "^0.8.0",
"p-limit": "^3.1.0",
"pathe": "^0.2.0",
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
const { readJSON } = require('fs-extra')
const { join, dirname, relative } = require('pathe')
const slash = require('slash')

const defaultFailBuild = (message, { error }) => {
throw new Error(`${message}\n${error && error.stack}`)
Expand Down Expand Up @@ -131,7 +132,7 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) =>
`${publish}/serverless/**`,
`${publish}/*.json`,
`${publish}/BUILD_ID`,
...ignore.map((path) => `!${path}`),
...ignore.map((path) => `!${slash(path)}`),
)

const nextRoot = resolveModuleRoot('next')
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { join, relative } = require('path')

const { copyFile, ensureDir, writeFile, writeJSON } = require('fs-extra')
const { join, relative } = require('pathe')

const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } = require('../constants')
const getHandler = require('../templates/getHandler')
Expand Down
30 changes: 29 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir } = require('fs-extra')
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir, readJson } = require('fs-extra')
const path = require('path')
const process = require('process')
const os = require('os')
Expand Down Expand Up @@ -41,10 +41,19 @@ const changeCwd = function (cwd) {
const onBuildHasRun = (netlifyConfig) =>
Boolean(netlifyConfig.functions[HANDLER_FUNCTION_NAME]?.included_files?.some((file) => file.includes('BUILD_ID')))

const rewriteAppDir = async function () {
const manifest = path.join('.next', 'required-server-files.json')
const manifestContent = await readJson(manifest)
manifestContent.appDir = process.cwd()

await writeJSON(manifest, manifestContent)
}

// Move .next from sample project to current directory
const moveNextDist = async function () {
await stubModules(['next', 'sharp'])
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), '.next'))
await rewriteAppDir()
}

const stubModules = async function (modules) {
Expand Down Expand Up @@ -231,6 +240,25 @@ describe('onBuild()', () => {
expect(readFileSync(handlerPagesFile, 'utf8')).toMatchSnapshot()
expect(readFileSync(odbHandlerPagesFile, 'utf8')).toMatchSnapshot()
})

test('generates entrypoints with correct references', async () => {
await moveNextDist()
await plugin.onBuild(defaultArgs)

const handlerFile = path.join(
constants.INTERNAL_FUNCTIONS_SRC,
HANDLER_FUNCTION_NAME,
`${HANDLER_FUNCTION_NAME}.js`,
)
const odbHandlerFile = path.join(constants.INTERNAL_FUNCTIONS_SRC, ODB_FUNCTION_NAME, `${ODB_FUNCTION_NAME}.js`)
expect(existsSync(handlerFile)).toBeTruthy()
expect(existsSync(odbHandlerFile)).toBeTruthy()

expect(readFileSync(handlerFile, 'utf8')).toMatch(`(config, "../../..", pageRoot, staticManifest)`)
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`(config, "../../..", pageRoot, staticManifest)`)
expect(readFileSync(handlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`)
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`)
})
})

describe('onPostBuild', () => {
Expand Down