Browse Source

set up webpack to be served from express in prod mode

Ben Ward 3 năm trước cách đây
mục cha
commit
0b0c459212
9 tập tin đã thay đổi với 74 bổ sung23 xóa
  1. 1 0
      dist/app.bundle.js
  2. 32 0
      dist/app.bundle.js.LICENSE.txt
  3. 1 0
      dist/index.html
  4. 3 10
      package-lock.json
  5. 4 2
      package.json
  6. 2 3
      server/server.js
  7. 14 8
      webpack.common.js
  8. 11 0
      webpack.dev.js
  9. 6 0
      webpack.prod.js

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 1 - 0
dist/app.bundle.js


+ 32 - 0
dist/app.bundle.js.LICENSE.txt

@@ -0,0 +1,32 @@
+/*
+object-assign
+(c) Sindre Sorhus
+@license MIT
+*/
+
+/** @license React v0.20.2
+ * scheduler.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+/** @license React v17.0.2
+ * react-dom.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+/** @license React v17.0.2
+ * react.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */

+ 1 - 0
dist/index.html

@@ -0,0 +1 @@
+<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Ben's RPG Server</title><script defer="defer" src="app.bundle.js"></script></head><body><div id="app"></div></body></html>

+ 3 - 10
package-lock.json

@@ -1261,7 +1261,6 @@
       "version": "4.0.1",
       "resolved": "https://registry.npmjs.org/clone-deep/-/clone-deep-4.0.1.tgz",
       "integrity": "sha512-neHB9xuzh/wk0dIHweyAXv2aPGZIVk3pLMe+/RNzINf17fe0OG96QroktYAUm7SM1PBnzTabaLboqqxDyMU+SQ==",
-      "dev": true,
       "requires": {
         "is-plain-object": "^2.0.4",
         "kind-of": "^6.0.2",
@@ -2625,7 +2624,6 @@
       "version": "2.0.4",
       "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz",
       "integrity": "sha512-h5PpgXkWitc38BBMYawTYMWJHFZJVnBquFE57xFpjB8pJFiF6gZ+bU+WyI/yqXiFR5mdLsgYNaPe8uao6Uv9Og==",
-      "dev": true,
       "requires": {
         "isobject": "^3.0.1"
       }
@@ -2680,8 +2678,7 @@
     "isobject": {
       "version": "3.0.1",
       "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz",
-      "integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8=",
-      "dev": true
+      "integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8="
     },
     "jest-worker": {
       "version": "27.4.4",
@@ -2764,8 +2761,7 @@
     "kind-of": {
       "version": "6.0.3",
       "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz",
-      "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==",
-      "dev": true
+      "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw=="
     },
     "latest-version": {
       "version": "5.1.0",
@@ -3961,7 +3957,6 @@
       "version": "3.0.1",
       "resolved": "https://registry.npmjs.org/shallow-clone/-/shallow-clone-3.0.1.tgz",
       "integrity": "sha512-/6KqX+GVUdqPuPPd2LxDDxzX6CAbjJehAAOKlNpqqUpAqPM6HeL8f+o3a+JsyGjn2lv0WY8UsTgUJjU9Ok55NA==",
-      "dev": true,
       "requires": {
         "kind-of": "^6.0.2"
       }
@@ -4672,7 +4667,6 @@
       "version": "5.8.0",
       "resolved": "https://registry.npmjs.org/webpack-merge/-/webpack-merge-5.8.0.tgz",
       "integrity": "sha512-/SaI7xY0831XwP6kzuwhKWVKDP9t1QY1h65lAFLbZqMPIuYcD9QAW4u9STIbU9kaJbPBB/geU/gLr1wDjOhQ+Q==",
-      "dev": true,
       "requires": {
         "clone-deep": "^4.0.1",
         "wildcard": "^2.0.0"
@@ -4730,8 +4724,7 @@
     "wildcard": {
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/wildcard/-/wildcard-2.0.0.tgz",
-      "integrity": "sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw==",
-      "dev": true
+      "integrity": "sha512-JcKqAHLPxcdb9KM49dufGXn2x3ssnfjbcaQdLlfZsL9rH9wgDQjUtDxbo8NE0F6SFvydeu1VhZe7hZuHsB2/pw=="
     },
     "wrap-ansi": {
       "version": "7.0.0",

+ 4 - 2
package.json

@@ -5,7 +5,8 @@
   "main": "webpack.config.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
-    "start": "webpack serve --config webpack.config.js"
+    "start": "webpack serve --config webpack.dev.js",
+    "build": "webpack --config webpack.prod.js"
   },
   "keywords": [],
   "author": "",
@@ -30,6 +31,7 @@
     "mongoose": "^6.1.1",
     "nodemon": "^2.0.15",
     "react": "^17.0.2",
-    "react-dom": "^17.0.2"
+    "react-dom": "^17.0.2",
+    "webpack-merge": "^5.8.0"
   }
 }

+ 2 - 3
server/server.js

@@ -4,6 +4,7 @@ const cors = require('cors')
 const app = express()
 const apiPort = 3000
 const messageRouter = require('./routes/messages')
+const path = require("path")
 
 app.use(bodyParser.urlencoded({ extended: true }))
 app.use(cors())
@@ -12,9 +13,7 @@ app.use(bodyParser.json())
 const db = require('./db/db.js')
 db.on('error', console.error.bind(console, 'MongoDB connection error:'))
 
-app.get('/', (req, res) => {
-    res.send('Hello World!')
-})
+app.use("/", express.static(path.join(__dirname, '../dist')))
 
 
 app.use('/api', messageRouter)

+ 14 - 8
webpack.config.js → webpack.common.js

@@ -1,13 +1,14 @@
 const HtmlWebPackPlugin = require("html-webpack-plugin");
-const htmlPlugin = new HtmlWebPackPlugin({template: "./src/index.html", filename: "./index.html"});
+const path = require('path');
+const htmlPlugin = new HtmlWebPackPlugin({
+    title: "Production",
+    template: "src/index.html"
+});
+
 module.exports = {
-    devServer: {
-        allowedHosts: [
-          '.bwlw.uk',
-          'localhost'
-        ]
+    entry: {
+        app: './src/index.js',
     },
-    mode: 'development',
     module: {
         rules: [
             {
@@ -22,5 +23,10 @@ module.exports = {
             }
         ]
     },
-    plugins: [htmlPlugin]
+    plugins: [htmlPlugin],
+    output: {
+        filename: '[name].bundle.js',
+        path: path.resolve(__dirname, 'dist'),
+        clean: true
+    }
 };

+ 11 - 0
webpack.dev.js

@@ -0,0 +1,11 @@
+const { merge } = require('webpack-merge');
+const common = require('./webpack.common.js');
+
+module.exports = merge(common, {
+  mode: 'development',
+  devtool: 'inline-source-map',
+  devServer: {
+    static: './dist',
+    allowedHosts: ['.bwlw.uk', 'localhost']
+  },
+});

+ 6 - 0
webpack.prod.js

@@ -0,0 +1,6 @@
+const { merge } = require('webpack-merge');
+const common = require('./webpack.common.js');
+
+module.exports = merge(common, {
+  mode: 'production',
+});

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác