Files
homebridge/lib/util/autoamd.js
Nick Farina dbedf7fe01 Convert to ES5, add web server
* No compilation step
 * Beginnings of web interface
 * Simple express server; React-based frontend
 * CommonJS style across codebase; auto-converts to RequireJS for browser
 * Using diffsync for realtime UI
 * "Provider" -> "Plugin"
 * Plugins expose one or more Providers
2015-08-11 22:27:59 -07:00

30 lines
1.2 KiB
JavaScript

var interceptor = require('express-interceptor');
/**
* Express middleware that converts CommonJS-style code to RequireJS style for the browser (assuming require.js is loaded).
*/
module.exports = function(urlPrefix) {
return interceptor(function(req, res){
return {
// Only URLs with the given prefix will be converted to require.js style
isInterceptable: function(){
return req.originalUrl.indexOf(urlPrefix) == 0;
},
intercept: function(body, send) {
send(toRequireJS(body));
}
};
});
}
// From https://github.com/shovon/connect-commonjs-amd/blob/master/src/middleware.coffee
function toRequireJS(str) {
var requireCalls = str.match(/require\((\s+)?('[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*")(\s+)?\)/g) || [];
requireCalls = requireCalls.map(function(str) {
return (str.match(/('[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*")/))[0];
});
requireCalls.unshift("'require'");
str = "define([" + (requireCalls.join(', ')) + "], function (require) {\nvar module = { exports: {} }\n , exports = module.exports;\n\n(function () {\n\n" + str + "\n\n})();\n\nreturn module.exports;\n});";
return str;
};