-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowsersync.js
68 lines (54 loc) · 2.44 KB
/
browsersync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import fs from "fs";
import chalk from "chalk";
import { dirname, join, normalize } from "path";
import { fileURLToPath } from 'url';
import browsersync from "browser-sync";
// __dirName...
// This is a polyfill for __dirname, which isn't supported when using esmodules.
function fileDirName(meta) {
const __filename = fileURLToPath(meta.url);
const __dirname = dirname(__filename);
return __dirname;
}
const __dirname = fileDirName(import.meta);
// Command arguments...
// Server strategy.
const args = process.argv.length > 2 ? process.argv.slice(2) : [];
const serverStrategy = args.includes("release") ? "release" : "development";
console.log(chalk.black.bgWhiteBright("Browsersync server strategy:", serverStrategy));
// baseURL...
const fusion_json = JSON.parse(fs.readFileSync(join(__dirname, "fusion.json")));
let baseURL = "";
if (serverStrategy === "release") {
baseURL = Object.hasOwn(fusion_json, "baseURL") ? fusion_json.baseURL : "";
}
// 404...
function _404() {
try {
const pathTo404html = join(__dirname, "build", baseURL, "404.html");
return fs.readFileSync(pathTo404html);
} catch (error) {
return "<html style=\"height: 100%;\"><body style=\"height: 100%; background-color: red;\"><div style=\"width: 100%; height: 100%; display: table; text-align: center;\"><div style=\"display: table-cell; vertical-align: middle;\"><p style=\"font-size: 4rem; color: white; margin-top: 0; font-weight: bold;\">Page Not Found!</p><p style=\"font-size: 2rem; color: black; margin-bottom: 0;\">Please create your own 404.html page for your project.</p></div></div></body></html>";
}
}
// Options...
const defaultOptions = {
server: "build",
files: "build/**",
watch: true
};
const pathToUserOptions = join(__dirname, "browsersync-options.json");
let configOptions = fs.existsSync(pathToUserOptions) && JSON.parse(fs.readFileSync(pathToUserOptions))?.[serverStrategy] || {};
configOptions = { ...defaultOptions, ...configOptions };
// If thee's a baseURL the site should be served from build/baseURL.
if (baseURL !== "") configOptions.startPath = normalize(`${baseURL}`);
console.log(chalk.black.bgWhiteBright("Browsersync configuration options:"), configOptions);
// Start the server...
const bs = browsersync.create();
bs.init(configOptions, (err, bs) => {
bs.addMiddleware("*", (req, res) => {
// Provides the 404 content without redirect.
res.write(_404());
res.end();
});
});