megawac / acorn-umd

Parse acorn ast for AMD, CommonJS, and ES6 definitions.
MIT License
4 stars 1 forks source link

Question: How can I add a custom loader? #4

Open reggi opened 9 years ago

reggi commented 9 years ago

I'm trying to track down required files and some of them are files I'm loading using readFileSync.

var ymlTests = fs.readFileSync(path.join(__dirname, './recursive-deps-tests.yml'), 'utf8')
var tests = yaml.safeLoad(ymlTests)

I thought it should be possible for me to wrap this functionality like this:

var customLoader = function(file){
  var yamlContents = fs.readFileSync(path.join(__dirname, file), 'utf8')
  return yaml.safeLoad(yamlContents)
}
var tests = customLoader('./recursive-deps-tests.yml')
var files = customLoader('./recursive-deps-files.yml')

And be able to add customLoader to return these in the returned list of modules.

Thoughts?

megawac commented 9 years ago

Right, that could be a meaningful enhancement for adding other loads (like spy/curl/etc). What API were you thinking to use this @reggi? Something like

var imports = umd(ast, {
    es6: true, amd: false, cjs: true,

   customLoads: [{
      test: node => node.callee.name === 'customLoader',
      parse() { // optional
         // manipulate the node as desired
     }
   }]
});
reggi commented 9 years ago

Yeah that functionality looks great.