jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/375101 )

Change subject: Chore: consolidate configuration variables
......................................................................


Chore: consolidate configuration variables

Consolidate many of the loosely tied but tightly coupled configurations
and their defaults using typing.

Change-Id: I7feba28241d8ccbc403c716fa3bff34c6f03c120
---
M docs/development.md
A src/server/configuration.ts
M src/server/index.ts
M webpack.config.ts
4 files changed, 45 insertions(+), 27 deletions(-)

Approvals:
  Jhernandez: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/docs/development.md b/docs/development.md
index 4505a5c..092853a 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -118,9 +118,4 @@
 
 ## Environment variables
 
-* `NODE_ENV`: unset or `production`; defaults to development. Controls debug
-  functionality.
-* `PORT`: unset or a natural number; defaults to 3000. Controls server port.
-* `VERBOSE`: unset or `1`; defaults to disabled. Controls build output.
-* `WEBPACK_DEV_SERVER_PORT`: unset or a natural number; defaults to 8080.
-  Controls debug client port.
+See [configuration](../src/server/configuration.ts).
diff --git a/src/server/configuration.ts b/src/server/configuration.ts
new file mode 100644
index 0000000..73f4f95
--- /dev/null
+++ b/src/server/configuration.ts
@@ -0,0 +1,13 @@
+export const production: boolean = process.env.NODE_ENV === "production";
+
+export const verbose: boolean = Boolean(JSON.parse(process.env.VERBOSE || 
"0"));
+
+export const serverPort: number = JSON.parse(process.env.PORT || "3000");
+
+export const serverUrl: string = `http://localhost:${serverPort}`;
+
+export const webpackDevServerPort: number = JSON.parse(
+  process.env.WEBPACK_DEV_SERVER_PORT || "8080"
+);
+
+export const webpackDevServerUrl: string = 
`http://localhost:${webpackDevServerPort}`;
diff --git a/src/server/index.ts b/src/server/index.ts
index 5be92b7..2029297 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -2,19 +2,22 @@
 import "ignore-styles";
 
 import * as express from "express";
+import {
+  production,
+  serverPort,
+  serverUrl,
+  webpackDevServerUrl
+} from "./configuration";
 import { api } from "../common/routers/api";
 import page from "./templates/page";
 import { render } from "preact-render-to-string";
 
-const isProd: boolean = process.env.NODE_ENV === "production";
-
 // The asset manifest built or the webpack-dev-server URL (which has no
 // manifest).
-const assets = isProd
+const assets = production
   ? require("../../dist/public/assets-manifest.json")
-  : "http://localhost:8080";;
+  : webpackDevServerUrl;
 
-const { PORT = 3000 } = process.env;
 const server = express();
 
 server.use(express.static("dist/public"));
@@ -34,10 +37,10 @@
   });
 });
 
-server.listen(PORT, () => {
-  console.log(`Server started on http://localhost:${PORT}/`); // 
eslint-disable-line no-console
+server.listen(serverPort, () => {
+  console.log(`Server started on ${serverUrl}/`); // eslint-disable-line 
no-console
 
-  if (!isProd) {
+  if (!production) {
     const touch = require("touch");
 
     // The server is now listening and ready to receive requests. If
diff --git a/webpack.config.ts b/webpack.config.ts
index 0bb11f2..d524d29 100644
--- a/webpack.config.ts
+++ b/webpack.config.ts
@@ -3,9 +3,11 @@
 import * as ExtractTextPlugin from "extract-text-webpack-plugin";
 import * as path from "path";
 import * as webpack from "webpack";
-
-const isProd = process.env.NODE_ENV === "production";
-const isVerbose = Boolean(JSON.parse(process.env.VERBOSE || "false"));
+import {
+  production,
+  verbose,
+  webpackDevServerPort
+} from "./src/server/configuration";
 
 const paths = {
   client: {
@@ -17,7 +19,7 @@
 // https://github.com/webpack/webpack/blob/7fe0371/lib/Stats.js#L886
 // https://github.com/webpack/webpack/blob/7fe0371/lib/Stats.js#L101-L131
 const stats = {
-  all: isVerbose, // Default all outputs to verbosity.
+  all: verbose, // Default all outputs to verbosity.
   errors: true,
   errorDetails: true,
   warnings: true
@@ -35,10 +37,10 @@
     // Use chunkhash instead of hash to get per-file/chunk hashing instead of
     // global build hashing, to improve caching from browsers.
     // See: https://webpack.js.org/guides/caching/#output-filenames
-    chunkFilename: isProd ? "[name].[chunkhash].js" : "[name].js",
+    chunkFilename: production ? "[name].[chunkhash].js" : "[name].js",
 
     // Use constant filenames for developmental server.
-    filename: isProd ? "[name].[chunkhash].js" : "[name].js"
+    filename: production ? "[name].[chunkhash].js" : "[name].js"
   },
 
   resolve: {
@@ -52,7 +54,7 @@
         test: /\.tsx?$/,
         loader: "ts-loader",
         options: {
-          logLevel: isVerbose ? "info" : "warn"
+          logLevel: verbose ? "info" : "warn"
         }
       },
       {
@@ -65,22 +67,27 @@
     ]
   },
 
-  devtool: isProd ? "source-map" : "cheap-module-eval-source-map",
+  devtool: production ? "source-map" : "cheap-module-eval-source-map",
 
   // For development builds, serve the packaged result over
   // http://localhost:8080/ and live reload the browser when the bundle is
   // rebuilt.
-  devServer: isProd
+  devServer: production
     ? undefined
     : {
         // Forbid static files. All responses are in memory.
         contentBase: false,
 
+        // Explicitly specify the default port so a failure to allocate will
+        // cause Webpack to exit with a nonzero. Otherwise, a free port is
+        // allocated  and requests fail to execute.
+        port: webpackDevServerPort,
+
         // Log warnings and errors in the browser console.
-        clientLogLevel: isVerbose ? "info" : "warning",
+        clientLogLevel: verbose ? "info" : "warning",
 
         // Hide bundling start and finish messages.
-        noInfo: !isVerbose,
+        noInfo: !verbose,
 
         // Show warnings and errors as an obtrusive opaque overlay in the
         // browser.
@@ -92,11 +99,11 @@
 
 configuration.plugins = [
   new ExtractTextPlugin({
-    filename: isProd ? "[name].[contenthash].css" : "[name].css"
+    filename: production ? "[name].[contenthash].css" : "[name].css"
   })
 ];
 
-if (isProd) {
+if (production) {
   // Generate a json manifest with the entry points and assets names to use
   // in the server to pass to the HTML page template
   configuration.plugins.push(

-- 
To view, visit https://gerrit.wikimedia.org/r/375101
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7feba28241d8ccbc403c716fa3bff34c6f03c120
Gerrit-PatchSet: 1
Gerrit-Project: marvin
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <[email protected]>
Gerrit-Reviewer: Jhernandez <[email protected]>
Gerrit-Reviewer: Sniedzielski <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to