Skip to content

Commit

Permalink
feat: use koa-proxy proxy static resource
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Feb 23, 2018
1 parent c172729 commit 86a7277
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 45 deletions.
37 changes: 12 additions & 25 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
const path = require('path');
const fs = require('fs');
const Constant = require('./lib/constant');
const Utils = require('./lib/utils');

const proxy = require('koa-proxy');
module.exports = app => {

const config = app.config.webpack;
app.use(function* (next) {
if (app.webpack_build_success) {
yield* next;
Expand All @@ -19,29 +18,17 @@ module.exports = app => {
}
});

if (app.config.webpack.proxy) {
app.use(function* (next) {
const url = this.url.split('?')[0];
const ext = path.extname(url).toLocaleLowerCase().replace(/^\./, '');
const proxyMapping = app.config.webpack.proxyMapping;
const matched = Object.keys(proxyMapping).some(item => {
return item === ext;
});
if (matched) {
const filepath = Utils.normalizeProxyUrlFile(app, url);
this.set('Content-Type', proxyMapping[ext]);
const content = yield app.webpack.fileSystem.readWebpackMemoryFile(filepath, url, 'web');
if (content) {
this.body = content;
} else {
yield next;
}
} else {
yield next;
}
});
if (config.proxy) {
let proxyConfig = config.proxy;
if (typeof proxyConfig === 'boolean') {
proxyConfig = {
host: 'http://127.0.0.1:9000',
match: /^\/public\//,
yieldNext: true,
};
}
app.use(proxy(proxyConfig));
}
const config = app.config.webpack;
app.messenger.setMaxListeners(config.maxListeners || 10000);

app.messenger.on(Constant.EVENT_WEBPACK_BUILD_STATE, data => {
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
15 changes: 6 additions & 9 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ module.exports = () => {
/**
* webpack build config
* @property {Number} port - webpack dev server port
* @property {Boolean} proxy - js/css static resource http absolution path mapping to relative path
* http://ip:port//public/client/js/vendor.js -> /public/client/js/vendor.js
* @property {Object} proxy - static resource http relative path mapping to true path @see https://github.com/popomore/koa-proxy
* /public/client/js/vendor.js -> http://ip:port//public/client/js/vendor.js
* @property {Object} proxyMapping support proxy mapping, default js/css/json, not support image
* @property {Array} [webpackConfigList] - webpack building config
*/
config.webpack = {
port: 9000,
proxy: true,
proxyMapping: {
js: 'text/javascript; charset=UTF-8',
css: 'text/css; charset=UTF-8',
json: 'application/json; charset=UTF-8',
html: 'text/html; charset=UTF-8',
map: 'application/json; charset=UTF-8'
proxy: {
host: 'http://127.0.0.1:9000', // target host that matched path will be proxy to
match: /^\/public\//, // path pattern.
yieldNext: true,
},
webpackConfigList: [],
};
Expand Down
10 changes: 2 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const mkdirp = require('mkdirp');
const imgRegex = /\.(png|jpe?g|gif|svg)(\?.*)?$/;
exports.readWebpackMemoryFile = (compilerList, filePath) => {
for (let i = 0; i < compilerList.length; i++) {
Expand All @@ -11,14 +10,9 @@ exports.readWebpackMemoryFile = (compilerList, filePath) => {
});
if (fileCompiler && fileCompiler.length) {
const ext = path.extname(filePath).toLocaleLowerCase();
if (ext === false && imgRegex.test(ext)) {
if (imgRegex.test(ext)) {
const base64 = fileCompiler[0].outputFileSystem.readFileSync(filePath).toString('base64');
const base64Image = `data:image/${ext.replace(/^\./, '')};base64,${base64}`;
if (!fs.existsSync(filePath)) {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, base64, 'base64');
}
return base64Image;
return `data:image/${ext.replace(/^\./, '')};base64,${base64}`;
}
return fileCompiler[0].outputFileSystem.readFileSync(filePath).toString('utf-8');
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-webpack",
"version": "3.3.2",
"version": "4.0.0-beta.1",
"description": "webpack dev server plugin for egg, support read file in memory and hot reload.",
"eggPlugin": {
"name": "webpack",
Expand All @@ -17,8 +17,9 @@
"hot-reload"
],
"dependencies": {
"koa-proxy": "^0.8.0",
"mkdirp": "^0.5.1",
"webpack-tool": "^3.0.0"
"webpack-tool": "next"
},
"devDependencies": {
"autod": "^2.7.1",
Expand Down

0 comments on commit 86a7277

Please sign in to comment.