Skip to content
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

Closed
adam-lynch opened this issue Nov 20, 2015 · 7 comments
Closed

browserified.require() but only if the exposed name was required #1440

adam-lynch opened this issue Nov 20, 2015 · 7 comments

Comments

@adam-lynch
Copy link

Let's say I use the API somewhat like this:

browserified = browserify(...);
browserified.require('./........./something.js', {expose: 'monkey'});

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 required 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's required and call browserified.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 required, that would be good. I feel this is possible and must be there already, but I just can't see it in the docs.

@jmm
Copy link
Collaborator

jmm commented Nov 20, 2015

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' resolver proptery after the pipeline is constructed.)

@adam-lynch
Copy link
Author

@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.

@jmm
Copy link
Collaborator

jmm commented Nov 20, 2015

@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. ('monkey', '/abs/path/something.js', 'monkey'). I'm assuming you were using expose above just to make that ID available within the bundle, but not sure.

@adam-lynch
Copy link
Author

@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?

I'm assuming you were using expose above just to make that ID available within the bundle, but not sure.

Yep.

@jmm
Copy link
Collaborator

jmm commented Nov 20, 2015

@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 b.plugin() then you don't need to pass a custom function to pathmodify. If you need to calculate the path relative to the parent (requiring) module then you can pass a custom function to pathmodify like:

mods: [function (rec) {}]
// or
mods: [pathmodify.mod.id("monkey", function (rec) {})]

@adam-lynch
Copy link
Author

@jmm great, that's what I thought. Thanks! And another thanks for sorting it so fast 👍

@jmm
Copy link
Collaborator

jmm commented Nov 20, 2015

@adam-lynch Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants