Skip to content

Commit c8e3ffd

Browse files
authored
Optimize webpack usage, fix #34 (#58)
* introduce prod/dev build https://webpack.js.org/guides/production/ * use it properly with express https://webpack.js.org/guides/development/#using-webpack-dev-middleware to provide a fast npm start (5 second)
1 parent b175c50 commit c8e3ffd

File tree

7 files changed

+82
-45
lines changed

7 files changed

+82
-45
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
with:
2323
node-version: ${{ matrix.node-version }}
2424
- run: npm install
25+
- run: npm run build
2526
- run: npm test
2627
DeployPages:
2728
runs-on: ubuntu-latest
@@ -37,6 +38,7 @@ jobs:
3738
- name: Build
3839
run: |
3940
npm install
41+
npm run build
4042
cp -R public/ ../
4143
git checkout -b gh-pages
4244
rm -Rf ./*

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
[![Discord](https://img.shields.io/badge/chat-on%20discord-brightgreen.svg)](https://discord.gg/GsEFRM8)
55
[![Try it on gitpod](https://img.shields.io/badge/try-on%20gitpod-brightgreen.svg)](https://gitpod.io/#https://github.com/PrismarineJS/prismarine-web-client)
66

7-
### A Minecraft client running in a web page. **Live demo at https://prismarine.js.org/prismarine-web-client/**
8-
7+
A Minecraft client running in a web page. **Live demo at https://prismarine.js.org/prismarine-web-client/**
98

109

1110
## How it Works
12-
prismarine-web-client runs mineflayer and prismarine-viewer in the browser, which connects over WebSocket to a proxy which translates the WebSocket connection into TCP to connect to normal Minecraft servers.
11+
prismarine-web-client runs mineflayer and prismarine-viewer in the browser, which connects over WebSocket to a proxy
12+
which translates the WebSocket connection into TCP to connect to normal Minecraft servers.
1313

1414
## Screenshot
1515
![Screenshot of prismarine-web-client in action](screenshot.png)
1616

1717
## Live Demo
18-
Click on this link to open it in your browser, no installation necessary: https://prismarine-web-client.js.org
18+
Click on this link to open it in your browser, no installation necessary: https://prismarine.js.org/prismarine-web-client
1919

2020
*Tested on Chrome & Firefox for desktop platforms.*
2121

22-
## Usage (for self-hosted installations)
23-
If you want the latest version or want to use auth, you can host an instance yourself.
24-
25-
Run these commands in bash:
22+
## Usage
23+
To host it yourself, run these commands in bash:
2624
```bash
2725
$ npm install -g prismarine-web-client
2826
$ prismarine-web-client
@@ -31,20 +29,16 @@ Finally, open `http://localhost:8080` in your browser.
3129

3230
## Features
3331

34-
* Display mobs (though sometimes messed up)
35-
* Display players
36-
* Display other entities as colored rectangles
32+
* Display mobs and players
3733
* Display blocks
3834
* Movement (you can move, and you see entities moving live)
3935
* Place and break blocks
4036

4137
## Roadmap
4238
* Containers (inventory, chests, etc.)
43-
* More Customisation (e.g. mouse sensitivity, text size, issue #40)
4439
* Sounds
4540
* More World Interactions (attacking entities, etc.)
4641
* Cosmetic Rendering Features (day night cycle, fog, etc.)
47-
* Server-Side Support Plugins (for auth bypass & possibly hosting its own websocket proxy to reduce latency, issue #13)
4842

4943
## Development
5044

@@ -61,10 +55,17 @@ Finally, run
6155

6256
```bash
6357
$ npm install
64-
$ npm run build-start
58+
$ npm start
6559
```
6660

67-
Then connect to http://localhost:8080 in your browser.
61+
This will start express and webpack in development mode: whenever you save a file, the build will be redone (it takes 5s),
62+
and you can refresh the page to get the new result.
63+
64+
Connect to http://localhost:8080 in your browser.
65+
66+
You may want to disable auto saving in your IDE to avoid constant rebuilding, see https://webpack.js.org/guides/development/#adjusting-your-text-editor
6867

68+
To check the production build (take a minute to build), you can run `npm run build-start`
6969

70+
If you're interested in contributing, you can check projects at https://github.com/PrismarineJS/prismarine-web-client/projects
7071

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"description": "A minecraft client running in a browser",
55
"main": "index.js",
66
"scripts": {
7-
"prepare": "webpack",
8-
"start": "webpack serve",
7+
"build": "webpack --config webpack.prod.js",
8+
"dev-build": "webpack --config webpack.dev.js",
9+
"start": "node server.js 8080 dev",
910
"prod-start": "node server.js",
10-
"build-start": "npm run prepare && npm run prod-start",
11-
"public-start": "npm run prepare && node server.js 80",
11+
"build-start": "npm run build && npm run prod-start",
12+
"prepublishOnly": "npm run build",
1213
"lint": "standard",
1314
"fix": "standard --fix",
1415
"test": "npm run lint && mocha"
@@ -41,8 +42,8 @@
4142
"homepage": "https://github.com/PrismarineJS/prismarine-web-client#readme",
4243
"dependencies": {
4344
"compression": "^1.7.4",
44-
"express": "^4.17.1",
45-
"net-browserify": "PrismarineJS/net-browserify"
45+
"net-browserify": "PrismarineJS/net-browserify",
46+
"express": "^4.17.1"
4647
},
4748
"devDependencies": {
4849
"assert": "^2.0.0",
@@ -70,6 +71,8 @@
7071
"timers-browserify": "^2.0.12",
7172
"webpack": "^5.11.0",
7273
"webpack-cli": "^4.2.0",
73-
"webpack-dev-server": "^3.11.0"
74+
"webpack-dev-middleware": "^3.7.3",
75+
"webpack-dev-server": "^3.11.0",
76+
"webpack-merge": "^5.7.3"
7477
}
7578
}

server.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,21 @@ const app = express()
1010

1111
app.use(compression())
1212
app.use(netApi({ allowOrigin: '*' }))
13-
app.use(express.static(path.join(__dirname, './public')))
14-
app.use(express.json({ limit: '100kb' }))
13+
if (process.argv[3] === 'dev') {
14+
// https://webpack.js.org/guides/development/#using-webpack-dev-middleware
15+
const webpackDevMiddleware = require('webpack-dev-middleware')
16+
const config = require('./webpack.dev.js')
17+
const webpack = require('webpack')
18+
const compiler = webpack(config)
19+
20+
app.use(
21+
webpackDevMiddleware(compiler, {
22+
publicPath: config.output.publicPath
23+
})
24+
)
25+
} else {
26+
app.use(express.static(path.join(__dirname, './public')))
27+
}
1528

1629
// Start the server
1730
const server = app.listen(process.argv[2] === undefined ? 8080 : process.argv[2], function () {

webpack.config.js renamed to webpack.common.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
const webpack = require('webpack')
22
const path = require('path')
3-
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
43
const CopyPlugin = require('copy-webpack-plugin')
5-
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
4+
// https://webpack.js.org/guides/production/
65

76
const config = {
8-
// devtool: 'inline-source-map',
9-
// mode: 'development',
10-
mode: 'production',
117
entry: path.resolve(__dirname, './index.js'),
128
output: {
139
path: path.resolve(__dirname, './public'),
14-
filename: './index.js'
10+
filename: './index.js',
11+
publicPath: '/'
1512
},
1613
resolve: {
1714
alias: {
@@ -44,7 +41,6 @@ const config = {
4441
},
4542
plugins: [
4643
// fix "process is not defined" error:
47-
new CleanWebpackPlugin(),
4844
new webpack.ProvidePlugin({
4945
process: 'process/browser'
5046
}),
@@ -67,20 +63,8 @@ const config = {
6763
{ from: path.join(__dirname, 'assets/mojangles.ttf'), to: './' },
6864
{ from: path.join(__dirname, 'extra-textures/'), to: './extra-textures/' }
6965
]
70-
}),
71-
new webpack.optimize.ModuleConcatenationPlugin(),
72-
new LodashModuleReplacementPlugin()
73-
],
74-
devServer: {
75-
contentBase: path.resolve(__dirname, './public'),
76-
compress: true,
77-
inline: true,
78-
// open: true,
79-
hot: true,
80-
watchOptions: {
81-
ignored: /node_modules/
82-
}
83-
}
66+
})
67+
]
8468
}
8569

8670
module.exports = config

webpack.dev.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { merge } = require('webpack-merge')
2+
const common = require('./webpack.common.js')
3+
const path = require('path')
4+
5+
module.exports = merge(common, {
6+
mode: 'development',
7+
devtool: 'inline-source-map',
8+
cache: true,
9+
devServer: {
10+
contentBase: path.resolve(__dirname, './public'),
11+
compress: true,
12+
inline: true,
13+
// open: true,
14+
hot: true,
15+
watchOptions: {
16+
ignored: /node_modules/
17+
}
18+
}
19+
})

webpack.prod.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { merge } = require('webpack-merge')
2+
const common = require('./webpack.common.js')
3+
4+
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
5+
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
6+
const webpack = require('webpack')
7+
8+
module.exports = merge(common, {
9+
mode: 'production',
10+
plugins: [
11+
new CleanWebpackPlugin(),
12+
new webpack.optimize.ModuleConcatenationPlugin(),
13+
new LodashModuleReplacementPlugin()
14+
]
15+
})

0 commit comments

Comments
 (0)