Skip to content

Commit

Permalink
refactor: setupFeatures method (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and hiroppy committed May 16, 2019
1 parent df6a020 commit 6020e21
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/Server.js
Expand Up @@ -521,51 +521,49 @@ class Server {
},
};

const defaultFeatures = ['setup', 'before', 'headers', 'middleware'];
const runnableFeatures = [];

if (this.options.proxy) {
defaultFeatures.push('proxy', 'middleware');
// compress is placed last and uses unshift so that it will be the first middleware used
if (this.options.compress) {
runnableFeatures.push('compress');
}

const contentBase = this.options.contentBase;
runnableFeatures.push('setup', 'before', 'headers', 'middleware');

if (contentBase !== false) {
defaultFeatures.push('contentBaseFiles');
if (this.options.proxy) {
runnableFeatures.push('proxy', 'middleware');
}

if (this.options.watchContentBase) {
defaultFeatures.push('watchContentBase');
if (this.options.contentBase !== false) {
runnableFeatures.push('contentBaseFiles');
}

if (this.options.historyApiFallback) {
defaultFeatures.push('historyApiFallback', 'middleware');
runnableFeatures.push('historyApiFallback', 'middleware');

if (contentBase !== false) {
defaultFeatures.push('contentBaseFiles');
if (this.options.contentBase !== false) {
runnableFeatures.push('contentBaseFiles');
}
}

defaultFeatures.push('magicHtml');

// checking if it's set to true or not set (Default : undefined => true)
this.serveIndex = this.serveIndex || this.serveIndex === undefined;

const shouldHandleServeIndex = contentBase && this.serveIndex;

if (shouldHandleServeIndex) {
defaultFeatures.push('contentBaseIndex');
if (this.options.contentBase && this.serveIndex) {
runnableFeatures.push('contentBaseIndex');
}

// compress is placed last and uses unshift so that it will be the first middleware used
if (this.options.compress) {
defaultFeatures.unshift('compress');
if (this.options.watchContentBase) {
runnableFeatures.push('watchContentBase');
}

runnableFeatures.push('magicHtml');

if (this.options.after) {
defaultFeatures.push('after');
runnableFeatures.push('after');
}

(this.options.features || defaultFeatures).forEach((feature) => {
(this.options.features || runnableFeatures).forEach((feature) => {
features[feature]();
});
}
Expand Down

0 comments on commit 6020e21

Please sign in to comment.