cor-lang
Version:
The Language of the Web
118 lines (91 loc) • 2.78 kB
JavaScript
(function(cor){
var
loader = cor.loader,
path = cor.path;
//Custom plugin for .js files
loader.addPlugin({
ext : '.js',
rComments : /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
rRequire : /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
rootPath : null,
getRootPath: function() {
if (cor.isBrowser) {
if (!this.rootPath) {
this.rootPath = window.location.protocol + '//' + window.location.host;
}
return this.rootPath;
}
else {
return '';
}
},
findRequired: function(str) {
var
matches = [];
str.
replace(this.rComments, '').
replace(this.rRequire, function collectRequired(s, m){
matches.push(m);
});
return matches;
},
toJs: function(txt, from) {
return {
src : txt,
suffix: '//# sourceURL=' + this.getRootPath() + from
};
}
});
var
Compiler = cor.Compiler;
//Custom plugin for .cor files
loader.addPlugin({
ext : '.cor',
rComments : /\/\/([\s\S]*?)\n|---([\s\S]*?)---/g,
rRequire : /(?:^|\s)use\s*['"]([\w-\.\/]*?)['"]/g,
findRequired: function(str, from) {
var parsed,
matches = [],
me = this;
str.
replace(this.rComments, '').
replace(this.rRequire, function collectRequired(s, m) {
var srcPath, mappedPath;
if (path.ext(m) === '.cor') {
throw loader.error("Can not require directly require '.cor' modules", from);
}
srcPath = cor.yy.generateRoute(m);
matches.push(srcPath);
});
return matches;
},
toJs: function(src, from) {
var
ast, js, smap,
suffix = '',
comp = new Compiler(src, from, cor.isBrowser, 1);
ast = comp.parse();
js = comp.compile(ast);
smap = comp.sourceMap;
if (smap
&& typeof btoa !== 'undefined'
&& typeof unescape !== 'undefined'
&& typeof encodeURIComponent !== 'undefined') {
smap = smap.generate({
source : from.split('/').pop(),
sourceContent: src
});
//console.log(smap);
//console.log(new Function(js));
suffix = '//# sourceMappingURL=data:application/json;base64,' + (btoa(unescape(encodeURIComponent(smap))));
suffix += '\n//# sourceURL=' + from;
}
//console.log(src);
return {
src : js,
suffix: suffix,
usesRuntime: comp.env.usesRuntime,
};
}
});
})(typeof cor === 'undefined' ? {} : cor);