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 <[email protected]>
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

0 commit comments

Comments
 (0)