Skip to content

Commit d89a626

Browse files
committed
chore: add eslint configs
1 parent c2668af commit d89a626

10 files changed

+103
-77
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
max_line_length = 120
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.next
2+
node_modules
3+
test

.eslintrc.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { overrides } = require("@netlify/eslint-config-node/react_config");
2+
3+
module.exports = {
4+
extends: "@netlify/eslint-config-node/react_config",
5+
rules: {
6+
"max-depth": 0,
7+
complexity: 0,
8+
"fp/no-let": 0,
9+
"fp/no-loops": 0,
10+
"fp/no-mutation": 0,
11+
"fp/no-mutating-methods": 0,
12+
"id-length": 0,
13+
"max-statements": 0,
14+
"no-await-in-loop": 0,
15+
"node/exports-style": 0,
16+
"node/global-require": 0,
17+
"node/prefer-global/process": 0,
18+
"no-magic-numbers": 0,
19+
"no-param-reassign": 0,
20+
"no-promise-executor-return": 0,
21+
"no-prototype-builtins": 0,
22+
"no-shadow": 0,
23+
"no-unused-vars": 0,
24+
"prefer-regex-literals": 0,
25+
"promise/prefer-await-to-callbacks": 0,
26+
"require-await": 0,
27+
"unicorn/consistent-function-scoping": 0,
28+
"unicorn/filename-case": 0,
29+
"unicorn/no-array-push-push": 0,
30+
},
31+
env: {
32+
jest: true,
33+
},
34+
overrides: [...overrides],
35+
};

commitlint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = { extends: ['@commitlint/config-conventional'] }
1+
module.exports = { extends: ["@commitlint/config-conventional"] };

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"is-svg": "^4.3.1",
5858
"mime-types": "^2.1.30",
5959
"moize": "^6.0.0",
60+
"next": "^11.1.0",
6061
"node-fetch": "^2.6.1",
6162
"semver": "^7.3.5",
6263
"sharp": "^0.29.0"
@@ -66,7 +67,6 @@
6667
"eslint-config-next": "^11.0.0",
6768
"husky": "^4.3.0",
6869
"jest": "^27.0.0",
69-
"next": "^11.1.0",
7070
"prettier": "^2.1.2",
7171
"react": "^17.0.1",
7272
"react-dom": "^17.0.1"

src/copyNextDist.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
const { join } = require("path");
2-
const { copySync, existsSync } = require('fs-extra')
32

4-
const copyNextDist = async (siteRoot) => {
3+
const { copySync, existsSync } = require("fs-extra");
4+
5+
const copyNextDist = (siteRoot) => {
56
// TO-DO: get distDir from next.config.js
6-
const distPath = join(siteRoot, '.next')
7+
const distPath = join(siteRoot, ".next");
78
if (!existsSync(distPath)) {
8-
throw new Error(`No dist found in ${distPath}`)
9+
throw new Error(`No dist found in ${distPath}`);
910
}
10-
copySync(distPath, join('.', '.next'), {
11-
overwrite: true
12-
})
13-
}
11+
copySync(distPath, join(".", ".next"), {
12+
overwrite: true,
13+
});
14+
};
1415

15-
module.exports = copyNextDist
16+
module.exports = copyNextDist;

src/getHandler.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const { Server } = require("http");
2+
23
const { Bridge } = require("@vercel/node/dist/bridge");
34
// This path is specific to next@canary. In a live version we'd resolve various versions of next
45
const NextServer = require("next/dist/server/next-server").default;
5-
function makeHandler() {
6+
7+
const makeHandler =
8+
() =>
69
// We return a function and then call `toString()` on it to serialise it as the launcher function
7-
return (conf) => {
10+
(conf) => {
811
const nextServer = new NextServer({
912
conf,
1013
dir: ".",
@@ -14,27 +17,25 @@ function makeHandler() {
1417
const server = new Server(async (req, res) => {
1518
try {
1619
await requestHandler(req, res);
17-
} catch (err) {
18-
console.error(err);
19-
process.exit(1);
20+
} catch (error) {
21+
console.error(error);
22+
throw new Error("server function error");
2023
}
2124
});
2225
const bridge = new Bridge(server);
2326
bridge.listen();
2427

2528
return async (event, context) => {
26-
let result = await bridge.launcher(event, context);
29+
const result = await bridge.launcher(event, context);
2730
/** @type import("@netlify/functions").HandlerResponse */
2831
return {
2932
...result,
3033
isBase64Encoded: result.encoding === "base64",
3134
};
3235
};
3336
};
34-
}
3537

36-
exports.getHandler = async function (isODB = false) {
37-
return `
38+
const getHandler = (isODB = false) => `
3839
const { Server } = require("http");
3940
// We copy the file here rather than requiring from the node module
4041
const { Bridge } = require("./bridge");
@@ -45,9 +46,8 @@ const { builder } = require("@netlify/functions");
4546
const { config } = require(process.cwd() + "/.next/required-server-files.json")
4647
4748
exports.handler = ${
48-
isODB
49-
? `builder((${makeHandler().toString()})(config));`
50-
: `(${makeHandler().toString()})(config);`
51-
}
49+
isODB ? `builder((${makeHandler().toString()})(config));` : `(${makeHandler().toString()})(config);`
50+
}
5251
`;
53-
};
52+
53+
module.exports = getHandler;

src/getNextRoot.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
const { existsSync } = require('fs')
2-
const path = require('path')
1+
const { existsSync } = require("fs");
2+
const path = require("path");
33

44
/**
55
* If we're in a monorepo then the Next root may not be the same as the base directory
66
* If there's no next.config.js in the root, we instead look for it as a sibling of the publish dir
77
*/
88
const getNextRoot = ({ netlifyConfig }) => {
9-
let nextRoot = process.cwd()
9+
let nextRoot = process.cwd();
1010
if (
11-
!existsSync(path.join(nextRoot, 'next.config.js')) &&
11+
!existsSync(path.join(nextRoot, "next.config.js")) &&
1212
netlifyConfig.build.publish &&
1313
netlifyConfig.build.publish !== nextRoot
1414
) {
15-
nextRoot = path.dirname(netlifyConfig.build.publish)
15+
nextRoot = path.dirname(netlifyConfig.build.publish);
1616
}
17-
return nextRoot
18-
}
17+
return nextRoot;
18+
};
1919

20-
module.exports = getNextRoot
20+
module.exports = getNextRoot;

src/index.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// @ts-check
2-
const { getHandler } = require("./getHandler");
3-
const fs = require("fs").promises;
4-
const { ensureDir, existsSync, remove } = require("fs-extra");
52
const path = require("path");
3+
4+
const { copyFile, ensureDir, writeFile } = require("fs-extra");
5+
66
const DEFAULT_FUNCTIONS_SRC = "netlify/functions";
7-
const { writeRedirects } = require("./writeRedirects");
8-
const getNextRoot = require("./getNextRoot")
9-
const copyNextDist = require("./copyNextDist")
7+
const copyNextDist = require("./copyNextDist");
8+
const getHandler = require("./getHandler");
9+
const getNextRoot = require("./getNextRoot");
10+
const writeRedirects = require("./writeRedirects");
1011

1112
const HANDLER_FUNCTION_NAME = "___netlify-handler";
1213
const ODB_FUNCTION_NAME = "___netlify-odb-handler";
@@ -22,35 +23,24 @@ module.exports = {
2223
netlifyConfig.functions[functionName].included_files = [];
2324
}
2425
// TO-DO: get distDir from next.config.js
25-
netlifyConfig.functions[functionName].included_files.push(
26-
`.next/server/**`,
27-
`.next/*.json`,
28-
`.next/BUILD_ID`
29-
);
26+
netlifyConfig.functions[functionName].included_files.push(`.next/server/**`, `.next/*.json`, `.next/BUILD_ID`);
3027
});
3128

3229
console.log({ netlifyConfig });
3330
},
3431

3532
async onBuild({
3633
netlifyConfig,
37-
constants: {
38-
PUBLISH_DIR,
39-
FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC,
40-
INTERNAL_FUNCTIONS_SRC,
41-
},
34+
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC },
4235
}) {
4336
const FUNCTION_DIR = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
4437
const bridgeFile = require.resolve("@vercel/node/dist/bridge");
4538

4639
const writeHandler = async (func, isODB) => {
4740
const handlerSource = await getHandler(isODB);
4841
await ensureDir(path.join(FUNCTION_DIR, func));
49-
await fs.writeFile(
50-
path.join(FUNCTION_DIR, func, `${func}.js`),
51-
handlerSource
52-
);
53-
await fs.copyFile(bridgeFile, path.join(FUNCTION_DIR, func, "bridge.js"));
42+
await writeFile(path.join(FUNCTION_DIR, func, `${func}.js`), handlerSource);
43+
await copyFile(bridgeFile, path.join(FUNCTION_DIR, func, "bridge.js"));
5444
};
5545

5646
await writeHandler(HANDLER_FUNCTION_NAME, false);

src/writeRedirects.js

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @ts-check
2-
const { writeFile } = require("fs").promises;
3-
const { readJSON } = require("fs-extra");
42
const path = require("path");
53

4+
const { readJSON, writeFile } = require("fs-extra");
5+
66
const ODB_FUNCTION_PATH = "/.netlify/functions/___netlify-odb-handler";
77
const HANDLER_FUNCTION_PATH = "/.netlify/functions/___netlify-handler";
88

@@ -27,41 +27,26 @@ const getNetlifyRoutes = (nextRoute) => {
2727
// create an incorrect route for the data route. For example, it creates
2828
// /_next/data/%BUILDID%.json, but NextJS looks for
2929
// /_next/data/%BUILDID%/index.json
30-
netlifyRoute = netlifyRoute.replace(
31-
/(\/_next\/data\/[^/]+).json/,
32-
"$1/index.json"
33-
);
30+
netlifyRoute = netlifyRoute.replace(/(\/_next\/data\/[^/]+).json/, "$1/index.json");
3431

3532
// Add second route to the front of the array
3633
netlifyRoutes.unshift(netlifyRoute);
3734
}
3835

3936
// Replace catch-all, e.g., [...slug]
40-
netlifyRoutes = netlifyRoutes.map((route) =>
41-
route.replace(CATCH_ALL_REGEX, "/:$1/*")
42-
);
37+
netlifyRoutes = netlifyRoutes.map((route) => route.replace(CATCH_ALL_REGEX, "/:$1/*"));
4338

4439
// Replace optional catch-all, e.g., [[...slug]]
45-
netlifyRoutes = netlifyRoutes.map((route) =>
46-
route.replace(OPTIONAL_CATCH_ALL_REGEX, "/*")
47-
);
40+
netlifyRoutes = netlifyRoutes.map((route) => route.replace(OPTIONAL_CATCH_ALL_REGEX, "/*"));
4841

4942
// Replace dynamic parameters, e.g., [id]
50-
netlifyRoutes = netlifyRoutes.map((route) =>
51-
route.replace(DYNAMIC_PARAMETER_REGEX, "/:$1")
52-
);
43+
netlifyRoutes = netlifyRoutes.map((route) => route.replace(DYNAMIC_PARAMETER_REGEX, "/:$1"));
5344

5445
return netlifyRoutes;
5546
};
5647

57-
exports.writeRedirects = async function ({
58-
publishDir = "out",
59-
nextRoot = process.cwd(),
60-
netlifyConfig,
61-
}) {
62-
const { dynamicRoutes } = await readJSON(
63-
path.join(nextRoot, ".next", "prerender-manifest.json")
64-
);
48+
const writeRedirects = async ({ publishDir = "out", nextRoot = process.cwd(), netlifyConfig }) => {
49+
const { dynamicRoutes } = await readJSON(path.join(nextRoot, ".next", "prerender-manifest.json"));
6550

6651
const redirects = [];
6752
Object.entries(dynamicRoutes).forEach(([route, { dataRoute, fallback }]) => {
@@ -93,3 +78,5 @@ exports.writeRedirects = async function ({
9378
// `;
9479
// await writeFile(path.join(nextRoot, publishDir, "_redirects"), odbRedirects);
9580
};
81+
82+
module.exports = writeRedirects;

0 commit comments

Comments
 (0)