Skip to content

Commit e0c8011

Browse files
committed
Use the site's Functions directory
1 parent 1f90614 commit e0c8011

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

index.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const { PHASE_PRODUCTION_BUILD } = require('next/constants')
77
const { default: loadConfig } = require('next/dist/next-server/server/config')
88
const findUp = require('find-up')
99
const makeDir = require('make-dir')
10-
const pathExists = require('path-exists')
11-
const cpx = require('cpx')
10+
const { copy } = require('cpx')
1211

1312
const isStaticExportProject = require('./helpers/isStaticExportProject')
1413

1514
const pWriteFile = util.promisify(fs.writeFile)
15+
const pCopy = util.promisify(copy)
1616

1717
// * Helpful Plugin Context *
1818
// - Between the prebuild and build steps, the project's build command is run
@@ -73,26 +73,15 @@ module.exports = {
7373
console.log(`** Adding next.config.js with target set to 'serverless' **`)
7474
}
7575
},
76-
async onBuild({ constants }) {
76+
async onBuild({ constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) {
7777
console.log(`** Running Next on Netlify package **`)
7878
nextOnNetlify()
7979

8080
// Next-on-netlify puts its files into out_functions and out_publish
8181
// Copy files from next-on-netlify's output to the right functions/publish dirs
82-
83-
// TO-DO: use FUNCTIONS_DIST when internal bug is fixed
84-
const { PUBLISH_DIR } = constants
85-
// if (!(await pathExists(FUNCTIONS_DIST))) {
86-
// await makeDir(FUNCTIONS_DIST)
87-
// }
88-
const hasPublishDir = await pathExists(PUBLISH_DIR)
89-
if (!hasPublishDir) {
90-
await makeDir(PUBLISH_DIR)
91-
}
92-
93-
// TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
94-
// with function names that next-on-netlify can generate
95-
// cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
96-
cpx.copySync('out_publish/**/*', PUBLISH_DIR)
82+
await makeDir(PUBLISH_DIR)
83+
await Promise.all([pCopy('out_functions/**', FUNCTIONS_SRC), pCopy('out_publish/**', PUBLISH_DIR)])
9784
},
9885
}
86+
87+
const DEFAULT_FUNCTIONS_SRC = 'netlify-automatic-functions'

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
"find-up": "^4.1.0",
2929
"make-dir": "^3.1.0",
3030
"next": "^9.5.3",
31-
"next-on-netlify": "^2.6.0",
32-
"path-exists": "^4.0.0"
31+
"next-on-netlify": "^2.6.0"
3332
},
3433
"devDependencies": {
3534
"@netlify/eslint-config-node": "^0.3.0",
3635
"husky": "^4.3.0",
3736
"jest": "^26.6.1",
37+
"path-exists": "^4.0.0",
3838
"prettier": "^2.1.2",
3939
"react": "^17.0.1",
4040
"react-dom": "^17.0.1",

test/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('onBuild()', () => {
137137
test('runs next on netlify', async () => {
138138
await plugin.onBuild({
139139
constants: {
140-
PUBLISH_DIR: '',
140+
PUBLISH_DIR: '.',
141141
},
142142
})
143143

@@ -155,7 +155,7 @@ describe('onBuild()', () => {
155155
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
156156
})
157157

158-
test('calls copySync with correct args', async () => {
158+
test('copy files to the publish directory', async () => {
159159
await useFixture('publish_copy_files')
160160
const PUBLISH_DIR = 'publish'
161161
await plugin.onBuild({
@@ -166,4 +166,19 @@ describe('onBuild()', () => {
166166

167167
expect(await pathExists(`${PUBLISH_DIR}/subdir/dummy.txt`)).toBeTruthy()
168168
})
169+
170+
test.each([
171+
{ FUNCTIONS_SRC: 'functions', resolvedFunctions: 'functions' },
172+
{ FUNCTIONS_SRC: undefined, resolvedFunctions: 'netlify-automatic-functions' },
173+
])('copy files to the functions directory', async ({ FUNCTIONS_SRC, resolvedFunctions }) => {
174+
await useFixture('functions_copy_files')
175+
await plugin.onBuild({
176+
constants: {
177+
FUNCTIONS_SRC,
178+
PUBLISH_DIR: '.',
179+
},
180+
})
181+
182+
expect(await pathExists(`${resolvedFunctions}/next_random/next_random.js`)).toBeTruthy()
183+
})
169184
})

0 commit comments

Comments
 (0)