Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit efa53fd

Browse files
ascorbickodiakhq[bot]
andauthoredDec 10, 2021
fix: handle Windows paths correctly (#948)
* fix: handle Windows paths correctly * chore: enable `ignore` command Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent b2f8f5b commit efa53fd

File tree

6 files changed

+107
-20
lines changed

6 files changed

+107
-20
lines changed
 

‎src/helpers/cache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { posix } from 'path'
1+
import { join } from 'path'
22

33
export const restoreCache = async ({ cache, publish }) => {
4-
const cacheDir = posix.join(publish, 'cache')
4+
const cacheDir = join(publish, 'cache')
55

66
if (await cache.restore(cacheDir)) {
77
console.log('Next.js cache restored.')
@@ -11,9 +11,9 @@ export const restoreCache = async ({ cache, publish }) => {
1111
}
1212

1313
export const saveCache = async ({ cache, publish }) => {
14-
const cacheDir = posix.join(publish, 'cache')
14+
const cacheDir = join(publish, 'cache')
1515

16-
const buildManifest = posix.join(publish, 'build-manifest.json')
16+
const buildManifest = join(publish, 'build-manifest.json')
1717
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
1818
console.log('Next.js cache saved.')
1919
} else {

‎src/helpers/functions.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,16 @@ export const generateFunctions = async (
3737
* This is just so that the nft bundler knows about them. We'll eventually do this better.
3838
*/
3939
export const generatePagesResolver = async ({
40-
constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
41-
netlifyConfig,
40+
constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, PUBLISH_DIR },
4241
target,
4342
}: {
4443
constants: NetlifyPluginConstants
45-
netlifyConfig: NetlifyConfig
4644
target: string
4745
}): Promise<void> => {
4846
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
4947

5048
const jsSource = await getPageResolver({
51-
netlifyConfig,
49+
publish: PUBLISH_DIR,
5250
target,
5351
})
5452

‎src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const plugin: NetlifyPlugin = {
5757
configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) })
5858

5959
await generateFunctions(constants, appDir)
60-
await generatePagesResolver({ netlifyConfig, target, constants })
60+
await generatePagesResolver({ target, constants })
6161

6262
await movePublicFiles({ appDir, outdir, publish })
6363

‎src/templates/getPageResolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { HANDLER_FUNCTION_NAME } from '../constants'
99
// Generate a file full of require.resolve() calls for all the pages in the
1010
// build. This is used by the nft bundler to find all the pages.
1111

12-
export const getPageResolver = async ({ netlifyConfig, target }) => {
12+
export const getPageResolver = async ({ publish, target }: { publish: string; target: string }) => {
1313
const functionDir = posix.resolve(posix.join('.netlify', 'functions', HANDLER_FUNCTION_NAME))
14-
const root = posix.join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless', 'pages')
14+
const root = posix.resolve(slash(publish), target === 'server' ? 'server' : 'serverless', 'pages')
1515

1616
const pages = await glob('**/*.js', {
1717
cwd: root,

‎test/__snapshots__/index.js.snap

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,76 @@ exports.resolvePages = () => {
7070
}"
7171
`;
7272

73+
exports[`onBuild() generates a file referencing all when publish dir is a subdirectory 1`] = `
74+
"// This file is purely to allow nft to know about these pages. It should be temporary.
75+
exports.resolvePages = () => {
76+
try {
77+
require.resolve('../../../web/.next/server/pages/_app.js')
78+
require.resolve('../../../web/.next/server/pages/_document.js')
79+
require.resolve('../../../web/.next/server/pages/_error.js')
80+
require.resolve('../../../web/.next/server/pages/api/enterPreview.js')
81+
require.resolve('../../../web/.next/server/pages/api/exitPreview.js')
82+
require.resolve('../../../web/.next/server/pages/api/hello-background.js')
83+
require.resolve('../../../web/.next/server/pages/api/hello.js')
84+
require.resolve('../../../web/.next/server/pages/api/shows/[...params].js')
85+
require.resolve('../../../web/.next/server/pages/api/shows/[id].js')
86+
require.resolve('../../../web/.next/server/pages/deep/import.js')
87+
require.resolve('../../../web/.next/server/pages/getServerSideProps/[id].js')
88+
require.resolve('../../../web/.next/server/pages/getServerSideProps/all/[[...slug]].js')
89+
require.resolve('../../../web/.next/server/pages/getServerSideProps/static.js')
90+
require.resolve('../../../web/.next/server/pages/getStaticProps/[id].js')
91+
require.resolve('../../../web/.next/server/pages/getStaticProps/env.js')
92+
require.resolve('../../../web/.next/server/pages/getStaticProps/static.js')
93+
require.resolve('../../../web/.next/server/pages/getStaticProps/with-revalidate.js')
94+
require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[...slug].js')
95+
require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[id].js')
96+
require.resolve('../../../web/.next/server/pages/getStaticProps/withFallbackBlocking/[id].js')
97+
require.resolve('../../../web/.next/server/pages/getStaticProps/withRevalidate/[id].js')
98+
require.resolve('../../../web/.next/server/pages/getStaticProps/withRevalidate/withFallback/[id].js')
99+
require.resolve('../../../web/.next/server/pages/index.js')
100+
require.resolve('../../../web/.next/server/pages/middle/_middleware.js')
101+
require.resolve('../../../web/.next/server/pages/previewTest.js')
102+
require.resolve('../../../web/.next/server/pages/shows/[...params].js')
103+
require.resolve('../../../web/.next/server/pages/shows/[id].js')
104+
} catch {}
105+
}"
106+
`;
107+
108+
exports[`onBuild() generates a file referencing all when publish dir is a subdirectory 2`] = `
109+
"// This file is purely to allow nft to know about these pages. It should be temporary.
110+
exports.resolvePages = () => {
111+
try {
112+
require.resolve('../../../web/.next/server/pages/_app.js')
113+
require.resolve('../../../web/.next/server/pages/_document.js')
114+
require.resolve('../../../web/.next/server/pages/_error.js')
115+
require.resolve('../../../web/.next/server/pages/api/enterPreview.js')
116+
require.resolve('../../../web/.next/server/pages/api/exitPreview.js')
117+
require.resolve('../../../web/.next/server/pages/api/hello-background.js')
118+
require.resolve('../../../web/.next/server/pages/api/hello.js')
119+
require.resolve('../../../web/.next/server/pages/api/shows/[...params].js')
120+
require.resolve('../../../web/.next/server/pages/api/shows/[id].js')
121+
require.resolve('../../../web/.next/server/pages/deep/import.js')
122+
require.resolve('../../../web/.next/server/pages/getServerSideProps/[id].js')
123+
require.resolve('../../../web/.next/server/pages/getServerSideProps/all/[[...slug]].js')
124+
require.resolve('../../../web/.next/server/pages/getServerSideProps/static.js')
125+
require.resolve('../../../web/.next/server/pages/getStaticProps/[id].js')
126+
require.resolve('../../../web/.next/server/pages/getStaticProps/env.js')
127+
require.resolve('../../../web/.next/server/pages/getStaticProps/static.js')
128+
require.resolve('../../../web/.next/server/pages/getStaticProps/with-revalidate.js')
129+
require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[...slug].js')
130+
require.resolve('../../../web/.next/server/pages/getStaticProps/withFallback/[id].js')
131+
require.resolve('../../../web/.next/server/pages/getStaticProps/withFallbackBlocking/[id].js')
132+
require.resolve('../../../web/.next/server/pages/getStaticProps/withRevalidate/[id].js')
133+
require.resolve('../../../web/.next/server/pages/getStaticProps/withRevalidate/withFallback/[id].js')
134+
require.resolve('../../../web/.next/server/pages/index.js')
135+
require.resolve('../../../web/.next/server/pages/middle/_middleware.js')
136+
require.resolve('../../../web/.next/server/pages/previewTest.js')
137+
require.resolve('../../../web/.next/server/pages/shows/[...params].js')
138+
require.resolve('../../../web/.next/server/pages/shows/[id].js')
139+
} catch {}
140+
}"
141+
`;
142+
73143
exports[`onBuild() generates static files manifest 1`] = `
74144
Array [
75145
Array [

‎test/index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const plugin = require('../src')
1111
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } = require('../src/constants')
1212
const { join } = require('pathe')
1313
const { matchMiddleware, stripLocale, matchesRedirect, matchesRewrite } = require('../src/helpers/files')
14+
const { dirname } = require('path')
1415

1516
const FIXTURES_DIR = `${__dirname}/fixtures`
1617
const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default`
@@ -83,19 +84,20 @@ const changeCwd = function (cwd) {
8384
const onBuildHasRun = (netlifyConfig) =>
8485
Boolean(netlifyConfig.functions[HANDLER_FUNCTION_NAME]?.included_files?.some((file) => file.includes('BUILD_ID')))
8586

86-
const rewriteAppDir = async function () {
87-
const manifest = path.join('.next', 'required-server-files.json')
87+
const rewriteAppDir = async function (dir = '.next') {
88+
const manifest = path.join(dir, 'required-server-files.json')
8889
const manifestContent = await readJson(manifest)
8990
manifestContent.appDir = process.cwd()
9091

9192
await writeJSON(manifest, manifestContent)
9293
}
9394

9495
// Move .next from sample project to current directory
95-
const moveNextDist = async function () {
96+
const moveNextDist = async function (dir = '.next') {
9697
await stubModules(['next', 'sharp'])
97-
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), '.next'))
98-
await rewriteAppDir()
98+
await ensureDir(dirname(dir))
99+
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir))
100+
await rewriteAppDir(dir)
99101
}
100102

101103
const stubModules = async function (modules) {
@@ -129,7 +131,7 @@ beforeEach(async () => {
129131
restoreCwd = changeCwd(tmpDir.path)
130132
cleanup = tmpDir.cleanup
131133

132-
netlifyConfig.build.publish = path.posix.resolve('.next')
134+
netlifyConfig.build.publish = path.resolve('.next')
133135
netlifyConfig.build.environment = {}
134136

135137
netlifyConfig.redirects = []
@@ -183,7 +185,7 @@ describe('preBuild()', () => {
183185
utils: { ...utils, cache: { restore } },
184186
})
185187

186-
expect(restore).toHaveBeenCalledWith(path.posix.resolve('.next/cache'))
188+
expect(restore).toHaveBeenCalledWith(path.resolve('.next/cache'))
187189
})
188190

189191
it('forces the target to "server"', async () => {
@@ -337,6 +339,23 @@ describe('onBuild()', () => {
337339
expect(readFileSync(odbHandlerPagesFile, 'utf8')).toMatchSnapshot()
338340
})
339341

342+
test('generates a file referencing all when publish dir is a subdirectory', async () => {
343+
const dir = 'web/.next'
344+
await moveNextDist(dir)
345+
netlifyConfig.build.publish = path.resolve(dir)
346+
const config = {
347+
...defaultArgs,
348+
netlifyConfig,
349+
constants: { ...constants, PUBLISH_DIR: dir },
350+
}
351+
await plugin.onBuild(config)
352+
const handlerPagesFile = path.join(constants.INTERNAL_FUNCTIONS_SRC, HANDLER_FUNCTION_NAME, 'pages.js')
353+
const odbHandlerPagesFile = path.join(constants.INTERNAL_FUNCTIONS_SRC, ODB_FUNCTION_NAME, 'pages.js')
354+
355+
expect(readFileSync(handlerPagesFile, 'utf8')).toMatchSnapshot()
356+
expect(readFileSync(odbHandlerPagesFile, 'utf8')).toMatchSnapshot()
357+
})
358+
340359
test('generates entrypoints with correct references', async () => {
341360
await moveNextDist()
342361
await plugin.onBuild(defaultArgs)
@@ -368,8 +387,8 @@ describe('onPostBuild', () => {
368387
utils: { ...utils, cache: { save }, functions: { list: jest.fn().mockResolvedValue([]) } },
369388
})
370389

371-
expect(save).toHaveBeenCalledWith(path.posix.resolve('.next/cache'), {
372-
digests: [path.posix.resolve('.next/build-manifest.json')],
390+
expect(save).toHaveBeenCalledWith(path.resolve('.next/cache'), {
391+
digests: [path.resolve('.next/build-manifest.json')],
373392
})
374393
})
375394

0 commit comments

Comments
 (0)
Please sign in to comment.