Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 25, 2021
1 parent 00cf351 commit ea12406
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 81 deletions.
186 changes: 113 additions & 73 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,51 +445,124 @@ class Server {
const compilerOptions = this.getCompilerOptions();
// TODO remove `{}` after drop webpack v4 support
const compilerWatchOptions = compilerOptions.watchOptions || {};
const getWatchOptions = (watchOptions = {}) => {
const getPolling = () => {
if (typeof watchOptions.usePolling !== "undefined") {
return watchOptions.usePolling;
}

const getDefaultWatchOptions = (watchOptions = {}) => {
// duplicate the same massaging of options that watchpack performs
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
// this isn't an elegant solution, but we'll improve it in the future
const usePolling =
typeof watchOptions.usePolling !== "undefined"
? watchOptions.usePolling
: Boolean(watchOptions.poll);
if (typeof watchOptions.poll !== "undefined") {
return Boolean(watchOptions.poll);
}

const interval =
// eslint-disable-next-line no-nested-ternary
typeof watchOptions.interval !== "undefined"
? watchOptions.interval
: typeof watchOptions.poll === "number"
? watchOptions.poll
: // eslint-disable-next-line no-undefined
undefined;
if (typeof compilerWatchOptions.poll !== "undefined") {
return Boolean(compilerWatchOptions.poll);
}

return false;
};
const getInterval = () => {
if (typeof watchOptions.interval !== "undefined") {
return watchOptions.interval;
}

if (typeof watchOptions.poll === "number") {
return watchOptions.poll;
}

const { poll, ...finalWatchOptions } = {
if (typeof compilerWatchOptions.poll === "number") {
return compilerWatchOptions.poll;
}
};

const usePolling = getPolling();
const interval = getInterval();
const { poll, ...rest } = watchOptions;

return {
ignoreInitial: true,
persistent: true,
followSymlinks: false,
atomic: false,
alwaysStat: true,
ignorePermissionErrors: true,
...compilerWatchOptions,
...watchOptions,
ignored: watchOptions.ignored,
// Respect options from compiler watchOptions
usePolling,
interval,
ignored: watchOptions.ignored,
// TODO: we respect these options for all watch options and allow developers to pass them to chokidar, but chokidar doesn't have these options maybe we need revisit that in future
...rest,
};

return finalWatchOptions;
};
const getStaticItem = (optionsForStatic) => {
const getDefaultStaticOptions = () => {
return {
directory: path.join(process.cwd(), "public"),
staticOptions: {},
publicPath: ["/"],
serveIndex: { icons: true },
watch: getWatchOptions(),
};
};

const defaultWatchOptions = getDefaultWatchOptions();
let item;

const defaultOptionsForStatic = {
directory: path.join(process.cwd(), "public"),
staticOptions: {},
publicPath: ["/"],
serveIndex: { icons: true },
// Respect options from compiler watchOptions
watch: defaultWatchOptions,
if (typeof optionsForStatic === "undefined") {
item = getDefaultStaticOptions();
} else if (typeof optionsForStatic === "string") {
item = {
...getDefaultStaticOptions(),
directory: optionsForStatic,
};
} else {
const def = getDefaultStaticOptions();

item = {
directory:
typeof optionsForStatic.directory !== "undefined"
? optionsForStatic.directory
: def.directory,
// TODO: do merge in the next major release
staticOptions:
typeof optionsForStatic.staticOptions !== "undefined"
? optionsForStatic.staticOptions
: def.staticOptions,
publicPath:
typeof optionsForStatic.publicPath !== "undefined"
? optionsForStatic.publicPath
: def.publicPath,
// TODO: do merge in the next major release
serveIndex:
// eslint-disable-next-line no-nested-ternary
typeof optionsForStatic.serveIndex !== "undefined"
? typeof optionsForStatic.serveIndex === "boolean" &&
optionsForStatic.serveIndex
? def.serveIndex
: optionsForStatic.serveIndex
: def.serveIndex,
watch:
// eslint-disable-next-line no-nested-ternary
typeof optionsForStatic.watch !== "undefined"
? // eslint-disable-next-line no-nested-ternary
typeof optionsForStatic.watch === "boolean"
? optionsForStatic.watch
? def.watch
: false
: getWatchOptions(optionsForStatic.watch)
: def.watch,
};
}

if (Server.isAbsoluteURL(item.directory)) {
throw new Error("Using a URL as static.directory is not supported");
}

// ensure that publicPath is an array
if (typeof item.publicPath === "string") {
item.publicPath = [item.publicPath];
}

return item;
};

if (typeof options.allowedHosts === "undefined") {
Expand Down Expand Up @@ -959,55 +1032,26 @@ class Server {
}

if (typeof options.static === "undefined") {
options.static = [defaultOptionsForStatic];
options.static = [getStaticItem()];
} else if (typeof options.static === "boolean") {
options.static = options.static ? [defaultOptionsForStatic] : false;
options.static = options.static ? [getStaticItem()] : false;
} else if (typeof options.static === "string") {
options.static = [
{ ...defaultOptionsForStatic, directory: options.static },
];
options.static = [getStaticItem(options.static)];
} else if (Array.isArray(options.static)) {
options.static = options.static.map((item) => {
if (typeof item === "string") {
return { ...defaultOptionsForStatic, directory: item };
return getStaticItem(item);
}

return { ...defaultOptionsForStatic, ...item };
return getStaticItem(item);
});
} else {
options.static = [{ ...defaultOptionsForStatic, ...options.static }];
}

if (options.static) {
options.static.forEach((staticOption) => {
if (Server.isAbsoluteURL(staticOption.directory)) {
throw new Error("Using a URL as static.directory is not supported");
}

// ensure that publicPath is an array
if (typeof staticOption.publicPath === "string") {
staticOption.publicPath = [staticOption.publicPath];
}

// ensure that watch is an object if true
if (staticOption.watch === true) {
staticOption.watch = defaultOptionsForStatic.watch;
} else if (typeof staticOption.watch === "object") {
staticOption.watch = {
...getDefaultWatchOptions(staticOption.watch),
};
}

// ensure that serveIndex is an object if true
if (staticOption.serveIndex === true) {
staticOption.serveIndex = defaultOptionsForStatic.serveIndex;
}
});
options.static = [getStaticItem(options.static)];
}

if (typeof options.watchFiles === "string") {
options.watchFiles = [
{ paths: options.watchFiles, options: defaultWatchOptions },
{ paths: options.watchFiles, options: getWatchOptions() },
];
} else if (
typeof options.watchFiles === "object" &&
Expand All @@ -1017,22 +1061,18 @@ class Server {
options.watchFiles = [
{
paths: options.watchFiles.paths,
options: {
...getDefaultWatchOptions(options.watchFiles.options || {}),
},
options: getWatchOptions(options.watchFiles.options || {}),
},
];
} else if (Array.isArray(options.watchFiles)) {
options.watchFiles = options.watchFiles.map((item) => {
if (typeof item === "string") {
return { paths: item, options: defaultWatchOptions };
return { paths: item, options: getWatchOptions() };
}

return {
paths: item.paths,
options: {
...getDefaultWatchOptions(item.options || {}),
},
options: getWatchOptions(item.options || {}),
};
});
} else {
Expand Down
4 changes: 0 additions & 4 deletions test/server/__snapshots__/Server.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down Expand Up @@ -1710,7 +1709,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down Expand Up @@ -1768,7 +1766,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down Expand Up @@ -1826,7 +1823,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down
4 changes: 0 additions & 4 deletions test/server/__snapshots__/Server.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down Expand Up @@ -1710,7 +1709,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down Expand Up @@ -1768,7 +1766,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down Expand Up @@ -1826,7 +1823,6 @@ Object {
},
"staticOptions": Object {},
"watch": Object {
"aggregateTimeout": 300,
"alwaysStat": true,
"atomic": false,
"followSymlinks": false,
Expand Down

0 comments on commit ea12406

Please sign in to comment.