Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

use configurable functionsDir and publishDir throughout NoN #89

Merged
merged 2 commits into from
Nov 17, 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: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ const copyPublicFiles = require("./lib/steps/copyPublicFiles");
const copyNextAssets = require("./lib/steps/copyNextAssets");
const setupPages = require("./lib/steps/setupPages");
const setupRedirects = require("./lib/steps/setupRedirects");
const {
NETLIFY_PUBLISH_PATH,
NETLIFY_FUNCTIONS_PATH,
} = require("./lib/config");

const nextOnNetlify = () => {
prepareFolders();
/** options param:
* {
* functionsDir: string to path
* publishDir: string to path
* }
*/
const nextOnNetlify = (options = {}) => {
const functionsPath = options.functionsDir || NETLIFY_FUNCTIONS_PATH;
const publishPath = options.publishDir || NETLIFY_PUBLISH_PATH;

copyPublicFiles();
prepareFolders({ functionsPath, publishPath });

copyNextAssets();
copyPublicFiles(publishPath);

setupPages();
copyNextAssets(publishPath);

setupRedirects();
setupPages({ functionsPath, publishPath });

setupRedirects(publishPath);
};

module.exports = nextOnNetlify;
12 changes: 12 additions & 0 deletions lib/helpers/getNextConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Get next.config.js
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
const { default: loadConfig } = require("next/dist/next-server/server/config");
const { resolve } = require("path");

const getNextConfig = () => {
// Load next.config.js
// Use same code as https://github.com/vercel/next.js/blob/25488f4a03db30cade4d086ba49cd9a50a2ac02e/packages/next/build/index.ts#L114
return loadConfig(PHASE_PRODUCTION_BUILD, resolve("."));
};

module.exports = getNextConfig;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i thought i'd want and need this file in another part but ended up not using it here (except calling from getNextDistDir). this is a function i'm also adding in the i18n work so we can leave it :)

9 changes: 3 additions & 6 deletions lib/helpers/getNextDistDir.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Get the NextJS distDir specified in next.config.js
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
const { default: loadConfig } = require("next/dist/next-server/server/config");
const { resolve, join } = require("path");
const { join } = require("path");
const getNextConfig = require("./getNextConfig");

const getNextDistDir = ({ nextConfigPath }) => {
// Load next.config.js
// Use same code as https://github.com/vercel/next.js/blob/25488f4a03db30cade4d086ba49cd9a50a2ac02e/packages/next/build/index.ts#L114
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, resolve("."));
const nextConfig = getNextConfig();

return join(".", nextConfig.distDir);
};
Expand Down
10 changes: 3 additions & 7 deletions lib/helpers/setupNetlifyFunctionForPage.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
const { copySync } = require("fs-extra");
const { join } = require("path");
const {
NEXT_DIST_DIR,
NETLIFY_FUNCTIONS_PATH,
FUNCTION_TEMPLATE_PATH,
} = require("../config");
const { NEXT_DIST_DIR, FUNCTION_TEMPLATE_PATH } = require("../config");
const getNetlifyFunctionName = require("./getNetlifyFunctionName");

// Create a Netlify Function for the page with the given file path
const setupNetlifyFunctionForPage = (filePath) => {
const setupNetlifyFunctionForPage = ({ filePath, functionsPath }) => {
// Set function name based on file path
const functionName = getNetlifyFunctionName(filePath);
const functionDirectory = join(NETLIFY_FUNCTIONS_PATH, functionName);
const functionDirectory = join(functionsPath, functionName);

// Copy function template
const functionTemplateCopyPath = join(
Expand Down
10 changes: 7 additions & 3 deletions lib/helpers/setupStaticFileForPage.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const { copySync } = require("fs-extra");
const { join } = require("path");
const { NEXT_DIST_DIR, NETLIFY_PUBLISH_PATH } = require("../config");
const { NEXT_DIST_DIR } = require("../config");

// Copy the static asset from pages/inputPath to out_publish/outputPath
const setupStaticFileForPage = (inputPath, outputPath = null) => {
const setupStaticFileForPage = ({
inputPath,
outputPath = null,
publishPath,
}) => {
// If no outputPath is set, default to the same as inputPath
outputPath = outputPath || inputPath;

// Perform copy operation
copySync(
join(NEXT_DIST_DIR, "serverless", "pages", inputPath),
join(NETLIFY_PUBLISH_PATH, outputPath),
join(publishPath, outputPath),
{
overwrite: false,
errorOnExist: true,
Expand Down
7 changes: 3 additions & 4 deletions lib/pages/api/setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const { logTitle, logItem } = require("../../helpers/logger");
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
const pages = require("./pages");

// Create a Netlify Function for every API endpoint
const setup = () => {
const setup = (functionsPath) => {
logTitle(
"💫 Setting up API endpoints as Netlify Functions in",
NETLIFY_FUNCTIONS_PATH
functionsPath
);

// Create Netlify Function for every page
pages.forEach(({ filePath }) => {
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
setupNetlifyFunctionForPage({ filePath, functionsPath });
});
};

Expand Down
7 changes: 3 additions & 4 deletions lib/pages/getInitialProps/setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const { logTitle, logItem } = require("../../helpers/logger");
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
const pages = require("./pages");

// Create a Netlify Function for every page with getInitialProps
const setup = () => {
const setup = (functionsPath) => {
logTitle(
"💫 Setting up pages with getInitialProps as Netlify Functions in",
NETLIFY_FUNCTIONS_PATH
functionsPath
);

// Create Netlify Function for every page
pages.forEach(({ filePath }) => {
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
setupNetlifyFunctionForPage({ filePath, functionsPath });
});
};

Expand Down
7 changes: 3 additions & 4 deletions lib/pages/getServerSideProps/setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const { logTitle, logItem } = require("../../helpers/logger");
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
const pages = require("./pages");

// Create a Netlify Function for every page with getServerSideProps
const setup = () => {
const setup = (functionsPath) => {
logTitle(
"💫 Setting up pages with getServerSideProps as Netlify Functions in",
NETLIFY_FUNCTIONS_PATH
functionsPath
);

// Create Netlify Function for every page
pages.forEach(({ filePath }) => {
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
setupNetlifyFunctionForPage({ filePath, functionsPath });
});
};

Expand Down
15 changes: 9 additions & 6 deletions lib/pages/getStaticProps/setup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const { join } = require("path");
const { logTitle, logItem } = require("../../helpers/logger");
const { NETLIFY_PUBLISH_PATH } = require("../../config");
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
const isRouteWithFallback = require("../../helpers/isRouteWithFallback");
const setupStaticFileForPage = require("../../helpers/setupStaticFileForPage");
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
const pages = require("./pages");

// Copy pre-rendered SSG pages
const setup = () => {
const setup = ({ functionsPath, publishPath }) => {
logTitle(
"🔥 Copying pre-rendered pages with getStaticProps and JSON data to",
NETLIFY_PUBLISH_PATH
publishPath
);

// Keep track of the functions that have been set up, so that we do not set up
Expand All @@ -23,11 +22,15 @@ const setup = () => {

// Copy pre-rendered HTML page
const htmlPath = getFilePathForRoute(route, "html");
setupStaticFileForPage(htmlPath);
setupStaticFileForPage({ inputPath: htmlPath, publishPath });

// Copy page's JSON data
const jsonPath = getFilePathForRoute(route, "json");
setupStaticFileForPage(jsonPath, dataRoute);
setupStaticFileForPage({
inputPath: jsonPath,
outputPath: dataRoute,
publishPath,
});

// // Set up the Netlify function (this is ONLY for preview mode)
const relativePath = getFilePathForRoute(srcRoute || route, "js");
Expand All @@ -39,7 +42,7 @@ const setup = () => {
return;

logItem(filePath);
setupNetlifyFunctionForPage(filePath);
setupNetlifyFunctionForPage({ filePath, functionsPath });
filePathsDone.push(filePath);
});
};
Expand Down
7 changes: 3 additions & 4 deletions lib/pages/getStaticPropsWithFallback/setup.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const { join } = require("path");
const { logTitle, logItem } = require("../../helpers/logger");
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
const pages = require("./pages");

// Create a Netlify Function for every page with getStaticProps and fallback
const setup = () => {
const setup = (functionsPath) => {
logTitle(
"💫 Setting up pages with getStaticProps and fallback: true",
"as Netlify Functions in",
NETLIFY_FUNCTIONS_PATH
functionsPath
);

// Create Netlify Function for every page
pages.forEach(({ route }) => {
const relativePath = getFilePathForRoute(route, "js");
const filePath = join("pages", relativePath);
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
setupNetlifyFunctionForPage({ filePath, functionsPath });
});
};

Expand Down
7 changes: 3 additions & 4 deletions lib/pages/getStaticPropsWithRevalidate/setup.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const { join } = require("path");
const { logTitle, logItem } = require("../../helpers/logger");
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
const pages = require("./pages");

// Create a Netlify Function for every page with getStaticProps and revalidate
const setup = () => {
const setup = (functionsPath) => {
logTitle(
"💫 Setting up pages with getStaticProps and revalidation interval",
"as Netlify Functions in",
NETLIFY_FUNCTIONS_PATH
functionsPath
);

// Keep track of the functions that have been set up, so that we do not set up
Expand All @@ -27,7 +26,7 @@ const setup = () => {

// Set up the function
logItem(filePath);
setupNetlifyFunctionForPage(filePath);
setupNetlifyFunctionForPage({ filePath, functionsPath });
filePathsDone.push(filePath);
});
};
Expand Down
11 changes: 4 additions & 7 deletions lib/pages/withoutProps/setup.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
const { join, relative } = require("path");
const { copySync } = require("fs-extra");
const { logTitle, logItem } = require("../../helpers/logger");
const { NEXT_DIST_DIR, NETLIFY_PUBLISH_PATH } = require("../../config");
const { NEXT_DIST_DIR } = require("../../config");
const setupStaticFileForPage = require("../../helpers/setupStaticFileForPage");
const pages = require("./pages");

// Identify all pages that have been pre-rendered and copy each one to the
// Netlify publish directory.
const setup = () => {
logTitle(
"🔥 Copying pre-rendered pages without props to",
NETLIFY_PUBLISH_PATH
);
const setup = (publishPath) => {
logTitle("🔥 Copying pre-rendered pages without props to", publishPath);

// Copy each page to the Netlify publish directory
pages.forEach(({ filePath }) => {
logItem(filePath);

// The path to the file, relative to the pages directory
const relativePath = relative("pages", filePath);
setupStaticFileForPage(relativePath);
setupStaticFileForPage({ inputPath: relativePath, publishPath });
});
};

Expand Down
8 changes: 4 additions & 4 deletions lib/steps/copyNextAssets.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { join } = require("path");
const { copySync } = require("fs-extra");
const { logTitle } = require("../helpers/logger");
const { NETLIFY_PUBLISH_PATH, NEXT_DIST_DIR } = require("../config");
const { NEXT_DIST_DIR } = require("../config");

// Copy the NextJS' static assets from NextJS distDir to Netlify publish folder.
// These need to be available for NextJS to work.
const copyNextAssets = () => {
logTitle("💼 Copying static NextJS assets to", NETLIFY_PUBLISH_PATH);
const copyNextAssets = (publishPath) => {
logTitle("💼 Copying static NextJS assets to", publishPath);
copySync(
join(NEXT_DIST_DIR, "static"),
join(NETLIFY_PUBLISH_PATH, "_next", "static"),
join(publishPath, "_next", "static"),
{
overwrite: false,
errorOnExist: true,
Expand Down
8 changes: 4 additions & 4 deletions lib/steps/copyPublicFiles.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { existsSync, copySync } = require("fs-extra");
const { logTitle } = require("../helpers/logger");
const { NETLIFY_PUBLISH_PATH, PUBLIC_PATH } = require("../config");
const { PUBLIC_PATH } = require("../config");

// Copy files from public folder to Netlify publish folder
const copyPublicFiles = () => {
const copyPublicFiles = (publishPath) => {
// Abort if no public/ folder
if (!existsSync(PUBLIC_PATH)) return;

// Perform copy operation
logTitle("🌍️ Copying", PUBLIC_PATH, "folder to", NETLIFY_PUBLISH_PATH);
copySync(PUBLIC_PATH, NETLIFY_PUBLISH_PATH);
logTitle("🌍️ Copying", PUBLIC_PATH, "folder to", publishPath);
copySync(PUBLIC_PATH, publishPath);
};

module.exports = copyPublicFiles;
11 changes: 5 additions & 6 deletions lib/steps/prepareFolders.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const { emptyDirSync } = require("fs-extra");
const { logTitle, log } = require("../helpers/logger");
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH } = require("../config");

// Empty existing publish and functions folders
const prepareFolders = () => {
const prepareFolders = ({ functionsPath, publishPath }) => {
logTitle("🚀 Next on Netlify 🚀");
log(" ", "Functions directory:", NETLIFY_FUNCTIONS_PATH);
log(" ", "Publish directory: ", NETLIFY_PUBLISH_PATH);
log(" ", "Functions directory: ", functionsPath);
log(" ", "Publish directory: ", publishPath);
log(" ", "Make sure these are set in your netlify.toml file.");

emptyDirSync(NETLIFY_PUBLISH_PATH);
emptyDirSync(NETLIFY_FUNCTIONS_PATH);
emptyDirSync(publishPath);
emptyDirSync(functionsPath);
};

module.exports = prepareFolders;
16 changes: 8 additions & 8 deletions lib/steps/setupPages.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Set up all our NextJS pages according to the recipes defined in pages/
const setupPages = () => {
require("../pages/api/setup")();
require("../pages/getInitialProps/setup")();
require("../pages/getServerSideProps/setup")();
require("../pages/getStaticProps/setup")();
require("../pages/getStaticPropsWithFallback/setup")();
require("../pages/getStaticPropsWithRevalidate/setup")();
require("../pages/withoutProps/setup")();
const setupPages = ({ functionsPath, publishPath }) => {
require("../pages/api/setup")(functionsPath);
require("../pages/getInitialProps/setup")(functionsPath);
require("../pages/getServerSideProps/setup")(functionsPath);
require("../pages/getStaticProps/setup")({ functionsPath, publishPath });
require("../pages/getStaticPropsWithFallback/setup")(functionsPath);
require("../pages/getStaticPropsWithRevalidate/setup")(functionsPath);
require("../pages/withoutProps/setup")(publishPath);
};

module.exports = setupPages;
Loading