Skip to content

Commit 07169a0

Browse files
committed
init
1 parent 362f952 commit 07169a0

6 files changed

+254
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var acorn = require('acorn'),
2+
path = require('path'),
3+
isExpressRouting = require('./isExpressRouting'),
4+
SourceNode = require('source-map').SourceNode,
5+
SourceMapConsumer = require('source-map').SourceMapConsumer,
6+
makeIdentitySourceMap = require('./makeIdentitySourceMap');
7+
8+
module.exports = function(source, map) {
9+
if (this.cacheable) {
10+
this.cacheable();
11+
}
12+
13+
var resourcePath = this.resourcePath,
14+
filename = path.basename(resourcePath);
15+
16+
if (/[\\/]webpack[\\/]express-hot-loader[\\/]/.test(resourcePath)) {
17+
return this.callback(null, source, map);
18+
}
19+
20+
var fine = true;
21+
22+
// parse source
23+
// try {
24+
// var ast = acorn.parse(source, {ecmaVersion: 6, sourceType: 'module'});
25+
// }
26+
// catch (err) {
27+
// fine = false;
28+
// return this.callback(err);
29+
// }
30+
31+
/* USE PARSING ?
32+
var names = ast.body.filter(function(node) {
33+
return node.type === 'FunctionDeclaration';
34+
}).map(function(node) {
35+
return node.id.name;
36+
});
37+
*/
38+
39+
console.log('--------resource --------');
40+
console.log(resourcePath);
41+
if (/node_modules/.test(resourcePath)) {
42+
return this.callback(null, source, map);
43+
}
44+
45+
// cleaner
46+
var src = source.replace(/\r?\n|\r/g, ' ');
47+
48+
// code generation
49+
var prependTxt = [],
50+
appendTxt,
51+
separator = '\n\n';
52+
53+
var check = isExpressRouting.check(src);
54+
55+
var processor = require('./processor');
56+
57+
var exportApp = '';
58+
59+
if (check.containsExpressInstance) {
60+
exportApp = 'module.exports = app;\n';
61+
processor.setExpressResourcePath(resourcePath);
62+
}
63+
64+
prependTxt = [
65+
'var processor = require(' + JSON.stringify(require.resolve('./processor')) + ');\n',
66+
];
67+
68+
if (processor.mainExpressResourcePath != null &&
69+
processor.mainExpressResourcePath !== resourcePath) {
70+
// INJECT EXPRESS APP
71+
prependTxt.push('var expressFile = ' +JSON.stringify(processor.mainExpressResourcePath) + ';\n\t');
72+
prependTxt.push('var app = require(' + JSON.stringify(require.resolve(processor.mainExpressResourcePath)) + ');\n\t');
73+
}
74+
75+
prependTxt = prependTxt.join(' ');
76+
77+
appendTxt = [
78+
exportApp,
79+
'if (module.hot && ' +JSON.stringify(fine) +') {\n\t',
80+
'module.hot.dispose(function(data){\n\t\t',
81+
'if (module.hot.data.routes.length > 0 && app != null) {\n\t\t',
82+
'\tprocessor.doReload(app, module.hot.data);\n',
83+
'\t\t}\n',
84+
'\t});\n\n',
85+
86+
'var warning = '+JSON.stringify(check.containsExpressInstance)+' && '+(check.routes != null && check.routes.length > 0) + ';\n\n',
87+
88+
'module.hot.data = {\n\t',
89+
'routerNames: '+JSON.stringify(check.routerNames)+',\n\t',
90+
'routes: '+JSON.stringify(check.routes)+',\n\t',
91+
'warning: warning\n',
92+
'};\n\n',
93+
94+
'if (module.hot.data.warning) {\n',
95+
'\tprocessor.warn();\n',
96+
'}\n',
97+
98+
'}'
99+
].join(' ');
100+
101+
if (this.sourceMap === false) {
102+
return this.callback(null, [
103+
prependTxt,
104+
source,
105+
appendTxt
106+
].join(separator));
107+
}
108+
109+
var newCode = [
110+
prependTxt,
111+
source,
112+
appendTxt
113+
].join(separator);
114+
115+
if (!map) {
116+
117+
// var transform = require("babel-core").transform(source, {
118+
// sourceMaps: true,
119+
// filename: this.resourcePath,
120+
// sourceFileName: this.resourcePath
121+
// });
122+
123+
map = makeIdentitySourceMap(source, this.resourcePath);
124+
}
125+
126+
var node = new SourceNode(null, null, null, [
127+
new SourceNode(null, null, this.resourcePath, prependTxt),
128+
SourceNode.fromStringWithSourceMap(source, new SourceMapConsumer(map)),
129+
new SourceNode(null, null, this.resourcePath, appendTxt)
130+
]).join(separator);
131+
132+
var result = node.toStringWithSourceMap();
133+
134+
this.callback(null, result.code, result.map.toString());
135+
}

isExpressRouting.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
/* MATCH
4+
following declaration
5+
6+
var router = express.Router();
7+
const router = express.Router();
8+
let router = express.Router();
9+
*/
10+
//var routerNamePattern = /(?:var|const|let)\s*(\w+)\s*\=\s*(?:express\.Router)/g;
11+
12+
/* MATCH
13+
middleware and HTTP method routes
14+
*/
15+
var routerLevelPattern = /\.(?:all|get|param|post|delete|post|route|use)\('(\/([\w+\-\*\:]|\/?)*)/g;
16+
17+
var expressInstanciationPattern = /createServer\(/g;
18+
19+
module.exports = {
20+
21+
check: function (source) {
22+
console.log("CHECK SOURCE");
23+
24+
var containsExpressInstance = expressInstanciationPattern.test(source);
25+
26+
console.log("isExpress " + containsExpressInstance);
27+
28+
// var routerNames = [];
29+
30+
// var routeVariableNameMatch;
31+
// while ((routeVariableNameMatch = routerNamePattern.exec(source)) !== null) {
32+
// routerNames.push(routeVariableNameMatch[1]);
33+
// }
34+
35+
var routes = [];
36+
var uses;
37+
var lastIndex = -1;
38+
while ((uses = routerLevelPattern.exec(source)) !== null) {
39+
routes.push(uses[1]);
40+
if (lastIndex < 0) {
41+
lastIndex = routerLevelPattern.lastIndex;
42+
}
43+
}
44+
45+
// console.log(routerNames);
46+
console.log(routes);
47+
console.log(containsExpressInstance);
48+
49+
return {
50+
containsExpressInstance: containsExpressInstance,
51+
routes: routes
52+
// routerNames: routerNames
53+
};
54+
55+
}
56+
57+
};

makeIdentitySourceMap.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var SourceMapGenerator = require('source-map').SourceMapGenerator;
4+
5+
function makeIdentitySourceMap(content, resourcePath) {
6+
var map = new SourceMapGenerator();
7+
map.setSourceContent(resourcePath, content);
8+
9+
content.split('\n').map(function (line, index) {
10+
map.addMapping({
11+
source: resourcePath,
12+
original: {
13+
line: index + 1,
14+
column: 0
15+
},
16+
generated: {
17+
line: index + 1,
18+
column: 0
19+
}
20+
});
21+
});
22+
23+
return map.toJSON();
24+
}
25+
26+
module.exports = makeIdentitySourceMap;

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "express-hot-loader",
3+
"version": "0.0.1",
4+
"description": "Reload express routes",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/darul75/express-hot-reload.git"
9+
},
10+
"keywords": [
11+
"express",
12+
"javascript",
13+
"webpack",
14+
"hmr",
15+
"livereload",
16+
"live",
17+
"edit",
18+
"hot",
19+
"loader",
20+
"reload"
21+
],
22+
"author": "Julien Valéry",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/darul75/express-hot-reload/issues"
26+
},
27+
"homepage": "https://github.com/darul75/express-hot-reload",
28+
"dependencies": {
29+
"acorn": "^2.0.4",
30+
"express": "^4.12.3",
31+
"node-libs-browser": "^0.5.0",
32+
"source-map": "^0.4.4",
33+
"webpack": "^1.9.7"
34+
}
35+
}

processor.js

Whitespace-only changes.

0 commit comments

Comments
 (0)