From a8dd136168b604ba5f707c35f6fa736cfc9fd936 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 30 Dec 2016 13:20:44 +0200 Subject: [PATCH 1/5] Do not uglify builds when running start-dev --- package.json | 4 ++-- webpack.config.js | 33 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index b8b36936..699254c9 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ "start-dev": "npm-run-all --parallel watch start", "build": "npm-run-all build:*", "build:font-awesome": "node scripts/build-fontawesome.js", - "build:webpack": "webpack", - "watch": "webpack -w", + "build:webpack": "webpack -p", + "watch": "webpack -d --watch", "test": "npm-run-all -c test:* lint", "test:mocha": "mocha", "lint": "npm-run-all -c lint:*", diff --git a/webpack.config.js b/webpack.config.js index a09897ce..55f820d2 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -2,11 +2,12 @@ const webpack = require("webpack"); const path = require("path"); +const isWatch = process.argv.includes("--watch"); -module.exports = { +let config = { entry: { - app: path.resolve(__dirname, "client/js/lounge.js"), - vendor: [ + "js/bundle.js": path.resolve(__dirname, "client/js/lounge.js"), + "js/bundle.vendor.js": [ "handlebars/runtime", "jquery", "jquery-ui/ui/widgets/sortable", @@ -17,8 +18,8 @@ module.exports = { }, devtool: "source-map", output: { - path: path.resolve(__dirname, "client/js"), - filename: "bundle.js", + path: path.resolve(__dirname, "client"), + filename: "[name]", publicPath: "/" }, module: { @@ -53,14 +54,18 @@ module.exports = { ] }, plugins: [ - new webpack.optimize.CommonsChunkPlugin( - "vendor", // chunkName - "bundle.vendor.js" // filename - ), - new webpack.optimize.UglifyJsPlugin({ - compress: { - warnings: false - } - }), + new webpack.optimize.DedupePlugin(), + new webpack.optimize.CommonsChunkPlugin("js/bundle.vendor.js"), ] }; + +if (!isWatch) { + config.plugins.push(new webpack.optimize.UglifyJsPlugin({ + comments: false, + compress: { + warnings: false + } + })); +} + +module.exports = config; From d8f16909041907b636cd209b5963bf9d9491c5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 7 Jan 2017 18:04:46 -0500 Subject: [PATCH 2/5] Use Webpack configuration based on NODE_ENV instead of watch/no-watch Also, move the `DedupePlugin` to the prod-specific section. [Webpack doc](https://webpack.github.io/docs/list-of-plugins.html#dedupeplugin) itself recommends to not run this outside of production. Note that this currently breaks cross-OS support of `npm run build`. This will be fixed in a latter commit. --- package.json | 6 +++--- webpack.config.js | 15 +++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 699254c9..cdd0706f 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,14 @@ "start-dev": "npm-run-all --parallel watch start", "build": "npm-run-all build:*", "build:font-awesome": "node scripts/build-fontawesome.js", - "build:webpack": "webpack -p", - "watch": "webpack -d --watch", + "build:webpack": "webpack", + "watch": "webpack --watch", "test": "npm-run-all -c test:* lint", "test:mocha": "mocha", "lint": "npm-run-all -c lint:*", "lint:js": "eslint .", "lint:css": "stylelint \"**/*.css\"", - "prepublish": "npm run build" + "prepublish": "NODE_ENV=production npm run build" }, "keywords": [ "lounge", diff --git a/webpack.config.js b/webpack.config.js index 55f820d2..e2f4b094 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -2,7 +2,10 @@ const webpack = require("webpack"); const path = require("path"); -const isWatch = process.argv.includes("--watch"); + +// ******************** +// Common configuration +// ******************** let config = { entry: { @@ -54,12 +57,16 @@ let config = { ] }, plugins: [ - new webpack.optimize.DedupePlugin(), - new webpack.optimize.CommonsChunkPlugin("js/bundle.vendor.js"), + new webpack.optimize.CommonsChunkPlugin("js/bundle.vendor.js") ] }; -if (!isWatch) { +// ********************************* +// Production-specific configuration +// ********************************* + +if (process.env.NODE_ENV === "production") { + config.plugins.push(new webpack.optimize.DedupePlugin()); config.plugins.push(new webpack.optimize.UglifyJsPlugin({ comments: false, compress: { From 552fa3fae84dd7c75315dffe6e0bc28afabc14ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 7 Jan 2017 18:29:56 -0500 Subject: [PATCH 3/5] Make and document `npm run build` now a mandatory step of the install/build setup This has several reasons, benefits and consequences: - When running on root (which is not recommended anyway), `npm run build` was already necessary. - This allows to not use the `prepublish` hook, whose behavior is going to change in npm v5 and again in npm v6. - This allows to create both production and development builds when running from source. - It makes `npm run build` compatible with Windows again for development environments (lost in previous commit). - It uses the `prepublishOnly` hook added in npm v4. Since this hook is not available prior to that, deployment to npm from Travis has to be done on the Node.js v7 environment. --- .travis.yml | 5 ++++- README.md | 8 +++----- appveyor.yml | 1 + package.json | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index b8930fb9..5dbd8fee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,9 @@ cache: directories: - ~/.npm +before_script: + - npm run build + notifications: email: on_success: never @@ -23,6 +26,6 @@ deploy: api_key: secure: I9iN31GWI+Mz0xPw81N7qh1M6uidB+3BmiPUXt8QigX45zwp9EhvfZ0U/AIdUyQwzK2RK1zLRQSt+2/1jyeVi+U+AAsRRmaAUx8iqKaQPAkPnQtElolgRP04WSgo7fvNejfM7zS939bQNKG3RlSm04yPgu+ke2igf799p2bpFe2LtyoEeIiUfrUkBiMSpMguN9XF8a7jqCyIouTKjXHR24RmzJ9r7ZoMV27yQauS7XlD81bontzNRZxTytDKdJpZ+sxGIT9mbbtM4LUFX8MeNe3p/bjWavEhrO0ZIpkbOfS/L/w1375YDoNPXxCs288lnGUH+NbGNAEfn+BTz8cmUp7jI7QWR/kNACPeopdAX4OdZxT8wfQcfQZrfCuSpKciOMC7vGgPpQqjQ61t1RKcKs9VUnwC0SwWjyo8LlzkFKnP1ks0eDGYsSoPLdpC9+76UmePkQdxMhscO8TOgkOCcsTMLiyt6ABGOGKu2iE5SsjUYtPiSiRzSBAQENoO560+xBSVTKwqvvhzUAIt4AuAQSgsFjAylDdyzKoObHX12hBdALrqSOOSVwwIQ5/jTgNAsilURHo7KPD407PhRnLOsvumL0qg4sr9S1hjuUKnNla5dg9GY8FVjJ+b2t0A2vgfG1pR1e3vrJRXrpkfRorhmjvKAk2o5you5pQ1Itty7rM= on: - node: '4' + node: 7 tags: true repo: thelounge/lounge diff --git a/README.md b/README.md index e0439fbc..a40daffe 100644 --- a/README.md +++ b/README.md @@ -50,14 +50,12 @@ The following commands install the development version of The Lounge: git clone https://github.com/thelounge/lounge.git cd lounge npm install +NODE_ENV=production npm run build npm start ``` -A word of caution: - -- While it is the most recent codebase, this is not production-ready! -- It is not recommended to run this as root. However, if you decide to do so, - you will have to run `npm run build`. +⚠️ While it is the most recent codebase, this is not production-ready! Run at +your own risk. It is also not recommended to run this as root. ## Development setup diff --git a/appveyor.yml b/appveyor.yml index bc44f000..e36f70f2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,6 +13,7 @@ environment: install: - ps: Install-Product node $env:nodejs_version - appveyor-retry npm install + - npm run build - npm install mocha-appveyor-reporter - echo --reporter mocha-appveyor-reporter >> test/mocha.opts diff --git a/package.json b/package.json index cdd0706f..254e334e 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "lint": "npm-run-all -c lint:*", "lint:js": "eslint .", "lint:css": "stylelint \"**/*.css\"", - "prepublish": "NODE_ENV=production npm run build" + "prepublishOnly": "NODE_ENV=production npm run build" }, "keywords": [ "lounge", From c7e80006b90a0f363f35b83ad75afbbd18531600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 7 Jan 2017 18:53:56 -0500 Subject: [PATCH 4/5] Add an extra Travis CI job to make sure production build succeeds and passes tests --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5dbd8fee..e8f8e4fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,13 +6,16 @@ node_js: matrix: fast_finish: true + include: + - node_js: 7 # Version used to deploy to npm registry + env: BUILD_ENV=production cache: directories: - ~/.npm before_script: - - npm run build + - NODE_ENV=$BUILD_ENV npm run build notifications: email: @@ -27,5 +30,6 @@ deploy: secure: I9iN31GWI+Mz0xPw81N7qh1M6uidB+3BmiPUXt8QigX45zwp9EhvfZ0U/AIdUyQwzK2RK1zLRQSt+2/1jyeVi+U+AAsRRmaAUx8iqKaQPAkPnQtElolgRP04WSgo7fvNejfM7zS939bQNKG3RlSm04yPgu+ke2igf799p2bpFe2LtyoEeIiUfrUkBiMSpMguN9XF8a7jqCyIouTKjXHR24RmzJ9r7ZoMV27yQauS7XlD81bontzNRZxTytDKdJpZ+sxGIT9mbbtM4LUFX8MeNe3p/bjWavEhrO0ZIpkbOfS/L/w1375YDoNPXxCs288lnGUH+NbGNAEfn+BTz8cmUp7jI7QWR/kNACPeopdAX4OdZxT8wfQcfQZrfCuSpKciOMC7vGgPpQqjQ61t1RKcKs9VUnwC0SwWjyo8LlzkFKnP1ks0eDGYsSoPLdpC9+76UmePkQdxMhscO8TOgkOCcsTMLiyt6ABGOGKu2iE5SsjUYtPiSiRzSBAQENoO560+xBSVTKwqvvhzUAIt4AuAQSgsFjAylDdyzKoObHX12hBdALrqSOOSVwwIQ5/jTgNAsilURHo7KPD407PhRnLOsvumL0qg4sr9S1hjuUKnNla5dg9GY8FVjJ+b2t0A2vgfG1pR1e3vrJRXrpkfRorhmjvKAk2o5you5pQ1Itty7rM= on: node: 7 + condition: "$BUILD_ENV = production" tags: true repo: thelounge/lounge From bc8b699437f4205ebfffebc21aedc6c82d33aa22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 7 Jan 2017 19:05:42 -0500 Subject: [PATCH 5/5] Add a basic check for bundled application when starting the server Note that this will not detect if the client application was built with an old version of the repo. --- src/server.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/server.js b/src/server.js index 261f345f..34217944 100644 --- a/src/server.js +++ b/src/server.js @@ -18,6 +18,11 @@ var authFunction = localAuth; module.exports = function() { manager = new ClientManager(); + if (!fs.existsSync("client/js/bundle.js")) { + log.error(`The client application was not built. Run ${colors.bold("NODE_ENV=production npm run build")} to resolve this.`); + process.exit(); + } + var app = express() .use(allRequests) .use(index)