All files / src export-loader.js

93.33% Statements 14/15
100% Branches 2/2
100% Functions 1/1
93.33% Lines 14/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x 1x 1x 1x 1x         1x   1x   1x 1x   1x               1x       1x     1x 1x    
const { generateExports } = require('./esprima')
const d                   = require('debug')('norequire')
const esprima             = require('esprima')
const { readFileSync }    = require('fs')
const path                = require('path')
 
// main loader func, calls generateExports with dependencies
// which returns a func with a parser inside
function exportLoader(content) {
  try{
    // get current path from context or resolve when testing
    let currentPath = this.resourcePath || path.resolve("./test-utils/resourcePath.js")
    // get finalExports after parsing
    let { finalExports } = generateExports(esprima)(currentPath, readFileSync(currentPath,'utf8'))
    d(`finalExports is: ${finalExports}`)
    // replace all existing exports for duplicate error prevention
    content = content
      .replace(/export function/g,'function')
      .replace(/export (let|const|var)/g,'let')
      .replace(/export async function/g,'async function')
      .replace(/^export\s*{([\s\S]*?)}$/gm, '')
      .concat('\n')
      .concat(finalExports)
 
    d(content)
  }catch(e){
    d(`Failed on export loader main func with error:${e}`)
  }
  return content
}
 
module.exports = exportLoader;
module.exports.default = exportLoader;