webpack/examples/template-common.js

23 lines
705 B
JavaScript
Raw Normal View History

2012-10-09 06:12:10 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
require = require("enhanced-require")(module);
2012-04-05 20:59:01 +08:00
var fs = require("fs");
module.exports = function(template, filesReq, stdout, prefix) {
var regexp = new RegExp("\\{\\{" + (prefix ? prefix+":" : "") + "([^:\\}]+)\\}\\}", "g")
var cwd = process.cwd();
cwd = cwd.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
cwd = new RegExp(cwd, "g");
2012-04-05 20:59:01 +08:00
return template.replace(regexp, function(match) {
match = match.substr(2 + (prefix ? prefix.length+1 : 0), match.length - 4 - (prefix ? prefix.length+1 : 0));
if(match === "stdout")
return stdout;
return filesReq("./" + match);
}).replace(cwd, ".");
2012-04-05 20:59:01 +08:00
}