Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mingmingwon committed Jul 16, 2018
1 parent 89bf81b commit 4734523
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Introduction
A plugin that deploys webpack bundles to server. It's useful when server ftp/sftp is forbidden or accessing server need pin + dynamic token.
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.

# Install
```
Expand All @@ -10,10 +10,11 @@ npm i deploy-server-webpack-plugin -D

**Client config**

You need config your webpack conf file like this:
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.

```js
const DeployServerPlugin = require('deploy-server-webpack-plugin');
const path = require('path')
const DeployServerPlugin = require('deploy-server-webpack-plugin')

module.exports = {
// ...
Expand All @@ -23,7 +24,7 @@ module.exports = {
receiver: 'http://1.23.45.678:9999/receiver',
mapping: { // Object type
from: path.resolve(__dirname, '../dist'), // absolute path
dest: '/data/front'
dest: '/data/project/front'
}
})
]
Expand All @@ -32,6 +33,7 @@ module.exports = {
or

```js
const path = require('path')
const DeployServerPlugin = require('deploy-server-webpack-plugin')

module.exports = {
Expand All @@ -42,12 +44,12 @@ module.exports = {
receiver: 'http://1.23.45.678:9999/receiver',
mapping: [ // Array type
{
from: path.resolve(__dirname, '../dist/static'), // absolute path
dest: '/data/public/static',
from: path.resolve(__dirname, '../dist/static'),
dest: '/data/project/public/static',
},
{
from: path.resolve(__dirname, '../dist/index.tpl'), // absolute path
dest: '/data/views/index.tpl',
from: path.resolve(__dirname, '../dist/index.tpl'),
dest: '/data/project/views/index.tpl',
},
// ...
],
Expand All @@ -59,15 +61,15 @@ module.exports = {

**Server Config**

Please copy ./server folder to you remote server somewhere, init the project and start it.
Copy ./server folder to you server machine somewhere, init the project and start it.

```
npm i
npm run start
```
Next config your nginx/apache to allow your node service can be accessed.

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.
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.

# Options

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

# Others
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:
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:
```
client_max_body_size: 10M; #default 1M
```
4 changes: 2 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deploy-server-webpack-plugin",
"version": "0.0.1",
"version": "0.0.2",
"description": "A plugin that deploys wepack bundles to server",
"main": "dist/index.js",
"scripts": {
Expand All @@ -12,7 +12,14 @@
},
"keywords": [
"deploy server",
"webpack plugin"
"push server",
"publish server",
"release server",
"webpack plugin",
"node deploy",
"node push",
"node publish",
"node release"
],
"author": "Jordan Wang",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Koa = require('koa');
const koaBody = require('koa-body');
const Router = require('koa-router');
const KoaRouter = require('koa-router');
const fse = require('fs-extra');

const router = new Router();
const router = new KoaRouter();
router.post('/receiver', koaBody({
multipart: true
}), async (ctx, next) => {
}), async ctx => {
const { files, body: { dest, token } } = ctx.request;

// deal with "token" here if needed
Expand Down
34 changes: 17 additions & 17 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "receiver",
"version": "0.0.1",
"description": "file receiver",
"main": "app.js",
"scripts": {
"start": "pm2 start app.js",
"stop": "pm2 stop app.js"
},
"author": "Jordan Wang",
"license": "MIT",
"dependencies": {
"fs-extra": "^6.0.1",
"koa": "^2.5.2",
"koa-body": "^4.0.4",
"koa-router": "^7.4.0",
"pm2": "^3.0.0"
}
"name": "receiver",
"version": "0.0.2",
"description": "files receiver",
"main": "app.js",
"scripts": {
"start": "pm2 start app.js",
"stop": "pm2 stop app.js"
},
"author": "Jordan Wang",
"license": "MIT",
"dependencies": {
"fs-extra": "^6.0.1",
"koa": "^2.5.2",
"koa-body": "^4.0.4",
"koa-router": "^7.4.0",
"pm2": "^3.0.0"
}
}
19 changes: 9 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const request = require('request');

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

apply(compiler) {
compiler.plugin('after-emit', (compilation, callback) => {
this.validConfig(compilation);
this.validateConfig(compilation);
this.deployHandler(callback);
});
}

validConfig(compilation) {
validateConfig(compilation) {
const { receiver, token = '' } = this.config;
let { mapping } = this.config;

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

const mappingType = Object.prototype.toString.call(mapping);
if (mappingType === '[object Object]') {
const type = Object.prototype.toString.call(mapping);
if (type === '[object Object]') {
mapping = [mapping];
} else if (mappingType === '[object Array]') {
} else if (type === '[object Array]') {
// do nothing
} else {
this.error('Invalid param: mapping');
Expand Down Expand Up @@ -72,9 +71,9 @@ module.exports = class DeployServerWebpackPlugin {
// compatible with windows
const dest = (this.mapping[index].to + from.replace(item, '')).replace(/\\/g, '/');
this.deploy({
file: fs.createReadStream(from),
dest,
token: this.token,
file: fs.createReadStream(from)
token: this.token
});
});
});
Expand All @@ -88,7 +87,7 @@ module.exports = class DeployServerWebpackPlugin {
formData
}, (err, { statusCode } = {}, body) => {
const time = new Date().toLocaleTimeString();
if (!err && 200 === statusCode) {
if (!err && statusCode === 200) {
console.log(chalk.green(`[${time}] [success] => ${formData.dest}`));
return;
}
Expand All @@ -97,7 +96,7 @@ module.exports = class DeployServerWebpackPlugin {
}

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

0 comments on commit 4734523

Please sign in to comment.