index.js (985B)
1 module.exports = (function(){ 2 3 var fs = require('fs'), 4 path = require('path'); 5 6 return function importFile(fileName, fileMap){ 7 8 // To Prevent Circular Imports 9 fileMap = fileMap || {}; 10 11 // Determine Path for Importing dependent files 12 var filePath = path.dirname(fileName), 13 14 // Resolve to get the full path every time 15 mapPath = path.resolve(fileName); 16 17 // Add Error Handlers Later... 18 if( 19 // Check that File Exists 20 !fs.existsSync(fileName) || 21 22 // Check it hasn't been imported yet 23 fileMap[mapPath] 24 ){ return ""; } 25 26 // Mark as Read 27 fileMap[mapPath] = 1; 28 29 return fs.readFileSync(fileName) 30 .toString() 31 .replace( 32 // Regex to match import statements 33 /^(?:(?!\/[\/*]))([ \t]*)(.*)import [\"\'](.+)[\"\'];(?![^\*]+\*\/)/gm, 34 function(match, tabs, prefix, fileName){ 35 36 // Replace Import 37 return tabs + prefix + importFile(path.resolve(filePath, fileName+".js"), fileMap).replace(/\n/g, "\n"+tabs); 38 } 39 ); 40 }; 41 42 })();