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

Commit d779945

Browse files
use configurable functionsDir and publishDir throughout NoN (#89)
* use configurable functionsDir and publishDir throughout NoN * Update index.js Co-authored-by: ehmicky <ehmicky@users.noreply.github.com>
1 parent a9dade3 commit d779945

File tree

17 files changed

+96
-80
lines changed

17 files changed

+96
-80
lines changed

index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ const copyPublicFiles = require("./lib/steps/copyPublicFiles");
33
const copyNextAssets = require("./lib/steps/copyNextAssets");
44
const setupPages = require("./lib/steps/setupPages");
55
const setupRedirects = require("./lib/steps/setupRedirects");
6+
const {
7+
NETLIFY_PUBLISH_PATH,
8+
NETLIFY_FUNCTIONS_PATH,
9+
} = require("./lib/config");
610

7-
const nextOnNetlify = () => {
8-
prepareFolders();
11+
/** options param:
12+
* {
13+
* functionsDir: string to path
14+
* publishDir: string to path
15+
* }
16+
*/
17+
const nextOnNetlify = (options = {}) => {
18+
const functionsPath = options.functionsDir || NETLIFY_FUNCTIONS_PATH;
19+
const publishPath = options.publishDir || NETLIFY_PUBLISH_PATH;
920

10-
copyPublicFiles();
21+
prepareFolders({ functionsPath, publishPath });
1122

12-
copyNextAssets();
23+
copyPublicFiles(publishPath);
1324

14-
setupPages();
25+
copyNextAssets(publishPath);
1526

16-
setupRedirects();
27+
setupPages({ functionsPath, publishPath });
28+
29+
setupRedirects(publishPath);
1730
};
1831

1932
module.exports = nextOnNetlify;

lib/helpers/getNextConfig.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Get next.config.js
2+
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
3+
const { default: loadConfig } = require("next/dist/next-server/server/config");
4+
const { resolve } = require("path");
5+
6+
const getNextConfig = () => {
7+
// Load next.config.js
8+
// Use same code as https://github.com/vercel/next.js/blob/25488f4a03db30cade4d086ba49cd9a50a2ac02e/packages/next/build/index.ts#L114
9+
return loadConfig(PHASE_PRODUCTION_BUILD, resolve("."));
10+
};
11+
12+
module.exports = getNextConfig;

lib/helpers/getNextDistDir.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// Get the NextJS distDir specified in next.config.js
2-
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
3-
const { default: loadConfig } = require("next/dist/next-server/server/config");
4-
const { resolve, join } = require("path");
2+
const { join } = require("path");
3+
const getNextConfig = require("./getNextConfig");
54

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

118
return join(".", nextConfig.distDir);
129
};

lib/helpers/setupNetlifyFunctionForPage.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
const { copySync } = require("fs-extra");
22
const { join } = require("path");
3-
const {
4-
NEXT_DIST_DIR,
5-
NETLIFY_FUNCTIONS_PATH,
6-
FUNCTION_TEMPLATE_PATH,
7-
} = require("../config");
3+
const { NEXT_DIST_DIR, FUNCTION_TEMPLATE_PATH } = require("../config");
84
const getNetlifyFunctionName = require("./getNetlifyFunctionName");
95

106
// Create a Netlify Function for the page with the given file path
11-
const setupNetlifyFunctionForPage = (filePath) => {
7+
const setupNetlifyFunctionForPage = ({ filePath, functionsPath }) => {
128
// Set function name based on file path
139
const functionName = getNetlifyFunctionName(filePath);
14-
const functionDirectory = join(NETLIFY_FUNCTIONS_PATH, functionName);
10+
const functionDirectory = join(functionsPath, functionName);
1511

1612
// Copy function template
1713
const functionTemplateCopyPath = join(

lib/helpers/setupStaticFileForPage.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
const { copySync } = require("fs-extra");
22
const { join } = require("path");
3-
const { NEXT_DIST_DIR, NETLIFY_PUBLISH_PATH } = require("../config");
3+
const { NEXT_DIST_DIR } = require("../config");
44

55
// Copy the static asset from pages/inputPath to out_publish/outputPath
6-
const setupStaticFileForPage = (inputPath, outputPath = null) => {
6+
const setupStaticFileForPage = ({
7+
inputPath,
8+
outputPath = null,
9+
publishPath,
10+
}) => {
711
// If no outputPath is set, default to the same as inputPath
812
outputPath = outputPath || inputPath;
913

1014
// Perform copy operation
1115
copySync(
1216
join(NEXT_DIST_DIR, "serverless", "pages", inputPath),
13-
join(NETLIFY_PUBLISH_PATH, outputPath),
17+
join(publishPath, outputPath),
1418
{
1519
overwrite: false,
1620
errorOnExist: true,

lib/pages/api/setup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
const { logTitle, logItem } = require("../../helpers/logger");
2-
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
32
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
43
const pages = require("./pages");
54

65
// Create a Netlify Function for every API endpoint
7-
const setup = () => {
6+
const setup = (functionsPath) => {
87
logTitle(
98
"💫 Setting up API endpoints as Netlify Functions in",
10-
NETLIFY_FUNCTIONS_PATH
9+
functionsPath
1110
);
1211

1312
// Create Netlify Function for every page
1413
pages.forEach(({ filePath }) => {
1514
logItem(filePath);
16-
setupNetlifyFunctionForPage(filePath);
15+
setupNetlifyFunctionForPage({ filePath, functionsPath });
1716
});
1817
};
1918

lib/pages/getInitialProps/setup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
const { logTitle, logItem } = require("../../helpers/logger");
2-
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
32
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
43
const pages = require("./pages");
54

65
// Create a Netlify Function for every page with getInitialProps
7-
const setup = () => {
6+
const setup = (functionsPath) => {
87
logTitle(
98
"💫 Setting up pages with getInitialProps as Netlify Functions in",
10-
NETLIFY_FUNCTIONS_PATH
9+
functionsPath
1110
);
1211

1312
// Create Netlify Function for every page
1413
pages.forEach(({ filePath }) => {
1514
logItem(filePath);
16-
setupNetlifyFunctionForPage(filePath);
15+
setupNetlifyFunctionForPage({ filePath, functionsPath });
1716
});
1817
};
1918

lib/pages/getServerSideProps/setup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
const { logTitle, logItem } = require("../../helpers/logger");
2-
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
32
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
43
const pages = require("./pages");
54

65
// Create a Netlify Function for every page with getServerSideProps
7-
const setup = () => {
6+
const setup = (functionsPath) => {
87
logTitle(
98
"💫 Setting up pages with getServerSideProps as Netlify Functions in",
10-
NETLIFY_FUNCTIONS_PATH
9+
functionsPath
1110
);
1211

1312
// Create Netlify Function for every page
1413
pages.forEach(({ filePath }) => {
1514
logItem(filePath);
16-
setupNetlifyFunctionForPage(filePath);
15+
setupNetlifyFunctionForPage({ filePath, functionsPath });
1716
});
1817
};
1918

lib/pages/getStaticProps/setup.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
const { join } = require("path");
22
const { logTitle, logItem } = require("../../helpers/logger");
3-
const { NETLIFY_PUBLISH_PATH } = require("../../config");
43
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
54
const isRouteWithFallback = require("../../helpers/isRouteWithFallback");
65
const setupStaticFileForPage = require("../../helpers/setupStaticFileForPage");
76
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
87
const pages = require("./pages");
98

109
// Copy pre-rendered SSG pages
11-
const setup = () => {
10+
const setup = ({ functionsPath, publishPath }) => {
1211
logTitle(
1312
"🔥 Copying pre-rendered pages with getStaticProps and JSON data to",
14-
NETLIFY_PUBLISH_PATH
13+
publishPath
1514
);
1615

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

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

2827
// Copy page's JSON data
2928
const jsonPath = getFilePathForRoute(route, "json");
30-
setupStaticFileForPage(jsonPath, dataRoute);
29+
setupStaticFileForPage({
30+
inputPath: jsonPath,
31+
outputPath: dataRoute,
32+
publishPath,
33+
});
3134

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

4144
logItem(filePath);
42-
setupNetlifyFunctionForPage(filePath);
45+
setupNetlifyFunctionForPage({ filePath, functionsPath });
4346
filePathsDone.push(filePath);
4447
});
4548
};

lib/pages/getStaticPropsWithFallback/setup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
const { join } = require("path");
22
const { logTitle, logItem } = require("../../helpers/logger");
3-
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
43
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
54
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
65
const pages = require("./pages");
76

87
// Create a Netlify Function for every page with getStaticProps and fallback
9-
const setup = () => {
8+
const setup = (functionsPath) => {
109
logTitle(
1110
"💫 Setting up pages with getStaticProps and fallback: true",
1211
"as Netlify Functions in",
13-
NETLIFY_FUNCTIONS_PATH
12+
functionsPath
1413
);
1514

1615
// Create Netlify Function for every page
1716
pages.forEach(({ route }) => {
1817
const relativePath = getFilePathForRoute(route, "js");
1918
const filePath = join("pages", relativePath);
2019
logItem(filePath);
21-
setupNetlifyFunctionForPage(filePath);
20+
setupNetlifyFunctionForPage({ filePath, functionsPath });
2221
});
2322
};
2423

lib/pages/getStaticPropsWithRevalidate/setup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
const { join } = require("path");
22
const { logTitle, logItem } = require("../../helpers/logger");
3-
const { NETLIFY_FUNCTIONS_PATH } = require("../../config");
43
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
54
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
65
const pages = require("./pages");
76

87
// Create a Netlify Function for every page with getStaticProps and revalidate
9-
const setup = () => {
8+
const setup = (functionsPath) => {
109
logTitle(
1110
"💫 Setting up pages with getStaticProps and revalidation interval",
1211
"as Netlify Functions in",
13-
NETLIFY_FUNCTIONS_PATH
12+
functionsPath
1413
);
1514

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

2827
// Set up the function
2928
logItem(filePath);
30-
setupNetlifyFunctionForPage(filePath);
29+
setupNetlifyFunctionForPage({ filePath, functionsPath });
3130
filePathsDone.push(filePath);
3231
});
3332
};

lib/pages/withoutProps/setup.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
const { join, relative } = require("path");
22
const { copySync } = require("fs-extra");
33
const { logTitle, logItem } = require("../../helpers/logger");
4-
const { NEXT_DIST_DIR, NETLIFY_PUBLISH_PATH } = require("../../config");
4+
const { NEXT_DIST_DIR } = require("../../config");
55
const setupStaticFileForPage = require("../../helpers/setupStaticFileForPage");
66
const pages = require("./pages");
77

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

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

2017
// The path to the file, relative to the pages directory
2118
const relativePath = relative("pages", filePath);
22-
setupStaticFileForPage(relativePath);
19+
setupStaticFileForPage({ inputPath: relativePath, publishPath });
2320
});
2421
};
2522

lib/steps/copyNextAssets.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const { join } = require("path");
22
const { copySync } = require("fs-extra");
33
const { logTitle } = require("../helpers/logger");
4-
const { NETLIFY_PUBLISH_PATH, NEXT_DIST_DIR } = require("../config");
4+
const { NEXT_DIST_DIR } = require("../config");
55

66
// Copy the NextJS' static assets from NextJS distDir to Netlify publish folder.
77
// These need to be available for NextJS to work.
8-
const copyNextAssets = () => {
9-
logTitle("💼 Copying static NextJS assets to", NETLIFY_PUBLISH_PATH);
8+
const copyNextAssets = (publishPath) => {
9+
logTitle("💼 Copying static NextJS assets to", publishPath);
1010
copySync(
1111
join(NEXT_DIST_DIR, "static"),
12-
join(NETLIFY_PUBLISH_PATH, "_next", "static"),
12+
join(publishPath, "_next", "static"),
1313
{
1414
overwrite: false,
1515
errorOnExist: true,

lib/steps/copyPublicFiles.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const { existsSync, copySync } = require("fs-extra");
22
const { logTitle } = require("../helpers/logger");
3-
const { NETLIFY_PUBLISH_PATH, PUBLIC_PATH } = require("../config");
3+
const { PUBLIC_PATH } = require("../config");
44

55
// Copy files from public folder to Netlify publish folder
6-
const copyPublicFiles = () => {
6+
const copyPublicFiles = (publishPath) => {
77
// Abort if no public/ folder
88
if (!existsSync(PUBLIC_PATH)) return;
99

1010
// Perform copy operation
11-
logTitle("🌍️ Copying", PUBLIC_PATH, "folder to", NETLIFY_PUBLISH_PATH);
12-
copySync(PUBLIC_PATH, NETLIFY_PUBLISH_PATH);
11+
logTitle("🌍️ Copying", PUBLIC_PATH, "folder to", publishPath);
12+
copySync(PUBLIC_PATH, publishPath);
1313
};
1414

1515
module.exports = copyPublicFiles;

lib/steps/prepareFolders.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
const { emptyDirSync } = require("fs-extra");
22
const { logTitle, log } = require("../helpers/logger");
3-
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH } = require("../config");
43

54
// Empty existing publish and functions folders
6-
const prepareFolders = () => {
5+
const prepareFolders = ({ functionsPath, publishPath }) => {
76
logTitle("🚀 Next on Netlify 🚀");
8-
log(" ", "Functions directory:", NETLIFY_FUNCTIONS_PATH);
9-
log(" ", "Publish directory: ", NETLIFY_PUBLISH_PATH);
7+
log(" ", "Functions directory: ", functionsPath);
8+
log(" ", "Publish directory: ", publishPath);
109
log(" ", "Make sure these are set in your netlify.toml file.");
1110

12-
emptyDirSync(NETLIFY_PUBLISH_PATH);
13-
emptyDirSync(NETLIFY_FUNCTIONS_PATH);
11+
emptyDirSync(publishPath);
12+
emptyDirSync(functionsPath);
1413
};
1514

1615
module.exports = prepareFolders;

lib/steps/setupPages.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Set up all our NextJS pages according to the recipes defined in pages/
2-
const setupPages = () => {
3-
require("../pages/api/setup")();
4-
require("../pages/getInitialProps/setup")();
5-
require("../pages/getServerSideProps/setup")();
6-
require("../pages/getStaticProps/setup")();
7-
require("../pages/getStaticPropsWithFallback/setup")();
8-
require("../pages/getStaticPropsWithRevalidate/setup")();
9-
require("../pages/withoutProps/setup")();
2+
const setupPages = ({ functionsPath, publishPath }) => {
3+
require("../pages/api/setup")(functionsPath);
4+
require("../pages/getInitialProps/setup")(functionsPath);
5+
require("../pages/getServerSideProps/setup")(functionsPath);
6+
require("../pages/getStaticProps/setup")({ functionsPath, publishPath });
7+
require("../pages/getStaticPropsWithFallback/setup")(functionsPath);
8+
require("../pages/getStaticPropsWithRevalidate/setup")(functionsPath);
9+
require("../pages/withoutProps/setup")(publishPath);
1010
};
1111

1212
module.exports = setupPages;

0 commit comments

Comments
 (0)