javascript - 为什么需要browserify `paths`定义?

标签 javascript angularjs node.js gulp browserify

链接https://github.com/jhades/angularjs-gulp-example/blob/master/gulpfile.js使用 browserify paths 进行 gulp build-js 任务定义。我不明白它的必要性...是否可以仅将条目指定为 entries: './js/**/*.js', 这会导致它也可以搜索所有子目录...而不是显式指定路径:['./js/controllers', './js/services', './js/directives'], 哪些是同一父目录的所有子目录?

任何提示表示赞赏。

最佳答案

作者使用 paths 配置来启用非相对 require 调用,例如 these :

require('todoCtrl');
require('todoStorage');
require('todoFocus');
require('todoEscape');
require('footer');

Browserify 模拟 Node 的模块解析机制(解释为 here ),当 Node 解析非相对 require 时,它会在 node_modules 中查找。 paths 选项为 Browserify 提供了一个不在 node_modules 中的路径列表,它应该检查这些路径(检查 node_modules ) 尝试解析非相对 require 调用时。

如果您自己项目中对模块的所有 require 调用都使用相对路径(例如 require('./js/controllers/todoCtrl')),那么您将获胜不需要 paths 配置选项。

关于javascript - 为什么需要browserify `paths`定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41798623/

相关文章:

javascript - 如何通过在 React 中传递对父组件的引用来关注嵌套元素?

javascript - 如何将并行列表的对象转换为对象列表?

javascript - 多次拖放不起作用

Javascript JSON 对象 - 新 JSON 中的变量键

angularjs - Angular.JS : why can't the inputs be edited?

angularjs - 将 angular.injector 与 ngAnimate 一起使用

node.js - 异步/等待 graphql 查询或突变

node.js - 在一组点中查找比给定距离(接近度)更近的对

node.js - Mongoose 保存在 if 语句的第二部分不起作用

Angularjs ng-view 不起作用