Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
985 B

4 years ago
module.exports = (function(){
var fs = require('fs'),
path = require('path');
return function importFile(fileName, fileMap){
// To Prevent Circular Imports
fileMap = fileMap || {};
// Determine Path for Importing dependent files
var filePath = path.dirname(fileName),
// Resolve to get the full path every time
mapPath = path.resolve(fileName);
// Add Error Handlers Later...
if(
// Check that File Exists
!fs.existsSync(fileName) ||
// Check it hasn't been imported yet
fileMap[mapPath]
){ return ""; }
// Mark as Read
fileMap[mapPath] = 1;
return fs.readFileSync(fileName)
.toString()
.replace(
// Regex to match import statements
/^(?:(?!\/[\/*]))([ \t]*)(.*)import [\"\'](.+)[\"\'];(?![^\*]+\*\/)/gm,
function(match, tabs, prefix, fileName){
// Replace Import
return tabs + prefix + importFile(path.resolve(filePath, fileName+".js"), fileMap).replace(/\n/g, "\n"+tabs);
}
);
};
})();