benbria / aliasify

Rewrite require calls in browserify modules.
MIT License
204 stars 26 forks source link

Replace alias in dependencies? #25

Open michaelBenin opened 9 years ago

michaelBenin commented 9 years ago

I've been doing some testing on using aliasify to replace requires of underscore for lodash. I've enabled verbose to be true. I've ran into an issue where in Backbone it still requires underscore instead of lodash.

See the output below:

Output:

aliasify - /Users/michaelbenin/Projects/node-startup/apps/about/about_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/home/home_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/links/links_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/login/login_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/search/search_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/searchquery/searchquery_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/signup/signup_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/topics/topics_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/users/users_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/home/home_sub_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/nav/nav_view.js: replacing underscore with lodash
aliasify - /Users/michaelbenin/Projects/node-startup/apps/footer/footer_view.js: replacing underscore with lodash
>> Error: Cannot find module 'underscore' from '/Users/michaelbenin/Projects/node-startup/node_modules/backbone'

Configuration:

var aliasify = require('aliasify');

var aliasifyConfig = {
  aliases: {
    underscore: 'lodash'
  },
  verbose: true
};

// in my browserify file:

b.transform(aliasify, aliasifyConfig);
anthonyhastings commented 9 years ago

I'm having an almost identical problem but with trying to use Exoskeleton with backbone.nativeview. My problem is the same as yours though; Aliasify will cycle through my SOURCE FILES, but ignores looking in the dependencies that I have in my node_modules folder.

[10:07:42] Starting 'scripts'...
aliasify - /var/www/planner/app/static/js/src/main.js: replacing backbone with exoskeleton
Browserify Failed: Cannot find module 'backbone' from '/var/www/planner/app/static/node_modules/backbone.nativeview'
[10:07:42] 'scripts' errored after 854 ms

How do we make Aliasify cycle not just our source files, but the dependencies loaded from node_modules?

ball-hayden commented 9 years ago

This is not an aliasify issue, but a browserify one.

A transform is only applied to source files, not node_modules If you wish to apply aliasify to all files, you should run it as a "global transform"

(I had the same problem, and found this out via https://github.com/substack/node-browserify/issues/501)

anthonyhastings commented 9 years ago

Yes you seem to be correct. Whenever specifying the transform, we seem to need to set global i.e.

    bundle.transform('aliasify', {
        aliases: {
            'backbone': 'exoskeleton'
        },
        verbose: true,
        global: true
    })

Apologies for the confusion, I guess that's down to me being unclear as to how transforms work. There's a lot of the Browserify documents to digest!

tj commented 8 years ago

Seems to not work for me, I have:

  "browserify": {
    "transform": [
      ["aliasify", {
        "global": true,
        "debug": true,
        "aliases": {
          "react": "preact-compat",
          "react-dom": "preact-compat"
        }
      }]
    ]
  },

and still get plenty of:

Error: Cannot find module 'react' from '/Users/tj/dev/src/github.com/tj/vitals/client/node_modules/react-router/lib'
Error: Cannot find module 'react' from '/Users/tj/dev/src/github.com/tj/vitals/client/node_modules/react-router/lib'

I tried adding the package.json config as mentioned in the readme, but that seems to have no effect. Any suggestions?

michaelBenin commented 8 years ago

This might help with defining global transforms in the package.json:

https://github.com/substack/node-browserify/issues/566

tj commented 8 years ago

Breaks on const's, seems like it's working now otherwise!