diff --git a/index.js b/index.js index a941f2d22f..7490994e78 100644 --- a/index.js +++ b/index.js @@ -7,12 +7,12 @@ const { PHASE_PRODUCTION_BUILD } = require('next/constants') const { default: loadConfig } = require('next/dist/next-server/server/config') const findUp = require('find-up') const makeDir = require('make-dir') -const pathExists = require('path-exists') -const cpx = require('cpx') +const { copy } = require('cpx') const isStaticExportProject = require('./helpers/isStaticExportProject') const pWriteFile = util.promisify(fs.writeFile) +const pCopy = util.promisify(copy) // * Helpful Plugin Context * // - Between the prebuild and build steps, the project's build command is run @@ -73,26 +73,15 @@ module.exports = { console.log(`** Adding next.config.js with target set to 'serverless' **`) } }, - async onBuild({ constants }) { + async onBuild({ constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) { console.log(`** Running Next on Netlify package **`) nextOnNetlify() // Next-on-netlify puts its files into out_functions and out_publish // Copy files from next-on-netlify's output to the right functions/publish dirs - - // TO-DO: use FUNCTIONS_DIST when internal bug is fixed - const { PUBLISH_DIR } = constants - // if (!(await pathExists(FUNCTIONS_DIST))) { - // await makeDir(FUNCTIONS_DIST) - // } - const hasPublishDir = await pathExists(PUBLISH_DIR) - if (!hasPublishDir) { - await makeDir(PUBLISH_DIR) - } - - // TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict - // with function names that next-on-netlify can generate - // cpx.copySync('out_functions/**/*', FUNCTIONS_SRC); - cpx.copySync('out_publish/**/*', PUBLISH_DIR) + await makeDir(PUBLISH_DIR) + await Promise.all([pCopy('out_functions/**', FUNCTIONS_SRC), pCopy('out_publish/**', PUBLISH_DIR)]) }, } + +const DEFAULT_FUNCTIONS_SRC = 'netlify-automatic-functions' diff --git a/package.json b/package.json index 33dfbcc0df..1299ccf21c 100644 --- a/package.json +++ b/package.json @@ -28,13 +28,13 @@ "find-up": "^4.1.0", "make-dir": "^3.1.0", "next": "^9.5.3", - "next-on-netlify": "^2.6.0", - "path-exists": "^4.0.0" + "next-on-netlify": "^2.6.0" }, "devDependencies": { "@netlify/eslint-config-node": "^0.3.0", "husky": "^4.3.0", "jest": "^26.6.1", + "path-exists": "^4.0.0", "prettier": "^2.1.2", "react": "^17.0.1", "react-dom": "^17.0.1", diff --git a/test/fixtures/publish_copy_files/publish/subdir/dummy.txt b/test/fixtures/functions_copy_files/out_functions/next_random/nextJsPage.js similarity index 100% rename from test/fixtures/publish_copy_files/publish/subdir/dummy.txt rename to test/fixtures/functions_copy_files/out_functions/next_random/nextJsPage.js diff --git a/test/fixtures/publish_copy_files/subdir/dummy.txt b/test/fixtures/functions_copy_files/out_functions/next_random/next_random.js similarity index 100% rename from test/fixtures/publish_copy_files/subdir/dummy.txt rename to test/fixtures/functions_copy_files/out_functions/next_random/next_random.js diff --git a/test/index.js b/test/index.js index d345887643..be67b695c2 100644 --- a/test/index.js +++ b/test/index.js @@ -137,7 +137,7 @@ describe('onBuild()', () => { test('runs next on netlify', async () => { await plugin.onBuild({ constants: { - PUBLISH_DIR: '', + PUBLISH_DIR: '.', }, }) @@ -155,7 +155,7 @@ describe('onBuild()', () => { expect(await pathExists(PUBLISH_DIR)).toBeTruthy() }) - test('calls copySync with correct args', async () => { + test('copy files to the publish directory', async () => { await useFixture('publish_copy_files') const PUBLISH_DIR = 'publish' await plugin.onBuild({ @@ -166,4 +166,19 @@ describe('onBuild()', () => { expect(await pathExists(`${PUBLISH_DIR}/subdir/dummy.txt`)).toBeTruthy() }) + + test.each([ + { FUNCTIONS_SRC: 'functions', resolvedFunctions: 'functions' }, + { FUNCTIONS_SRC: undefined, resolvedFunctions: 'netlify-automatic-functions' }, + ])('copy files to the functions directory', async ({ FUNCTIONS_SRC, resolvedFunctions }) => { + await useFixture('functions_copy_files') + await plugin.onBuild({ + constants: { + FUNCTIONS_SRC, + PUBLISH_DIR: '.', + }, + }) + + expect(await pathExists(`${resolvedFunctions}/next_random/next_random.js`)).toBeTruthy() + }) })