Custom code in source handler


#1

Hey there!

I’m currently playing around with Meteor’s build plugins and would like to let the use decide how the code gets converted by adding binding handlers. This is what I’ve tried, but it obviously doesn’t work, as the file gets converted before the user has the chance to add a custom binding handler:

// This gets exported from the package and
// added to the registerBuildPlugin' sources
var ViewModel = (function () {
  var _bindings = {}
  return {
    addHandler: function (name, handler) {
      _bindings[name] = handler
    },
    getHandler: function (name) {
      return _bindings[name]
    }
  }
})()

Plugin.registerSourceHandler('html.jsx', function () {
  var handler = ViewModel.getHandler('visible')
  
  // This should log 'Visible Handler'
  console.log(handler())
})

// In the app, a user should be able to add binding handlers
ViewModel.addHandler('visible', function () {
  return 'Visible Handler'
})

Do you have any idea what I could do to make this work? Maybe adding another file handler for binding handler which reads and evals the code?

Hope it’s clear what I want to do :slight_smile: If not, let me know!


#2

Yeah you would need to register a special file extension or let the user specify an NPM package or something.

So like the user can add a file my-handler.build-handler.js that has some JS code that does stuff. Then you can eval it to get the configuration.


#3

Do you think it could be possible to add sources to registerBuildPlugin in the future, which are in the user’s app? Like

sources: [
  'viewmodel.js',
  '{}/*.binding.js',
  'handler.js'
]

This would allow the .binding.js files to access code from viewmodel.js.

Just saw that {}/ syntax somewhere; not sure if it’s right but I mean to access the actual app’s filepath here.

Right now another file extension seems to be the easiest solution. Thanks for your response!