benbria / browserify-transform-tools

Utilities for writing browserify transforms.
MIT License
65 stars 15 forks source link

supporting `require` aliases #13

Open jarofghosts opened 9 years ago

jarofghosts commented 9 years ago

hey, there!

i was wondering if there was any plan to support specifying aliases for require in the makeRequireTransform method. i ask because of things like proxyquireify and others that do "hygienic" require monkey-patching by providing an alias, but still need to be potentially transformed.

i started poking at PRing the change for discussion, but encountered a brick wall in the form of (what seems like) necessarily breaking the current API. ideally, in my estimation, the require "word" would be somehow linked to each "argument" in the transform callback. because of this, i decided to instead ask if it was even something considered worth supporting, and if so if there was any preference about how to support it. if desired, i am more than willing to do the work to support it.

thoughts?

thanks for your time!

jwalton commented 9 years ago

You're looking to, for example, transform:

proxyquire "foo"

into

proxyquire "bar"

? Could you not just pass in an options.functionName and change this line to use it in place of 'require'? I wonder if it might not be better to make a functionTransform, though, since you could easily apply this same logic to just about any function you care to name.

jarofghosts commented 9 years ago

i would prefer to support multiple in one go, so that you can use the same transform for every require alias. it might be far-fetched and possibly not a common enough case to warrant inclusion, which is why i asked.

jarofghosts commented 9 years ago

to answer your question, though, yes i would like to, for example, have this work:

const thing = require('thing')

const otherThing = proxyquire('other-thing')

(transform happens)

const thing = require('transformed-thing')

const otherThing = proxyquire('transformed-other-thing')
jwalton commented 9 years ago

So what breaking changes were you thinking about, then?

jarofghosts commented 9 years ago

sooo.. i was actually asking about this so that i could PR pkgify to support it. in the end, i just went off and made my own that supported it rather than complicate other peoples' modules.