Skip to content

Use the site's Functions directory #39

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 1 commit into from
Nov 16, 2020
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
25 changes: 7 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to change this value once the final filename has been decided.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait does this need to be defined before FUNCTIONS_SRC is defaulted to it on 76? 🤔

Copy link
Author

@ehmicky ehmicky Nov 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because default parameters are evaluated when the function is executed, and onPostBuild() is executed after the top-level scope has been executed.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 17 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('onBuild()', () => {
test('runs next on netlify', async () => {
await plugin.onBuild({
constants: {
PUBLISH_DIR: '',
PUBLISH_DIR: '.',
},
})

Expand All @@ -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({
Expand All @@ -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()
})
})