forked from joepie91/budo-express
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
4 years ago
|
"use strict";
|
||
|
|
||
|
const defaultValue = require("default-value");
|
||
|
const assureArray = require("assure-array");
|
||
|
const path = require("path");
|
||
|
|
||
|
const validateOptions = require("./validation/validate-options");
|
||
|
const bundle = require("./operations/bundle");
|
||
|
const productionServer = require("./operations/production-server");
|
||
|
const developmentServer = require("./operations/development-server");
|
||
|
|
||
|
function getDevelopmentMode(developmentModeSetting) {
|
||
|
if (developmentModeSetting === "auto") {
|
||
|
return (process.env.NODE_ENV === "development");
|
||
|
} else {
|
||
|
/* Hard-coded true or false */
|
||
|
return developmentModeSetting;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = function budoExpress(options = {}) {
|
||
|
validateOptions(options);
|
||
|
|
||
|
function projectPath(target) {
|
||
|
return path.resolve(options.basePath, target);
|
||
|
}
|
||
|
|
||
|
let staticBasePath = projectPath(options.staticPath);
|
||
|
|
||
|
let context = {
|
||
|
options: options,
|
||
|
entryPaths: assureArray(options.entryFiles).map((file) => projectPath(file)),
|
||
|
staticBasePath: staticBasePath,
|
||
|
staticPath: function staticPath(target) {
|
||
|
return path.resolve(staticBasePath, target);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
if (process.env.BUDO_BUILD === "1") {
|
||
|
bundle(context);
|
||
|
} else {
|
||
|
let productionHost = defaultValue(options.host, "::");
|
||
|
let developmentHost = (options.allowUnsafeHost) ? productionHost : "127.0.0.1";
|
||
|
|
||
|
let developmentModeSetting = defaultValue(options.developmentMode, "auto");
|
||
|
let developmentMode = getDevelopmentMode(developmentModeSetting);
|
||
|
|
||
|
if (developmentMode) {
|
||
|
developmentServer({
|
||
|
... context,
|
||
|
host: developmentHost
|
||
|
});
|
||
|
} else {
|
||
|
productionServer({
|
||
|
... context,
|
||
|
host: productionHost
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
};
|