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

Can't defined exposed name with require option #881

Open
guillaume86 opened this issue Aug 30, 2014 · 8 comments
Open

Can't defined exposed name with require option #881

guillaume86 opened this issue Aug 30, 2014 · 8 comments

Comments

@guillaume86
Copy link

Using the programmatic API, I try to bundle a file and expose it with a simple name:

bundle.expose(path, { expose: 'simpleName' }')

The resulting file keeps the path as the module identifier.
I digged in the source code and the problematic part seems to be:
https://github.com/substack/node-browserify/blob/master/index.js#L119-L126

I'm on windows and my path is C:\Folder\file.ext and the regex test evaluates to false (path don't start by . or /) so the id is set to the path.
Then the exposed name is not assigned since the id is already defined.

I don't really get what is the purpose of lines 119-121 but I guess it needs to be changed to support windows. If someone can explain it to me I can try to submit a pull request with windows support.

Edit:
It could also be a bug (I don't see why the expose parameter shouldn't be used in any case),
in that case changing the line 121 with : xtend(opts, { id: (expose || file), file: file }) should fix the problem.

@TimBeyer
Copy link

I'm experiencing a similar problem:

b.require('our-framework/lib/client-polyfills/continuation-local-storage', {
    expose: 'continuation-local-storage'
});

and

b.require('lodash', {
    expose: 'underscore'
});

fail to work correctly on 5.11.0.

I've spent some hours looking into the issue but I can't quite understand what is actually going on in browserify.

I noticed that the compiled javascript now contains
"lodash":undefined in the module lookup definitions, unlike the rest of the modules which look like "querystring":31.

Really not sure how to fix this problem.

@TimBeyer
Copy link

TimBeyer commented Sep 1, 2014

Possibly related to #839 and #850

@gobwas
Copy link

gobwas commented Sep 18, 2014

+1

@litek
Copy link

litek commented Sep 18, 2014

A temporary workaround is to use a path instead of a module reference.

E.g. this doesn't work

b.require('react/addons', {expose: 'react'});

This however does

b.require('./node_modules/react/addons', {expose: 'react'});

@insin
Copy link

insin commented Dec 2, 2014

This is still an issue in 6.3.3.

Say I'm doing:

b.require('superagent', {expose: 'thingy'})

The problem seems to be here - the id gets set to the expose value and nothing gets set for file:

var row = typeof file === 'object'
    ? xtend(file, opts)
    : (isExternalModule(file)
        ? xtend(opts, { id: expose || file })
        : xtend(opts, { file: file })
    )
;
if (!row.id) {
  row.id = expose || file;
}

So I get:

Error: Cannot find module 'thingy'

If I change that section to set the file and let the subsequent block deal with id:

var row = typeof file === 'object'
    ? xtend(file, opts)
    : xtend(opts, { file: file })
;
if (!row.id) {
  row.id = expose || file;
}

...then it works as expected.

I don't grok the codebase, so I don't know what the significance of the isExternalModule() check is here.

@youngjay
Copy link

+1
@insin 's fix works

@faiq
Copy link
Contributor

faiq commented Jan 5, 2015

@litek fix works as well, and doesn't require you to dig into the browserify codebase at all. It has me thinking that this might have something to do with resolving paths but I can't be too sure.

EDIT: this was a lie. It actually did not work properly for me. I set lodash's target to underscore, and that allowed backbone to use lodash but the rest of my app did not see lodash as a dependency

@faiq
Copy link
Contributor

faiq commented Jan 5, 2015

Okay I did some more digging and i found a pretty interesting difference when using the -r option with ./node_modues/whatever:target the isExternalModule(file) method returns false.

but when I do it i do it like this browserify -r whatever:target the isExternalModule(file) method returns true. this is with whatever being inside node_modules

so the real question is, is anything inside node_modules supposed to be an "external module" or is it not?

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

7 participants