简体   繁体   中英

Submodules in Browserify

/foo
  /bar.js
  /foobar.js
  /index.js

In node.js if you a require a directory (require('foo')), it would look into that directory and find an index.js file and return whatever exports I have in that file, so I can just bundle up the contents of the directory in an index.js file. Therefore, I dont have to require bar and foobar separately if index.js already includes them.

However this approach doesn't work with browserify. It seems like only thing browserify understands is relative paths.

/star
  /star.js
  /starfoo.js
  /index.js
/foo
  /bar.js
  /foobar.js
  /index.js

In other words I want to separate my project into submodules, call require on a directory as if I am calling require on a dependency. For example in the star.js file I want to be able to require('foo') and get the exports of bar.js and foobar.js (as long as /foo/index.js is importing bar.js and foobar.js)

edit: Looking at the react source code, i think what i am describing is possible

https://github.com/facebook/react/blob/master/src/isomorphic/ReactIsomorphic.js In this file they call require on React-Children in line 14.

var ReactChildren = require('ReactChildren');

However react children is couple directories deeper. https://github.com/facebook/react/blob/master/src/isomorphic/children/ReactChildren.js

Where is this mapping defined?

There isn't a way to specify a base directory because that's not how node modules work. If you want non-relative paths, use the node_modules directory. If you want require('foo') to work from any directory, just make a symlink from your project root:

ln -s foo node_modules/foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM