Skip to content

Commit ea12406

Browse files
refactor: code
1 parent 00cf351 commit ea12406

File tree

3 files changed

+113
-81
lines changed

3 files changed

+113
-81
lines changed

lib/Server.js

+113-73
Original file line numberDiff line numberDiff line change
@@ -445,51 +445,124 @@ class Server {
445445
const compilerOptions = this.getCompilerOptions();
446446
// TODO remove `{}` after drop webpack v4 support
447447
const compilerWatchOptions = compilerOptions.watchOptions || {};
448+
const getWatchOptions = (watchOptions = {}) => {
449+
const getPolling = () => {
450+
if (typeof watchOptions.usePolling !== "undefined") {
451+
return watchOptions.usePolling;
452+
}
448453

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

458-
const interval =
459-
// eslint-disable-next-line no-nested-ternary
460-
typeof watchOptions.interval !== "undefined"
461-
? watchOptions.interval
462-
: typeof watchOptions.poll === "number"
463-
? watchOptions.poll
464-
: // eslint-disable-next-line no-undefined
465-
undefined;
458+
if (typeof compilerWatchOptions.poll !== "undefined") {
459+
return Boolean(compilerWatchOptions.poll);
460+
}
461+
462+
return false;
463+
};
464+
const getInterval = () => {
465+
if (typeof watchOptions.interval !== "undefined") {
466+
return watchOptions.interval;
467+
}
468+
469+
if (typeof watchOptions.poll === "number") {
470+
return watchOptions.poll;
471+
}
466472

467-
const { poll, ...finalWatchOptions } = {
473+
if (typeof compilerWatchOptions.poll === "number") {
474+
return compilerWatchOptions.poll;
475+
}
476+
};
477+
478+
const usePolling = getPolling();
479+
const interval = getInterval();
480+
const { poll, ...rest } = watchOptions;
481+
482+
return {
468483
ignoreInitial: true,
469484
persistent: true,
470485
followSymlinks: false,
471486
atomic: false,
472487
alwaysStat: true,
473488
ignorePermissionErrors: true,
474-
...compilerWatchOptions,
475-
...watchOptions,
476-
ignored: watchOptions.ignored,
489+
// Respect options from compiler watchOptions
477490
usePolling,
478491
interval,
492+
ignored: watchOptions.ignored,
493+
// 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
494+
...rest,
479495
};
480-
481-
return finalWatchOptions;
482496
};
497+
const getStaticItem = (optionsForStatic) => {
498+
const getDefaultStaticOptions = () => {
499+
return {
500+
directory: path.join(process.cwd(), "public"),
501+
staticOptions: {},
502+
publicPath: ["/"],
503+
serveIndex: { icons: true },
504+
watch: getWatchOptions(),
505+
};
506+
};
483507

484-
const defaultWatchOptions = getDefaultWatchOptions();
508+
let item;
485509

486-
const defaultOptionsForStatic = {
487-
directory: path.join(process.cwd(), "public"),
488-
staticOptions: {},
489-
publicPath: ["/"],
490-
serveIndex: { icons: true },
491-
// Respect options from compiler watchOptions
492-
watch: defaultWatchOptions,
510+
if (typeof optionsForStatic === "undefined") {
511+
item = getDefaultStaticOptions();
512+
} else if (typeof optionsForStatic === "string") {
513+
item = {
514+
...getDefaultStaticOptions(),
515+
directory: optionsForStatic,
516+
};
517+
} else {
518+
const def = getDefaultStaticOptions();
519+
520+
item = {
521+
directory:
522+
typeof optionsForStatic.directory !== "undefined"
523+
? optionsForStatic.directory
524+
: def.directory,
525+
// TODO: do merge in the next major release
526+
staticOptions:
527+
typeof optionsForStatic.staticOptions !== "undefined"
528+
? optionsForStatic.staticOptions
529+
: def.staticOptions,
530+
publicPath:
531+
typeof optionsForStatic.publicPath !== "undefined"
532+
? optionsForStatic.publicPath
533+
: def.publicPath,
534+
// TODO: do merge in the next major release
535+
serveIndex:
536+
// eslint-disable-next-line no-nested-ternary
537+
typeof optionsForStatic.serveIndex !== "undefined"
538+
? typeof optionsForStatic.serveIndex === "boolean" &&
539+
optionsForStatic.serveIndex
540+
? def.serveIndex
541+
: optionsForStatic.serveIndex
542+
: def.serveIndex,
543+
watch:
544+
// eslint-disable-next-line no-nested-ternary
545+
typeof optionsForStatic.watch !== "undefined"
546+
? // eslint-disable-next-line no-nested-ternary
547+
typeof optionsForStatic.watch === "boolean"
548+
? optionsForStatic.watch
549+
? def.watch
550+
: false
551+
: getWatchOptions(optionsForStatic.watch)
552+
: def.watch,
553+
};
554+
}
555+
556+
if (Server.isAbsoluteURL(item.directory)) {
557+
throw new Error("Using a URL as static.directory is not supported");
558+
}
559+
560+
// ensure that publicPath is an array
561+
if (typeof item.publicPath === "string") {
562+
item.publicPath = [item.publicPath];
563+
}
564+
565+
return item;
493566
};
494567

495568
if (typeof options.allowedHosts === "undefined") {
@@ -959,55 +1032,26 @@ class Server {
9591032
}
9601033

9611034
if (typeof options.static === "undefined") {
962-
options.static = [defaultOptionsForStatic];
1035+
options.static = [getStaticItem()];
9631036
} else if (typeof options.static === "boolean") {
964-
options.static = options.static ? [defaultOptionsForStatic] : false;
1037+
options.static = options.static ? [getStaticItem()] : false;
9651038
} else if (typeof options.static === "string") {
966-
options.static = [
967-
{ ...defaultOptionsForStatic, directory: options.static },
968-
];
1039+
options.static = [getStaticItem(options.static)];
9691040
} else if (Array.isArray(options.static)) {
9701041
options.static = options.static.map((item) => {
9711042
if (typeof item === "string") {
972-
return { ...defaultOptionsForStatic, directory: item };
1043+
return getStaticItem(item);
9731044
}
9741045

975-
return { ...defaultOptionsForStatic, ...item };
1046+
return getStaticItem(item);
9761047
});
9771048
} else {
978-
options.static = [{ ...defaultOptionsForStatic, ...options.static }];
979-
}
980-
981-
if (options.static) {
982-
options.static.forEach((staticOption) => {
983-
if (Server.isAbsoluteURL(staticOption.directory)) {
984-
throw new Error("Using a URL as static.directory is not supported");
985-
}
986-
987-
// ensure that publicPath is an array
988-
if (typeof staticOption.publicPath === "string") {
989-
staticOption.publicPath = [staticOption.publicPath];
990-
}
991-
992-
// ensure that watch is an object if true
993-
if (staticOption.watch === true) {
994-
staticOption.watch = defaultOptionsForStatic.watch;
995-
} else if (typeof staticOption.watch === "object") {
996-
staticOption.watch = {
997-
...getDefaultWatchOptions(staticOption.watch),
998-
};
999-
}
1000-
1001-
// ensure that serveIndex is an object if true
1002-
if (staticOption.serveIndex === true) {
1003-
staticOption.serveIndex = defaultOptionsForStatic.serveIndex;
1004-
}
1005-
});
1049+
options.static = [getStaticItem(options.static)];
10061050
}
10071051

10081052
if (typeof options.watchFiles === "string") {
10091053
options.watchFiles = [
1010-
{ paths: options.watchFiles, options: defaultWatchOptions },
1054+
{ paths: options.watchFiles, options: getWatchOptions() },
10111055
];
10121056
} else if (
10131057
typeof options.watchFiles === "object" &&
@@ -1017,22 +1061,18 @@ class Server {
10171061
options.watchFiles = [
10181062
{
10191063
paths: options.watchFiles.paths,
1020-
options: {
1021-
...getDefaultWatchOptions(options.watchFiles.options || {}),
1022-
},
1064+
options: getWatchOptions(options.watchFiles.options || {}),
10231065
},
10241066
];
10251067
} else if (Array.isArray(options.watchFiles)) {
10261068
options.watchFiles = options.watchFiles.map((item) => {
10271069
if (typeof item === "string") {
1028-
return { paths: item, options: defaultWatchOptions };
1070+
return { paths: item, options: getWatchOptions() };
10291071
}
10301072

10311073
return {
10321074
paths: item.paths,
1033-
options: {
1034-
...getDefaultWatchOptions(item.options || {}),
1035-
},
1075+
options: getWatchOptions(item.options || {}),
10361076
};
10371077
});
10381078
} else {

test/server/__snapshots__/Server.test.js.snap.webpack4

-4
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,6 @@ Object {
16521652
},
16531653
"staticOptions": Object {},
16541654
"watch": Object {
1655-
"aggregateTimeout": 300,
16561655
"alwaysStat": true,
16571656
"atomic": false,
16581657
"followSymlinks": false,
@@ -1710,7 +1709,6 @@ Object {
17101709
},
17111710
"staticOptions": Object {},
17121711
"watch": Object {
1713-
"aggregateTimeout": 300,
17141712
"alwaysStat": true,
17151713
"atomic": false,
17161714
"followSymlinks": false,
@@ -1768,7 +1766,6 @@ Object {
17681766
},
17691767
"staticOptions": Object {},
17701768
"watch": Object {
1771-
"aggregateTimeout": 300,
17721769
"alwaysStat": true,
17731770
"atomic": false,
17741771
"followSymlinks": false,
@@ -1826,7 +1823,6 @@ Object {
18261823
},
18271824
"staticOptions": Object {},
18281825
"watch": Object {
1829-
"aggregateTimeout": 300,
18301826
"alwaysStat": true,
18311827
"atomic": false,
18321828
"followSymlinks": false,

test/server/__snapshots__/Server.test.js.snap.webpack5

-4
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,6 @@ Object {
16521652
},
16531653
"staticOptions": Object {},
16541654
"watch": Object {
1655-
"aggregateTimeout": 300,
16561655
"alwaysStat": true,
16571656
"atomic": false,
16581657
"followSymlinks": false,
@@ -1710,7 +1709,6 @@ Object {
17101709
},
17111710
"staticOptions": Object {},
17121711
"watch": Object {
1713-
"aggregateTimeout": 300,
17141712
"alwaysStat": true,
17151713
"atomic": false,
17161714
"followSymlinks": false,
@@ -1768,7 +1766,6 @@ Object {
17681766
},
17691767
"staticOptions": Object {},
17701768
"watch": Object {
1771-
"aggregateTimeout": 300,
17721769
"alwaysStat": true,
17731770
"atomic": false,
17741771
"followSymlinks": false,
@@ -1826,7 +1823,6 @@ Object {
18261823
},
18271824
"staticOptions": Object {},
18281825
"watch": Object {
1829-
"aggregateTimeout": 300,
18301826
"alwaysStat": true,
18311827
"atomic": false,
18321828
"followSymlinks": false,

0 commit comments

Comments
 (0)