Skip to content

Commit 1c8d850

Browse files
committed
feat(server): add host replace middleware
1 parent fc918b9 commit 1c8d850

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

lib/commands/server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var connect = require('connect'),
3232
logSymbols = require('log-symbols'),
3333
favicon = require('serve-favicon'),
3434
webpackDevMiddleware = require('webpack-dev-middleware'),
35+
hostReplaceMiddleware = require('../modules/HostReplaceMiddleware'),
3536
httpProxy = require('http-proxy-middleware');
3637

3738
var Manager = require('../modules/manager.js');
@@ -193,6 +194,8 @@ exports.run = function (options) {
193194
}
194195
});
195196

197+
app.use(hostReplaceMiddleware);
198+
196199
// compiler
197200
app.use(function (req, res, next) {
198201
var url = req.url,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var _keys = require('babel-runtime/core-js/object/keys');
4+
5+
var _keys2 = _interopRequireDefault(_keys);
6+
7+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8+
9+
var fs = require('fs');
10+
var path = require('path');
11+
12+
var Manager = require('../modules/manager.js');
13+
var utilMw = require('../utils/middleware.js');
14+
var utilFs = require('../utils/fs.js');
15+
16+
var tmplExtensions = ['.html', '.vm', '.string'];
17+
18+
module.exports = function (req, res, next) {
19+
var projectInfo = utilMw.getProjectInfo(req);
20+
var project = Manager.getProject(projectInfo.projectCwd, { cache: false });
21+
var extName = path.extname(req.url);
22+
23+
if (tmplExtensions.indexOf(extName) > -1) {
24+
var htmlFilePath = path.join(projectInfo.projectCwd, req.url.replace(/.+\/src\//, 'src/'));
25+
26+
var replacedContent = handleHtmlContent(htmlFilePath);
27+
28+
if (!replacedContent) {
29+
next();
30+
} else {
31+
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
32+
res.writeHead(200);
33+
res.end(replacedContent);
34+
}
35+
} else {
36+
next();
37+
}
38+
39+
function handleHtmlContent(htmlFilePath) {
40+
if (!utilFs.fileExists(htmlFilePath)) {
41+
return '';
42+
}
43+
44+
var htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
45+
46+
var staticHost = project.server && project.server.staticHost;
47+
if (staticHost) {
48+
(0, _keys2.default)(staticHost).map(function (hostName) {
49+
var hostReg = new RegExp('(src|href)=("|\')//(' + hostName + ')', 'g');
50+
51+
var matches = void 0;
52+
while ((matches = hostReg.exec(htmlContent)) !== null) {
53+
var host = matches[3];
54+
htmlContent = htmlContent.replace(host, staticHost[hostName]);
55+
}
56+
});
57+
}
58+
59+
return htmlContent;
60+
}
61+
};

lib/utils/middleware.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
module.exports = {
6+
getProjectInfo: function getProjectInfo(req) {
7+
var url = req.url,
8+
keys = url.split('/'),
9+
projectName = keys[1],
10+
projectCwd = path.join(process.cwd(), projectName);
11+
12+
return {
13+
projectName: projectName,
14+
projectCwd: projectCwd
15+
};
16+
}
17+
};

src/commands/server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const connect = require('connect'),
1414
logSymbols = require('log-symbols'),
1515
favicon = require('serve-favicon'),
1616
webpackDevMiddleware = require('webpack-dev-middleware'),
17+
hostReplaceMiddleware = require('../modules/HostReplaceMiddleware'),
1718
httpProxy = require('http-proxy-middleware');
1819

1920
const Manager = require('../modules/manager.js');
@@ -175,6 +176,8 @@ exports.run = (options) => {
175176
}
176177
});
177178

179+
app.use(hostReplaceMiddleware);
180+
178181
// compiler
179182
app.use(function (req, res, next) {
180183
let url = req.url,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const Manager = require('../modules/manager.js');
7+
const utilMw = require('../utils/middleware.js');
8+
const utilFs = require('../utils/fs.js');
9+
10+
const tmplExtensions = ['.html', '.vm', '.string'];
11+
12+
module.exports = function(req, res, next) {
13+
const projectInfo = utilMw.getProjectInfo(req);
14+
const project = Manager.getProject(projectInfo.projectCwd, { cache: false });
15+
const extName = path.extname(req.url);
16+
17+
if(tmplExtensions.indexOf(extName) > -1) {
18+
const htmlFilePath = path.join(
19+
projectInfo.projectCwd,
20+
req.url.replace(/.+\/src\//, 'src/')
21+
);
22+
23+
const replacedContent = handleHtmlContent(htmlFilePath);
24+
25+
if(!replacedContent) {
26+
next();
27+
} else {
28+
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
29+
res.writeHead(200);
30+
res.end(replacedContent);
31+
}
32+
} else {
33+
next();
34+
}
35+
36+
function handleHtmlContent(htmlFilePath) {
37+
if(!utilFs.fileExists(htmlFilePath)) {
38+
return '';
39+
}
40+
41+
let htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
42+
43+
const staticHost = project.server && project.server.staticHost;
44+
if(staticHost) {
45+
Object.keys(staticHost).map((hostName) => {
46+
const hostReg = new RegExp(`(src|href)=("|')\/\/(${hostName})`, 'g');
47+
48+
let matches;
49+
while((matches = hostReg.exec(htmlContent)) !== null) {
50+
const host = matches[3];
51+
htmlContent = htmlContent.replace(host, staticHost[hostName]);
52+
}
53+
});
54+
}
55+
56+
return htmlContent;
57+
}
58+
};

src/utils/middleware.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
module.exports = {
6+
getProjectInfo: function(req) {
7+
var url = req.url,
8+
keys = url.split('/'),
9+
projectName = keys[1],
10+
projectCwd = path.join(process.cwd(), projectName);
11+
12+
return {
13+
projectName: projectName,
14+
projectCwd: projectCwd
15+
};
16+
}
17+
};

0 commit comments

Comments
 (0)