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

Commit 7f01dd3

Browse files
authored
feat: add watch mode (#162)
1 parent c495782 commit 7f01dd3

8 files changed

+128
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Now you're all set.
199199

200200
From now on, whenever you want to preview your application locally, just run:
201201

202-
1. `npm run build`: This will run `next build` to build your Next.js app and `next-on-netlify` to prepare your Next.js app for compatibility with Netlify
202+
1. `npx next-on-netlify watch`: This will run `next build` to build your Next.js app and `next-on-netlify` to prepare your Next.js app for compatibility with Netlify. Any source code changes will trigger another build.
203203
1. `netlify dev`: This will emulate Netlify on your computer and let you preview your app on `http://localhost:8888`.
204204

205205
*Note:*

index.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
const { normalize } = require("path");
2+
const debounceFn = require("debounce-fn");
3+
const chokidar = require("chokidar");
4+
const execa = require("execa");
5+
6+
const { logTitle } = require("./lib/helpers/logger");
7+
28
const prepareFolders = require("./lib/steps/prepareFolders");
39
const copyPublicFiles = require("./lib/steps/copyPublicFiles");
410
const copyNextAssets = require("./lib/steps/copyNextAssets");
@@ -9,21 +15,10 @@ const setupHeaders = require("./lib/steps/setupHeaders");
915
const {
1016
NETLIFY_PUBLISH_PATH,
1117
NETLIFY_FUNCTIONS_PATH,
18+
SRC_FILES,
1219
} = require("./lib/config");
1320

14-
/** options param:
15-
* {
16-
* functionsDir: string to path
17-
* publishDir: string to path
18-
* }
19-
*/
20-
21-
const nextOnNetlify = (options = {}) => {
22-
const functionsPath = normalize(
23-
options.functionsDir || NETLIFY_FUNCTIONS_PATH
24-
);
25-
const publishPath = normalize(options.publishDir || NETLIFY_PUBLISH_PATH);
26-
21+
const build = (functionsPath, publishPath) => {
2722
const trackNextOnNetlifyFiles = prepareFolders({
2823
functionsPath,
2924
publishPath,
@@ -42,6 +37,47 @@ const nextOnNetlify = (options = {}) => {
4237
setupHeaders(publishPath);
4338

4439
trackNextOnNetlifyFiles();
40+
41+
logTitle("✅ Success! All done!");
42+
};
43+
44+
const watch = (functionsPath, publishPath) => {
45+
logTitle(`👀 Watching source code for changes`);
46+
47+
const runBuild = debounceFn(
48+
() => {
49+
try {
50+
execa.sync("next", ["build"], { stdio: "inherit" });
51+
build(functionsPath, publishPath);
52+
} catch (e) {
53+
console.log(e);
54+
}
55+
},
56+
{
57+
wait: 3000,
58+
}
59+
);
60+
61+
chokidar.watch(SRC_FILES).on("all", runBuild);
62+
};
63+
64+
/** options param:
65+
* {
66+
* functionsDir: string to path
67+
* publishDir: string to path
68+
* watch: { directory: string to path }
69+
* }
70+
*/
71+
72+
const nextOnNetlify = (options = {}) => {
73+
const functionsPath = normalize(
74+
options.functionsDir || NETLIFY_FUNCTIONS_PATH
75+
);
76+
const publishPath = normalize(options.publishDir || NETLIFY_PUBLISH_PATH);
77+
78+
options.watch
79+
? watch(functionsPath, publishPath)
80+
: build(functionsPath, publishPath);
4581
};
4682

4783
module.exports = nextOnNetlify;

lib/config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { join } = require("path");
22
const getNextDistDir = require("./helpers/getNextDistDir");
3+
const getNextSrcDirs = require("./helpers/getNextSrcDir");
34

45
// This is where next-on-netlify will place all static files.
56
// The publish key in netlify.toml should point to this folder.
@@ -17,7 +18,9 @@ const PUBLIC_PATH = join(".", "public/");
1718
const NEXT_CONFIG_PATH = join(".", "next.config.js");
1819

1920
// This is the folder that NextJS builds to; default is .next
20-
const NEXT_DIST_DIR = getNextDistDir({ nextConfigPath: NEXT_CONFIG_PATH });
21+
const NEXT_DIST_DIR = getNextDistDir();
22+
23+
const NEXT_SRC_DIRS = getNextSrcDirs();
2124

2225
// This is the folder with templates for Netlify Functions
2326
const TEMPLATES_DIR = join(__dirname, "templates");
@@ -35,6 +38,14 @@ const CUSTOM_HEADERS_PATH = join(".", "_headers");
3538
// creating the next/image redirect
3639
const NEXT_IMAGE_FUNCTION_NAME = "next_image";
3740

41+
const SRC_FILES = [
42+
PUBLIC_PATH,
43+
NEXT_CONFIG_PATH,
44+
CUSTOM_REDIRECTS_PATH,
45+
CUSTOM_HEADERS_PATH,
46+
...NEXT_SRC_DIRS,
47+
];
48+
3849
module.exports = {
3950
NETLIFY_PUBLISH_PATH,
4051
NETLIFY_FUNCTIONS_PATH,
@@ -46,4 +57,5 @@ module.exports = {
4657
CUSTOM_REDIRECTS_PATH,
4758
CUSTOM_HEADERS_PATH,
4859
NEXT_IMAGE_FUNCTION_NAME,
60+
SRC_FILES,
4961
};

lib/helpers/getNextDistDir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { join } = require("path");
33
const getNextConfig = require("./getNextConfig");
44

5-
const getNextDistDir = ({ nextConfigPath }) => {
5+
const getNextDistDir = () => {
66
const nextConfig = getNextConfig();
77

88
return join(".", nextConfig.distDir);

lib/helpers/getNextSrcDir.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { join } = require("path");
2+
3+
const getNextSrcDirs = () => {
4+
return ["pages", "src", "public", "styles"].map((dir) => join(".", dir));
5+
};
6+
7+
module.exports = getNextSrcDirs;

next-on-netlify.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
#!/usr/bin/env node
22
const { program } = require("commander");
33

4-
program
5-
.option(
6-
"--max-log-lines [number]",
7-
"lines of build output to show for each section",
8-
50
9-
)
10-
.parse(process.argv);
11-
124
const nextOnNetlify = require("./index");
13-
const { logTitle } = require("./lib/helpers/logger");
145

15-
nextOnNetlify();
6+
program.option(
7+
"--max-log-lines [number]",
8+
"lines of build output to show for each section",
9+
50
10+
);
11+
12+
program
13+
.command("watch")
14+
.description("re-runs next-on-netlify on changes")
15+
.action(() => {
16+
nextOnNetlify({ watch: true });
17+
});
18+
19+
program
20+
.command("build", { isDefault: true })
21+
.description("runs next-on-netlify")
22+
.action(() => {
23+
nextOnNetlify();
24+
});
1625

17-
logTitle("✅ Success! All done!");
26+
program.parse(process.argv);

package-lock.json

Lines changed: 35 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
},
5353
"dependencies": {
5454
"@sls-next/lambda-at-edge": "^1.5.2",
55+
"chokidar": "^3.5.1",
5556
"commander": "^6.0.0",
57+
"debounce-fn": "^4.0.0",
5658
"fs-extra": "^9.0.1",
5759
"jimp": "^0.16.1"
5860
},

0 commit comments

Comments
 (0)