Skip to content

Commit 5a23158

Browse files
committed
pass constants FUNCTIONS_SRC and PUBLISH_DIR to nextOnNetlify
1 parent 97cc69a commit 5a23158

File tree

6 files changed

+28
-40
lines changed

6 files changed

+28
-40
lines changed

index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ const util = require('util')
44

55
const findUp = require('find-up')
66
const makeDir = require('make-dir')
7-
const { copy } = require('cpx')
87

98
const isStaticExportProject = require('./helpers/isStaticExportProject')
109
const validateNextUsage = require('./helpers/validateNextUsage')
1110

1211
const pWriteFile = util.promisify(fs.writeFile)
13-
const pCopy = util.promisify(copy)
1412

1513
// * Helpful Plugin Context *
1614
// - Between the prebuild and build steps, the project's build command is run
@@ -72,16 +70,13 @@ module.exports = {
7270
async onBuild({ constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) {
7371
console.log(`** Running Next on Netlify package **`)
7472

73+
await makeDir(PUBLISH_DIR)
74+
7575
// We cannot load `next-on-netlify` (which depends on `next`) at the
7676
// top-level because we validate whether the site is using `next`
7777
// inside `onPreBuild`.
7878
const nextOnNetlify = require('next-on-netlify')
79-
nextOnNetlify()
80-
81-
// Next-on-netlify puts its files into out_functions and out_publish
82-
// Copy files from next-on-netlify's output to the right functions/publish dirs
83-
await makeDir(PUBLISH_DIR)
84-
await Promise.all([pCopy('out_functions/**', FUNCTIONS_SRC), pCopy('out_publish/**', PUBLISH_DIR)])
79+
nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR })
8580
},
8681
}
8782

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"cpx": "^1.5.0",
2828
"find-up": "^4.1.0",
2929
"make-dir": "^3.1.0",
30-
"next-on-netlify": "^2.6.0"
30+
"next-on-netlify": "^2.6.2"
3131
},
3232
"devDependencies": {
3333
"@netlify/eslint-config-node": "^0.3.0",

sample/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"date-fns": "^2.16.1",
1212
"next": "9.5.3",
13-
"next-on-netlify": "^2.6.0",
13+
"next-on-netlify": "^2.6.2",
1414
"react": "16.13.1",
1515
"react-dom": "16.13.1"
1616
}

test/index.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require('fs')
12
const path = require('path')
23
const process = require('process')
34
const { promisify } = require('util')
@@ -121,51 +122,43 @@ describe('preBuild()', () => {
121122
})
122123

123124
describe('onBuild()', () => {
124-
test('runs next on netlify', async () => {
125-
await plugin.onBuild({
126-
constants: {
127-
PUBLISH_DIR: '.',
128-
},
129-
})
130-
131-
expect(nextOnNetlify.mock.calls.length).toEqual(1)
132-
})
133-
134-
test('calls makeDir with correct path', async () => {
125+
test('runs NoN with functions_src & publish_dir options', async () => {
135126
const PUBLISH_DIR = 'some/path'
127+
const FUNCTIONS_SRC = 'other/path'
136128
await plugin.onBuild({
137129
constants: {
138130
PUBLISH_DIR,
131+
FUNCTIONS_SRC,
139132
},
140133
})
141134

142-
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
135+
const nextOnNetlifyOptions = nextOnNetlify.mock.calls[0][0]
136+
expect(nextOnNetlifyOptions.functionsDir).toEqual(FUNCTIONS_SRC)
137+
expect(nextOnNetlifyOptions.publishDir).toEqual(PUBLISH_DIR)
143138
})
144139

145-
test('copy files to the publish directory', async () => {
146-
await useFixture('publish_copy_files')
147-
const PUBLISH_DIR = 'publish'
140+
test('runs NoN with publish_dir option only', async () => {
141+
const defaultFunctionsSrc = 'netlify-automatic-functions'
142+
const PUBLISH_DIR = 'some/path'
148143
await plugin.onBuild({
149144
constants: {
150145
PUBLISH_DIR,
151146
},
152147
})
153148

154-
expect(await pathExists(`${PUBLISH_DIR}/subdir/dummy.txt`)).toBeTruthy()
149+
const nextOnNetlifyOptions = nextOnNetlify.mock.calls[0][0]
150+
expect(nextOnNetlifyOptions.functionsDir).toEqual(defaultFunctionsSrc)
151+
expect(nextOnNetlifyOptions.publishDir).toEqual(PUBLISH_DIR)
155152
})
156153

157-
test.each([
158-
{ FUNCTIONS_SRC: 'functions', resolvedFunctions: 'functions' },
159-
{ FUNCTIONS_SRC: undefined, resolvedFunctions: 'netlify-automatic-functions' },
160-
])('copy files to the functions directory', async ({ FUNCTIONS_SRC, resolvedFunctions }) => {
161-
await useFixture('functions_copy_files')
154+
test('calls makeDir with correct path', async () => {
155+
const PUBLISH_DIR = 'some/path'
162156
await plugin.onBuild({
163157
constants: {
164-
FUNCTIONS_SRC,
165-
PUBLISH_DIR: '.',
158+
PUBLISH_DIR,
166159
},
167160
})
168161

169-
expect(await pathExists(`${resolvedFunctions}/next_random/next_random.js`)).toBeTruthy()
162+
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
170163
})
171164
})

0 commit comments

Comments
 (0)