From da06a5b97fac6e949c02c7105bedaa2c546edd42 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Thu, 17 May 2018 10:10:33 -0700 Subject: [PATCH] Add server config, move plugin file --- server.js | 14 +++++++++----- server/actions/serveFile.js | 2 +- server/config.js | 6 ++++++ server/plugins/__tests__/.eslintrc | 5 +++++ .../__tests__/unpkgRewrite-test.js} | 2 +- .../unpkgRewrite.js} | 10 +++++++--- 6 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 server/config.js create mode 100644 server/plugins/__tests__/.eslintrc rename server/{utils/__tests__/unpkgRewriteBabelPlugin-test.js => plugins/__tests__/unpkgRewrite-test.js} (97%) rename server/{utils/unpkgRewriteBabelPlugin.js => plugins/unpkgRewrite.js} (84%) diff --git a/server.js b/server.js index a0a4ee2..5bf733f 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,9 @@ const path = require("path"); const throng = require("throng"); + const createServer = require("./server/createServer"); const createDevServer = require("./server/createDevServer"); - -const port = parseInt(process.env.PORT, 10) || 5000; +const config = require("./server/config"); function startServer(id) { const server = @@ -15,11 +15,15 @@ function startServer(id) { : createDevServer( path.resolve(__dirname, "public"), require("./webpack.config"), - `http://localhost:${port}` + config.origin ); - server.listen(port, () => { - console.log("Server #%s listening on port %s, Ctrl+C to stop", id, port); + server.listen(config.port, () => { + console.log( + "Server #%s listening on port %s, Ctrl+C to stop", + id, + config.port + ); }); } diff --git a/server/actions/serveFile.js b/server/actions/serveFile.js index 4696707..92a55be 100644 --- a/server/actions/serveFile.js +++ b/server/actions/serveFile.js @@ -7,7 +7,7 @@ const IndexPage = require("../components/IndexPage"); const renderPage = require("../utils/renderPage"); const getMetadata = require("../utils/getMetadata"); const getFileContentType = require("../utils/getFileContentType"); -const unpkgRewrite = require("../utils/unpkgRewriteBabelPlugin"); +const unpkgRewrite = require("../plugins/unpkgRewrite"); const getEntries = require("../utils/getEntries"); /** diff --git a/server/config.js b/server/config.js new file mode 100644 index 0000000..f20c6b2 --- /dev/null +++ b/server/config.js @@ -0,0 +1,6 @@ +exports.port = parseInt(process.env.PORT, 10) || 5000; + +exports.origin = + process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test" + ? "https://unpkg.com" + : `http://localhost:${exports.port}`; diff --git a/server/plugins/__tests__/.eslintrc b/server/plugins/__tests__/.eslintrc new file mode 100644 index 0000000..55f121d --- /dev/null +++ b/server/plugins/__tests__/.eslintrc @@ -0,0 +1,5 @@ +{ + "env": { + "jest": true + } +} diff --git a/server/utils/__tests__/unpkgRewriteBabelPlugin-test.js b/server/plugins/__tests__/unpkgRewrite-test.js similarity index 97% rename from server/utils/__tests__/unpkgRewriteBabelPlugin-test.js rename to server/plugins/__tests__/unpkgRewrite-test.js index ce43502..e6307b3 100644 --- a/server/utils/__tests__/unpkgRewriteBabelPlugin-test.js +++ b/server/plugins/__tests__/unpkgRewrite-test.js @@ -1,5 +1,5 @@ const babel = require("babel-core"); -const unpkgRewrite = require("../unpkgRewriteBabelPlugin"); +const unpkgRewrite = require("../unpkgRewrite"); const testCases = [ { diff --git a/server/utils/unpkgRewriteBabelPlugin.js b/server/plugins/unpkgRewrite.js similarity index 84% rename from server/utils/unpkgRewriteBabelPlugin.js rename to server/plugins/unpkgRewrite.js index 81674b2..7923fb9 100644 --- a/server/utils/unpkgRewriteBabelPlugin.js +++ b/server/plugins/unpkgRewrite.js @@ -1,9 +1,11 @@ const URL = require("whatwg-url"); const warning = require("warning"); +const config = require("../config"); + const bareIdentifierFormat = /^((?:@[^\/]+\/)?[^\/]+)(\/.*)?$/; -function unpkgRewriteBabelPlugin(dependencies = {}) { +function unpkgRewrite(dependencies = {}) { return { inherits: require("babel-plugin-syntax-export-extensions"), @@ -34,11 +36,13 @@ function unpkgRewriteBabelPlugin(dependencies = {}) { const version = dependencies[packageName] || "latest"; - path.node.source.value = `https://unpkg.com/${packageName}@${version}${file}?module`; + path.node.source.value = `${ + config.origin + }/${packageName}@${version}${file}?module`; } } } }; } -module.exports = unpkgRewriteBabelPlugin; +module.exports = unpkgRewrite;