-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
browserified.require() but only if the exposed name was required #1440
Comments
I don't know if there's a better way, but you could do that with my pathmodify plugin. If you didn't need it to be dynamic, maybe you could use the browser field. Also, people keep asking to be able to set a custom resolve function, most recently #1441. (FWIW, pathmodify currently relies on undocumented functionality -- overwriting module-deps' |
@jmm so this would do it? browserified = browserify(...);
browserfied.plugin(pathmodify, {
mods: [
pathmodify.mod.id('monkey', './........./something.js')
]
}) Can't use browser field since I'd be requiring a lot of third-party files with this. |
@adam-lynch Yeah, except you almost certainly want to use an absolute path for the second arg. (Although if you need to you can access the parent (requiring) module path and use that to decide what string to substitute for resolution). Also, if you need it to be exposed, pass a third arg, e.g. |
@jmm I was just going to ask about paths because I tried it and it worked but I needed to pass a path relative to where it was being required from (I think). It seems to work fine now if an absolute path is passed. Am I right in saying I've no need for a custom function then?
Yep. |
@adam-lynch Yes, a relative path would be evaluated in relation to the parent (requiring) module. If you know how to calculate the absolute path to the substitute when you call mods: [function (rec) {}]
// or
mods: [pathmodify.mod.id("monkey", function (rec) {})] |
@jmm great, that's what I thought. Thanks! And another thanks for sorting it so fast 👍 |
@adam-lynch Thanks! |
Let's say I use the API somewhat like this:
Let's say I call the second line in a loop or from some config, etc. and the exposed name isn't necessarily the same as anything in the path.
Is there a way I can get it so it's only included in the bundle if it is actually
require
d in the bundle? As far as I can tell I have these options:require
it by it's path in my bundle but this is not good enough. I need to be able to require it by the exposed name.require
it by exposed name. Analyse the bundle with detective to get what'srequire
d and callbrowserified.require
for each of the files required.If there was a way to specify a mapping between custom exposed names and filepaths, or a custom function to resolve the string which is
require
d, that would be good. I feel this is possible and must be there already, but I just can't see it in the docs.The text was updated successfully, but these errors were encountered: