Advertisement
DigitalMag

simplest bundler

Jul 21st, 2020
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // import "fs";
  2.  
  3. var fs = require("fs");
  4.  
  5. const extensions = ['.js', '.ts']
  6.  
  7. integrate("base.ts", 'result.js')
  8.  
  9.  
  10.  
  11. function integrate(from, to){
  12.  
  13.     var content = fs.readFileSync(from).toString();    
  14.  
  15.     content = removeLazy(content)
  16.  
  17.     var content = importInsert(content);    
  18.  
  19.     fs.writeFileSync(to, content)    
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26. function removeLazy(content){    
  27.  
  28.     return content.replace(/\/\*-lazy\*\/[\s\S]*?\/\*lazy-\*\//, '');    
  29. }
  30.  
  31. function importInsert(content){
  32.  
  33.     let regex = /import \* as (?<module>\w+) from \"\.\/(?<filename>\w+)\"/g;
  34.     return content.replace(regex, packer)
  35.  
  36. }
  37.  
  38. function packer(match, module, fileName, offset, source){
  39.  
  40.     for(let ext of extensions){
  41.         if (fs.existsSync(fileName + ext))
  42.         {  
  43.             fileName = fileName + ext;
  44.             break;            
  45.         }
  46.     }
  47.  
  48.     var content = fs.readFileSync(fileName).toString()
  49.  
  50.     // content = Convert(content)
  51.  
  52.    
  53.  
  54.     return content;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement