简体   繁体   English

Browserify中的子模块

[英]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. 在node.js中,如果您需要一个目录(require('foo')),它将进入该目录并找到一个index.js文件,并返回该文件中的所有导出内容,因此我可以将其内容打包index.js文件中目录的名称。 Therefore, I dont have to require bar and foobar separately if index.js already includes them. 因此,如果index.js已包含bar和foobar,则不必分别要求它们。

However this approach doesn't work with browserify. 但是,这种方法不适用于browserify。 It seems like only thing browserify understands is relative paths. 似乎只有browserify能够理解的是相对路径。

/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. 换句话说,我想将项目分成子模块,在目录上调用require,就好像在依赖项上调用require一样。 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) 例如在star.js文件中,我希望能够require('foo')并获取bar.js和foobar.js的导出(只要/foo/index.js导入bar.js和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. https://github.com/facebook/react/blob/master/src/isomorphic/ReactIsomorphic.js在此文件中,它们在第14行的React-Children上调用require。

var ReactChildren = require('ReactChildren'); var ReactChildren = require('ReactChildren');

However react children is couple directories deeper. 但是反应孩子是夫妇目录更深。 https://github.com/facebook/react/blob/master/src/isomorphic/children/ReactChildren.js 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. 如果要使用非相对路径,请使用node_modules目录。 If you want require('foo') to work from any directory, just make a symlink from your project root: 如果您需要require('foo')在任何目录下工作,只需从项目根目录进行符号链接:

ln -s foo node_modules/foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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