Skip to content

Commit 4734523

Browse files
committed
optimization
1 parent 89bf81b commit 4734523

File tree

6 files changed

+53
-45
lines changed

6 files changed

+53
-45
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Introduction
2-
A plugin that deploys webpack bundles to server. It's useful when server ftp/sftp is forbidden or accessing server need pin + dynamic token.
2+
A plugin that deploys webpack bundles to server. It's useful when server ftp/sftp is forbidden or accessing server need pin + dynamic token each time.
33

44
# Install
55
```
@@ -10,10 +10,11 @@ npm i deploy-server-webpack-plugin -D
1010

1111
**Client config**
1212

13-
You need config your webpack conf file like this:
13+
Modify your webpack config file like below, change the "from" and "dest" fields according to your own project, "from" is based on webpack output dirname, "dest" means the path on server you want to push. token field validation need server side to cooperate.
1414

1515
```js
16-
const DeployServerPlugin = require('deploy-server-webpack-plugin');
16+
const path = require('path')
17+
const DeployServerPlugin = require('deploy-server-webpack-plugin')
1718

1819
module.exports = {
1920
// ...
@@ -23,7 +24,7 @@ module.exports = {
2324
receiver: 'http://1.23.45.678:9999/receiver',
2425
mapping: { // Object type
2526
from: path.resolve(__dirname, '../dist'), // absolute path
26-
dest: '/data/front'
27+
dest: '/data/project/front'
2728
}
2829
})
2930
]
@@ -32,6 +33,7 @@ module.exports = {
3233
or
3334

3435
```js
36+
const path = require('path')
3537
const DeployServerPlugin = require('deploy-server-webpack-plugin')
3638

3739
module.exports = {
@@ -42,12 +44,12 @@ module.exports = {
4244
receiver: 'http://1.23.45.678:9999/receiver',
4345
mapping: [ // Array type
4446
{
45-
from: path.resolve(__dirname, '../dist/static'), // absolute path
46-
dest: '/data/public/static',
47+
from: path.resolve(__dirname, '../dist/static'),
48+
dest: '/data/project/public/static',
4749
},
4850
{
49-
from: path.resolve(__dirname, '../dist/index.tpl'), // absolute path
50-
dest: '/data/views/index.tpl',
51+
from: path.resolve(__dirname, '../dist/index.tpl'),
52+
dest: '/data/project/views/index.tpl',
5153
},
5254
// ...
5355
],
@@ -59,15 +61,15 @@ module.exports = {
5961

6062
**Server Config**
6163

62-
Please copy ./server folder to you remote server somewhere, init the project and start it.
64+
Copy ./server folder to you server machine somewhere, init the project and start it.
6365

6466
```
6567
npm i
6668
npm run start
6769
```
6870
Next config your nginx/apache to allow your node service can be accessed.
6971

70-
Try to visit "@your host/receiver" in browser, when you see "Method Not Allowed", it means node server started success, but 'GET' method is not allowed because we only config "POST" router to upload files.
72+
Try to visit "@your host/receiver" in browser, when you see "Method Not Allowed", it means node server started success, but 'GET' method is not allowed because we only config "POST" router to upload files. Server code is based on Koa, change it according your demand.
7173

7274
# Options
7375

@@ -78,7 +80,7 @@ Try to visit "@your host/receiver" in browser, when you see "Method Not Allowed"
7880
|token|String|false|for security if needed|
7981

8082
# Others
81-
Sometimes bundle files are too big and uploading appears "504 Gateay Time-out" error, enlarge client_max_body_size value in nginx.conf may solve this problem:
83+
Sometimes bundle file is too big and uploading appears "504 Gateay Time-out" error, enlarge client_max_body_size value in nginx.conf may solve this problem:
8284
```
8385
client_max_body_size: 10M; #default 1M
8486
```

dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deploy-server-webpack-plugin",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A plugin that deploys wepack bundles to server",
55
"main": "dist/index.js",
66
"scripts": {
@@ -12,7 +12,14 @@
1212
},
1313
"keywords": [
1414
"deploy server",
15-
"webpack plugin"
15+
"push server",
16+
"publish server",
17+
"release server",
18+
"webpack plugin",
19+
"node deploy",
20+
"node push",
21+
"node publish",
22+
"node release"
1623
],
1724
"author": "Jordan Wang",
1825
"license": "MIT",

server/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const Koa = require('koa');
22
const koaBody = require('koa-body');
3-
const Router = require('koa-router');
3+
const KoaRouter = require('koa-router');
44
const fse = require('fs-extra');
55

6-
const router = new Router();
6+
const router = new KoaRouter();
77
router.post('/receiver', koaBody({
88
multipart: true
9-
}), async (ctx, next) => {
9+
}), async ctx => {
1010
const { files, body: { dest, token } } = ctx.request;
1111

1212
// deal with "token" here if needed

server/package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"name": "receiver",
3-
"version": "0.0.1",
4-
"description": "file receiver",
5-
"main": "app.js",
6-
"scripts": {
7-
"start": "pm2 start app.js",
8-
"stop": "pm2 stop app.js"
9-
},
10-
"author": "Jordan Wang",
11-
"license": "MIT",
12-
"dependencies": {
13-
"fs-extra": "^6.0.1",
14-
"koa": "^2.5.2",
15-
"koa-body": "^4.0.4",
16-
"koa-router": "^7.4.0",
17-
"pm2": "^3.0.0"
18-
}
2+
"name": "receiver",
3+
"version": "0.0.2",
4+
"description": "files receiver",
5+
"main": "app.js",
6+
"scripts": {
7+
"start": "pm2 start app.js",
8+
"stop": "pm2 stop app.js"
9+
},
10+
"author": "Jordan Wang",
11+
"license": "MIT",
12+
"dependencies": {
13+
"fs-extra": "^6.0.1",
14+
"koa": "^2.5.2",
15+
"koa-body": "^4.0.4",
16+
"koa-router": "^7.4.0",
17+
"pm2": "^3.0.0"
18+
}
1919
}

src/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const fs = require('fs');
2-
const path = require('path');
32
const chalk = require('chalk');
43
const request = require('request');
54

@@ -10,12 +9,12 @@ module.exports = class DeployServerWebpackPlugin {
109

1110
apply(compiler) {
1211
compiler.plugin('after-emit', (compilation, callback) => {
13-
this.validConfig(compilation);
12+
this.validateConfig(compilation);
1413
this.deployHandler(callback);
1514
});
1615
}
1716

18-
validConfig(compilation) {
17+
validateConfig(compilation) {
1918
const { receiver, token = '' } = this.config;
2019
let { mapping } = this.config;
2120

@@ -26,10 +25,10 @@ module.exports = class DeployServerWebpackPlugin {
2625
this.error('Missing param: mapping');
2726
}
2827

29-
const mappingType = Object.prototype.toString.call(mapping);
30-
if (mappingType === '[object Object]') {
28+
const type = Object.prototype.toString.call(mapping);
29+
if (type === '[object Object]') {
3130
mapping = [mapping];
32-
} else if (mappingType === '[object Array]') {
31+
} else if (type === '[object Array]') {
3332
// do nothing
3433
} else {
3534
this.error('Invalid param: mapping');
@@ -72,9 +71,9 @@ module.exports = class DeployServerWebpackPlugin {
7271
// compatible with windows
7372
const dest = (this.mapping[index].to + from.replace(item, '')).replace(/\\/g, '/');
7473
this.deploy({
74+
file: fs.createReadStream(from),
7575
dest,
76-
token: this.token,
77-
file: fs.createReadStream(from)
76+
token: this.token
7877
});
7978
});
8079
});
@@ -88,7 +87,7 @@ module.exports = class DeployServerWebpackPlugin {
8887
formData
8988
}, (err, { statusCode } = {}, body) => {
9089
const time = new Date().toLocaleTimeString();
91-
if (!err && 200 === statusCode) {
90+
if (!err && statusCode === 200) {
9291
console.log(chalk.green(`[${time}] [success] => ${formData.dest}`));
9392
return;
9493
}
@@ -97,7 +96,7 @@ module.exports = class DeployServerWebpackPlugin {
9796
}
9897

9998
error(err) {
100-
console.log(`\n${chalk.yellow('[deploy-server-webpack-plugin] ' + err)}`);
99+
console.log(`\n${chalk.yellow('[deploy-server-webpack-plugin] ' + err + '. Deploy interrupted.')}`);
101100
process.exit();
102101
}
103102
};

0 commit comments

Comments
 (0)